Skip to content

Commit

Permalink
fix: resource snowflake_resource_monitor behavior conflict from provi…
Browse files Browse the repository at this point in the history
…der 0.54.0 to 0.55.0 (#1468)

* SNOW-710421: Adding more functionality to resource_monitor update

* SNOW-710421: reviewdog 🐶 fixes

* SNOW-710421: Cleaning up code

* SNOW-710421: Adds ConflictsWith for warehouses and set_for_account

* SNOW-710421: Fix broken ConflictsWith
  • Loading branch information
sfc-gh-jlove authored Jan 11, 2023
1 parent 1a00052 commit 8ce0c53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
40 changes: 30 additions & 10 deletions pkg/resources/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,16 @@ var resourceMonitorSchema = map[string]*schema.Schema{
Description: "A list of percentage thresholds at which to send an alert to subscribed users.",
},
"set_for_account": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"warehouses"},
Description: "Specifies whether the resource monitor should be applied globally to your Snowflake account.",
Default: false,
Type: schema.TypeBool,
Optional: true,
Description: "Specifies whether the resource monitor should be applied globally to your Snowflake account.",
Default: false,
},
"warehouses": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"set_for_account"},
Description: "A list of warehouses to apply the resource monitor to.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
Optional: true,
Description: "A list of warehouses to apply the resource monitor to.",
Elem: &schema.Schema{Type: schema.TypeString},
},
}

Expand All @@ -103,11 +101,27 @@ func ResourceMonitor() *schema.Resource {
}
}

func checkAccountAgainstWarehouses(d *schema.ResourceData, name string) error {
account := d.Get("set_for_account").(bool)
v := d.Get("warehouses")

if len(v.(*schema.Set).List()) > 0 && account {
return fmt.Errorf("error creating resource monitor %v on account err = set_for_account cannot be true and give warehouses", name)
}
return nil
}

// CreateResourceMonitor implements schema.CreateFunc.
func CreateResourceMonitor(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
name := d.Get("name").(string)

check := checkAccountAgainstWarehouses(d, name)

if check != nil {
return check
}

cb := snowflake.NewResourceMonitorBuilder(name).Create()
// Set optionals
if v, ok := d.GetOk("notify_users"); ok {
Expand Down Expand Up @@ -284,6 +298,12 @@ func UpdateResourceMonitor(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
id := d.Id()

check := checkAccountAgainstWarehouses(d, id)

if check != nil {
return check
}

ub := snowflake.NewResourceMonitorBuilder(id).Alter()
var runSetStatement bool

Expand Down
14 changes: 14 additions & 0 deletions pkg/resources/resource_monitor_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,36 @@ func TestAcc_ResourceMonitor(t *testing.T) {

func resourceMonitorConfig(accName string) string {
return fmt.Sprintf(`
resource "snowflake_warehouse" "warehouse" {
name = "test"
comment = "foo"
warehouse_size = "small"
}
resource "snowflake_resource_monitor" "test" {
name = "%v"
credit_quota = 100
set_for_account = false
notify_triggers = [40]
warehouses = [snowflake_warehouse.warehouse.id]
}
`, accName)
}

func resourceMonitorConfig2(accName string) string {
return fmt.Sprintf(`
resource "snowflake_warehouse" "warehouse" {
name = "test"
comment = "foo"
warehouse_size = "small"
}
resource "snowflake_resource_monitor" "test" {
name = "%v"
credit_quota = 150
set_for_account = true
notify_triggers = [50]
warehouses = []
}
`, accName)
}
Expand Down

0 comments on commit 8ce0c53

Please sign in to comment.