Skip to content

Commit

Permalink
Migrate data source migration schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed Apr 2, 2024
1 parent 2595caa commit d93c6bc
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 105 deletions.
76 changes: 0 additions & 76 deletions pagerduty/data_source_pagerduty_extension_schema.go

This file was deleted.

1 change: 0 additions & 1 deletion pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func Provider(isMux bool) *schema.Provider {
"pagerduty_user_contact_method": dataSourcePagerDutyUserContactMethod(),
"pagerduty_team": dataSourcePagerDutyTeam(),
"pagerduty_vendor": dataSourcePagerDutyVendor(),
"pagerduty_extension_schema": dataSourcePagerDutyExtensionSchema(),
"pagerduty_service": dataSourcePagerDutyService(),
"pagerduty_service_integration": dataSourcePagerDutyServiceIntegration(),
"pagerduty_business_service": dataSourcePagerDutyBusinessService(),
Expand Down
96 changes: 96 additions & 0 deletions pagerdutyplugin/data_source_pagerduty_extension_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package pagerduty

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

"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

type dataSourceExtensionSchema struct{ client *pagerduty.Client }

var _ datasource.DataSourceWithConfigure = (*dataSourceExtensionSchema)(nil)

func (*dataSourceExtensionSchema) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "pagerduty_extension_schema"
}

func (*dataSourceExtensionSchema) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true},
"name": schema.StringAttribute{Required: true},
"type": schema.StringAttribute{Computed: true},
},
}
}

func (d *dataSourceExtensionSchema) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
resp.Diagnostics.Append(ConfigurePagerdutyClient(&d.client, req.ProviderData)...)
}

func (d *dataSourceExtensionSchema) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
log.Println("[INFO] Reading PagerDuty extension schema")

var searchName types.String
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &searchName)...)
if resp.Diagnostics.HasError() {
return
}

var found *pagerduty.ExtensionSchema
// TODO delete and comment in PR: changed to 2min because 5min/30s is 10 attempts
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
list, err := d.client.ListExtensionSchemasWithContext(ctx, pagerduty.ListExtensionSchemaOptions{})
if err != nil {
if util.IsBadRequestError(err) {
return retry.NonRetryableError(err)
}
return retry.RetryableError(err)
}

for _, extensionSchema := range list.ExtensionSchemas {
if strings.EqualFold(extensionSchema.Label, searchName.ValueString()) {
found = &extensionSchema
break
}
}
return nil
})
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error reading PagerDuty extension schema %s", searchName),
err.Error(),
)
}

if found == nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to locate any extension schema with the name: %s", searchName),
"",
)
return
}

model := dataSourceExtensionSchemaModel{
ID: types.StringValue(found.ID),
Name: types.StringValue(found.Label),
Type: types.StringValue(found.Type),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

type dataSourceExtensionSchemaModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package pagerduty

import (
"context"
"fmt"
"testing"

"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccDataSourcePagerDutyExtensionSchema_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyScheduleDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),

Check failure on line 16 in pagerdutyplugin/data_source_pagerduty_extension_schema_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: testAccProtoV5ProviderFactories
CheckDestroy: testAccCheckPagerDutyScheduleDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyExtensionSchemaConfig,
Expand All @@ -26,7 +28,6 @@ func TestAccDataSourcePagerDutyExtensionSchema_Basic(t *testing.T) {

func testAccDataSourcePagerDutyExtensionSchema(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

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

Expand Down Expand Up @@ -55,3 +56,19 @@ data "pagerduty_extension_schema" "foo" {
name = "slack"
}
`

func testAccCheckPagerDutyScheduleDestroy(s *terraform.State) error {
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_schedule" {
continue
}

ctx := context.Background()
opts := pagerduty.GetScheduleOptions{}
if _, err := testAccProvider.client.GetScheduleWithContext(ctx, r.Primary.ID, opts); err == nil {
return fmt.Errorf("Schedule still exists")
}

}
return nil
}
6 changes: 3 additions & 3 deletions pagerdutyplugin/import_pagerduty_extension_servicenow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func TestAccPagerDutyExtensionServiceNow_import(t *testing.T) {
url := "https://example.com/receive_a_pagerduty_webhook"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),

Check failure on line 18 in pagerdutyplugin/import_pagerduty_extension_servicenow_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: testAccProtoV5ProviderFactories
CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyExtensionServiceNowConfig(name, extension_name, url, "false", "any"),
Expand Down
1 change: 1 addition & 0 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (p *Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp
func (p *Provider) DataSources(ctx context.Context) [](func() datasource.DataSource) {
return [](func() datasource.DataSource){
func() datasource.DataSource { return &dataSourceBusinessService{} },
func() datasource.DataSource { return &dataSourceExtensionSchema{} },
func() datasource.DataSource { return &dataSourceStandardsResourceScores{} },
func() datasource.DataSource { return &dataSourceStandardsResourcesScores{} },
func() datasource.DataSource { return &dataSourceStandards{} },
Expand Down
36 changes: 15 additions & 21 deletions pagerdutyplugin/resource_pagerduty_extension_servicenow_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package pagerduty

import (
"context"
"fmt"
"log"
"strings"
"testing"

"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func init() {
Expand All @@ -19,26 +20,18 @@ func init() {
})
}

func testSweepExtensionServiceNow(region string) error {
config, err := sharedConfigForRegion(region)
if err != nil {
return err
}

client, err := config.Client()
if err != nil {
return err
}
func testSweepExtensionServiceNow(_ string) error {
ctx := context.Background()

resp, _, err := client.Extensions.List(&pagerduty.ListExtensionsOptions{})
resp, err := testAccProvider.client.ListExtensionsWithContext(ctx, pagerduty.ListExtensionOptions{})
if err != nil {
return err
}

for _, extension := range resp.Extensions {
if strings.HasPrefix(extension.Name, "test") || strings.HasPrefix(extension.Name, "tf-") {
log.Printf("Destroying extension %s (%s)", extension.Name, extension.ID)
if _, err := client.Extensions.Delete(extension.ID); err != nil {
if err := testAccProvider.client.DeleteExtensionWithContext(ctx, extension.ID); err != nil {
return err
}
}
Expand All @@ -55,9 +48,9 @@ func TestAccPagerDutyExtensionServiceNow_Basic(t *testing.T) {
url_updated := "https://example.com/webhook_foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),

Check failure on line 52 in pagerdutyplugin/resource_pagerduty_extension_servicenow_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: testAccProtoV5ProviderFactories
CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyExtensionServiceNowConfig(name, extension_name, url, "false", "any"),
Expand Down Expand Up @@ -116,13 +109,14 @@ func TestAccPagerDutyExtensionServiceNow_Basic(t *testing.T) {
}

func testAccCheckPagerDutyExtensionServiceNowDestroy(s *terraform.State) error {
client, _ := testAccProvider.Meta().(*Config).Client()
ctx := context.Background()

for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_extension_servicenow" {
continue
}

if _, _, err := client.Extensions.Get(r.Primary.ID); err == nil {
if _, err := testAccProvider.client.GetExtensionWithContext(ctx, r.Primary.ID); err == nil {
return fmt.Errorf("Extension still exists")
}

Expand All @@ -131,6 +125,8 @@ func testAccCheckPagerDutyExtensionServiceNowDestroy(s *terraform.State) error {
}

func testAccCheckPagerDutyExtensionServiceNowExists(n string) resource.TestCheckFunc {
ctx := context.Background()

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -141,9 +137,7 @@ func testAccCheckPagerDutyExtensionServiceNowExists(n string) resource.TestCheck
return fmt.Errorf("No extension ID is set")
}

client, _ := testAccProvider.Meta().(*Config).Client()

found, _, err := client.Extensions.Get(rs.Primary.ID)
found, err := testAccProvider.client.GetExtensionWithContext(ctx, rs.Primary.ID)
if err != nil {
return err
}
Expand Down

0 comments on commit d93c6bc

Please sign in to comment.