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

Add service_region support #384

Merged
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
38 changes: 38 additions & 0 deletions pagerduty/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ import (

// Config defines the configuration options for the PagerDuty client
type Config struct {
// The PagerDuty API URL
ApiUrl string

// The PagerDuty APP URL
AppUrl string

// The PagerDuty API V2 token
Token string

// The PagerDuty User level token for Slack
UserToken string

// Skip validation of the token against the PagerDuty API
SkipCredsValidation bool

Expand All @@ -40,6 +49,7 @@ func (c *Config) Client() (*pagerduty.Client, error) {
httpClient.Transport = logging.NewTransport("PagerDuty", http.DefaultTransport)

config := &pagerduty.Config{
BaseURL: c.ApiUrl,
Debug: logging.IsDebugOrHigher(),
HTTPClient: httpClient,
Token: c.Token,
Expand All @@ -63,3 +73,31 @@ func (c *Config) Client() (*pagerduty.Client, error) {

return client, nil
}

func (c *Config) SlackClient() (*pagerduty.Client, error) {
// Validate that the user level PagerDuty token is set
if c.UserToken == "" {
return nil, fmt.Errorf(invalidCreds)
}

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

config := &pagerduty.Config{
BaseURL: c.AppUrl,
Debug: logging.IsDebugOrHigher(),
HTTPClient: httpClient,
Token: c.UserToken,
UserAgent: c.UserAgent,
}

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

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

return client, nil
}
26 changes: 26 additions & 0 deletions pagerduty/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@ func TestConfigSkipCredsValidation(t *testing.T) {
t.Fatalf("error: expected the client to not fail: %v", err)
}
}

// Test config with a custom ApiUrl
func TestConfigCustomApiUrl(t *testing.T) {
config := Config{
Token: "foo",
ApiUrl: "https://api.domain.tld",
SkipCredsValidation: true,
}

if _, err := config.Client(); err != nil {
t.Fatalf("error: expected the client to not fail: %v", err)
}
}

// Test config with a custom AppUrl
func TestConfigCustomAppUrl(t *testing.T) {
config := Config{
Token: "foo",
AppUrl: "https://app.domain.tld",
SkipCredsValidation: true,
}

if _, err := config.Client(); err != nil {
t.Fatalf("error: expected the client to not fail: %v", err)
}
}
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 @@ -24,7 +24,7 @@ func dataSourcePagerDutyBusinessService() *schema.Resource {
}

func dataSourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -24,7 +24,7 @@ func dataSourcePagerDutyEscalationPolicy() *schema.Resource {
}

func dataSourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

Expand Down
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 @@ -29,7 +29,7 @@ func dataSourcePagerDutyExtensionSchema() *schema.Resource {
}

func dataSourcePagerDutyExtensionSchemaRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -30,7 +30,7 @@ func dataSourcePagerDutyPriority() *schema.Resource {
}

func dataSourcePagerDutyPriorityRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -31,7 +31,7 @@ func dataSourcePagerDutyRuleset() *schema.Resource {
}

func dataSourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -24,7 +24,7 @@ func dataSourcePagerDutySchedule() *schema.Resource {
}

func dataSourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -24,7 +24,7 @@ func dataSourcePagerDutyService() *schema.Resource {
}

func dataSourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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

func dataSourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -25,7 +25,7 @@ func dataSourcePagerDutyTag() *schema.Resource {
}

func dataSourcePagerDutyTagRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -33,7 +33,7 @@ func dataSourcePagerDutyTeam() *schema.Resource {
}

func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -28,7 +28,7 @@ func dataSourcePagerDutyUser() *schema.Resource {
}

func dataSourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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 @@ -59,7 +59,7 @@ func dataSourcePagerDutyUserContactMethod() *schema.Resource {
}

func dataSourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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

func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

Expand Down
26 changes: 25 additions & 1 deletion pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"runtime"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
Expand All @@ -24,6 +25,18 @@ func Provider() *schema.Provider {
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PAGERDUTY_TOKEN", nil),
},

"user_token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PAGERDUTY_USER_TOKEN", nil),
},

"service_region": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -101,12 +114,23 @@ func handleNotFoundError(err error, d *schema.ResourceData) error {
}

func providerConfigure(data *schema.ResourceData, terraformVersion string) (interface{}, error) {
var ServiceRegion = strings.ToLower(data.Get("service_region").(string))

if ServiceRegion == "us" || ServiceRegion == "" {
ServiceRegion = ""
} else {
ServiceRegion = ServiceRegion + "."
}

config := Config{
ApiUrl: "https://api." + ServiceRegion + "pagerduty.com",
AppUrl: "https://app." + ServiceRegion + "pagerduty.com",
SkipCredsValidation: data.Get("skip_credentials_validation").(bool),
Token: data.Get("token").(string),
UserToken: data.Get("user_token").(string),
UserAgent: fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, terraformVersion),
}

log.Println("[INFO] Initializing PagerDuty client")
return config.Client()
return &config, nil
}
10 changes: 9 additions & 1 deletion pagerduty/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("PAGERDUTY_TOKEN"); v == "" {
t.Fatal("PAGERDUTY_TOKEN must be set for acceptance tests")
}

if v := os.Getenv("PAGERDUTY_USER_TOKEN"); v == "" {
t.Fatal("PAGERDUTY_USER_TOKEN must be set for acceptance tests")
}
}

// timeNowInLoc returns the current time in the given location.
Expand Down Expand Up @@ -68,9 +72,13 @@ func testAccPreCheckPagerDutyAbility(t *testing.T, ability string) {
if v := os.Getenv("PAGERDUTY_TOKEN"); v == "" {
t.Fatal("PAGERDUTY_TOKEN must be set for acceptance tests")
}
if v := os.Getenv("PAGERDUTY_USER_TOKEN"); v == "" {
t.Fatal("PAGERDUTY_USER_TOKEN must be set for acceptance tests")
}

config := &Config{
Token: os.Getenv("PAGERDUTY_TOKEN"),
Token: os.Getenv("PAGERDUTY_TOKEN"),
UserToken: os.Getenv("PAGERDUTY_USER_TOKEN"),
}

client, err := config.Client()
Expand Down
8 changes: 4 additions & 4 deletions pagerduty/resource_pagerduty_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func buildAddonStruct(d *schema.ResourceData) *pagerduty.Addon {
}

func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

addon := buildAddonStruct(d)

Expand All @@ -59,7 +59,7 @@ func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) erro
}

func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

Expand All @@ -83,7 +83,7 @@ func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error
}

func resourcePagerDutyAddonUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

addon := buildAddonStruct(d)

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

func resourcePagerDutyAddonDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestAccPagerDutyAddon_Basic(t *testing.T) {
}

func testAccCheckPagerDutyAddonDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
client, _ := testAccProvider.Meta().(*Config).Client()
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_addon" {
continue
Expand All @@ -106,7 +106,7 @@ func testAccCheckPagerDutyAddonExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No add-on ID is set")
}

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

found, _, err := client.Addons.Get(rs.Primary.ID)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pagerduty/resource_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func buildBusinessServiceStruct(d *schema.ResourceData) (*pagerduty.BusinessServ
}

func resourcePagerDutyBusinessServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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

func resourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

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

func resourcePagerDutyBusinessServiceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

func resourcePagerDutyBusinessServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
client, _ := meta.(*Config).Client()

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

Expand Down
5 changes: 2 additions & 3 deletions pagerduty/resource_pagerduty_business_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func init() {
Expand Down Expand Up @@ -132,7 +131,7 @@ func testAccCheckPagerDutyBusinessServiceExists(n string) resource.TestCheckFunc
return fmt.Errorf("No Business Service ID is set")
}

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

found, _, err := client.BusinessServices.Get(rs.Primary.ID)
if err != nil {
Expand All @@ -148,7 +147,7 @@ func testAccCheckPagerDutyBusinessServiceExists(n string) resource.TestCheckFunc
}

func testAccCheckPagerDutyBusinessServiceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
client, _ := testAccProvider.Meta().(*Config).Client()
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_business_service" {
continue
Expand Down
Loading