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

     1  package rancher
     2  
     3  import (
     4  	"log"
     5  
     6  	rancherClient "github.com/rancher/go-rancher/client"
     7  	"github.com/raphink/go-rancher/catalog"
     8  )
     9  
    10  // Config is the configuration parameters for a Rancher API
    11  type Config struct {
    12  	APIURL    string
    13  	AccessKey string
    14  	SecretKey string
    15  }
    16  
    17  // GlobalClient creates a Rancher client scoped to the global API
    18  func (c *Config) GlobalClient() (*rancherClient.RancherClient, error) {
    19  	client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
    20  		Url:       c.APIURL,
    21  		AccessKey: c.AccessKey,
    22  		SecretKey: c.SecretKey,
    23  	})
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	log.Printf("[INFO] Rancher Client configured for url: %s", c.APIURL)
    29  
    30  	return client, nil
    31  }
    32  
    33  // EnvironmentClient creates a Rancher client scoped to an Environment's API
    34  func (c *Config) EnvironmentClient(env string) (*rancherClient.RancherClient, error) {
    35  
    36  	url := c.APIURL + "/projects/" + env + "/schemas"
    37  	client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
    38  		Url:       url,
    39  		AccessKey: c.AccessKey,
    40  		SecretKey: c.SecretKey,
    41  	})
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	log.Printf("[INFO] Rancher Client configured for url: %s", url)
    47  
    48  	return client, nil
    49  }
    50  
    51  // RegistryClient creates a Rancher client scoped to a Registry's API
    52  func (c *Config) RegistryClient(id string) (*rancherClient.RancherClient, error) {
    53  	client, err := c.GlobalClient()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	reg, err := client.Registry.ById(id)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	return c.EnvironmentClient(reg.AccountId)
    63  }
    64  
    65  // CatalogClient creates a Rancher client scoped to a Catalog's API
    66  func (c *Config) CatalogClient() (*catalog.RancherClient, error) {
    67  
    68  	url := c.APIURL + "-catalog/schemas"
    69  	client, err := catalog.NewRancherClient(&catalog.ClientOpts{
    70  		Url:       url,
    71  		AccessKey: c.AccessKey,
    72  		SecretKey: c.SecretKey,
    73  	})
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	log.Printf("[INFO] Rancher Catalog Client configured for url: %s", url)
    79  
    80  	return client, nil
    81  }