Skip to content

Commit

Permalink
Support for user_labels in google_monitoring_slo (#6070) (#4363)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jun 6, 2022
1 parent 66b554f commit ab1c044
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/6070.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
monitoring: support for user_labels in `google_monitoring_slo`

```
45 changes: 45 additions & 0 deletions google-beta/resource_monitoring_custom_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ https://cloud.google.com/apis/design/resource_names.`,
},
},
},
"user_labels": {
Type: schema.TypeMap,
Optional: true,
Description: `Labels which have been used to annotate the service. Label keys must start
with a letter. Label keys and values may contain lowercase letters,
numbers, underscores, and dashes. Label keys and values have a maximum
length of 63 characters, and must be less than 128 bytes in size. Up to 64
label entries may be stored. For labels which do not have a semantic value,
the empty string may be supplied for the label value.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -104,6 +115,12 @@ func resourceMonitoringServiceCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
userLabelsProp, err := expandMonitoringServiceUserLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("user_labels"); ok || !reflect.DeepEqual(v, userLabelsProp) {
obj["userLabels"] = userLabelsProp
}
telemetryProp, err := expandMonitoringServiceTelemetry(d.Get("telemetry"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -201,6 +218,9 @@ func resourceMonitoringServiceRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("display_name", flattenMonitoringServiceDisplayName(res["displayName"], d, config)); err != nil {
return fmt.Errorf("Error reading Service: %s", err)
}
if err := d.Set("user_labels", flattenMonitoringServiceUserLabels(res["userLabels"], d, config)); err != nil {
return fmt.Errorf("Error reading Service: %s", err)
}
if err := d.Set("telemetry", flattenMonitoringServiceTelemetry(res["telemetry"], d, config)); err != nil {
return fmt.Errorf("Error reading Service: %s", err)
}
Expand Down Expand Up @@ -233,6 +253,12 @@ func resourceMonitoringServiceUpdate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
userLabelsProp, err := expandMonitoringServiceUserLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("user_labels"); ok || !reflect.DeepEqual(v, userLabelsProp) {
obj["userLabels"] = userLabelsProp
}
telemetryProp, err := expandMonitoringServiceTelemetry(d.Get("telemetry"), d, config)
if err != nil {
return err
Expand All @@ -257,6 +283,10 @@ func resourceMonitoringServiceUpdate(d *schema.ResourceData, meta interface{}) e
updateMask = append(updateMask, "displayName")
}

if d.HasChange("user_labels") {
updateMask = append(updateMask, "userLabels")
}

if d.HasChange("telemetry") {
updateMask = append(updateMask, "telemetry")
}
Expand Down Expand Up @@ -340,6 +370,10 @@ func flattenMonitoringServiceDisplayName(v interface{}, d *schema.ResourceData,
return v
}

func flattenMonitoringServiceUserLabels(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenMonitoringServiceTelemetry(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -368,6 +402,17 @@ func expandMonitoringServiceDisplayName(v interface{}, d TerraformResourceData,
return v, nil
}

func expandMonitoringServiceUserLabels(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 expandMonitoringServiceTelemetry(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ resource "google_monitoring_custom_service" "custom" {
telemetry {
resource_name = "//product.googleapis.com/foo/foo/services/test%{random_suffix}"
}
user_labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}
`, context)
}
Expand Down
44 changes: 44 additions & 0 deletions google-beta/resource_monitoring_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ integer fraction of a day and at least 60s.`,
ValidateFunc: validateRegexp(`^[a-z0-9\-]+$`),
Description: `The id to use for this ServiceLevelObjective. If omitted, an id will be generated instead.`,
},
"user_labels": {
Type: schema.TypeMap,
Optional: true,
Description: `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.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -763,6 +773,12 @@ func resourceMonitoringSloCreate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("calendar_period"); !isEmptyValue(reflect.ValueOf(calendarPeriodProp)) && (ok || !reflect.DeepEqual(v, calendarPeriodProp)) {
obj["calendarPeriod"] = calendarPeriodProp
}
userLabelsProp, err := expandMonitoringSloUserLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("user_labels"); ok || !reflect.DeepEqual(v, userLabelsProp) {
obj["userLabels"] = userLabelsProp
}
serviceLevelIndicatorProp, err := expandMonitoringSloServiceLevelIndicator(nil, d, config)
if err != nil {
return err
Expand Down Expand Up @@ -876,6 +892,9 @@ func resourceMonitoringSloRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("calendar_period", flattenMonitoringSloCalendarPeriod(res["calendarPeriod"], d, config)); err != nil {
return fmt.Errorf("Error reading Slo: %s", err)
}
if err := d.Set("user_labels", flattenMonitoringSloUserLabels(res["userLabels"], d, config)); err != nil {
return fmt.Errorf("Error reading Slo: %s", err)
}
// Terraform must set the top level schema field, but since this object contains collapsed properties
// it's difficult to know what the top level should be. Instead we just loop over the map returned from flatten.
if flattenedProp := flattenMonitoringSloServiceLevelIndicator(res["serviceLevelIndicator"], d, config); flattenedProp != nil {
Expand Down Expand Up @@ -938,6 +957,12 @@ func resourceMonitoringSloUpdate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("calendar_period"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, calendarPeriodProp)) {
obj["calendarPeriod"] = calendarPeriodProp
}
userLabelsProp, err := expandMonitoringSloUserLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("user_labels"); ok || !reflect.DeepEqual(v, userLabelsProp) {
obj["userLabels"] = userLabelsProp
}
serviceLevelIndicatorProp, err := expandMonitoringSloServiceLevelIndicator(nil, d, config)
if err != nil {
return err
Expand Down Expand Up @@ -981,6 +1006,10 @@ func resourceMonitoringSloUpdate(d *schema.ResourceData, meta interface{}) error
updateMask = append(updateMask, "calendarPeriod")
}

if d.HasChange("user_labels") {
updateMask = append(updateMask, "userLabels")
}

if d.HasChange("basic_sli") {
updateMask = append(updateMask, "serviceLevelIndicator.basicSli")
}
Expand Down Expand Up @@ -1118,6 +1147,10 @@ func flattenMonitoringSloCalendarPeriod(v interface{}, d *schema.ResourceData, c
return v
}

func flattenMonitoringSloUserLabels(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenMonitoringSloServiceLevelIndicator(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -1610,6 +1643,17 @@ func expandMonitoringSloCalendarPeriod(v interface{}, d TerraformResourceData, c
return v, nil
}

func expandMonitoringSloUserLabels(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 expandMonitoringSloServiceLevelIndicator(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
transformed := make(map[string]interface{})
transformedBasicSli, err := expandMonitoringSloServiceLevelIndicatorBasicSli(d.Get("basic_sli"), d, config)
Expand Down
5 changes: 5 additions & 0 deletions google-beta/resource_monitoring_slo_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ resource "google_monitoring_slo" "appeng_slo" {
threshold = "1s"
}
}
user_labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}
`, context)
}
Expand Down
4 changes: 4 additions & 0 deletions google-beta/resource_monitoring_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ resource "google_monitoring_slo" "primary" {
threshold = "1s"
}
}
user_labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}
`
}
Expand Down
14 changes: 14 additions & 0 deletions website/docs/r/monitoring_custom_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ resource "google_monitoring_custom_service" "custom" {
telemetry {
resource_name = "//product.googleapis.com/foo/foo/services/test"
}
user_labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}
```

Expand All @@ -68,6 +73,15 @@ The following arguments are supported:
(Optional)
Name used for UI elements listing this Service.

* `user_labels` -
(Optional)
Labels which have been used to annotate the service. Label keys must start
with a letter. Label keys and values may contain lowercase letters,
numbers, underscores, and dashes. Label keys and values have a maximum
length of 63 characters, and must be less than 128 bytes in size. Up to 64
label entries may be stored. For labels which do not have a semantic value,
the empty string may be supplied for the label value.

* `telemetry` -
(Optional)
Configuration for how to query telemetry on a Service.
Expand Down
13 changes: 13 additions & 0 deletions website/docs/r/monitoring_slo.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ resource "google_monitoring_slo" "appeng_slo" {
threshold = "1s"
}
}
user_labels = {
my_key = "my_value"
my_other_key = "my_other_value"
}
}
```
## Example Usage - Monitoring Slo Request Based
Expand Down Expand Up @@ -274,6 +279,14 @@ The following arguments are supported:
<calendarPeriod>".
Possible values are `DAY`, `WEEK`, `FORTNIGHT`, and `MONTH`.

* `user_labels` -
(Optional)
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.

* `basic_sli` -
(Optional)
Basic Service-Level Indicator (SLI) on a well-known service type.
Expand Down

0 comments on commit ab1c044

Please sign in to comment.