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

Orca 5140 eo external data cache var #981

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/alenapan/go-pagerduty v0.0.0-20250214201653-b8d11443fbc3
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/alenapan/go-pagerduty v0.0.0-20250214201653-b8d11443fbc3 h1:MBfCyrZBjR+vny6GBIdZlIelQZA11YL7EQK1GjdSiwQ=
github.com/alenapan/go-pagerduty v0.0.0-20250214201653-b8d11443fbc3/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-20250214190935-c474ec404dae h1:le7p/QnSPOQ4tnt+xjrAIu58JOdPyiNauGR8B2hRaoU=
github.com/heimweh/go-pagerduty v0.0.0-20250214190935-c474ec404dae/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
61 changes: 54 additions & 7 deletions pagerduty/event_orchestration_cache_variable_util.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 @@ -27,6 +28,7 @@ var resourceEventOrchestrationCacheVariableConfigurationSchema = map[string]*sch
ValidateDiagFunc: validateValueDiagFunc([]string{
"recent_value",
"trigger_event_count",
"external_data",
}),
},
"regex": {
Expand All @@ -37,6 +39,10 @@ var resourceEventOrchestrationCacheVariableConfigurationSchema = map[string]*sch
Type: schema.TypeString,
Optional: true,
},
"data_type": {
Type: schema.TypeString,
Optional: true,
},
"ttl_seconds": {
Type: schema.TypeInt,
Optional: true,
Expand All @@ -63,30 +69,69 @@ var dataSourceEventOrchestrationCacheVariableConfigurationSchema = map[string]*s
Type: schema.TypeString,
Computed: true,
},
"data_type": {
Type: schema.TypeString,
Optional: true,
},
"ttl_seconds": {
Type: schema.TypeInt,
Computed: true,
},
}

func checkConfiguration(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
func checkEventOrchestrationCacheVariableConfiguration(context context.Context, diff *schema.ResourceDiff, i interface{}) error {
c := diff.Get("condition").([]interface{})
t := diff.Get("configuration.0.type").(string)
s := diff.Get("configuration.0.source").(string)
r := diff.Get("configuration.0.regex").(string)
dt := diff.Get("configuration.0.data_type").(string)
ts := diff.Get("configuration.0.ttl_seconds").(int)

if t == "recent_value" && (r == "" || s == "") {
return fmt.Errorf("Invalid configuration: regex and source cannot be null when type is recent_value")
missingAttrErr := ""
unsupportedAttrErr := ""

if t == "recent_value" {
if r == "" || s == "" {
missingAttrErr = "regex and source cannot be null"
}
if dt != "" || ts != 0 {
unsupportedAttrErr = "data_type and ttl_seconds cannot be used"
}
}
if t == "trigger_event_count" && ts == 0 {
return fmt.Errorf("Invalid configuration: ttl_seconds cannot be null when type is trigger_event_count")
if t == "trigger_event_count" {
if ts == 0 {
missingAttrErr = "ttl_seconds cannot be null"
}
if r != "" || s != "" || dt != "" {
unsupportedAttrErr = "regex, source, and data_type cannot be used"
}
}
if (r != "" || s != "") && ts != 0 {
return fmt.Errorf("Invalid configuration: ttl_seconds cannot be used in conjuction with regex and source")
if t == "external_data" {
if dt == "" || ts == 0 {
missingAttrErr = "data_type and ttl_seconds cannot be null"
}
if len(c) > 0 || r != "" || s != "" {
unsupportedAttrErr = "condition, regex and source cannot be used"
}
}

if missingAttrErr != "" || unsupportedAttrErr != "" {
errors := formatErrors([]string{missingAttrErr, unsupportedAttrErr})
return fmt.Errorf("Invalid configuration: %s when type is %s", errors, t)
}
return nil
}

func formatErrors(errors []string) string {
var filtered []string
for _, s := range errors {
if s != "" {
filtered = append(filtered, s)
}
}
return strings.Join(filtered, ", and ")
}

func getIdentifier(cacheVariableType string) string {
switch cacheVariableType {
case pagerduty.CacheVariableTypeGlobal:
Expand Down Expand Up @@ -169,6 +214,7 @@ func expandEventOrchestrationCacheVariableConfiguration(v interface{}) *pagerdut
conf.Type = c["type"].(string)
conf.Regex = c["regex"].(string)
conf.Source = c["source"].(string)
conf.DataType = c["data_type"].(string)
conf.TTLSeconds = c["ttl_seconds"].(int)
}

Expand All @@ -193,6 +239,7 @@ func flattenEventOrchestrationCacheVariableConfiguration(conf *pagerduty.EventOr
"type": conf.Type,
"regex": conf.Regex,
"source": conf.Source,
"data_type": conf.DataType,
"ttl_seconds": conf.TTLSeconds,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func resourcePagerDutyEventOrchestrationGlobalCacheVariable() *schema.Resource {
Importer: &schema.ResourceImporter{
StateContext: resourcePagerDutyEventOrchestrationGlobalCacheVariableImport,
},
CustomizeDiff: checkConfiguration,
CustomizeDiff: checkEventOrchestrationCacheVariableConfiguration,
Schema: map[string]*schema.Schema{
"event_orchestration": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -26,20 +27,46 @@ func TestAccPagerDutyEventOrchestrationGlobalCacheVariable_Basic(t *testing.T) {
orchn1 := "orch_1"
name2 := fmt.Sprintf("tf_global_cache_variable_updated_%s", acctest.RandString(5))
orchn2 := "orch_2"

invalidConfigRecentValue := `
configuration {
type = "recent_value"
regex = ".*"
ttl_seconds = 300
}
`
invalidConfigTriggerEventCount := `
configuration {
type = "trigger_event_count"
data_type = "string"
ttl_seconds = 60
}
`
invalidConfigExternalData := `
configuration {
type = "external_data"
data_type = "boolean"
}
`
config1 := `
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
`
type = "trigger_event_count"
ttl_seconds = 60
}
`
config2 := `
configuration {
type = "recent_value"
source = "event.summary"
regex = ".*"
}
`
type = "recent_value"
source = "event.summary"
regex = ".*"
}
`
config3 := `
configuration {
type = "external_data"
data_type = "boolean"
ttl_seconds = 1200
}
`
cond1 := ``
cond2 := `
condition {
Expand All @@ -54,6 +81,25 @@ func TestAccPagerDutyEventOrchestrationGlobalCacheVariable_Basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableDestroy,
Steps: []resource.TestStep{
// cache variable with an invalid configuration - recent_value:
{
Config: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableConfig(orch, name1, orchn1, disabled1, invalidConfigRecentValue, cond1),
PlanOnly: true,
ExpectError: regexp.MustCompile("Invalid configuration: regex and source cannot be null, and data_type and ttl_seconds cannot be used when type is recent_value"),
},
// cache variable with an invalid configuration - trigger_event_count:
{
Config: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableConfig(orch, name1, orchn1, disabled1, invalidConfigTriggerEventCount, cond1),
PlanOnly: true,
ExpectError: regexp.MustCompile("Invalid configuration: regex, source, and data_type cannot be used when type is trigger_event_count"),
},
// cache variable with an invalid configuration - external_data:
{
Config: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableConfig(orch, name1, orchn1, disabled1, invalidConfigExternalData, cond1),
PlanOnly: true,
ExpectError: regexp.MustCompile("Invalid configuration: data_type and ttl_seconds cannot be null when type is external_data"),
},
// configure a valid cache variable:
{
Config: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableConfig(orch, name1, orchn1, disabled1, config1, cond1),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -80,6 +126,16 @@ func TestAccPagerDutyEventOrchestrationGlobalCacheVariable_Basic(t *testing.T) {
resource.TestCheckResourceAttr(cv, "configuration.0.regex", ".*"),
),
},
// update config again:
{
Config: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableConfig(orch, name1, orchn1, disabled1, config3, cond1),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableID(cv, orchn1),
resource.TestCheckResourceAttr(cv, "configuration.0.type", "external_data"),
resource.TestCheckResourceAttr(cv, "configuration.0.data_type", "boolean"),
resource.TestCheckResourceAttr(cv, "configuration.0.ttl_seconds", "1200"),
),
},
// update condition:
{
Config: testAccCheckPagerDutyEventOrchestrationGlobalCacheVariableConfig(orch, name1, orchn1, disabled1, config1, cond2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func resourcePagerDutyEventOrchestrationServiceCacheVariable() *schema.Resource
Importer: &schema.ResourceImporter{
StateContext: resourcePagerDutyEventOrchestrationServiceCacheVariableImport,
},
CustomizeDiff: checkConfiguration,
CustomizeDiff: checkEventOrchestrationCacheVariableConfiguration,
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -26,20 +27,46 @@ func TestAccPagerDutyEventOrchestrationServiceCacheVariable_Basic(t *testing.T)
svcn1 := "svc_1"
name2 := fmt.Sprintf("tf_service_cache_variable_updated_%s", acctest.RandString(5))
svcn2 := "svc_2"

invalidConfigRecentValue := `
configuration {
type = "recent_value"
regex = ".*"
ttl_seconds = 300
}
`
invalidConfigTriggerEventCount := `
configuration {
type = "trigger_event_count"
data_type = "string"
ttl_seconds = 60
}
`
invalidConfigExternalData := `
configuration {
type = "external_data"
data_type = "boolean"
}
`
config1 := `
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
`
type = "trigger_event_count"
ttl_seconds = 60
}
`
config2 := `
configuration {
type = "recent_value"
source = "event.summary"
regex = ".*"
}
`
type = "recent_value"
source = "event.summary"
regex = ".*"
}
`
config3 := `
configuration {
type = "external_data"
data_type = "boolean"
ttl_seconds = 1200
}
`
cond1 := ``
cond2 := `
condition {
Expand All @@ -54,6 +81,25 @@ func TestAccPagerDutyEventOrchestrationServiceCacheVariable_Basic(t *testing.T)
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableDestroy,
Steps: []resource.TestStep{
// cache variable with an invalid configuration - recent_value:
{
Config: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableConfig(svc, name1, svcn1, disabled1, invalidConfigRecentValue, cond1),
PlanOnly: true,
ExpectError: regexp.MustCompile("Invalid configuration: regex and source cannot be null, and data_type and ttl_seconds cannot be used when type is recent_value"),
},
// cache variable with an invalid configuration - trigger_event_count:
{
Config: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableConfig(svc, name1, svcn1, disabled1, invalidConfigTriggerEventCount, cond1),
PlanOnly: true,
ExpectError: regexp.MustCompile("Invalid configuration: regex, source, and data_type cannot be used when type is trigger_event_count"),
},
// cache variable with an invalid configuration - external_data:
{
Config: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableConfig(svc, name1, svcn1, disabled1, invalidConfigExternalData, cond1),
PlanOnly: true,
ExpectError: regexp.MustCompile("Invalid configuration: data_type and ttl_seconds cannot be null when type is external_data"),
},
// configure a valid cache variable:
{
Config: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableConfig(svc, name1, svcn1, disabled1, config1, cond1),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -80,6 +126,16 @@ func TestAccPagerDutyEventOrchestrationServiceCacheVariable_Basic(t *testing.T)
resource.TestCheckResourceAttr(cv, "configuration.0.regex", ".*"),
),
},
// update config again:
{
Config: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableConfig(svc, name1, svcn1, disabled1, config3, cond1),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyEventOrchestrationServiceCacheVariableID(cv, svcn1),
resource.TestCheckResourceAttr(cv, "configuration.0.type", "external_data"),
resource.TestCheckResourceAttr(cv, "configuration.0.data_type", "boolean"),
resource.TestCheckResourceAttr(cv, "configuration.0.ttl_seconds", "1200"),
),
},
// update condition:
{
Config: testAccCheckPagerDutyEventOrchestrationServiceCacheVariableConfig(svc, name1, svcn1, disabled1, config1, cond2),
Expand Down

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

Loading