Skip to content

Commit

Permalink
[ORCA-3486] - Reuse shared Event Orchestration Path logic, add import…
Browse files Browse the repository at this point in the history
… tests (#509)

* ORCA-3486 - add import tests for router/unrouted

* ORCA-3486 - add import tests for router, unrouted

* reuse severity/event_action validation functions in unrouted/service

* reuse variables and extractions schema in router/unrouted

* reuse shared conditions schema and mapping functions in router/unrouted/service
  • Loading branch information
alenapan authored May 20, 2022
1 parent 342bd0e commit fb2f206
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 237 deletions.
82 changes: 75 additions & 7 deletions pagerduty/event_orchestration_path_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,65 @@ import (
"github.com/heimweh/go-pagerduty/pagerduty"
)

var PagerDutyEventOrchestrationPathParent = map[string]*schema.Schema{
"id": {
var eventOrchestrationPathConditionsSchema = map[string]*schema.Schema{
"expression": {
Type: schema.TypeString,
Required: true,
},
}

var eventOrchestrationPathVariablesSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"path": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
Required: true,
},
"self": {
"value": {
Type: schema.TypeString,
Computed: true,
Required: true,
},
}

var PagerDutyEventOrchestrationPathConditions = map[string]*schema.Schema{
"expression": {
var eventOrchestrationPathExtractionsSchema = map[string]*schema.Schema{
"regex": {
Type: schema.TypeString,
Optional: true,
},
"source": {
Type: schema.TypeString,
Optional: true,
},
"target": {
Type: schema.TypeString,
Required: true,
},
"template": {
Type: schema.TypeString,
Optional: true,
},
}

func validateEventOrchestrationPathSeverity() schema.SchemaValidateFunc {
return validateValueFunc([]string{
"info",
"error",
"warning",
"critical",
})
}

func validateEventOrchestrationPathEventAction() schema.SchemaValidateFunc {
return validateValueFunc([]string{
"trigger",
"resolve",
})
}

func checkExtractions(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
Expand Down Expand Up @@ -66,6 +105,35 @@ func checkExtractionAttributes(diff *schema.ResourceDiff, loc string) error {
return nil
}

func expandEventOrchestrationPathConditions(v interface{}) []*pagerduty.EventOrchestrationPathRuleCondition {
conditions := []*pagerduty.EventOrchestrationPathRuleCondition{}

for _, cond := range v.([]interface{}) {
c := cond.(map[string]interface{})

cx := &pagerduty.EventOrchestrationPathRuleCondition{
Expression: c["expression"].(string),
}

conditions = append(conditions, cx)
}

return conditions
}

func flattenEventOrchestrationPathConditions(conditions []*pagerduty.EventOrchestrationPathRuleCondition) []interface{} {
var flattendConditions []interface{}

for _, condition := range conditions {
flattendCondition := map[string]interface{}{
"expression": condition.Expression,
}
flattendConditions = append(flattendConditions, flattendCondition)
}

return flattendConditions
}

func expandEventOrchestrationPathVariables(v interface{}) []*pagerduty.EventOrchestrationPathActionVariables {
res := []*pagerduty.EventOrchestrationPathActionVariables{}

Expand Down
38 changes: 38 additions & 0 deletions pagerduty/import_pagerduty_event_orchestration_path_router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccPagerDutyEventOrchestrationPathRouter_import(t *testing.T) {
team := fmt.Sprintf("tf-name-%s", acctest.RandString(5))
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
service := fmt.Sprintf("tf-%s", acctest.RandString(5))
orchestration := fmt.Sprintf("tf-orchestration-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEventOrchestrationRouterDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyEventOrchestrationRouterConfigWithMultipleRules(team, escalationPolicy, service, orchestration),
},
{
ResourceName: "pagerduty_event_orchestration_router.router",
ImportStateIdFunc: testAccCheckPagerDutyEventOrchestrationPathRouterID,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckPagerDutyEventOrchestrationPathRouterID(s *terraform.State) (string, error) {
return s.RootModule().Resources["pagerduty_event_orchestration.orch"].Primary.ID, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccPagerDutyEventOrchestrationPathService_import(t *testing.T) {
CheckDestroy: testAccCheckPagerDutyEventOrchestrationServicePathDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyEventOrchestrationPathServiceAutomationActionsConfig(escalationPolicy, service),
Config: testAccCheckPagerDutyEventOrchestrationPathServiceAllActionsConfig(escalationPolicy, service),
},
{
ResourceName: "pagerduty_event_orchestration_service.serviceA",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccPagerDutyEventOrchestrationPathUnrouted_import(t *testing.T) {
team := fmt.Sprintf("tf-name-%s", acctest.RandString(5))
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
service := fmt.Sprintf("tf-%s", acctest.RandString(5))
orchestration := fmt.Sprintf("tf-orchestration-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEventOrchestrationPathUnroutedDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyEventOrchestrationPathUnroutedWithAllConfig(team, escalationPolicy, service, orchestration),
},
{
ResourceName: "pagerduty_event_orchestration_unrouted.unrouted",
ImportStateIdFunc: testAccCheckPagerDutyEventOrchestrationPathUnroutedID,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckPagerDutyEventOrchestrationPathUnroutedID(s *terraform.State) (string, error) {
return s.RootModule().Resources["pagerduty_event_orchestration.orch"].Primary.ID, nil
}
38 changes: 4 additions & 34 deletions pagerduty/resource_pagerduty_event_orchestration_path_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func resourcePagerDutyEventOrchestrationPathRouter() *schema.Resource {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: PagerDutyEventOrchestrationPathConditions,
Schema: eventOrchestrationPathConditionsSchema,
},
},
"actions": {
Expand Down Expand Up @@ -235,32 +235,15 @@ func expandRules(v interface{}) []*pagerduty.EventOrchestrationPathRule {
ID: r["id"].(string),
Label: r["label"].(string),
Disabled: r["disabled"].(bool),
Conditions: expandRouterConditions(r["conditions"].(interface{})),
Actions: expandRouterActions(r["actions"].([]interface{})),
Conditions: expandEventOrchestrationPathConditions(r["conditions"]),
Actions: expandRouterActions(r["actions"]),
}

rules = append(rules, ruleInSet)
}
return rules
}

func expandRouterConditions(v interface{}) []*pagerduty.EventOrchestrationPathRuleCondition {
items := v.([]interface{})
conditions := []*pagerduty.EventOrchestrationPathRuleCondition{}

for _, cond := range items {
c := cond.(map[string]interface{})

cx := &pagerduty.EventOrchestrationPathRuleCondition{
Expression: c["expression"].(string),
}

conditions = append(conditions, cx)
}

return conditions
}

func expandRouterActions(v interface{}) *pagerduty.EventOrchestrationPathRuleActions {
var actions = new(pagerduty.EventOrchestrationPathRuleActions)
for _, ai := range v.([]interface{}) {
Expand Down Expand Up @@ -302,7 +285,7 @@ func flattenRules(rules []*pagerduty.EventOrchestrationPathRule) []interface{} {
"id": rule.ID,
"label": rule.Label,
"disabled": rule.Disabled,
"conditions": flattenRouterConditions(rule.Conditions),
"conditions": flattenEventOrchestrationPathConditions(rule.Conditions),
"actions": flattenRouterActions(rule.Actions),
}
flattenedRules = append(flattenedRules, flattenedRule)
Expand All @@ -311,19 +294,6 @@ func flattenRules(rules []*pagerduty.EventOrchestrationPathRule) []interface{} {
return flattenedRules
}

func flattenRouterConditions(conditions []*pagerduty.EventOrchestrationPathRuleCondition) []interface{} {
var flattendConditions []interface{}

for _, condition := range conditions {
flattendCondition := map[string]interface{}{
"expression": condition.Expression,
}
flattendConditions = append(flattendConditions, flattendCondition)
}

return flattendConditions
}

func flattenRouterActions(actions *pagerduty.EventOrchestrationPathRuleActions) []map[string]interface{} {
var actionsMap []map[string]interface{}

Expand Down
Loading

0 comments on commit fb2f206

Please sign in to comment.