Skip to content

Commit

Permalink
Move AlertPolicy labels to user_labels and make them a map (hashicorp…
Browse files Browse the repository at this point in the history
…#635)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed Apr 25, 2019
1 parent bffe098 commit 77935da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
45 changes: 29 additions & 16 deletions google-beta/resource_monitoring_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,17 @@ func resourceMonitoringAlertPolicy() *schema.Resource {
Optional: true,
Default: true,
},
"labels": {
"notification_channels": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"notification_channels": {
Type: schema.TypeList,
"user_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Elem: &schema.Schema{Type: schema.TypeString},
},
"creation_record": {
Type: schema.TypeList,
Expand All @@ -294,6 +292,14 @@ func resourceMonitoringAlertPolicy() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Deprecated: "labels is removed as it was never used. See user_labels for the correct field",
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -338,11 +344,11 @@ func resourceMonitoringAlertPolicyCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("notification_channels"); !isEmptyValue(reflect.ValueOf(notificationChannelsProp)) && (ok || !reflect.DeepEqual(v, notificationChannelsProp)) {
obj["notificationChannels"] = notificationChannelsProp
}
labelsProp, err := expandMonitoringAlertPolicyLabels(d.Get("labels"), d, config)
userLabelsProp, err := expandMonitoringAlertPolicyUserLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
} else if v, ok := d.GetOkExists("user_labels"); !isEmptyValue(reflect.ValueOf(userLabelsProp)) && (ok || !reflect.DeepEqual(v, userLabelsProp)) {
obj["userLabels"] = userLabelsProp
}
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil {
Expand Down Expand Up @@ -431,7 +437,7 @@ func resourceMonitoringAlertPolicyRead(d *schema.ResourceData, meta interface{})
if err := d.Set("notification_channels", flattenMonitoringAlertPolicyNotificationChannels(res["notificationChannels"], d)); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
if err := d.Set("labels", flattenMonitoringAlertPolicyLabels(res["labels"], d)); err != nil {
if err := d.Set("user_labels", flattenMonitoringAlertPolicyUserLabels(res["userLabels"], d)); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
if err := d.Set("documentation", flattenMonitoringAlertPolicyDocumentation(res["documentation"], d)); err != nil {
Expand Down Expand Up @@ -475,11 +481,11 @@ func resourceMonitoringAlertPolicyUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("notification_channels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, notificationChannelsProp)) {
obj["notificationChannels"] = notificationChannelsProp
}
labelsProp, err := expandMonitoringAlertPolicyLabels(d.Get("labels"), d, config)
userLabelsProp, err := expandMonitoringAlertPolicyUserLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
} else if v, ok := d.GetOkExists("user_labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, userLabelsProp)) {
obj["userLabels"] = userLabelsProp
}
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil {
Expand Down Expand Up @@ -863,7 +869,7 @@ func flattenMonitoringAlertPolicyNotificationChannels(v interface{}, d *schema.R
return v
}

func flattenMonitoringAlertPolicyLabels(v interface{}, d *schema.ResourceData) interface{} {
func flattenMonitoringAlertPolicyUserLabels(v interface{}, d *schema.ResourceData) interface{} {
return v
}

Expand Down Expand Up @@ -1338,8 +1344,15 @@ func expandMonitoringAlertPolicyNotificationChannels(v interface{}, d TerraformR
return v, nil
}

func expandMonitoringAlertPolicyLabels(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
func expandMonitoringAlertPolicyUserLabels(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

func expandMonitoringAlertPolicyDocumentation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ resource "google_monitoring_alert_policy" "alert_policy" {
}
}
}
user_labels = {
foo = "bar"
}
}
`, context)
}
Expand Down
12 changes: 10 additions & 2 deletions website/docs/r/monitoring_alert_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ resource "google_monitoring_alert_policy" "alert_policy" {
}
}
}
user_labels = {
foo = "bar"
}
}
```

Expand Down Expand Up @@ -584,9 +588,13 @@ The `aggregations` block supports:
entries in this field is
`projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]`

* `labels` -
* `user_labels` -
(Optional)
User-supplied key/value data to be used for organizing AlertPolicy objects.
This field is intended to be used for organizing and identifying the AlertPolicy
objects.The field can contain up to 64 entries. Each key and value is limited
to 63 Unicode characters or 128 bytes, whichever is smaller. Labels and values
can contain only lowercase letters, numerals, underscores, and dashes. Keys
must begin with a letter.

* `documentation` -
(Optional)
Expand Down

0 comments on commit 77935da

Please sign in to comment.