Skip to content

Commit

Permalink
Migrate resource pagerduty_schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed May 7, 2024
1 parent 1129196 commit 7ba0d61
Show file tree
Hide file tree
Showing 6 changed files with 3,228 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 @@ -152,6 +152,7 @@ func Provider(isMux bool) *schema.Provider {
delete(p.DataSourcesMap, "pagerduty_business_service")

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

0 comments on commit 7ba0d61

Please sign in to comment.