Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSGI-2571] Migrate Resource pagerduty_service_dependency to TF Plugin Framework #813

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
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_service")
}

p.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand Down
27 changes: 27 additions & 0 deletions pagerduty/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/heimweh/go-pagerduty/pagerduty"
)

Expand Down Expand Up @@ -331,3 +332,29 @@ func testAccGetPagerDutyAccountDomain(t *testing.T) string {
}
return accountDomain
}

func testAccCheckPagerDutyServiceExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Service ID is set")
}

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

found, _, err := client.Services.Get(rs.Primary.ID, &pagerduty.GetServiceOptions{})
if err != nil {
return err
}

if found.ID != rs.Primary.ID {
return fmt.Errorf("Service not found: %v - %v", rs.Primary.ID, found)
}

return nil
}
}
140 changes: 0 additions & 140 deletions pagerduty/resource_pagerduty_addon_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package pagerduty
import (
"context"
"fmt"
"log"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -231,3 +233,31 @@ func testAccCheckPagerDutyEventOrchestrationServiceCacheVariableDeletedConfig(sv
}
`, svc, svc)
}

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

client, err := config.Client()
if err != nil {
return err
}

resp, _, err := client.Services.List(&pagerduty.ListServicesOptions{})
if err != nil {
return err
}

for _, service := range resp.Services {
if strings.HasPrefix(service.Name, "test") || strings.HasPrefix(service.Name, "tf-") {
log.Printf("Destroying service %s (%s)", service.Name, service.ID)
if _, err := client.Services.Delete(service.ID); err != nil {
return err
}
}
}

return nil
}
34 changes: 34 additions & 0 deletions pagerduty/resource_pagerduty_incident_workflow_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,37 @@ func testAccCheckPagerDutyIncidentWorkflowTriggerExists(n string) resource.TestC
return nil
}
}

func testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, service string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
name = "%s"
email = "%s"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}

resource "pagerduty_escalation_policy" "foo" {
name = "%s"
description = "bar"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = pagerduty_user.foo.id
}
}
}

resource "pagerduty_service" "foo" {
name = "%s"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = pagerduty_escalation_policy.foo.id
}
`, username, email, escalationPolicy, service)
}
15 changes: 15 additions & 0 deletions pagerduty/resource_pagerduty_maintenance_window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,18 @@ resource "pagerduty_maintenance_window" "foo" {
}
`, desc, start, end)
}

func testAccCheckPagerDutyAddonDestroy(s *terraform.State) error {
client, _ := testAccProvider.Meta().(*Config).Client()
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_addon" {
continue
}

if _, _, err := client.Addons.Get(r.Primary.ID); err == nil {
return fmt.Errorf("Add-on still exists")
}

}
return nil
}
18 changes: 9 additions & 9 deletions pagerdutyplugin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type Config struct {
mu sync.Mutex

// The PagerDuty API URL
ApiUrl string
APIURL string

// Override default PagerDuty API URL
ApiUrlOverride string
APIURLOverride string

// The PagerDuty APP URL
AppUrl string
AppURL string

// The PagerDuty API V2 token
Token string
Expand All @@ -52,7 +52,7 @@ type Config struct {
}

type AppOauthScopedToken struct {
ClientId, ClientSecret, Subdomain string
ClientID, ClientSecret, Subdomain string
}

const invalidCreds = `
Expand All @@ -75,17 +75,17 @@ func (c *Config) Client(ctx context.Context) (*pagerduty.Client, error) {
httpClient.Timeout = 1 * time.Minute
httpClient.Transport = logging.NewTransport("PagerDuty", http.DefaultTransport)

apiUrl := c.ApiUrl
if c.ApiUrlOverride != "" {
apiUrl = c.ApiUrlOverride
apiURL := c.APIURL
if c.APIURLOverride != "" {
apiURL = c.APIURLOverride
}

maxRetries := 1
retryInterval := 60 // seconds

clientOpts := []pagerduty.ClientOptions{
WithHTTPClient(httpClient),
pagerduty.WithAPIEndpoint(apiUrl),
pagerduty.WithAPIEndpoint(apiURL),
pagerduty.WithTerraformProvider(c.TerraformVersion),
pagerduty.WithRetryPolicy(maxRetries, retryInterval),
}
Expand All @@ -97,7 +97,7 @@ func (c *Config) Client(ctx context.Context) (*pagerduty.Client, error) {
accountAndScopes = append(accountAndScopes, availableOauthScopes()...)
opt := pagerduty.WithScopedOAuthAppTokenSource(pagerduty.NewFileTokenSource(
ctx,
c.AppOauthScopedToken.ClientId,
c.AppOauthScopedToken.ClientID,
c.AppOauthScopedToken.ClientSecret,
accountAndScopes,
tokenFile,
Expand Down
6 changes: 3 additions & 3 deletions pagerdutyplugin/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestConfigSkipCredsValidation(t *testing.T) {
func TestConfigCustomApiUrl(t *testing.T) {
config := Config{
Token: "foo",
ApiUrl: "https://api.domain.tld",
APIURL: "https://api.domain.tld",
SkipCredsValidation: true,
}

Expand All @@ -45,7 +45,7 @@ func TestConfigCustomApiUrl(t *testing.T) {
func TestConfigCustomApiUrlOverride(t *testing.T) {
config := Config{
Token: "foo",
ApiUrlOverride: "https://api.domain-override.tld",
APIURLOverride: "https://api.domain-override.tld",
SkipCredsValidation: true,
}

Expand All @@ -58,7 +58,7 @@ func TestConfigCustomApiUrlOverride(t *testing.T) {
func TestConfigCustomAppUrl(t *testing.T) {
config := Config{
Token: "foo",
AppUrl: "https://app.domain.tld",
AppURL: "https://app.domain.tld",
SkipCredsValidation: true,
}

Expand Down
6 changes: 3 additions & 3 deletions pagerdutyplugin/data_source_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type dataSourceBusinessService struct{ client *pagerduty.Client }

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

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

func (*dataSourceBusinessService) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
func (*dataSourceBusinessService) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true},
Expand All @@ -33,7 +33,7 @@ func (*dataSourceBusinessService) Schema(ctx context.Context, req datasource.Sch
}
}

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

Expand Down
Loading