diff --git a/pkg/resources/resource_monitor.go b/pkg/resources/resource_monitor.go index 498ef9b9d4..09ab7b545c 100644 --- a/pkg/resources/resource_monitor.go +++ b/pkg/resources/resource_monitor.go @@ -36,7 +36,6 @@ var resourceMonitorSchema = map[string]*schema.Schema{ Optional: true, Computed: true, Description: "The number of credits allocated monthly to the resource monitor.", - ForceNew: true, }, "frequency": { Type: schema.TypeString, @@ -44,20 +43,17 @@ var resourceMonitorSchema = map[string]*schema.Schema{ Computed: true, Description: "The frequency interval at which the credit usage resets to 0. If you set a frequency for a resource monitor, you must also set START_TIMESTAMP.", ValidateFunc: validation.StringInSlice(validFrequencies, false), - ForceNew: true, }, "start_timestamp": { Type: schema.TypeString, Optional: true, Computed: true, Description: "The date and time when the resource monitor starts monitoring credit usage for the assigned warehouses.", - ForceNew: true, }, "end_timestamp": { Type: schema.TypeString, Optional: true, Description: "The date and time when the resource monitor suspends the assigned warehouses.", - ForceNew: true, }, "suspend_triggers": { Type: schema.TypeSet, @@ -101,7 +97,7 @@ func ResourceMonitor() *schema.Resource { return &schema.Resource{ Create: CreateResourceMonitor, Read: ReadResourceMonitor, - // Update: UpdateResourceMonitor, @TODO implement updates + Update: UpdateResourceMonitor, Delete: DeleteResourceMonitor, Schema: resourceMonitorSchema, @@ -287,6 +283,42 @@ func extractTriggerInts(s sql.NullString) ([]int, error) { return out, nil } +func UpdateResourceMonitor(d *schema.ResourceData, meta interface{}) error { + db := meta.(*sql.DB) + id := d.Id() + + stmt := snowflake.NewResourceMonitorBuilder(id).Alter() + var runSetStatement bool + + if d.HasChange("credit_quota") { + runSetStatement = true + stmt.SetInt(`CREDIT_QUOTA`, d.Get("credit_quota").(int)) + } + + if d.HasChange("frequency") { + runSetStatement = true + stmt.SetString(`FREQUENCY`, d.Get("frequency").(string)) + } + + if d.HasChange("start_timestamp") { + runSetStatement = true + stmt.SetString(`START_TIMESTAMP`, d.Get("start_timestamp").(string)) + } + + if d.HasChange("end_timestamp") { + runSetStatement = true + stmt.SetString(`END_TIMESTAMP`, d.Get("end_timestamp").(string)) + } + + if runSetStatement { + if err := snowflake.Exec(db, stmt.Statement()); err != nil { + return fmt.Errorf("error updating resource monitor %v\n%w", id, err) + } + } + + return ReadResourceMonitor(d, meta) +} + // DeleteResourceMonitor implements schema.DeleteFunc. func DeleteResourceMonitor(d *schema.ResourceData, meta interface{}) error { db := meta.(*sql.DB) diff --git a/pkg/resources/resource_monitor_acceptance_test.go b/pkg/resources/resource_monitor_acceptance_test.go index 0a4cf35d59..54b34e30b3 100644 --- a/pkg/resources/resource_monitor_acceptance_test.go +++ b/pkg/resources/resource_monitor_acceptance_test.go @@ -27,6 +27,15 @@ func TestAcc_ResourceMonitor(t *testing.T) { resource.TestCheckResourceAttr("snowflake_resource_monitor.test", "set_for_account", "false"), ), }, + // CHANGE PROPERTIES + { + Config: resourceMonitorConfig2(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_resource_monitor.test", "name", name), + resource.TestCheckResourceAttr("snowflake_resource_monitor.test", "credit_quota", "150"), + resource.TestCheckResourceAttr("snowflake_resource_monitor.test", "set_for_account", "false"), + ), + }, // IMPORT { ResourceName: "snowflake_resource_monitor.test", @@ -47,6 +56,16 @@ resource "snowflake_resource_monitor" "test" { `, accName) } +func resourceMonitorConfig2(accName string) string { + return fmt.Sprintf(` +resource "snowflake_resource_monitor" "test" { + name = "%v" + credit_quota = 150 + set_for_account = false +} +`, accName) +} + func TestAcc_ResourceMonitorNotifyUsers(t *testing.T) { userEnv := os.Getenv("RESOURCE_MONITOR_NOTIFY_USERS_TEST") if userEnv == "" {