Skip to content

Commit

Permalink
implement integration action custom_priority, update go-sdk (#207)
Browse files Browse the repository at this point in the history
authored-by: ilteriş tabak <[email protected]>
  • Loading branch information
ilteristabak authored Dec 24, 2020
1 parent 158f793 commit e5fcdff
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.6 (December 24, 2020)
IMPROVEMENTS:
* **Integration Actions :** add custom priority for create action (#177)


## 0.5.5 (December 16, 2020)
Improvement:
* Add support for webhook integration (#197)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.14
require (
github.com/hashicorp/go-retryablehttp v0.6.6
github.com/hashicorp/terraform-plugin-sdk v1.15.0
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.3-0.20201204102414-d90cad05198b
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.4
github.com/pkg/errors v0.8.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.2-0.20200911071451-adf7a1c79aac h1:S
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.2-0.20200911071451-adf7a1c79aac/go.mod h1:VOkJ7STzYj+nXRhMcBTcmt8uZZ17KZKJdZtJpgHLbT8=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.3-0.20201204102414-d90cad05198b h1:Nzbg89GTBuyy6Cy0+M3bwHyy1YHO3W9CS3Z909GImlE=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.3-0.20201204102414-d90cad05198b/go.mod h1:VOkJ7STzYj+nXRhMcBTcmt8uZZ17KZKJdZtJpgHLbT8=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.4 h1:ZMCrwMWK2FKSOGJjxrJdaNuwBRIIhUJgByZvX83jIkU=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.4/go.mod h1:VOkJ7STzYj+nXRhMcBTcmt8uZZ17KZKJdZtJpgHLbT8=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
8 changes: 8 additions & 0 deletions opsgenie/resource_opsgenie_integration_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func resourceOpsgenieIntegrationAction() *schema.Resource {
Type: schema.TypeString,
},
},
"custom_priority": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -520,6 +524,9 @@ func expandOpsgenieIntegrationActions(input interface{}) []integration.Integrati
if priority := inputMap["priority"]; priority != nil {
action.Priority = priority.(string)
}
if customPriority := inputMap["custom_priority"]; customPriority != nil {
action.CustomPriority = customPriority.(string)
}
filters := expandOpsgenieFilter(inputMap["filter"].([]interface{}))
action.Filter = &filters

Expand Down Expand Up @@ -601,6 +608,7 @@ func flattenOpsgenieIntegrationActions(input []integration.IntegrationAction) []
if action.Type == "create" {
actionMap["source"] = action.Source
actionMap["priority"] = action.Priority
actionMap["custom_priority"] = action.CustomPriority
actionMap["message"] = action.Message
actionMap["description"] = action.Description
actionMap["entity"] = action.Entity
Expand Down
86 changes: 86 additions & 0 deletions opsgenie/resource_opsgenie_integration_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ func TestAccOpsGenieIntegrationAction_basic(t *testing.T) {
})
}

func TestAccOpsGenieIntegrationAction_custompriority(t *testing.T) {
customPriority := "{{condition_name.extract(/^\\[(\\S+)\\].*$/, 1)}"
customPriorityEscaped := "{{condition_name.extract(/^\\\\[(\\\\S+)\\\\].*$/, 1)}"
rs := acctest.RandString(6)
config := testAccOpsGenieIntegrationAction_custompriority(rs, customPriorityEscaped)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testCheckOpsGenieIntegrationActionDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckOpsGenieIntegrationActionCustomPriorityExists("opsgenie_integration_action.testcustom", customPriority),
),
},
},
})
}

func TestAccOpsGenieIntegrationAction_complete(t *testing.T) {
rString := acctest.RandString(6)

Expand Down Expand Up @@ -150,6 +170,35 @@ func testCheckOpsGenieIntegrationActionExists(name string) resource.TestCheckFun
}
}

func testCheckOpsGenieIntegrationActionCustomPriorityExists(name, customPriority 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,
}

iAction, err := client.GetActions(context.Background(), &req)
if err != nil {
return fmt.Errorf("Bad: ApiIntegration with id %q does not exist", Id)
}
if iAction.Create[0].CustomPriority != customPriority {
return fmt.Errorf("Bad: CustomPriority %s for ApiIntegration with id %q does not exist", customPriority, Id)
}
return nil
}
}

func testAccOpsGenieIntegrationAction_basic(rString string) string {
return fmt.Sprintf(`
resource "opsgenie_integration_action" "test" {
Expand All @@ -173,6 +222,43 @@ resource "opsgenie_api_integration" "test" {
`, rString)
}

func testAccOpsGenieIntegrationAction_custompriority(rString, crString string) string {
return fmt.Sprintf(`
resource "opsgenie_api_integration" "test" {
type = "API"
name = "genieintegration-custompri-%s"
}
resource "opsgenie_integration_action" "testcustom" {
integration_id = opsgenie_api_integration.test.id
create {
name = "Create high priority alerts"
tags = ["CRITICAL", "SEV-0"]
user = "Acceptance test user"
note = "{{note}}"
alias = "{{alias}}"
source = "{{source}}"
message = "{{message}}"
description = "{{description}}"
entity = "{{entity}}"
custom_priority = "%s"
alert_actions = ["Check error rate"]
extra_properties = map(
"Environment", "test-env",
"Region", "us-west-2"
)
filter {
type = "match-all-conditions"
conditions {
field = "priority"
operation = "equals"
expected_value = "P1"
}
}
}
}
`, rString, crString)
}

func testAccOpsGenieIntegrationAction_complete(rString string) string {
return fmt.Sprintf(`
resource "opsgenie_user" "test" {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ github.com/mitchellh/mapstructure
github.com/mitchellh/reflectwalk
# github.com/oklog/run v1.0.0
github.com/oklog/run
# github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.3-0.20201204102414-d90cad05198b
# github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.4
## explicit
github.com/opsgenie/opsgenie-go-sdk-v2/alert
github.com/opsgenie/opsgenie-go-sdk-v2/client
Expand Down
20 changes: 20 additions & 0 deletions website/docs/r/integration_action.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ resource "opsgenie_integration_action" "test_action" {
}
}
}
create {
name = "Create alert with priority from message"
custom_priority = "{{message.substringAfter(\"[custom]\")}}"
filter {
type = "match-all-conditions"
conditions {
field = "tags"
operation = "contains"
expected_value = "P5"
}
conditions {
field = "message"
operation = "Starts With"
expected_value = "[custom]"
}
}
}
close {
name = "Low priority alerts"
Expand Down Expand Up @@ -138,6 +156,8 @@ The following arguments are common and supported for all actions:

* `priority` - (Optional) Alert priority.

* `custom_priority` - (Optional) Custom alert priority. e.g. {{message.substring(0,2)}}

* `extra_properties` - (Optional) Set of user defined properties specified as a map.

* `message` - (Optional) Alert text limited to 130 characters.
Expand Down

0 comments on commit e5fcdff

Please sign in to comment.