Skip to content

Commit

Permalink
Support for UpdateResourceMonitor (#1437)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennylu2 authored Jan 4, 2023
1 parent bd1d3cc commit 0dff065
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
42 changes: 37 additions & 5 deletions pkg/resources/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,24 @@ 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,
Optional: true,
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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions pkg/resources/resource_monitor_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 == "" {
Expand Down

0 comments on commit 0dff065

Please sign in to comment.