From eb228188fb10e5bc7fc32b326618011c0672816e Mon Sep 17 00:00:00 2001 From: Tomasz Dudzik Date: Mon, 27 May 2024 10:20:51 +0200 Subject: [PATCH] Add recurrence to muting rule --- go.mod | 4 +- go.sum | 6 ++- .../resource_signalfx_alert_muting_rule.go | 43 ++++++++++++++++++ ...esource_signalfx_alert_muting_rule_test.go | 45 ++++++------------- .../docs/r/alert_muting_rule.html.markdown | 8 ++++ 5 files changed, 71 insertions(+), 35 deletions(-) diff --git a/go.mod b/go.mod index 1580ea23..b1a4066b 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.4 github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 github.com/mitchellh/go-homedir v1.1.0 - github.com/signalfx/signalfx-go v1.35.0 + github.com/signalfx/signalfx-go v1.36.0 github.com/stretchr/testify v1.8.2 ) @@ -63,7 +63,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect google.golang.org/grpc v1.57.1 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a105e16a..d1b315f4 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,8 @@ github.com/signalfx/signalfx-go v1.34.0 h1:OQ6tyMY4efWB57EPIQqrpWrAfcSdyfa+bLtmA github.com/signalfx/signalfx-go v1.34.0/go.mod h1:IpGZLPvCKNFyspAXoS480jB02mocTpo0KYd8jbl6/T8= github.com/signalfx/signalfx-go v1.35.0 h1:/IA4XuvxUOulipT71oawyRInk08JTdHPlA4ETZ20Ae4= github.com/signalfx/signalfx-go v1.35.0/go.mod h1:IpGZLPvCKNFyspAXoS480jB02mocTpo0KYd8jbl6/T8= +github.com/signalfx/signalfx-go v1.36.0 h1:btfNrjb/MWAku7Ibj5BUamArVkyV9XI/fYqA0ytLdEo= +github.com/signalfx/signalfx-go v1.36.0/go.mod h1:aVrA69k02raBhDtIL1l+KBu+H5eEmS5b0IFbqgKh7eg= github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= @@ -224,8 +226,8 @@ google.golang.org/grpc v1.57.1 h1:upNTNqv0ES+2ZOOqACwVtS3Il8M12/+Hz41RCPzAjQg= google.golang.org/grpc v1.57.1/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/signalfx/resource_signalfx_alert_muting_rule.go b/signalfx/resource_signalfx_alert_muting_rule.go index fb065941..f9ee2cab 100644 --- a/signalfx/resource_signalfx_alert_muting_rule.go +++ b/signalfx/resource_signalfx_alert_muting_rule.go @@ -57,6 +57,27 @@ func alertMutingRuleResource() *schema.Resource { }, }, }, + "recurrence": { + Type: schema.TypeSet, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "unit": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"d", "w"}, false), + Description: "unit of the period. Can be days (d) or weeks (w)", + }, + "value": { + Type: schema.TypeInt, + Required: true, + Description: "amount of time, expressed as an integer applicable to the unit", + ValidateFunc: validation.IntAtLeast(1), + }, + }, + }, + }, "start_time": { Type: schema.TypeInt, Required: true, @@ -126,6 +147,17 @@ func getPayloadAlertMutingRule(d *schema.ResourceData) (*alertmuting.CreateUpdat StopTime: int64(d.Get("stop_time").(int) * 1000), } + if recurrence, ok := d.GetOk("recurrence"); ok { + tfRecurrences := recurrence.(*schema.Set).List() + if len(tfRecurrences) > 0 { + recurrence := tfRecurrences[0].(map[string]interface{}) + cuamrr.Recurrence = &alertmuting.AlertMutingRuleRecurrence{ + Unit: recurrence["unit"].(string), + Value: int32(recurrence["value"].(int)), + } + } + } + return cuamrr, nil } @@ -232,6 +264,17 @@ func alertMutingRuleAPIToTF(d *schema.ResourceData, amr *alertmuting.AlertMuting } } + if amr.Recurrence != nil { + d.Set("recurrence", []interface{}{ + map[string]interface{}{ + "unit": amr.Recurrence.Unit, + "value": amr.Recurrence.Value, + }, + }) + } else { + d.Set("recurrence", nil) + } + return nil } diff --git a/signalfx/resource_signalfx_alert_muting_rule_test.go b/signalfx/resource_signalfx_alert_muting_rule_test.go index fb70ef25..30326692 100644 --- a/signalfx/resource_signalfx_alert_muting_rule_test.go +++ b/signalfx/resource_signalfx_alert_muting_rule_test.go @@ -11,34 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -const newAlertMutingRuleConfig = ` -resource "signalfx_alert_muting_rule" "rool_mooter_one" { - description = "mooted it" - - start_time = 1573063243 - - filter { - property = "foo" - property_value = "bar" - } -} -` - -const updatedAlertMutingRuleConfig = ` -resource "signalfx_alert_muting_rule" "rool_mooter_one" { - description = "mooted it NEW" - - start_time = 1573063243 - - filter { - property = "foo" - property_value = "bar" - } -} -` - func TestAccCreateUpdateFutureAlertMutingRule(t *testing.T) { - firstTime := time.Now().Unix() + 86400 secondTime := time.Now().Unix() + (86400 * 2) @@ -52,6 +25,11 @@ func TestAccCreateUpdateFutureAlertMutingRule(t *testing.T) { property = "foo" property_value = "bar" } + + recurrence { + unit = "d" + value = 2 + } } `, firstTime) @@ -65,6 +43,11 @@ func TestAccCreateUpdateFutureAlertMutingRule(t *testing.T) { property = "foo" property_value = "bar" } + + recurrence { + unit = "d" + value = 4 + } } `, secondTime) @@ -80,6 +63,8 @@ func TestAccCreateUpdateFutureAlertMutingRule(t *testing.T) { testAccCreateUpdateAlertMutingRuleResourceExists, resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "description", "mooted it FUTURE"), resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "start_time", strconv.Itoa(int(firstTime))), + resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "recurrence.0.unit", "d"), + resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "recurrence.0.value", "2"), ), }, { @@ -97,16 +82,14 @@ func TestAccCreateUpdateFutureAlertMutingRule(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCreateUpdateAlertMutingRuleResourceExists, resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "start_time", strconv.Itoa(int(secondTime))), + resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "recurrence.0.unit", "d"), + resource.TestCheckResourceAttr("signalfx_alert_muting_rule.rool_mooter_two", "recurrence.0.value", "4"), ), }, }, }) } -func TestAccCreateUpdateAlertMutingRule(t *testing.T) { - -} - func testAccCreateUpdateAlertMutingRuleResourceExists(s *terraform.State) error { client := newTestClient() for _, rs := range s.RootModule().Resources { diff --git a/website/docs/r/alert_muting_rule.html.markdown b/website/docs/r/alert_muting_rule.html.markdown index 5d35c69f..54852988 100644 --- a/website/docs/r/alert_muting_rule.html.markdown +++ b/website/docs/r/alert_muting_rule.html.markdown @@ -29,6 +29,11 @@ resource "signalfx_alert_muting_rule" "rool_mooter_one" { property = "foo" property_value = "bar" } + + recurrence { + unit = "d" + value = 2 + } } ``` @@ -42,6 +47,9 @@ resource "signalfx_alert_muting_rule" "rool_mooter_one" { * `property` - (Required) The property to filter. * `property_value` - (Required) The property value to filter. * `negated` - (Optional) Determines if this is a "not" filter. Defaults to `false`. +* `recurrence` - (Optional) Defines the recurrence of the muting rule. Allows setting a recurring muting rule based on specified days or weeks. + * `unit` - (Required) The unit of the period. Can be days (d) or weeks (w). + * `value` - (Required) The amount of time, expressed as an integer, applicable to the unit specified. ## Attributes