-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ORCA-4811] Add support for dynamic_route_to
action to pagerduty_event_orchestration_router
#1
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,9 +63,29 @@ func resourcePagerDutyEventOrchestrationPathRouter() *schema.Resource { | |
MaxItems: 1, // there can only be one action for router | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"dynamic_route_to": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"lookup_by": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"regex": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"source": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"route_to": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Optional: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation: We can't have either |
||
ValidateFunc: func(v interface{}, key string) (warns []string, errs []error) { | ||
value := v.(string) | ||
if value == "unrouted" { | ||
|
@@ -294,7 +314,12 @@ func expandRouterActions(v interface{}) *pagerduty.EventOrchestrationPathRuleAct | |
actions := new(pagerduty.EventOrchestrationPathRuleActions) | ||
for _, ai := range v.([]interface{}) { | ||
am := ai.(map[string]interface{}) | ||
actions.RouteTo = am["route_to"].(string) | ||
dra := am["dynamic_route_to"] | ||
if !isNilFunc(dra) && len(dra.([]interface{})) > 0 { | ||
actions.DynamicRouteTo = expandRouterDynamicRouteToAction(dra) | ||
} else { | ||
actions.RouteTo = am["route_to"].(string) | ||
} | ||
Comment on lines
+318
to
+322
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation: The |
||
} | ||
|
||
return actions | ||
|
@@ -311,6 +336,17 @@ func expandCatchAll(v interface{}) *pagerduty.EventOrchestrationPathCatchAll { | |
return catchAll | ||
} | ||
|
||
func expandRouterDynamicRouteToAction(v interface{}) *pagerduty.EventOrchestrationPathDynamicRouteTo { | ||
dr := new(pagerduty.EventOrchestrationPathDynamicRouteTo) | ||
for _, i := range v.([]interface{}) { | ||
dra := i.(map[string]interface{}) | ||
dr.LookupBy = dra["lookup_by"].(string) | ||
dr.Regex = dra["regex"].(string) | ||
dr.Source = dra["source"].(string) | ||
} | ||
return dr | ||
} | ||
|
||
func flattenSets(orchPathSets []*pagerduty.EventOrchestrationPathSet) []interface{} { | ||
var flattenedSets []interface{} | ||
for _, set := range orchPathSets { | ||
|
@@ -344,7 +380,11 @@ func flattenRouterActions(actions *pagerduty.EventOrchestrationPathRuleActions) | |
var actionsMap []map[string]interface{} | ||
|
||
am := make(map[string]interface{}) | ||
am["route_to"] = actions.RouteTo | ||
if actions.DynamicRouteTo != nil { | ||
am["dynamic_route_to"] = flattenRouterDynamicRouteToAction(actions.DynamicRouteTo) | ||
} else { | ||
am["route_to"] = actions.RouteTo | ||
} | ||
actionsMap = append(actionsMap, am) | ||
return actionsMap | ||
} | ||
|
@@ -360,6 +400,18 @@ func flattenCatchAll(catchAll *pagerduty.EventOrchestrationPathCatchAll) []map[s | |
return caMap | ||
} | ||
|
||
func flattenRouterDynamicRouteToAction(dra *pagerduty.EventOrchestrationPathDynamicRouteTo) []map[string]interface{} { | ||
var dr []map[string]interface{} | ||
|
||
dr = append(dr, map[string]interface{}{ | ||
"lookup_by": dra.LookupBy, | ||
"regex": dra.Regex, | ||
"source": dra.Source, | ||
}) | ||
|
||
return dr | ||
} | ||
|
||
func resourcePagerDutyEventOrchestrationPathRouterImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
client, err := meta.(*Config).Client() | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"github.com/heimweh/go-pagerduty/pagerduty" | ||
) | ||
|
||
func init() { | ||
|
@@ -23,6 +24,17 @@ func TestAccPagerDutyEventOrchestrationPathRouter_Basic(t *testing.T) { | |
service := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
orchestration := fmt.Sprintf("tf-orchestration-%s", acctest.RandString(5)) | ||
|
||
dynamicRouteToByNameInput := &pagerduty.EventOrchestrationPathDynamicRouteTo{ | ||
LookupBy: "service_name", | ||
Regex: ".*", | ||
Source: "event.custom_details.pd_service_name", | ||
} | ||
dynamicRouteToByIDInput := &pagerduty.EventOrchestrationPathDynamicRouteTo{ | ||
LookupBy: "service_id", | ||
Regex: "ID:(.*)", | ||
Source: "event.custom_details.pd_service_id", | ||
} | ||
Comment on lines
+27
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation: I'm using these variables to 1. build the config and 2. use in the verification functions |
||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
|
@@ -48,6 +60,27 @@ func TestAccPagerDutyEventOrchestrationPathRouter_Basic(t *testing.T) { | |
"pagerduty_event_orchestration_router.router", "unrouted", true), //test for catch_all route_to prop, by default it should be unrouted | ||
), | ||
}, | ||
// Configure a Dynamic Routing rule: | ||
{ | ||
Config: testAccCheckPagerDutyEventOrchestrationRouterDynamicRouteToConfig(team, escalationPolicy, service, orchestration, dynamicRouteToByNameInput), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyEventOrchestrationRouterExists("pagerduty_event_orchestration_router.router"), | ||
testAccCheckPagerDutyEventOrchestrationRouterPathDynamicRouteToMatch("pagerduty_event_orchestration_router.router", dynamicRouteToByNameInput), | ||
testAccCheckPagerDutyEventOrchestrationRouterPathRouteToMatch( | ||
"pagerduty_event_orchestration_router.router", "unrouted", true), | ||
), | ||
}, | ||
// Update the Dynamic Routing rule added in the previous test case: | ||
{ | ||
Config: testAccCheckPagerDutyEventOrchestrationRouterDynamicRouteToConfig(team, escalationPolicy, service, orchestration, dynamicRouteToByIDInput), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckPagerDutyEventOrchestrationRouterExists("pagerduty_event_orchestration_router.router"), | ||
testAccCheckPagerDutyEventOrchestrationRouterPathDynamicRouteToMatch("pagerduty_event_orchestration_router.router", dynamicRouteToByIDInput), | ||
testAccCheckPagerDutyEventOrchestrationRouterPathRouteToMatch( | ||
"pagerduty_event_orchestration_router.router", "unrouted", true), | ||
), | ||
}, | ||
// Delete the Dynamic Routing rule added in the previous test cases: | ||
{ | ||
Config: testAccCheckPagerDutyEventOrchestrationRouterConfigWithConditions(team, escalationPolicy, service, orchestration), | ||
Check: resource.ComposeTestCheckFunc( | ||
|
@@ -279,6 +312,45 @@ func testAccCheckPagerDutyEventOrchestrationRouterConfig(t, ep, s, o string) str | |
`) | ||
} | ||
|
||
func testAccCheckPagerDutyEventOrchestrationRouterDynamicRouteToConfig(t, ep, s, o string, dynamicRouteToByNameInput *pagerduty.EventOrchestrationPathDynamicRouteTo) string { | ||
routerConfig := fmt.Sprintf( | ||
`resource "pagerduty_event_orchestration_router" "router" { | ||
event_orchestration = pagerduty_event_orchestration.orch.id | ||
|
||
catch_all { | ||
actions { | ||
route_to = "unrouted" | ||
} | ||
} | ||
set { | ||
id = "start" | ||
rule { | ||
disabled = false | ||
label = "dynamic routing rule" | ||
actions { | ||
dynamic_route_to { | ||
lookup_by = "%s" | ||
regex = "%s" | ||
source = "%s" | ||
} | ||
} | ||
} | ||
rule { | ||
label = "static routing rule" | ||
condition { | ||
expression = "event.summary matches part '[prod]'" | ||
} | ||
actions { | ||
route_to = pagerduty_service.bar.id | ||
} | ||
} | ||
} | ||
} | ||
`, dynamicRouteToByNameInput.LookupBy, dynamicRouteToByNameInput.Regex, dynamicRouteToByNameInput.Source) | ||
|
||
return fmt.Sprintf("%s%s", createBaseConfig(t, ep, s, o), routerConfig) | ||
} | ||
|
||
func testAccCheckPagerDutyEventOrchestrationRouterConfigWithConditions(t, ep, s, o string) string { | ||
return fmt.Sprintf("%s%s", createBaseConfig(t, ep, s, o), | ||
`resource "pagerduty_event_orchestration_router" "router" { | ||
|
@@ -510,3 +582,28 @@ func testAccCheckPagerDutyEventOrchestrationRouterPathRouteToMatch(router, servi | |
return nil | ||
} | ||
} | ||
|
||
func testAccCheckPagerDutyEventOrchestrationRouterPathDynamicRouteToMatch(router string, expectedDynamicRouteTo *pagerduty.EventOrchestrationPathDynamicRouteTo) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
r, rOk := s.RootModule().Resources[router] | ||
if !rOk { | ||
return fmt.Errorf("Not found: %s", router) | ||
} | ||
|
||
rLookupBy := r.Primary.Attributes["set.0.rule.0.actions.0.dynamic_route_to.0.lookup_by"] | ||
rRegex := r.Primary.Attributes["set.0.rule.0.actions.0.dynamic_route_to.0.regex"] | ||
rSource := r.Primary.Attributes["set.0.rule.0.actions.0.dynamic_route_to.0.source"] | ||
|
||
if rLookupBy != expectedDynamicRouteTo.LookupBy { | ||
return fmt.Errorf("Event Orchestration Router `dynamic_route_to.lookup_by` (%v) does not match expected value: %v", rLookupBy, expectedDynamicRouteTo.LookupBy) | ||
} | ||
if rRegex != expectedDynamicRouteTo.Regex { | ||
return fmt.Errorf("Event Orchestration Router `dynamic_route_to.regex` (%v) does not match expected value: %v", rRegex, expectedDynamicRouteTo.Regex) | ||
} | ||
if rSource != expectedDynamicRouteTo.Source { | ||
return fmt.Errorf("Event Orchestration Router `dynamic_route_to.source` (%v) does not match expected value: %v", rSource, expectedDynamicRouteTo.Source) | ||
} | ||
|
||
return nil | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotation: This change as well as the changes in
go.sum
,vendor/github.com/heimweh/go-pagerduty/pagerduty/event_orchestration_path.go
, andvendor/modules.txt
is the result of temporarily pointing the provider to a feature branch ofgh.neting.cc/heimweh/go-pagerduty
:$ go mod edit -replace "github.com/heimweh/go-pagerduty=github.com/alexzakabluk/go-pagerduty@rule-actions-updates" $ go mod tidy $ go mod vendor
We can merge this change into the base branch and upgrade to the latest
go-pagerduty
version before merging into https://github.com/PagerDuty/terraform-provider-pagerduty. If we don't merge this change the base branch won't compile since theDynamicRouteTo
property will be missing in the API Client.