Skip to content
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

Kchan dummy commit 2 #907

Closed
wants to merge 8 commits into from
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ require (
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

replace github.com/heimweh/go-pagerduty => github.com/alexzakabluk/go-pagerduty v0.0.0-20240607142119-ac9a64bba6da
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c h1:kMFnB0vCcX
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/alexzakabluk/go-pagerduty v0.0.0-20240607142119-ac9a64bba6da h1:O2mVKSglj/gEz+Z7GX+L4Y1zGRIiDxPHdfp6Ri7klww=
github.com/alexzakabluk/go-pagerduty v0.0.0-20240607142119-ac9a64bba6da/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
Expand Down Expand Up @@ -95,8 +97,6 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/heimweh/go-pagerduty v0.0.0-20240503143637-3459408ac715 h1:DbdS2LIPkhsqgRcQzOAux0RpTJSH8VYOrN4rZZgznak=
github.com/heimweh/go-pagerduty v0.0.0-20240503143637-3459408ac715/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down
4 changes: 4 additions & 0 deletions pagerduty/event_orchestration_path_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,7 @@ func emptyOrchestrationPathStructBuilder(pathType string) *pagerduty.EventOrches

return commonEmptyOrchestrationPath()
}

func isNonEmptyList(arg interface{}) bool {
return !isNilFunc(arg) && len(arg.([]interface{})) > 0
}
108 changes: 105 additions & 3 deletions pagerduty/resource_pagerduty_event_orchestration_path_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -22,6 +23,7 @@ func resourcePagerDutyEventOrchestrationPathRouter() *schema.Resource {
Importer: &schema.ResourceImporter{
StateContext: resourcePagerDutyEventOrchestrationPathRouterImport,
},
CustomizeDiff: checkDynamicRoutingRule,
Schema: map[string]*schema.Schema{
"event_orchestration": {
Type: schema.TypeString,
Expand Down Expand Up @@ -63,9 +65,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,
ValidateFunc: func(v interface{}, key string) (warns []string, errs []error) {
value := v.(string)
if value == "unrouted" {
Expand Down Expand Up @@ -113,6 +135,54 @@ func resourcePagerDutyEventOrchestrationPathRouter() *schema.Resource {
}
}

func checkDynamicRoutingRule(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
rNum := diff.Get("set.0.rule.#").(int)
draIdxs := []int{}
errorMsgs := []string{}

for ri := 0; ri < rNum; ri++ {
dra := diff.Get(fmt.Sprintf("set.0.rule.%d.actions.0.dynamic_route_to", ri))
hasDra := isNonEmptyList(dra)
if !hasDra {
continue
}
draIdxs = append(draIdxs, ri)
}
// 1. Only the first rule of the first ("start") set can have the Dynamic Routing action:
if len(draIdxs) > 1 {
idxs := []string{}
for _, idx := range draIdxs {
idxs = append(idxs, fmt.Sprintf("%d", idx))
}
errorMsgs = append(errorMsgs, fmt.Sprintf("A Router can have at most one Dynamic Routing rule; Rules with the dynamic_route_to action found at indexes: %s", strings.Join(idxs, ", ")))
}
// 2. The Dynamic Routing action can only be used in the first rule of the first set:
if len(draIdxs) > 0 && draIdxs[0] != 0 {
errorMsgs = append(errorMsgs, fmt.Sprintf("The Dynamic Routing rule must be the first rule in a Router"))
}
// 3. If the Dynamic Routing rule is the first rule of the first set,
// validate its configuration. It cannot have any conditions or the `route_to` action:
if len(draIdxs) == 1 && draIdxs[0] == 0 {
condNum := diff.Get("set.0.rule.0.condition.#").(int)
// diff.NewValueKnown(str) will return false if the value is based on interpolation that was unavailable at diff time,
// which may be the case for the `route_to` action when it references a pagerduty_service resource.
// Source: https://pkg.go.dev/github.com/hashicorp/terraform-plugin-sdk/helper/schema#ResourceDiff.NewValueKnown
routeToValueKnown := diff.NewValueKnown("set.0.rule.0.actions.0.route_to")
routeTo := diff.Get("set.0.rule.0.actions.0.route_to").(string)
if condNum > 0 {
errorMsgs = append(errorMsgs, fmt.Sprintf("Dynamic Routing rules cannot have conditions"))
}
if !routeToValueKnown || routeToValueKnown && routeTo != "" {
errorMsgs = append(errorMsgs, fmt.Sprintf("Dynamic Routing rules cannot have the `route_to` action"))
}
}

if len(errorMsgs) > 0 {
return fmt.Errorf("Invalid Dynamic Routing rule configuration:\n- %s", strings.Join(errorMsgs, "\n- "))
}
return nil
}

func resourcePagerDutyEventOrchestrationPathRouterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

Expand Down Expand Up @@ -294,7 +364,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 isNonEmptyList(dra) {
actions.DynamicRouteTo = expandRouterDynamicRouteToAction(dra)
} else {
actions.RouteTo = am["route_to"].(string)
}
}

return actions
Expand All @@ -311,6 +386,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 {
Expand Down Expand Up @@ -344,7 +430,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
}
Expand All @@ -360,6 +450,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 {
Expand Down
Loading
Loading