-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation of custom fields for terraform
- Loading branch information
1 parent
5431f12
commit 07c3f62
Showing
37 changed files
with
4,198 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.