2016-12-05 16:29:41 +01:00
|
|
|
package rancher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
rancherClient "github.com/rancher/go-rancher/client"
|
|
|
|
"github.com/raphink/go-rancher/catalog"
|
|
|
|
)
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
// Config is the configuration parameters for a Rancher API
|
2016-12-05 16:29:41 +01:00
|
|
|
type Config struct {
|
|
|
|
APIURL string
|
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
}
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
// GlobalClient creates a Rancher client scoped to the global API
|
|
|
|
func (c *Config) GlobalClient() (*rancherClient.RancherClient, error) {
|
2016-12-05 16:29:41 +01:00
|
|
|
client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
|
|
|
|
Url: c.APIURL,
|
|
|
|
AccessKey: c.AccessKey,
|
|
|
|
SecretKey: c.SecretKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-02-05 11:35:48 +01:00
|
|
|
return nil, err
|
2016-12-05 16:29:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Rancher Client configured for url: %s", c.APIURL)
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
return client, nil
|
2016-12-05 16:29:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
// EnvironmentClient creates a Rancher client scoped to an Environment's API
|
2016-12-05 16:29:41 +01:00
|
|
|
func (c *Config) EnvironmentClient(env string) (*rancherClient.RancherClient, error) {
|
|
|
|
|
|
|
|
url := c.APIURL + "/projects/" + env + "/schemas"
|
|
|
|
client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
|
|
|
|
Url: url,
|
|
|
|
AccessKey: c.AccessKey,
|
|
|
|
SecretKey: c.SecretKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Rancher Client configured for url: %s", url)
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
// RegistryClient creates a Rancher client scoped to a Registry's API
|
2016-12-05 16:29:41 +01:00
|
|
|
func (c *Config) RegistryClient(id string) (*rancherClient.RancherClient, error) {
|
2017-02-05 11:35:48 +01:00
|
|
|
client, err := c.GlobalClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reg, err := client.Registry.ById(id)
|
2016-12-05 16:29:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.EnvironmentClient(reg.AccountId)
|
|
|
|
}
|
|
|
|
|
2017-02-05 11:35:48 +01:00
|
|
|
// CatalogClient creates a Rancher client scoped to a Catalog's API
|
2016-12-05 16:29:41 +01:00
|
|
|
func (c *Config) CatalogClient() (*catalog.RancherClient, error) {
|
|
|
|
|
|
|
|
url := c.APIURL + "-catalog/schemas"
|
|
|
|
client, err := catalog.NewRancherClient(&catalog.ClientOpts{
|
|
|
|
Url: url,
|
|
|
|
AccessKey: c.AccessKey,
|
|
|
|
SecretKey: c.SecretKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Rancher Catalog Client configured for url: %s", url)
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|