Repository: chanzuckerberg/terraform-provider-snowflake
Pull request #831 (2022-01-25 17:22:36)

fix: add warehouses attribute to resource monitor
Currently resource monitors can only be set at the account level, or not at all. This PR adds an attribute to the resource monitor called "warehouses" which is a set of string corresponding to warehouse names. These warehouses will be assigned to the resource monitor when it is created. 


https://docs.snowflake.com/en/user-guide/resource-monitors.html#assigning-warehouses-to-a-resource-monitor

## Test Plan

<!-- detail ways in which this PR has been tested or needs to be tested -->
* [ ] acceptance tests
<!-- add more below if you think they are relevant -->
* [ ] …

## References
<!-- issues documentation links, etc  -->

* 

alldoami: /ok-to-test sha=6dfb661
github-actions[bot]: Integration tests success for [6dfb661](https://github.com/chanzuckerberg/terraform-provider-snowflake/actions/runs/1747096799)
alldoami: run `make docs`
sfc-gh-swinkler: okay i ran `make docs`. 
alldoami: /ok-to-test sha=fa6aaf0
github-actions[bot]: Integration tests success for [fa6aaf0](https://github.com/chanzuckerberg/terraform-provider-snowflake/actions/runs/1748882226)
alldoami: /ok-to-test sha=b05707a
github-actions[bot]: Integration tests success for [b05707a](https://github.com/chanzuckerberg/terraform-provider-snowflake/actions/runs/1752368490)

Patch:
diff --git a/docs/resources/resource_monitor.md b/docs/resources/resource_monitor.md
index 989bc4db..181f92ec 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 626a2c3b..6e6afbde 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 a40b1d18..e9090b84 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"`


Test patch:
diff --git a/pkg/snowflake/resource_monitor_test.go b/pkg/snowflake/resource_monitor_test.go
index 8be9c2b8..31818ca1 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)
+}