diff --git a/docs/resources/resource_monitor.md b/docs/resources/resource_monitor.md index 989bc4dba7..181f92ece0 100644 --- a/docs/resources/resource_monitor.md +++ b/docs/resources/resource_monitor.md @@ -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 diff --git a/pkg/resources/resource_monitor.go b/pkg/resources/resource_monitor.go index 626a2c3b72..6e6afbde2a 100644 --- a/pkg/resources/resource_monitor.go +++ b/pkg/resources/resource_monitor.go @@ -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 @@ -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 } diff --git a/pkg/snowflake/resource_monitor.go b/pkg/snowflake/resource_monitor.go index a40b1d18a2..e9090b84d3 100644 --- a/pkg/snowflake/resource_monitor.go +++ b/pkg/snowflake/resource_monitor.go @@ -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"` diff --git a/pkg/snowflake/resource_monitor_test.go b/pkg/snowflake/resource_monitor_test.go index 8be9c2b890..31818ca1ae 100644 --- a/pkg/snowflake/resource_monitor_test.go +++ b/pkg/snowflake/resource_monitor_test.go @@ -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) +}