Skip to content

Commit

Permalink
fix: add warehouses attribute to resource monitor (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-swinkler authored Jan 26, 2022
1 parent af041a6 commit b041e46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/resource_monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ resource snowflake_resource_monitor monitor {
- **start_timestamp** (String) The date and time when the resource monitor starts monitoring credit usage for the assigned warehouses.
- **suspend_immediate_triggers** (Set of Number) A list of percentage thresholds at which to immediately suspend all warehouses.
- **suspend_triggers** (Set of Number) A list of percentage thresholds at which to suspend all warehouses.
- **warehouses** (Set of String) A list of warehouses to apply the resource monitor to.

## Import

Expand Down
15 changes: 15 additions & 0 deletions pkg/resources/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ var resourceMonitorSchema = map[string]*schema.Schema{
Default: false,
ForceNew: true,
},
"warehouses": {
Type: schema.TypeSet,
Optional: true,
Description: "A list of warehouses to apply the resource monitor to.",
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
},
}

// ResourceMonitor returns a pointer to the resource representing a resource monitor
Expand Down Expand Up @@ -142,6 +149,14 @@ func CreateResourceMonitor(d *schema.ResourceData, meta interface{}) error {
}
}

if v, ok := d.GetOk("warehouses"); ok {
for _, w := range v.(*schema.Set).List() {
if err := snowflake.Exec(db, cb.SetOnWarehouse(w.(string))); err != nil {
return errors.Wrapf(err, "error setting resource monitor %v on warehouse %v", name, w.(string))
}
}
}

if err := ReadResourceMonitor(d, meta); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/snowflake/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func (rcb *ResourceMonitorCreateBuilder) SetOnAccount() string {
return fmt.Sprintf(`ALTER ACCOUNT SET RESOURCE_MONITOR = "%v"`, rcb.name)
}

// SetOnWarehouse returns the SQL query that will set the resource monitor on the specified warehouse
func (rcb *ResourceMonitorCreateBuilder) SetOnWarehouse(warehouse string) string {
return fmt.Sprintf(`ALTER WAREHOUSE "%v" SET RESOURCE_MONITOR = "%v"`, warehouse, rcb.name)
}

type resourceMonitor struct {
Name sql.NullString `db:"name"`
CreditQuota sql.NullString `db:"credit_quota"`
Expand Down
9 changes: 9 additions & 0 deletions pkg/snowflake/resource_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ func TestResourceMonitorSetOnAccount(t *testing.T) {
q := s.Create().SetOnAccount()
r.Equal(`ALTER ACCOUNT SET RESOURCE_MONITOR = "test_resource_monitor"`, q)
}

func TestResourceMonitorSetOnWarehouse(t *testing.T) {
r := require.New(t)
s := snowflake.ResourceMonitor("test_resource_monitor")
r.NotNil(s)

q := s.Create().SetOnWarehouse("test_warehouse")
r.Equal(`ALTER WAREHOUSE "test_warehouse" SET RESOURCE_MONITOR = "test_resource_monitor"`, q)
}

0 comments on commit b041e46

Please sign in to comment.