github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/ovh/config.go (about)

     1  package ovh
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/ovh/go-ovh/ovh"
     8  )
     9  
    10  type Config struct {
    11  	Endpoint          string
    12  	ApplicationKey    string
    13  	ApplicationSecret string
    14  	ConsumerKey       string
    15  	OVHClient         *ovh.Client
    16  }
    17  
    18  /* type used to verify client access to ovh api
    19   */
    20  type PartialMe struct {
    21  	Firstname string `json:"firstname"`
    22  }
    23  
    24  func clientDefault(c *Config) (*ovh.Client, error) {
    25  	client, err := ovh.NewClient(
    26  		c.Endpoint,
    27  		c.ApplicationKey,
    28  		c.ApplicationSecret,
    29  		c.ConsumerKey,
    30  	)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return client, nil
    35  }
    36  
    37  func (c *Config) loadAndValidate() error {
    38  	validEndpoint := false
    39  
    40  	ovhEndpoints := [2]string{ovh.OvhEU, ovh.OvhCA}
    41  
    42  	for _, e := range ovhEndpoints {
    43  		if ovh.Endpoints[c.Endpoint] == e {
    44  			validEndpoint = true
    45  		}
    46  	}
    47  
    48  	if !validEndpoint {
    49  		return fmt.Errorf("%s must be one of %#v endpoints\n", c.Endpoint, ovh.Endpoints)
    50  	}
    51  
    52  	targetClient, err := clientDefault(c)
    53  	if err != nil {
    54  		return fmt.Errorf("Error getting ovh client: %q\n", err)
    55  	}
    56  
    57  	var me PartialMe
    58  	err = targetClient.Get("/me", &me)
    59  	if err != nil {
    60  		return fmt.Errorf("OVH client seems to be misconfigured: %q\n", err)
    61  	}
    62  
    63  	log.Printf("[DEBUG] Logged in on OVH API as %s!", me.Firstname)
    64  	c.OVHClient = targetClient
    65  
    66  	return nil
    67  }