Skip to content

Commit

Permalink
Merge pull request PagerDuty#822 from c-kaieong/ORCA-4601-add-support…
Browse files Browse the repository at this point in the history
…-for-cache-variables

[ORCA-4601] Add support for Event Orchestration Cache Variables
  • Loading branch information
imjaroiswebdev authored Apr 3, 2024
2 parents 773702b + 1727dfc commit a8a550f
Show file tree
Hide file tree
Showing 21 changed files with 2,369 additions and 66 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/hashicorp/terraform-plugin-mux v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/heimweh/go-pagerduty v0.0.0-20240226201314-bfc8dce0a3ff
github.com/heimweh/go-pagerduty v0.0.0-20240403153232-5876af2ce24a
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ 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-20240226201314-bfc8dce0a3ff h1:w5M1cWVKdfMoQnSyzmLYFSpByn7cnC86vKZTiCBPiwY=
github.com/heimweh/go-pagerduty v0.0.0-20240226201314-bfc8dce0a3ff/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s=
github.com/heimweh/go-pagerduty v0.0.0-20240403153232-5876af2ce24a h1:upvfy2kYdl/poYpITYmq6ZqJb5mu9zHm4V0YeXlyNOM=
github.com/heimweh/go-pagerduty v0.0.0-20240403153232-5876af2ce24a/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pagerduty

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func dataSourcePagerDutyEventOrchestrationGlobalCacheVariable() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePagerDutyEventOrchestrationGlobalCacheVariableRead,
Schema: map[string]*schema.Schema{
"event_orchestration": {
Type: schema.TypeString,
Required: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"condition": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: dataSourceEventOrchestrationCacheVariableConditionSchema,
},
},
"configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: dataSourceEventOrchestrationCacheVariableConfigurationSchema,
},
},
"disabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourcePagerDutyEventOrchestrationGlobalCacheVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return dataSourceEventOrchestrationCacheVariableRead(ctx, d, meta, pagerduty.CacheVariableTypeGlobal)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package pagerduty

import (
"fmt"
"regexp"
"testing"

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

func TestAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariable_Basic(t *testing.T) {
on := fmt.Sprintf("tf-orchestration-%s", acctest.RandString(5))
name := fmt.Sprintf("tf_global_cache_variable_%s", acctest.RandString(5))
irn := "pagerduty_event_orchestration_global_cache_variable.orch_cv"
n := "data.pagerduty_event_orchestration_global_cache_variable.by_id"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
// find by id
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableByIdConfig(on, name),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariable(irn, n),
),
},
// find by id, ignore name
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableByIdNameConfig(on, name),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariable(irn, n),
),
},
// find by name
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableByNameConfig(on, name),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariable(irn, n),
),
},
// id and name are both not set
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableIdNameNullConfig(on, name),
ExpectError: regexp.MustCompile("Invalid Event Orchestration Cache Variable data source configuration: ID and name cannot both be null"),
},
// bad event_orchestration
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBadOrchConfig(on, name),
ExpectError: regexp.MustCompile("Unable to find a Cache Variable with ID '(.+)' on PagerDuty Event Orchestration 'bad-orchestration-id'"),
},
// bad id
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBadIdConfig(on, name),
ExpectError: regexp.MustCompile("Unable to find a Cache Variable with ID 'bad-cache-var-id' on PagerDuty Event Orchestration '(.+)'"),
},
// bad name
{
Config: testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBadNameConfig(on, name),
ExpectError: regexp.MustCompile("Unable to find a Cache Variable on Event Orchestration '(.+)' with name 'bad-cache-var-name'"),
},
},
})
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariable(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

srcR := s.RootModule().Resources[src]
srcA := srcR.Primary.Attributes

r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("Expected the Event Orchestration Cache Variable ID to be set")
}

testAtts := []string{
"id", "name", "configuration.0.type", "configuration.0.ttl_seconds",
}

for _, att := range testAtts {
if a[att] != srcA[att] {
return fmt.Errorf("Expected the Event Orchestration Cache Variable %s to be: %s, but got: %s", att, srcA[att], a[att])
}
}

return nil
}
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBaseConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableByIdConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = pagerduty_event_orchestration.orch.id
id = pagerduty_event_orchestration_global_cache_variable.orch_cv.id
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableByIdNameConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = pagerduty_event_orchestration.orch.id
id = pagerduty_event_orchestration_global_cache_variable.orch_cv.id
name = "No such name"
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableByNameConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%[1]s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%[2]s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%[2]s"
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableIdNameNullConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = pagerduty_event_orchestration.orch.id
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBadOrchConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = "bad-orchestration-id"
id = pagerduty_event_orchestration_global_cache_variable.orch_cv.id
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBadIdConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = pagerduty_event_orchestration.orch.id
id = "bad-cache-var-id"
}
`, on, name)
}

func testAccDataSourcePagerDutyEventOrchestrationGlobalCacheVariableBadNameConfig(on, name string) string {
return fmt.Sprintf(`
resource "pagerduty_event_orchestration" "orch" {
name = "%s"
}
resource "pagerduty_event_orchestration_global_cache_variable" "orch_cv" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "%s"
configuration {
type = "trigger_event_count"
ttl_seconds = 60
}
}
data "pagerduty_event_orchestration_global_cache_variable" "by_id" {
event_orchestration = pagerduty_event_orchestration.orch.id
name = "bad-cache-var-name"
}
`, on, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pagerduty

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func dataSourcePagerDutyEventOrchestrationServiceCacheVariable() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePagerDutyEventOrchestrationServiceCacheVariableRead,
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Required: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"condition": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: dataSourceEventOrchestrationCacheVariableConditionSchema,
},
},
"configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: dataSourceEventOrchestrationCacheVariableConfigurationSchema,
},
},
"disabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourcePagerDutyEventOrchestrationServiceCacheVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return dataSourceEventOrchestrationCacheVariableRead(ctx, d, meta, pagerduty.CacheVariableTypeService)
}
Loading

0 comments on commit a8a550f

Please sign in to comment.