Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

[MCTB-6001] Improve code consistency #19

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pagerduty/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ const invalidCreds = `

No valid credentials found for PagerDuty provider.
Please see https://www.terraform.io/docs/providers/pagerduty/index.html
for more information on providing credentials for this provider.
`
for more information on providing credentials for this provider`

// Client returns a PagerDuty client, initializing when necessary.
func (c *Config) Client() (*pagerduty.Client, error) {
Expand All @@ -68,8 +67,7 @@ func (c *Config) Client() (*pagerduty.Client, error) {
return nil, fmt.Errorf(invalidCreds)
}

var httpClient *http.Client
httpClient = http.DefaultClient
httpClient := http.DefaultClient
httpClient.Transport = logging.NewTransport("PagerDuty", http.DefaultTransport)

var apiUrl = c.ApiUrl
Expand Down Expand Up @@ -121,8 +119,7 @@ func (c *Config) SlackClient() (*pagerduty.Client, error) {
return nil, fmt.Errorf(invalidCreds)
}

var httpClient *http.Client
httpClient = http.DefaultClient
httpClient := http.DefaultClient
httpClient.Transport = logging.NewTransport("PagerDuty", http.DefaultTransport)

config := &pagerduty.Config{
Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func dataSourcePagerDutyBusinessServiceRead(ctx context.Context, d *schema.Resou

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any business service with the name: %s", searchName),
fmt.Errorf("unable to locate any business service with the name: %s", searchName),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func dataSourcePagerDutyEscalationPolicyRead(ctx context.Context, d *schema.Reso

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any escalation policy with the name: %s", searchName),
fmt.Errorf("unable to locate any escalation policy with the name: %s", searchName),
)
}

Expand Down
18 changes: 10 additions & 8 deletions pagerduty/data_source_pagerduty_event_orchestration.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
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/nordcloud/go-pagerduty/pagerduty"
)

func dataSourcePagerDutyEventOrchestration() *schema.Resource {
return &schema.Resource{
Read: dataSourcePagerDutyEventOrchestrationRead,
ReadContext: dataSourcePagerDutyEventOrchestrationRead,

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -56,20 +58,20 @@ func dataSourcePagerDutyEventOrchestration() *schema.Resource {
}
}

func dataSourcePagerDutyEventOrchestrationRead(d *schema.ResourceData, meta interface{}) error {
func dataSourcePagerDutyEventOrchestrationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(*Config).Client()
if err != nil {
return err
return diag.FromErr(err)
}

log.Printf("[INFO] Reading PagerDuty Event Orchestration")

searchName := d.Get("name").(string)

return resource.Retry(5*time.Minute, func() *resource.RetryError {
return diag.FromErr(resource.RetryContext(ctx, 10*time.Minute, func() *resource.RetryError {
resp, _, err := client.EventOrchestrations.List()
if err != nil {
return resource.RetryableError(err)
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.EventOrchestration
Expand All @@ -83,7 +85,7 @@ func dataSourcePagerDutyEventOrchestrationRead(d *schema.ResourceData, meta inte

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any Event Orchestration with the name: %s", searchName),
fmt.Errorf("unable to locate any Event Orchestration with the name: %s", searchName),
)
}

Expand All @@ -102,5 +104,5 @@ func dataSourcePagerDutyEventOrchestrationRead(d *schema.ResourceData, meta inte
}

return nil
})
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func dataSourcePagerDutyEventOrchestrationIntegrationRead(ctx context.Context, d
lbl := d.Get("label").(string)

if id == "" && lbl == "" {
return diag.FromErr(fmt.Errorf("Invalid Event Orchestration Integration data source configuration: ID and label cannot both be null"))
return diag.FromErr(fmt.Errorf("invalid Event Orchestration Integration data source configuration: ID and label cannot both be null"))
}

oid := d.Get("event_orchestration").(string)
Expand Down Expand Up @@ -142,13 +142,13 @@ func getEventOrchestrationIntegrationByLabel(ctx context.Context, d *schema.Reso

if count == 0 {
return resource.NonRetryableError(
fmt.Errorf("Unable to find an Integration on Event Orchestration '%s' with label '%s'", oid, lbl),
fmt.Errorf("unable to find an Integration on Event Orchestration '%s' with label '%s'", oid, lbl),
)
}

if count > 1 {
return resource.NonRetryableError(
fmt.Errorf("Ambiguous Integration label: '%s'. Found %v Integrations with this label on Event Orchestration '%s'. Please use the Integration ID instead or make Integration labels unique within Event Orchestration.", lbl, count, oid),
fmt.Errorf("ambiguous Integration label: '%s'. Found %v Integrations with this label on Event Orchestration '%s'. Please use the Integration ID instead or make Integration labels unique within Event Orchestration", lbl, count, oid),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_event_orchestrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func dataSourcePagerDutyEventOrchestrationsRead(d *schema.ResourceData, meta int
}
}
if len(eoList) == 0 {
return resource.NonRetryableError(fmt.Errorf("Unable to locate any Event Orchestration matching the expression: %s", nameFilter))
return resource.NonRetryableError(fmt.Errorf("unable to locate any Event Orchestration matching the expression: %s", nameFilter))
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_event_orchestrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ data "pagerduty_event_orchestrations" "not_found" {
}

func testAccDataSourcePagerDutyEventOrchestrationsInvalidRegexConfig() string {
return fmt.Sprintf(`
return `
data "pagerduty_event_orchestrations" "invalid_regex" {
name_filter = ")"
}
`)
`
}
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_extension_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func dataSourcePagerDutyExtensionSchemaRead(ctx context.Context, d *schema.Resou

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any extension schema with the name: %s", searchName),
fmt.Errorf("unable to locate any extension schema with the name: %s", searchName),
)
}

Expand Down
28 changes: 11 additions & 17 deletions pagerduty/data_source_pagerduty_license.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package pagerduty

import (
"context"
"fmt"
"log"
"net/http"
"strings"
"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/nordcloud/go-pagerduty/pagerduty"
Expand Down Expand Up @@ -75,30 +76,23 @@ var licenseSchema = map[string]*schema.Schema{

func dataSourcePagerDutyLicense() *schema.Resource {
return &schema.Resource{
Read: dataSourcePagerDutyLicenseRead,
Schema: licenseSchema,
ReadContext: dataSourcePagerDutyLicenseRead,
Schema: licenseSchema,
}
}

func dataSourcePagerDutyLicenseRead(d *schema.ResourceData, meta interface{}) error {
func dataSourcePagerDutyLicenseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(*Config).Client()
if err != nil {
return err
return diag.FromErr(err)
}

log.Printf("[INFO] Fetching PagerDuty Licenses")

return resource.Retry(5*time.Minute, func() *resource.RetryError {
return diag.FromErr(resource.RetryContext(ctx, 10*time.Minute, func() *resource.RetryError {
licenses, _, err := client.Licenses.List()
if err != nil {
if isErrCode(err, http.StatusBadRequest) {
return resource.NonRetryableError(err)
}

// 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)
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

id, name, description := d.Get("id").(string), d.Get("name").(string), d.Get("description").(string)
Expand All @@ -107,7 +101,7 @@ func dataSourcePagerDutyLicenseRead(d *schema.ResourceData, meta interface{}) er
if found == nil {
ids := licensesToStringOfIds(licenses)
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any license with ids in [%s] with the configured id: '%s', name: '%s' or description: '%s'", ids, id, name, description))
fmt.Errorf("unable to locate any license with ids in [%s] with the configured id: '%s', name: '%s' or description: '%s'", ids, id, name, description))
}

d.SetId(found.ID)
Expand All @@ -123,7 +117,7 @@ func dataSourcePagerDutyLicenseRead(d *schema.ResourceData, meta interface{}) er
d.Set("html_url", found.HTMLURL)

return nil
})
}))
}

func licensesToStringOfIds(licenses []*pagerduty.License) string {
Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func dataSourcePagerDutyPriorityRead(ctx context.Context, d *schema.ResourceData

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any priority with name: %s", searchTeam),
fmt.Errorf("unable to locate any priority with name: %s", searchTeam),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func dataSourcePagerDutyRulesetRead(ctx context.Context, d *schema.ResourceData,

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any ruleset with the name: %s", searchName),
fmt.Errorf("unable to locate any ruleset with the name: %s", searchName),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func dataSourcePagerDutyScheduleRead(ctx context.Context, d *schema.ResourceData

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any schedule with the name: %s", searchName),
fmt.Errorf("unable to locate any schedule with the name: %s", searchName),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func dataSourcePagerDutyServiceRead(ctx context.Context, d *schema.ResourceData,

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any service with the name: %s", searchName),
fmt.Errorf("unable to locate any service with the name: %s", searchName),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func dataSourcePagerDutyTagRead(ctx context.Context, d *schema.ResourceData, met

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any tag with label: %s", searchTag),
fmt.Errorf("unable to locate any tag with label: %s", searchTag),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func dataSourcePagerDutyTeamRead(ctx context.Context, d *schema.ResourceData, me

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any team with name: %s", searchTeam),
fmt.Errorf("unable to locate any team with name: %s", searchTeam),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func dataSourcePagerDutyUserRead(ctx context.Context, d *schema.ResourceData, me

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any user with the email: %s", searchEmail),
fmt.Errorf("unable to locate any user with the email: %s", searchEmail),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_user_contact_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func dataSourcePagerDutyUserContactMethodRead(ctx context.Context, d *schema.Res
}

if found == nil {
return resource.NonRetryableError(fmt.Errorf("Unable to locate any contact methods with the label: %s", searchLabel))
return resource.NonRetryableError(fmt.Errorf("unable to locate any contact methods with the label: %s", searchLabel))
}

d.SetId(found.ID)
Expand Down
19 changes: 9 additions & 10 deletions pagerduty/data_source_pagerduty_users.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package pagerduty

import (
"context"
"log"
"strconv"
"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/nordcloud/go-pagerduty/pagerduty"
)

func dataSourcePagerDutyUsers() *schema.Resource {
return &schema.Resource{
Read: dataSourcePagerDutyUsersRead,
ReadContext: dataSourcePagerDutyUsersRead,

Schema: map[string]*schema.Schema{
"team_ids": {
Expand Down Expand Up @@ -47,10 +49,10 @@ func dataSourcePagerDutyUsers() *schema.Resource {
}
}

func dataSourcePagerDutyUsersRead(d *schema.ResourceData, meta interface{}) error {
func dataSourcePagerDutyUsersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(*Config).Client()
if err != nil {
return err
return diag.FromErr(err)
}

log.Printf("[INFO] Reading PagerDuty users")
Expand All @@ -65,13 +67,10 @@ func dataSourcePagerDutyUsersRead(d *schema.ResourceData, meta interface{}) erro
TeamIDs: teamIds,
}

return resource.Retry(5*time.Minute, func() *resource.RetryError {
return diag.FromErr(resource.RetryContext(ctx, 10*time.Minute, func() *resource.RetryError {
resp, err := client.Users.ListAll(o)
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)
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var users []map[string]interface{}
Expand All @@ -89,5 +88,5 @@ func dataSourcePagerDutyUsersRead(d *schema.ResourceData, meta interface{}) erro
d.Set("users", users)

return nil
})
}))
}
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func dataSourcePagerDutyVendorRead(ctx context.Context, d *schema.ResourceData,

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any vendor with the name: %s", searchName),
fmt.Errorf("unable to locate any vendor with the name: %s", searchName),
)
}

Expand Down
6 changes: 3 additions & 3 deletions pagerduty/event_orchestration_path_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ func checkExtractionAttributes(diff *schema.ResourceDiff, loc string) error {
t := diff.Get(fmt.Sprintf("%s.template", prefix)).(string)

if r == "" && t == "" {
return fmt.Errorf("Invalid configuration in %s: regex and template cannot both be null", prefix)
return fmt.Errorf("invalid configuration in %s: regex and template cannot both be null", prefix)
}
if r != "" && t != "" {
return fmt.Errorf("Invalid configuration in %s: regex and template cannot both have values", prefix)
return fmt.Errorf("invalid configuration in %s: regex and template cannot both have values", prefix)
}

s := diff.Get(fmt.Sprintf("%s.source", prefix)).(string)
if r != "" && s == "" {
return fmt.Errorf("Invalid configuration in %s: source can't be blank", prefix)
return fmt.Errorf("invalid configuration in %s: source can't be blank", prefix)
}
}
return nil
Expand Down
Loading
Loading