Move AlertPolicy labels to user_labels and make them a map (#3494)

Signed-off-by: Modular Magician <magic-modules@google.com>
This commit is contained in:
The Magician 2019-04-25 14:11:20 -07:00 committed by Riley Karson
parent d28bfc7aaf
commit ca07709f6c
3 changed files with 44 additions and 19 deletions

View File

@ -259,13 +259,6 @@ func resourceMonitoringAlertPolicy() *schema.Resource {
Optional: true, Optional: true,
Default: true, Default: true,
}, },
"labels": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"notification_channels": { "notification_channels": {
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
@ -273,6 +266,11 @@ func resourceMonitoringAlertPolicy() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
}, },
}, },
"user_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"creation_record": { "creation_record": {
Type: schema.TypeList, Type: schema.TypeList,
Computed: true, Computed: true,
@ -294,6 +292,14 @@ func resourceMonitoringAlertPolicy() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, 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": { "project": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
@ -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)) { } else if v, ok := d.GetOkExists("notification_channels"); !isEmptyValue(reflect.ValueOf(notificationChannelsProp)) && (ok || !reflect.DeepEqual(v, notificationChannelsProp)) {
obj["notificationChannels"] = notificationChannelsProp obj["notificationChannels"] = notificationChannelsProp
} }
labelsProp, err := expandMonitoringAlertPolicyLabels(d.Get("labels"), d, config) userLabelsProp, err := expandMonitoringAlertPolicyUserLabels(d.Get("user_labels"), d, config)
if err != nil { if err != nil {
return err return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) { } else if v, ok := d.GetOkExists("user_labels"); !isEmptyValue(reflect.ValueOf(userLabelsProp)) && (ok || !reflect.DeepEqual(v, userLabelsProp)) {
obj["labels"] = labelsProp obj["userLabels"] = userLabelsProp
} }
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config) documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil { if err != nil {
@ -431,7 +437,7 @@ func resourceMonitoringAlertPolicyRead(d *schema.ResourceData, meta interface{})
if err := d.Set("notification_channels", flattenMonitoringAlertPolicyNotificationChannels(res["notificationChannels"], d)); err != nil { if err := d.Set("notification_channels", flattenMonitoringAlertPolicyNotificationChannels(res["notificationChannels"], d)); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err) 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) return fmt.Errorf("Error reading AlertPolicy: %s", err)
} }
if err := d.Set("documentation", flattenMonitoringAlertPolicyDocumentation(res["documentation"], d)); err != nil { if err := d.Set("documentation", flattenMonitoringAlertPolicyDocumentation(res["documentation"], d)); err != nil {
@ -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)) { } else if v, ok := d.GetOkExists("notification_channels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, notificationChannelsProp)) {
obj["notificationChannels"] = notificationChannelsProp obj["notificationChannels"] = notificationChannelsProp
} }
labelsProp, err := expandMonitoringAlertPolicyLabels(d.Get("labels"), d, config) userLabelsProp, err := expandMonitoringAlertPolicyUserLabels(d.Get("user_labels"), d, config)
if err != nil { if err != nil {
return err return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { } else if v, ok := d.GetOkExists("user_labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, userLabelsProp)) {
obj["labels"] = labelsProp obj["userLabels"] = userLabelsProp
} }
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config) documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil { if err != nil {
@ -863,7 +869,7 @@ func flattenMonitoringAlertPolicyNotificationChannels(v interface{}, d *schema.R
return v return v
} }
func flattenMonitoringAlertPolicyLabels(v interface{}, d *schema.ResourceData) interface{} { func flattenMonitoringAlertPolicyUserLabels(v interface{}, d *schema.ResourceData) interface{} {
return v return v
} }
@ -1338,8 +1344,15 @@ func expandMonitoringAlertPolicyNotificationChannels(v interface{}, d TerraformR
return v, nil return v, nil
} }
func expandMonitoringAlertPolicyLabels(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { func expandMonitoringAlertPolicyUserLabels(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) {
return v, nil 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) { func expandMonitoringAlertPolicyDocumentation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {

View File

@ -65,6 +65,10 @@ resource "google_monitoring_alert_policy" "alert_policy" {
} }
} }
} }
user_labels = {
foo = "bar"
}
} }
`, context) `, context)
} }

View File

@ -58,6 +58,10 @@ resource "google_monitoring_alert_policy" "alert_policy" {
} }
} }
} }
user_labels = {
foo = "bar"
}
} }
``` ```
@ -584,9 +588,13 @@ The `aggregations` block supports:
entries in this field is entries in this field is
`projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]` `projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]`
* `labels` - * `user_labels` -
(Optional) (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` - * `documentation` -
(Optional) (Optional)