Skip to content

Commit

Permalink
Merge pull request #458 from dobs/safer-clients
Browse files Browse the repository at this point in the history
Safer HTTP client initialization and usage.
  • Loading branch information
Scott McAllister authored Feb 9, 2022
2 parents 38dab7f + 114ac30 commit 661767b
Show file tree
Hide file tree
Showing 38 changed files with 513 additions and 130 deletions.
32 changes: 29 additions & 3 deletions pagerduty/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"fmt"
"log"
"net/http"
"sync"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/heimweh/go-pagerduty/pagerduty"
)

// Config defines the configuration options for the PagerDuty client
type Config struct {
mu sync.Mutex

// The PagerDuty API URL
ApiUrl string

Expand All @@ -31,6 +34,9 @@ type Config struct {

// UserAgent for API Client
UserAgent string

client *pagerduty.Client
slackClient *pagerduty.Client
}

const invalidCreds = `
Expand All @@ -40,8 +46,16 @@ Please see https://www.terraform.io/docs/providers/pagerduty/index.html
for more information on providing credentials for this provider.
`

// Client returns a new PagerDuty client
// Client returns a PagerDuty client, initializing when necessary.
func (c *Config) Client() (*pagerduty.Client, error) {
c.mu.Lock()
defer c.mu.Unlock()

// Return the previously-configured client if available.
if c.client != nil {
return c.client, nil
}

// Validate that the PagerDuty token is set
if c.Token == "" {
return nil, fmt.Errorf(invalidCreds)
Expand Down Expand Up @@ -77,12 +91,22 @@ func (c *Config) Client() (*pagerduty.Client, error) {
}
}

c.client = client

log.Printf("[INFO] PagerDuty client configured")

return client, nil
return c.client, nil
}

func (c *Config) SlackClient() (*pagerduty.Client, error) {
c.mu.Lock()
defer c.mu.Unlock()

// Return the previously-configured client if available.
if c.slackClient != nil {
return c.slackClient, nil
}

// Validate that the user level PagerDuty token is set
if c.UserToken == "" {
return nil, fmt.Errorf(invalidCreds)
Expand All @@ -105,7 +129,9 @@ func (c *Config) SlackClient() (*pagerduty.Client, error) {
return nil, err
}

c.slackClient = client

log.Printf("[INFO] PagerDuty client configured for slack")

return client, nil
return c.slackClient, nil
}
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func dataSourcePagerDutyBusinessService() *schema.Resource {
}

func dataSourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty business service")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func dataSourcePagerDutyEscalationPolicy() *schema.Resource {
}

func dataSourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty escalation policy")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_extension_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func dataSourcePagerDutyExtensionSchema() *schema.Resource {
}

func dataSourcePagerDutyExtensionSchemaRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty Extension Schema")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func dataSourcePagerDutyPriority() *schema.Resource {
}

func dataSourcePagerDutyPriorityRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty priority")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func dataSourcePagerDutyRuleset() *schema.Resource {
}

func dataSourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty ruleset")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func dataSourcePagerDutySchedule() *schema.Resource {
}

func dataSourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty schedule")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func dataSourcePagerDutyService() *schema.Resource {
}

func dataSourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty service")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_service_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func dataSourcePagerDutyServiceIntegration() *schema.Resource {
}

func dataSourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty service")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func dataSourcePagerDutyTag() *schema.Resource {
}

func dataSourcePagerDutyTagRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty tag")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func dataSourcePagerDutyTeam() *schema.Resource {
}

func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty team")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func dataSourcePagerDutyUser() *schema.Resource {
}

func dataSourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty user")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_user_contact_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func dataSourcePagerDutyUserContactMethod() *schema.Resource {
}

func dataSourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty user's contact method")

Expand Down
5 changes: 4 additions & 1 deletion pagerduty/data_source_pagerduty_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func dataSourcePagerDutyVendor() *schema.Resource {
}

func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty vendor")

Expand Down
23 changes: 18 additions & 5 deletions pagerduty/resource_pagerduty_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func buildAddonStruct(d *schema.ResourceData) *pagerduty.Addon {
}

func fetchPagerDutyAddon(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

return resource.Retry(2*time.Minute, func() *resource.RetryError {
addon, _, err := client.Addons.Get(d.Id())
if err != nil {
Expand All @@ -64,13 +68,16 @@ func fetchPagerDutyAddon(d *schema.ResourceData, meta interface{}, errCallback f
}

func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

addon := buildAddonStruct(d)

log.Printf("[INFO] Creating PagerDuty add-on %s", addon.Name)

addon, _, err := client.Addons.Install(addon)
addon, _, err = client.Addons.Install(addon)
if err != nil {
return err
}
Expand All @@ -86,7 +93,10 @@ func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error
}

func resourcePagerDutyAddonUpdate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

addon := buildAddonStruct(d)

Expand All @@ -100,7 +110,10 @@ func resourcePagerDutyAddonUpdate(d *schema.ResourceData, meta interface{}) erro
}

func resourcePagerDutyAddonDelete(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Deleting PagerDuty add-on %s", d.Id())

Expand Down
20 changes: 16 additions & 4 deletions pagerduty/resource_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func buildBusinessServiceStruct(d *schema.ResourceData) (*pagerduty.BusinessServ
}

func resourcePagerDutyBusinessServiceCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {

Expand All @@ -118,7 +121,10 @@ func resourcePagerDutyBusinessServiceCreate(d *schema.ResourceData, meta interfa
}

func resourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty business service %s", d.Id())

Expand Down Expand Up @@ -149,7 +155,10 @@ func resourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface
}

func resourcePagerDutyBusinessServiceUpdate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

businessService, err := buildBusinessServiceStruct(d)
if err != nil {
Expand All @@ -168,7 +177,10 @@ func resourcePagerDutyBusinessServiceUpdate(d *schema.ResourceData, meta interfa
}

func resourcePagerDutyBusinessServiceDelete(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Deleting PagerDuty business service %s", d.Id())

Expand Down
21 changes: 17 additions & 4 deletions pagerduty/resource_pagerduty_business_service_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func buildBusinessServiceSubscriberStruct(d *schema.ResourceData) (*pagerduty.Bu
}

func resourcePagerDutyBusinessServiceSubscriberCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

businessServiceId := d.Get("business_service_id").(string)

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -82,7 +86,10 @@ func resourcePagerDutyBusinessServiceSubscriberCreate(d *schema.ResourceData, me
}

func resourcePagerDutyBusinessServiceSubscriberRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

businessServiceId := d.Get("business_service_id").(string)
businessServiceSubscriber, _ := buildBusinessServiceSubscriberStruct(d)
Expand Down Expand Up @@ -113,7 +120,10 @@ func resourcePagerDutyBusinessServiceSubscriberRead(d *schema.ResourceData, meta
}

func resourcePagerDutyBusinessServiceSubscriberDelete(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return err
}

businessServiceId := d.Get("business_service_id").(string)
businessServiceSubscriber, _ := buildBusinessServiceSubscriberStruct(d)
Expand All @@ -135,7 +145,10 @@ func createSubscriberID(businessServiceId string, subscriberType string, subscri

func resourcePagerDutyBusinessServiceSubscriberImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
ids := strings.Split(d.Id(), ".")
client, _ := meta.(*Config).Client()
client, err := meta.(*Config).Client()
if err != nil {
return []*schema.ResourceData{}, err
}

if len(ids) != 3 {
return []*schema.ResourceData{}, fmt.Errorf("error importing pagerduty_business_service_subscriber. Expecting an importation ID formed as '<business_service_id>.<subscriber_type>.<subscriber_id>'")
Expand Down
Loading

0 comments on commit 661767b

Please sign in to comment.