forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
65 lines (51 loc) · 1.55 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pagerduty
import (
"fmt"
"log"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/heimweh/go-pagerduty/pagerduty"
)
// Config defines the configuration options for the PagerDuty client
type Config struct {
// The PagerDuty API V2 token
Token string
// Skip validation of the token against the PagerDuty API
SkipCredsValidation bool
// UserAgent for API Client
UserAgent string
}
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.
`
// Client returns a new PagerDuty client
func (c *Config) Client() (*pagerduty.Client, error) {
// Validate that the PagerDuty token is set
if c.Token == "" {
return nil, fmt.Errorf(invalidCreds)
}
var httpClient *http.Client
httpClient = http.DefaultClient
httpClient.Transport = logging.NewTransport("PagerDuty", http.DefaultTransport)
config := &pagerduty.Config{
Debug: logging.IsDebugOrHigher(),
HTTPClient: httpClient,
Token: c.Token,
UserAgent: c.UserAgent,
}
client, err := pagerduty.NewClient(config)
if err != nil {
return nil, err
}
if !c.SkipCredsValidation {
// Validate the credentials by calling the abilities endpoint,
// if we get a 401 response back we return an error to the user
if err := client.ValidateAuth(); err != nil {
return nil, fmt.Errorf(fmt.Sprintf("%s\n%s", err, invalidCreds))
}
}
log.Printf("[INFO] PagerDuty client configured")
return client, nil
}