github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/builtin/providers/rancher/config.go (about) 1 package rancher 2 3 import ( 4 "log" 5 6 "github.com/rancher/go-rancher/catalog" 7 rancherClient "github.com/rancher/go-rancher/v2" 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 globalClient, err := c.GlobalClient() 37 if err != nil { 38 return nil, err 39 } 40 41 project, err := globalClient.Project.ById(env) 42 if err != nil { 43 return nil, err 44 } 45 projectURL := project.Links["self"] 46 47 log.Printf("[INFO] Rancher Client configured for url: %s/schemas", projectURL) 48 49 return rancherClient.NewRancherClient(&rancherClient.ClientOpts{ 50 Url: projectURL + "/schemas", 51 AccessKey: c.AccessKey, 52 SecretKey: c.SecretKey, 53 }) 54 } 55 56 // RegistryClient creates a Rancher client scoped to a Registry's API 57 func (c *Config) RegistryClient(id string) (*rancherClient.RancherClient, error) { 58 client, err := c.GlobalClient() 59 if err != nil { 60 return nil, err 61 } 62 reg, err := client.Registry.ById(id) 63 if err != nil { 64 return nil, err 65 } 66 67 return c.EnvironmentClient(reg.AccountId) 68 } 69 70 // CatalogClient creates a Rancher client scoped to a Catalog's API 71 func (c *Config) CatalogClient() (*catalog.RancherClient, error) { 72 return catalog.NewRancherClient(&catalog.ClientOpts{ 73 Url: c.APIURL, 74 AccessKey: c.AccessKey, 75 SecretKey: c.SecretKey, 76 }) 77 }