File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package pagerduty
22
33import (
4+ "fmt"
45 "log"
56
67 "github.com/PagerDuty/go-pagerduty"
78)
89
910// Config defines the configuration options for the PagerDuty client
1011type Config struct {
12+ // The PagerDuty API V2 token
1113 Token string
14+
15+ // Skip validation of the token against the PagerDuty API
16+ SkipCredsValidation bool
1217}
1318
19+ const invalidCreds = `
20+
21+ No valid credentials found for PagerDuty provider.
22+ Please see https://www.terraform.io/docs/providers/pagerduty/index.html
23+ for more information on providing credentials for this provider.
24+ `
25+
1426// Client returns a new PagerDuty client
1527func (c * Config ) Client () (* pagerduty.Client , error ) {
28+ // Validate that the PagerDuty token is set
29+ if c .Token == "" {
30+ return nil , fmt .Errorf (invalidCreds )
31+ }
32+
1633 client := pagerduty .NewClient (c .Token )
1734
35+ if ! c .SkipCredsValidation {
36+ // Validate the credentials by calling the abilities endpoint,
37+ // if we get a 401 response back we return an error to the user
38+ if _ , err := client .ListAbilities (); err != nil {
39+ if isUnauthorized (err ) {
40+ return nil , fmt .Errorf (fmt .Sprintf ("%s\n %s" , err , invalidCreds ))
41+ }
42+ return nil , err
43+ }
44+ }
45+
1846 log .Printf ("[INFO] PagerDuty client configured" )
1947
2048 return client , nil
Original file line number Diff line number Diff line change 1+ package pagerduty
2+
3+ import (
4+ "testing"
5+ )
6+
7+ // Test config with an empty token
8+ func TestConfigEmptyToken (t * testing.T ) {
9+ config := Config {
10+ Token : "" ,
11+ }
12+
13+ if _ , err := config .Client (); err == nil {
14+ t .Fatalf ("expected error, but got nil" )
15+ }
16+ }
17+
18+ // Test config with invalid token but with SkipCredsValidation
19+ func TestConfigSkipCredsValidation (t * testing.T ) {
20+ config := Config {
21+ Token : "foo" ,
22+ SkipCredsValidation : true ,
23+ }
24+
25+ if _ , err := config .Client (); err != nil {
26+ t .Fatalf ("error: expected the client to not fail: %v" , err )
27+ }
28+ }
Original file line number Diff line number Diff line change @@ -9,3 +9,7 @@ func isNotFound(err error) bool {
99
1010 return false
1111}
12+
13+ func isUnauthorized (err error ) bool {
14+ return strings .Contains (err .Error (), "HTTP response code: 401" )
15+ }
Original file line number Diff line number Diff line change @@ -16,6 +16,12 @@ func Provider() terraform.ResourceProvider {
1616 Required : true ,
1717 DefaultFunc : schema .EnvDefaultFunc ("PAGERDUTY_TOKEN" , nil ),
1818 },
19+
20+ "skip_credentials_validation" : {
21+ Type : schema .TypeBool ,
22+ Optional : true ,
23+ Default : false ,
24+ },
1925 },
2026
2127 DataSourcesMap : map [string ]* schema.Resource {
@@ -40,7 +46,11 @@ func Provider() terraform.ResourceProvider {
4046}
4147
4248func providerConfigure (data * schema.ResourceData ) (interface {}, error ) {
43- config := Config {Token : data .Get ("token" ).(string )}
49+ config := Config {
50+ Token : data .Get ("token" ).(string ),
51+ SkipCredsValidation : data .Get ("skip_credentials_validation" ).(bool ),
52+ }
53+
4454 log .Println ("[INFO] Initializing PagerDuty client" )
4555 return config .Client ()
4656}
Original file line number Diff line number Diff line change @@ -39,3 +39,4 @@ resource "pagerduty_user" "earline" {
3939The following arguments are supported:
4040
4141* ` token ` - (Required) The v2 authorization token. See [ API Documentation] ( https://v2.developer.pagerduty.com/docs/authentication ) for more information.
42+ * ` skip_credentials_validation ` - (Optional) Skip validation of the token against the PagerDuty API.
You can’t perform that action at this time.
0 commit comments