Skip to content

Commit

Permalink
fix integration_action filter conditions field extra-properties (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanheuverzwijn authored May 17, 2024
1 parent 67be33b commit 1fe1e11
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
49 changes: 44 additions & 5 deletions opsgenie/resource_opsgenie_integration_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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-")

Check failure on line 684 in opsgenie/resource_opsgenie_integration_action.go

View workflow job for this annotation

GitHub Actions / ci-test

undefined: strings.CutPrefix

Check failure on line 684 in opsgenie/resource_opsgenie_integration_action.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: strings.CutPrefix
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)
}
Expand Down
74 changes: 74 additions & 0 deletions opsgenie/resource_opsgenie_integration_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit 1fe1e11

Please sign in to comment.