github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/config.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/PagerDuty/go-pagerduty"
     8  )
     9  
    10  // Config defines the configuration options for the PagerDuty client
    11  type Config struct {
    12  	// The PagerDuty API V2 token
    13  	Token string
    14  
    15  	// Skip validation of the token against the PagerDuty API
    16  	SkipCredsValidation bool
    17  }
    18  
    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  
    26  // Client returns a new PagerDuty client
    27  func (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  
    33  	client := pagerduty.NewClient(c.Token)
    34  
    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  
    46  	log.Printf("[INFO] PagerDuty client configured")
    47  
    48  	return client, nil
    49  }