Skip to content

Commit

Permalink
Migrate resource schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed May 23, 2024
1 parent 6e08b1e commit d5ed40f
Show file tree
Hide file tree
Showing 8 changed files with 3,078 additions and 18 deletions.
1 change: 1 addition & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func Provider(isMux bool) *schema.Provider {

delete(p.ResourcesMap, "pagerduty_addon")
delete(p.ResourcesMap, "pagerduty_business_service")
delete(p.ResourcesMap, "pagerduty_schedule")
}

p.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand Down
5 changes: 5 additions & 0 deletions pagerdutyplugin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ Please see https://www.terraform.io/docs/providers/pagerduty/index.html
for more information on providing credentials for this provider.
`

// RetryNotFound is defined to have a named boolean when passing it to
// requestThing functions. If true, they should be further attempts to get a
// resource from the API even if we receive a 404 during a time window.
const RetryNotFound = true

// Client returns a PagerDuty client, initializing when necessary.
func (c *Config) Client(ctx context.Context) (*pagerduty.Client, error) {
c.mu.Lock()
Expand Down
18 changes: 0 additions & 18 deletions pagerdutyplugin/data_source_pagerduty_extension_schema_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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"
)
Expand Down Expand Up @@ -56,19 +54,3 @@ data "pagerduty_extension_schema" "foo" {
name = "ServiceNow (v7)"
}
`

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
}
53 changes: 53 additions & 0 deletions pagerdutyplugin/import_pagerduty_schedule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pagerduty

import (
"context"
"fmt"
"testing"
"time"

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

func TestAccPagerDutySchedule_import(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%[email protected]", username)
schedule := fmt.Sprintf("tf-%s", acctest.RandString(5))
location := "Europe/Berlin"
t.Setenv("PAGERDUTY_TIME_ZONE", location)
start := testAccTimeNow().Add(24 * time.Hour).Round(1 * time.Hour).Format(time.RFC3339)
rotationVirtualStart := testAccTimeNow().Add(24 * time.Hour).Round(1 * time.Hour).Format(time.RFC3339)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
CheckDestroy: testAccCheckPagerDutyUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyScheduleConfig(username, email, schedule, location, start, rotationVirtualStart),
},
{
ResourceName: "pagerduty_schedule.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

ctx := context.Background()
if _, err := testAccProvider.client.GetUserWithContext(ctx, r.Primary.ID, pagerduty.GetUserOptions{}); err == nil {
return fmt.Errorf("User still exists")
}
}
return nil
}
23 changes: 23 additions & 0 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (p *Provider) Resources(_ context.Context) [](func() resource.Resource) {
func() resource.Resource { return &resourceBusinessService{} },
func() resource.Resource { return &resourceExtensionServiceNow{} },
func() resource.Resource { return &resourceExtension{} },
func() resource.Resource { return &resourceSchedule{} },
func() resource.Resource { return &resourceServiceDependency{} },
func() resource.Resource { return &resourceTagAssignment{} },
func() resource.Resource { return &resourceTag{} },
Expand Down Expand Up @@ -205,3 +206,25 @@ func extractString(ctx context.Context, schema SchemaGetter, name string, diags
diags.Append(d...)
return s.ValueStringPointer()
}

func buildPagerdutyAPIObjectFromIDs(ctx context.Context, list types.List, apiType string, diags *diag.Diagnostics) []pagerduty.APIObject {
if list.IsNull() || list.IsUnknown() {
return nil
}

var target []types.String
diags.Append(list.ElementsAs(ctx, &target, false)...)
if diags.HasError() {
return nil
}

response := make([]pagerduty.APIObject, 0, len(target))
for _, id := range target {
response = append(response, pagerduty.APIObject{
ID: id.ValueString(),
Type: apiType,
})
}

return response
}
Loading

0 comments on commit d5ed40f

Please sign in to comment.