Skip to content

Commit

Permalink
implementation of custom fields for terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelson-pagerduty committed Feb 13, 2023
1 parent 5431f12 commit 07c3f62
Show file tree
Hide file tree
Showing 37 changed files with 4,198 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,5 @@ PAGERDUTY_ACC_INCIDENT_WORKFLOWS=1 make testacc TESTARGS="-run PagerDutyIncident

| Variable Name | Feature Set |
|------------------------------------|--------------------|
| `PAGERDUTY_ACC_INCIDENT_WORKFLOWS` | Incident Workflows |
| `PAGERDUTY_ACC_INCIDENT_WORKFLOWS` | Incident Workflows |
| `PAGERDUTY_ACC_CUSTOM_FIELDS` | Custom Fields |
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
cloud.google.com/go v0.71.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/heimweh/go-pagerduty v0.0.0-20230117201746-310c225f8600
github.com/heimweh/go-pagerduty v0.0.0-20230213205146-a7761402c92d
github.com/klauspost/compress v1.15.9 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
go.mongodb.org/mongo-driver v1.10.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/heimweh/go-pagerduty v0.0.0-20230117201746-310c225f8600 h1:mf9awB4qvSXN6fIo94bd/MnG2n3ts+i2KA+mZ3N92z4=
github.com/heimweh/go-pagerduty v0.0.0-20230117201746-310c225f8600/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY=
github.com/heimweh/go-pagerduty v0.0.0-20230213205146-a7761402c92d h1:9OZp5Bgl1WDswugwBKLiTcxeCT/t0c293zUtXLL6OUc=
github.com/heimweh/go-pagerduty v0.0.0-20230213205146-a7761402c92d/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
93 changes: 93 additions & 0 deletions pagerduty/data_source_pagerduty_custom_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package pagerduty

import (
"context"
"fmt"
"log"
"time"

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

func dataSourcePagerDutyField() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePagerDutyCustomFieldRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"datatype": {
Type: schema.TypeString,
Computed: true,
},
"multi_value": {
Type: schema.TypeBool,
Computed: true,
},
"fixed_options": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourcePagerDutyCustomFieldRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(*Config).Client()
if err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] Reading PagerDuty data source")

searchName := d.Get("name").(string)

err = resource.RetryContext(ctx, 5*time.Minute, func() *resource.RetryError {
resp, _, err := client.CustomFields.ListContext(ctx, nil)
if err != nil {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.CustomField

for _, field := range resp.Fields {
if field.Name == searchName {
found = field
break
}
}

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("unable to locate any field with name: %s", searchName),
)
}

err = flattenCustomField(d, found)
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if err != nil {
return diag.FromErr(err)
}
return nil
}
45 changes: 45 additions & 0 deletions pagerduty/data_source_pagerduty_custom_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pagerduty

import (
"fmt"
"testing"

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

func TestAccDataSourcePagerDutyCustomField(t *testing.T) {
fieldName := fmt.Sprintf("tf_%s", acctest.RandString(5))
dataSourceName := fmt.Sprintf("data.pagerduty_custom_field.%s", fieldName)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckCustomFieldTests(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyCustomFieldConfig(fieldName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "name", fieldName),
resource.TestCheckResourceAttr(dataSourceName, "datatype", "string"),
),
},
},
})
}

func testAccDataSourcePagerDutyCustomFieldConfig(name string) string {
return fmt.Sprintf(`
resource "pagerduty_custom_field" "input" {
name = "%[1]s"
display_name = "%[1]s"
datatype = "string"
}
data "pagerduty_custom_field" "%[1]s" {
name = pagerduty_custom_field.input.name
}
`, name)
}
77 changes: 77 additions & 0 deletions pagerduty/data_source_pagerduty_field_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package pagerduty

import (
"context"
"fmt"
"log"
"time"

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

func dataSourcePagerDutyFieldSchema() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePagerDutyFieldSchemaRead,
Schema: map[string]*schema.Schema{
"title": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourcePagerDutyFieldSchemaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(*Config).Client()
if err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] Reading PagerDuty data source")

search := d.Get("title").(string)

err = resource.RetryContext(ctx, 5*time.Minute, func() *resource.RetryError {
resp, _, err := client.CustomFieldSchemas.ListContext(ctx, nil)
if err != nil {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.CustomFieldSchema

for _, fs := range resp.Schemas {
if fs.Title == search {
found = fs
break
}
}

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("unable to locate any field schema with title: %s", search),
)
}

err = flattenFieldSchema(d, found)
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if err != nil {
return diag.FromErr(err)
}
return nil
}
44 changes: 44 additions & 0 deletions pagerduty/data_source_pagerduty_field_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pagerduty

import (
"fmt"
"testing"

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

func TestAccDataSourcePagerDutyCustomFieldSchema(t *testing.T) {
schemaTitle := fmt.Sprintf("tf-%s", acctest.RandString(5))
dataSourceName := fmt.Sprintf("data.pagerduty_custom_field_schema.%s", schemaTitle)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckCustomFieldTests(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyFieldSchemaConfig(schemaTitle),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "title", schemaTitle),
resource.TestCheckResourceAttr(dataSourceName, "description", "some description"),
),
},
},
})
}

func testAccDataSourcePagerDutyFieldSchemaConfig(title string) string {
return fmt.Sprintf(`
resource "pagerduty_custom_field_schema" "input" {
title = "%[1]s"
description = "some description"
}
data "pagerduty_custom_field_schema" "%[1]s" {
title = pagerduty_custom_field_schema.input.title
}
`, title)
}
32 changes: 32 additions & 0 deletions pagerduty/import_pagerduty_custom_field_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pagerduty

import (
"fmt"
"testing"

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

func TestAccPagerDutyFieldSchema_import(t *testing.T) {
schemaTitle := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckCustomFieldTests(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckPagerDutyCustomFieldDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyCustomFieldSchemaConfigBasic(schemaTitle),
},
{
ResourceName: "pagerduty_custom_field_schema.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
32 changes: 32 additions & 0 deletions pagerduty/import_pagerduty_custom_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pagerduty

import (
"fmt"
"testing"

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

func TestAccPagerDutyField_import(t *testing.T) {
fieldName := fmt.Sprintf("tf_%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckCustomFieldTests(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckPagerDutyCustomFieldDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyCustomFieldConfig(fieldName, "string"),
},
{
ResourceName: "pagerduty_custom_field.input",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
7 changes: 7 additions & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func Provider() *schema.Provider {
"pagerduty_automation_actions_runner": dataSourcePagerDutyAutomationActionsRunner(),
"pagerduty_automation_actions_action": dataSourcePagerDutyAutomationActionsAction(),
"pagerduty_incident_workflow": dataSourcePagerDutyIncidentWorkflow(),
"pagerduty_custom_field": dataSourcePagerDutyField(),
"pagerduty_custom_field_schema": dataSourcePagerDutyFieldSchema(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -103,6 +105,11 @@ func Provider() *schema.Provider {
"pagerduty_incident_workflow": resourcePagerDutyIncidentWorkflow(),
"pagerduty_incident_workflow_trigger": resourcePagerDutyIncidentWorkflowTrigger(),
"pagerduty_automation_actions_action_service_association": resourcePagerDutyAutomationActionsActionServiceAssociation(),
"pagerduty_custom_field": resourcePagerDutyCustomField(),
"pagerduty_custom_field_option": resourcePagerDutyCustomFieldOption(),
"pagerduty_custom_field_schema": resourcePagerDutyCustomFieldSchema(),
"pagerduty_custom_field_schema_field_configuration": resourcePagerDutyCustomFieldSchemaFieldConfiguration(),
"pagerduty_custom_field_schema_assignment": resourcePagerDutyCustomFieldSchemaAssignment(),
},
}

Expand Down
Loading

0 comments on commit 07c3f62

Please sign in to comment.