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-4601] Add support for Event Orchestration Cache Variables #822

Merged
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: 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think before sending this PR for review to José we'll need to point this branch to your branch of the API Client, so the checks can pass and the acceptance tests are passing locally. At least that's what we used to do in previous EO PRs, then after both PRs are reviewed/approved we'd merge the API Client and point this branch to the new version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c-kaieong not sure if the provider or internal docs have the instructions but this command should do it:

go mod edit -replace github.com/heimweh/go-pagerduty=github.com/c-kaieong/go-pagerduty@ORCA-4600-introduce-cache-variables && go mod tidy && go mod vendor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The internal docs show how to point to a local directory, but not a branch. I'll add that in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
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
Loading