diff --git a/opsgenie/resource_opsgenie_integration_action.go b/opsgenie/resource_opsgenie_integration_action.go index b271214c..a6b7fe9b 100644 --- a/opsgenie/resource_opsgenie_integration_action.go +++ b/opsgenie/resource_opsgenie_integration_action.go @@ -2,10 +2,13 @@ package opsgenie import ( "context" + "fmt" + "log" + "strings" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ogClient "github.com/opsgenie/opsgenie-go-sdk-v2/client" "github.com/opsgenie/opsgenie-go-sdk-v2/og" - "log" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/opsgenie/opsgenie-go-sdk-v2/integration" @@ -564,13 +567,43 @@ func expandOpsgenieFilter(input []interface{}) integration.Filter { filter := integration.Filter{} for _, r := range input { inputMap := r.(map[string]interface{}) - conditions := expandOpsgenieConditions(inputMap["conditions"].([]interface{})) + conditions := expandOpsgenieIntegrationConditions(inputMap["conditions"].([]interface{})) filter.Conditions = conditions filter.ConditionMatchType = og.ConditionMatchType(inputMap["type"].(string)) } return filter } +func expandOpsgenieIntegrationConditions(input []interface{}) []og.Condition { + conditions := make([]og.Condition, 0, len(input)) + + if input == nil { + return conditions + } + for _, v := range input { + inputMap := v.(map[string]interface{}) + condition := og.Condition{} + key := inputMap["key"].(string) + if key != "" { + condition.Key = inputMap["key"].(string) + } + condition.Field = og.ConditionFieldType(inputMap["field"].(string)) + if condition.Field == og.ExtraProperties { + condition.Field = og.ConditionFieldType(fmt.Sprintf("extra_properties_key_prefix-%s", condition.Key)) + condition.Key = "" + } + isNot := inputMap["not"].(bool) + condition.IsNot = &isNot + condition.Operation = og.ConditionOperation(inputMap["operation"].(string)) + condition.ExpectedValue = inputMap["expected_value"].(string) + order := inputMap["order"].(int) + condition.Order = &order + conditions = append(conditions, condition) + } + + return conditions +} + func expandOpsgenieIntegrationActions(input interface{}) []integration.IntegrationAction { actions := make([]integration.IntegrationAction, 0) @@ -648,12 +681,18 @@ func flattenOpsgenieFilter(input *integration.Filter) []map[string]interface{} { for _, r := range input.Conditions { conditionMap := make(map[string]interface{}) conditionMap["order"] = r.Order - if r.Key != "" { - conditionMap["key"] = r.Key + key, found := strings.CutPrefix(string(r.Field), "extra_properties_key_prefix-") + if found { + conditionMap["key"] = key + conditionMap["field"] = og.ExtraProperties + } else { + conditionMap["field"] = r.Field + if r.Key != "" { + conditionMap["key"] = r.Key + } } conditionMap["expected_value"] = r.ExpectedValue conditionMap["operation"] = r.Operation - conditionMap["field"] = r.Field conditionMap["not"] = r.IsNot conditions = append(conditions, conditionMap) } diff --git a/opsgenie/resource_opsgenie_integration_action_test.go b/opsgenie/resource_opsgenie_integration_action_test.go index 7dbf1970..5f5e4b27 100644 --- a/opsgenie/resource_opsgenie_integration_action_test.go +++ b/opsgenie/resource_opsgenie_integration_action_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "os" "strings" "testing" @@ -116,6 +117,55 @@ func TestAccOpsGenieIntegrationAction_complete(t *testing.T) { }) } +func TestAccOpsGenieIntegrationAction_extraProperties(t *testing.T) { + rString := acctest.RandString(6) + + config := testAccOpsGenieIntegrationAction_extraProperties(rString) + + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + CheckDestroy: testCheckOpsGenieIntegrationActionDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckOpsGenieIntegrationActionExists("opsgenie_integration_action.test"), + testCheckOpsGenieIntegrationActionHasExtraProperties("opsgenie_integration_action.test"), + ), + }, + }, + }) +} + +func testCheckOpsGenieIntegrationActionHasExtraProperties(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + client, err := integration.NewClient(testAccProvider.Meta().(*OpsgenieClient).client.Config) + if err != nil { + return err + } + Id := rs.Primary.Attributes["id"] + + req := integration.GetIntegrationActionsRequest{ + Id: Id, + } + + r, err := client.GetActions(context.Background(), &req) + if err != nil { + return fmt.Errorf("Bad: ApiIntegration with id %q does not exist", Id) + } + if r.Create[0].Filter.Conditions[0].Field != "extra_properties_key_prefix-TEST_KEY" { + return fmt.Errorf("Bad: ApiIntegration with id %q does not have a condition with key 'TEST_KEY'. Got '%s' instead", Id, r.Create[0].Filter.Conditions[0].Key) + } + return nil + } +} + func testCheckOpsGenieIntegrationActionDestroy(s *terraform.State) error { client, err := integration.NewClient(testAccProvider.Meta().(*OpsgenieClient).client.Config) if err != nil { @@ -401,3 +451,27 @@ resource "opsgenie_integration_action" "test_email" { } `, rString, rString, rString, rString, rString, rString) } + +func testAccOpsGenieIntegrationAction_extraProperties(rString string) string { + return fmt.Sprintf(` +resource "opsgenie_integration_action" "test" { + integration_id = opsgenie_api_integration.test.id + create { + name = "Test close action" + filter { + type = "match-all-conditions" + conditions { + field = "extra-properties" + operation = "equals" + expected_value = "TEST_VALUE" + key = "TEST_KEY" + } + } + } +} +resource "opsgenie_api_integration" "test" { + type = "API" + name = "genieintegration-%s" +} +`, rString) +}