Skip to content

Commit

Permalink
feat: support object parameters on account level (#1583)
Browse files Browse the repository at this point in the history
* add test cases for update repl schedule

* linting

* update doc

* update docs

* update docs

* update docs
  • Loading branch information
sfc-gh-swinkler authored Feb 28, 2023
1 parent ab638f0 commit fb24802
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 63 deletions.
13 changes: 11 additions & 2 deletions docs/resources/object_parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ resource "snowflake_object_parameter" "o3" {
name = snowflake_table.t.name
}
}
// Setting object parameter at account level
resource "snowflake_object_parameter" "o4" {
key = "DATA_RETENTION_TIME_IN_DAYS"
value = "89"
}
```

<!-- schema generated by tfplugindocs -->
Expand All @@ -69,10 +75,13 @@ resource "snowflake_object_parameter" "o3" {
### Required

- `key` (String) Name of object parameter. Valid values are those in [object parameters](https://docs.snowflake.com/en/sql-reference/parameters.html#object-parameters).
- `object_identifier` (Block List, Min: 1) Specifies the object identifier for the object parameter. (see [below for nested schema](#nestedblock--object_identifier))
- `object_type` (String) Type of object to which the parameter applies. Valid values are those in [object types](https://docs.snowflake.com/en/sql-reference/parameters.html#object-types).
- `value` (String) Value of object parameter, as a string. Constraints are the same as those for the parameters in Snowflake documentation.

### Optional

- `object_identifier` (Block List) Specifies the object identifier for the object parameter. If no value is provided, then the resource will default to setting the object parameter at account level. (see [below for nested schema](#nestedblock--object_identifier))
- `object_type` (String) Type of object to which the parameter applies. Valid values are those in [object types](https://docs.snowflake.com/en/sql-reference/parameters.html#object-types). If no value is provided, then the resource will default to setting the object parameter at account level.

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
6 changes: 6 additions & 0 deletions examples/resources/snowflake_object_parameter/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ resource "snowflake_object_parameter" "o3" {
name = snowflake_table.t.name
}
}

// Setting object parameter at account level
resource "snowflake_object_parameter" "o4" {
key = "DATA_RETENTION_TIME_IN_DAYS"
value = "89"
}
6 changes: 3 additions & 3 deletions pkg/resources/account_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func CreateAccountParameter(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(key)
p, err := snowflake.ShowParameter(db, key, snowflake.ParameterTypeAccount)
p, err := snowflake.ShowAccountParameter(db, key)
if err != nil {
return fmt.Errorf("error reading account parameter err = %w", err)
}
Expand All @@ -81,7 +81,7 @@ func CreateAccountParameter(d *schema.ResourceData, meta interface{}) error {
func ReadAccountParameter(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
key := d.Id()
p, err := snowflake.ShowParameter(db, key, snowflake.ParameterTypeAccount)
p, err := snowflake.ShowAccountParameter(db, key)
if err != nil {
return fmt.Errorf("error reading account parameter err = %w", err)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func DeleteAccountParameter(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("error creating account parameter err = %w", err)
}
_, err = snowflake.ShowParameter(db, key, snowflake.ParameterTypeAccount)
_, err = snowflake.ShowAccountParameter(db, key)
if err != nil {
return fmt.Errorf("error reading account parameter err = %w", err)
}
Expand Down
91 changes: 63 additions & 28 deletions pkg/resources/object_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ var objectParameterSchema = map[string]*schema.Schema{
},
"object_type": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
Description: "Type of object to which the parameter applies. Valid values are those in [object types](https://docs.snowflake.com/en/sql-reference/parameters.html#object-types).",
Description: "Type of object to which the parameter applies. Valid values are those in [object types](https://docs.snowflake.com/en/sql-reference/parameters.html#object-types). If no value is provided, then the resource will default to setting the object parameter at account level.",
RequiredWith: []string{"object_identifier"},
ValidateFunc: validation.StringInSlice(snowflake.GetParameterObjectTypeSetAsStrings(), false),
},
"object_identifier": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Description: "Specifies the object identifier for the object parameter.",
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Description: "Specifies the object identifier for the object parameter. If no value is provided, then the resource will default to setting the object parameter at account level.",
RequiredWith: []string{"object_type"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -83,19 +85,13 @@ func CreateObjectParameter(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
key := d.Get("key").(string)
value := d.Get("value").(string)
objectType := snowflake.ObjectType(d.Get("object_type").(string))
objectDatabase, objectSchema, objectName := expandObjectIdentifier(d.Get("object_identifier"))
fullyQualifierObjectIdentifier := snowflakeValidation.FormatFullyQualifiedObjectID(objectDatabase, objectSchema, objectName)

parameterDefault := snowflake.GetParameterDefaults(snowflake.ParameterTypeObject)[key]
if parameterDefault.Validate != nil {
if err := parameterDefault.Validate(value); err != nil {
return err
}
}
if ok := slices.Contains(parameterDefault.AllowedObjectTypes, objectType); !ok {
return fmt.Errorf("object_type '%v' is not allowed for parameter '%v'", objectType, key)
}

// add quotes to value if it is a string
typeString := reflect.TypeOf("")
Expand All @@ -104,15 +100,35 @@ func CreateObjectParameter(d *schema.ResourceData, meta interface{}) error {
}

builder := snowflake.NewParameter(key, value, snowflake.ParameterTypeObject, db)
builder.WithObjectIdentifier(fullyQualifierObjectIdentifier)
builder.WithObjectType(objectType)

var fullyQualifierObjectIdentifier string
if v, ok := d.GetOk("object_identifier"); ok {
objectDatabase, objectSchema, objectName := expandObjectIdentifier(v.([]interface{}))
fullyQualifierObjectIdentifier = snowflakeValidation.FormatFullyQualifiedObjectID(objectDatabase, objectSchema, objectName)
builder.WithObjectIdentifier(fullyQualifierObjectIdentifier)
}

var objectType snowflake.ObjectType
if v, ok := d.GetOk("object_type"); ok {
objectType = snowflake.ObjectType(v.(string))
if ok := slices.Contains(parameterDefault.AllowedObjectTypes, objectType); !ok {
return fmt.Errorf("object_type '%v' is not allowed for parameter '%v'", objectType, key)
}
builder.WithObjectType(objectType)
}

err := builder.SetParameter()
if err != nil {
return fmt.Errorf("error creating object parameter err = %w", err)
}
id := fmt.Sprintf("%v❄️%v❄️%v", key, objectType, fullyQualifierObjectIdentifier)
d.SetId(id)
p, err := snowflake.ShowObjectParameter(db, key, objectType, fullyQualifierObjectIdentifier)
var p *snowflake.Parameter
if fullyQualifierObjectIdentifier != "" {
p, err = snowflake.ShowObjectParameter(db, key, objectType, fullyQualifierObjectIdentifier)
} else {
p, err = snowflake.ShowAccountParameter(db, key)
}
if err != nil {
return fmt.Errorf("error reading object parameter err = %w", err)
}
Expand All @@ -132,16 +148,22 @@ func ReadObjectParameter(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("unexpected format of ID (%v), expected key❄️object_type❄️object_identifier", id)
}
key := parts[0]
objectType := snowflake.ObjectType(parts[1])
objectIdentifier := parts[2]
p, err := snowflake.ShowObjectParameter(db, key, objectType, objectIdentifier)
var p *snowflake.Parameter
var err error
if parts[1] == "" {
p, err = snowflake.ShowAccountParameter(db, key)
} else {
objectType := snowflake.ObjectType(parts[1])
objectIdentifier := parts[2]
p, err = snowflake.ShowObjectParameter(db, key, objectType, objectIdentifier)
}
if err != nil {
return fmt.Errorf("error reading object parameter err = %w", err)
}
err = d.Set("value", p.Value.String)
if err != nil {
if err := d.Set("value", p.Value.String); err != nil {
return err
}

return nil
}

Expand All @@ -154,10 +176,6 @@ func UpdateObjectParameter(d *schema.ResourceData, meta interface{}) error {
func DeleteObjectParameter(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
key := d.Get("key").(string)
objectType := snowflake.ObjectType(d.Get("object_type").(string))
objectDatabase, objectSchema, objectName := expandObjectIdentifier(d.Get("object_identifier"))
fullyQualifierObjectIdentifier := snowflakeValidation.FormatFullyQualifiedObjectID(objectDatabase, objectSchema, objectName)

parameterDefault := snowflake.GetParameterDefaults(snowflake.ParameterTypeObject)[key]
defaultValue := parameterDefault.DefaultValue
value := fmt.Sprintf("%v", defaultValue)
Expand All @@ -168,13 +186,30 @@ func DeleteObjectParameter(d *schema.ResourceData, meta interface{}) error {
value = fmt.Sprintf("'%s'", value)
}
builder := snowflake.NewParameter(key, value, snowflake.ParameterTypeObject, db)
builder.WithObjectIdentifier(fullyQualifierObjectIdentifier)
builder.WithObjectType(objectType)

var fullyQualifierObjectIdentifier string
if v, ok := d.GetOk("object_identifier"); ok {
objectDatabase, objectSchema, objectName := expandObjectIdentifier(v.([]interface{}))
fullyQualifierObjectIdentifier = snowflakeValidation.FormatFullyQualifiedObjectID(objectDatabase, objectSchema, objectName)
builder.WithObjectIdentifier(fullyQualifierObjectIdentifier)
}
var objectType snowflake.ObjectType
if v, ok := d.GetOk("object_type"); ok {
objectType = snowflake.ObjectType(v.(string))
if ok := slices.Contains(parameterDefault.AllowedObjectTypes, objectType); !ok {
return fmt.Errorf("object_type '%v' is not allowed for parameter '%v'", objectType, key)
}
builder.WithObjectType(objectType)
}
err := builder.SetParameter()
if err != nil {
return fmt.Errorf("error restoring default for object parameter err = %w", err)
return fmt.Errorf("error deleting object parameter err = %w", err)
}
if fullyQualifierObjectIdentifier != "" {
_, err = snowflake.ShowObjectParameter(db, key, objectType, fullyQualifierObjectIdentifier)
} else {
_, err = snowflake.ShowAccountParameter(db, key)
}
_, err = snowflake.ShowObjectParameter(db, key, objectType, fullyQualifierObjectIdentifier)
if err != nil {
return fmt.Errorf("error reading object parameter err = %w", err)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/resources/object_parameter_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ func TestAcc_ObjectParameter(t *testing.T) {
})
}

func TestAcc_ObjectParameterAccount(t *testing.T) {
prefix := "tst-terraform" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: objectParameterAccount(prefix, "ENABLE_STREAM_TASK_REPLICATION", "true"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_object_parameter.p", "key", "ENABLE_STREAM_TASK_REPLICATION"),
resource.TestCheckResourceAttr("snowflake_object_parameter.p", "value", "true"),
),
},
},
})
}

func objectParameterBasic(prefix, key, value string) string {
s := `
resource "snowflake_database" "d" {
Expand All @@ -42,3 +59,16 @@ resource "snowflake_object_parameter" "p" {
`
return fmt.Sprintf(s, prefix, key, value)
}

func objectParameterAccount(prefix, key, value string) string {
s := `
resource "snowflake_database" "d" {
name = "%s"
}
resource "snowflake_object_parameter" "p" {
key = "%s"
value = "%s"
}
`
return fmt.Sprintf(s, prefix, key, value)
}
6 changes: 3 additions & 3 deletions pkg/resources/session_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func CreateSessionParameter(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(key)
p, err := snowflake.ShowParameter(db, key, snowflake.ParameterTypeSession)
p, err := snowflake.ShowAccountParameter(db, key)
if err != nil {
return fmt.Errorf("error reading session parameter err = %w", err)
}
Expand All @@ -81,7 +81,7 @@ func CreateSessionParameter(d *schema.ResourceData, meta interface{}) error {
func ReadSessionParameter(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
key := d.Id()
p, err := snowflake.ShowParameter(db, key, snowflake.ParameterTypeSession)
p, err := snowflake.ShowAccountParameter(db, key)
if err != nil {
return fmt.Errorf("error reading session parameter err = %w", err)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func DeleteSessionParameter(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("error creating account parameter err = %w", err)
}
_, err = snowflake.ShowParameter(db, key, snowflake.ParameterTypeSession)
_, err = snowflake.ShowAccountParameter(db, key)
if err != nil {
return fmt.Errorf("error reading a parameter err = %w", err)
}
Expand Down
Loading

0 comments on commit fb24802

Please sign in to comment.