2016-09-23 21:43:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
2017-03-19 18:37:46 +01:00
|
|
|
"fmt"
|
2016-09-23 21:43:06 +02:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/PagerDuty/go-pagerduty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config defines the configuration options for the PagerDuty client
|
|
|
|
type Config struct {
|
2017-03-19 18:37:46 +01:00
|
|
|
// The PagerDuty API V2 token
|
2016-09-23 21:43:06 +02:00
|
|
|
Token string
|
2017-03-19 18:37:46 +01:00
|
|
|
|
|
|
|
// Skip validation of the token against the PagerDuty API
|
|
|
|
SkipCredsValidation bool
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 18:37:46 +01:00
|
|
|
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.
|
|
|
|
`
|
|
|
|
|
2016-09-23 21:43:06 +02:00
|
|
|
// Client returns a new PagerDuty client
|
|
|
|
func (c *Config) Client() (*pagerduty.Client, error) {
|
2017-03-19 18:37:46 +01:00
|
|
|
// Validate that the PagerDuty token is set
|
|
|
|
if c.Token == "" {
|
|
|
|
return nil, fmt.Errorf(invalidCreds)
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:43:06 +02:00
|
|
|
client := pagerduty.NewClient(c.Token)
|
|
|
|
|
2017-03-19 18:37:46 +01:00
|
|
|
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.ListAbilities(); err != nil {
|
|
|
|
if isUnauthorized(err) {
|
|
|
|
return nil, fmt.Errorf(fmt.Sprintf("%s\n%s", err, invalidCreds))
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:43:06 +02:00
|
|
|
log.Printf("[INFO] PagerDuty client configured")
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|