Skip to content

Commit

Permalink
availability sli for SLO monitoring resource (#4344) (#2908)
Browse files Browse the repository at this point in the history
* inital check-in for review

* custom_flattner added

* extra line added at EOF

* PR comments implemented

* custom_expander logic is placed in encoder as it more appropriate, validation func added

* empty lines added at EOF

* PR comments implemented, Test cases added

* fixed encoder for the case where basicsli is not used in the config

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jan 27, 2021
1 parent 9f6fcc8 commit a28e1c4
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .changelog/4344.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
monitoring : added `availability` sli metric support for the resource `google_monitoring_slo`

```
78 changes: 77 additions & 1 deletion google-beta/resource_monitoring_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func validateMonitoringSloGoal(v interface{}, k string) (warnings []string, erro
return
}

func validateAvailabilitySli(v interface{}, key string) (ws []string, errs []error) {
if v.(bool) == false {
errs = append(errs, fmt.Errorf("%q must be set to true, got: %v", key, v))
}
return
}

func resourceMonitoringSlo() *schema.Resource {
return &schema.Resource{
Create: resourceMonitoringSloCreate,
Expand Down Expand Up @@ -100,9 +107,27 @@ Exactly one of the following must be set:
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"availability": {
Type: schema.TypeList,
Optional: true,
Description: `Availability based SLI, dervied from count of requests made to this service that return successfully.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
ValidateFunc: validateAvailabilitySli,
Description: `Whether an availability SLI is enabled or not. Must be set to 'true. Defaults to 'true'.`,
Default: true,
},
},
},
ExactlyOneOf: []string{"basic_sli.0.latency", "basic_sli.0.availability"},
},
"latency": {
Type: schema.TypeList,
Required: true,
Optional: true,
Description: `Parameters for a latency threshold SLI.`,
MaxItems: 1,
Elem: &schema.Resource{
Expand All @@ -116,6 +141,7 @@ this service that return in no more than threshold.`,
},
},
},
ExactlyOneOf: []string{"basic_sli.0.latency", "basic_sli.0.availability"},
},
"location": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -1107,6 +1133,8 @@ func flattenMonitoringSloServiceLevelIndicatorBasicSli(v interface{}, d *schema.
flattenMonitoringSloServiceLevelIndicatorBasicSliVersion(original["version"], d, config)
transformed["latency"] =
flattenMonitoringSloServiceLevelIndicatorBasicSliLatency(original["latency"], d, config)
transformed["availability"] =
flattenMonitoringSloServiceLevelIndicatorBasicSliAvailability(original["availability"], d, config)
return []interface{}{transformed}
}
func flattenMonitoringSloServiceLevelIndicatorBasicSliMethod(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand Down Expand Up @@ -1147,6 +1175,15 @@ func flattenMonitoringSloServiceLevelIndicatorBasicSliLatencyThreshold(v interfa
return v
}

func flattenMonitoringSloServiceLevelIndicatorBasicSliAvailability(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
transformed := make(map[string]interface{})
transformed["enabled"] = true
return []interface{}{transformed}
}

func flattenMonitoringSloServiceLevelIndicatorRequestBasedSli(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -1606,6 +1643,13 @@ func expandMonitoringSloServiceLevelIndicatorBasicSli(v interface{}, d Terraform
transformed["latency"] = transformedLatency
}

transformedAvailability, err := expandMonitoringSloServiceLevelIndicatorBasicSliAvailability(original["availability"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAvailability); val.IsValid() && !isEmptyValue(val) {
transformed["availability"] = transformedAvailability
}

return transformed, nil
}

Expand Down Expand Up @@ -1647,6 +1691,29 @@ func expandMonitoringSloServiceLevelIndicatorBasicSliLatencyThreshold(v interfac
return v, nil
}

func expandMonitoringSloServiceLevelIndicatorBasicSliAvailability(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedEnabled, err := expandMonitoringSloServiceLevelIndicatorBasicSliAvailabilityEnabled(original["enabled"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedEnabled); val.IsValid() && !isEmptyValue(val) {
transformed["enabled"] = transformedEnabled
}

return transformed, nil
}

func expandMonitoringSloServiceLevelIndicatorBasicSliAvailabilityEnabled(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandMonitoringSloServiceLevelIndicatorRequestBasedSli(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -2223,5 +2290,14 @@ func resourceMonitoringSloEncoder(d *schema.ResourceData, meta interface{}, obj
// Name/Service Level Objective ID is a query parameter and cannot
// be given in data
delete(obj, "sloId")
Sli := obj["serviceLevelIndicator"].(map[string]interface{})
if basicSli, ok := Sli["basicSli"].(map[string]interface{}); ok {
//Removing the dummy `enabled` attribute
if availability, ok := basicSli["availability"]; ok {
transAvailability := availability.(map[string]interface{})
delete(transAvailability, "enabled")
basicSli["availability"] = transAvailability
}
}
return obj, nil
}
54 changes: 54 additions & 0 deletions google-beta/resource_monitoring_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ func TestAccMonitoringSlo_basic(t *testing.T) {
})
}

func TestAccMonitoringSlo_availabilitySli(t *testing.T) {
t.Parallel()

var generatedId string
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMonitoringSloDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccMonitoringSlo_availabilitySli(),
Check: setTestCheckMonitoringSloId("google_monitoring_slo.primary", &generatedId),
},
{
ResourceName: "google_monitoring_slo.primary",
ImportState: true,
ImportStateVerify: true,
// Ignore input-only field for import
ImportStateVerifyIgnore: []string{"service"},
},
{
Config: testAccMonitoringSlo_basicUpdate(),
Check: testCheckMonitoringSloIdAfterUpdate("google_monitoring_slo.primary", &generatedId),
},
{
ResourceName: "google_monitoring_slo.primary",
ImportState: true,
ImportStateVerify: true,
// Ignore input-only field for import
ImportStateVerifyIgnore: []string{"service"},
},
},
})
}
func TestAccMonitoringSlo_requestBased(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -409,6 +443,26 @@ resource "google_monitoring_slo" "primary" {
`
}

func testAccMonitoringSlo_availabilitySli() string {
return `
data "google_monitoring_app_engine_service" "ae" {
module_id = "default"
}
resource "google_monitoring_slo" "primary" {
service = data.google_monitoring_app_engine_service.ae.service_id
goal = 0.9
rolling_period_days = 1
basic_sli {
availability {
}
}
}
`
}

func testAccMonitoringSloForSli(randSuffix, sliConfig string) string {
return fmt.Sprintf(`
resource "google_monitoring_custom_service" "srv" {
Expand Down
13 changes: 12 additions & 1 deletion website/docs/r/monitoring_slo.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ The `basic_sli` block supports:
field will result in an error.

* `latency` -
(Required)
(Optional)
Parameters for a latency threshold SLI.
Structure is documented below.

* `availability` -
(Optional)
Availability based SLI, dervied from count of requests made to this service that return successfully.
Structure is documented below.


The `latency` block supports:

Expand All @@ -364,6 +369,12 @@ The `latency` block supports:
Good service is defined to be the count of requests made to
this service that return in no more than threshold.

The `availability` block supports:

* `enabled` -
(Optional)
Whether an availability SLI is enabled or not. Must be set to `true. Defaults to `true`.

The `request_based_sli` block supports:

* `good_total_ratio` -
Expand Down

0 comments on commit a28e1c4

Please sign in to comment.