github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/spotinst/config.go (about)

     1  package spotinst
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/spotinst/spotinst-sdk-go/spotinst"
     8  )
     9  
    10  type Config struct {
    11  	Email        string
    12  	Password     string
    13  	ClientID     string
    14  	ClientSecret string
    15  	Token        string
    16  }
    17  
    18  // Validate returns an error in case of invalid configuration.
    19  func (c *Config) Validate() error {
    20  	msg := "%s\n\nNo valid credentials found for Spotinst Provider.\nPlease see https://www.terraform.io/docs/providers/spotinst/index.html\nfor more information on providing credentials for Spotinst Provider."
    21  
    22  	if c.Password != "" && c.Token != "" {
    23  		err := "ERR_CONFLICT: Both a password and a token were set, only one is required"
    24  		return fmt.Errorf(msg, err)
    25  	}
    26  
    27  	if c.Password != "" && (c.Email == "" || c.ClientID == "" || c.ClientSecret == "") {
    28  		err := "ERR_MISSING: A password was set without email, client_id or client_secret"
    29  		return fmt.Errorf(msg, err)
    30  	}
    31  
    32  	if c.Password == "" && c.Token == "" {
    33  		err := "ERR_MISSING: A token is required if not using password"
    34  		return fmt.Errorf(msg, err)
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  // Client returns a new client for accessing Spotinst.
    41  func (c *Config) Client() (*spotinst.Client, error) {
    42  	var clientOpts []spotinst.ClientOptionFunc
    43  	if c.Token != "" {
    44  		clientOpts = append(clientOpts, spotinst.SetToken(c.Token))
    45  	} else {
    46  		clientOpts = append(clientOpts, spotinst.SetCredentials(c.Email, c.Password, c.ClientID, c.ClientSecret))
    47  	}
    48  	client, err := spotinst.NewClient(clientOpts...)
    49  	if err != nil {
    50  		return nil, fmt.Errorf("Error setting up client: %s", err)
    51  	}
    52  	log.Printf("[INFO] Spotinst client configured")
    53  	return client, nil
    54  }