Skip to content

Commit

Permalink
[Rancher] - allow for importing resources using environment ID to tar…
Browse files Browse the repository at this point in the history
…get (#11688)
  • Loading branch information
johnrengelman authored and stack72 committed Feb 5, 2017
1 parent 64e2381 commit 1efd5ff
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 61 deletions.
21 changes: 13 additions & 8 deletions builtin/providers/rancher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,30 @@ import (
"github.com/raphink/go-rancher/catalog"
)

// Config is the configuration parameters for a Rancher API
type Config struct {
*rancherClient.RancherClient
APIURL string
AccessKey string
SecretKey string
}

// Create creates a generic Rancher client
func (c *Config) CreateClient() error {
// GlobalClient creates a Rancher client scoped to the global API
func (c *Config) GlobalClient() (*rancherClient.RancherClient, error) {
client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
Url: c.APIURL,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
})
if err != nil {
return err
return nil, err
}

log.Printf("[INFO] Rancher Client configured for url: %s", c.APIURL)

c.RancherClient = client

return nil
return client, nil
}

// EnvironmentClient creates a Rancher client scoped to an Environment's API
func (c *Config) EnvironmentClient(env string) (*rancherClient.RancherClient, error) {

url := c.APIURL + "/projects/" + env + "/schemas"
Expand All @@ -49,15 +48,21 @@ func (c *Config) EnvironmentClient(env string) (*rancherClient.RancherClient, er
return client, nil
}

// RegistryClient creates a Rancher client scoped to a Registry's API
func (c *Config) RegistryClient(id string) (*rancherClient.RancherClient, error) {
reg, err := c.Registry.ById(id)
client, err := c.GlobalClient()
if err != nil {
return nil, err
}
reg, err := client.Registry.ById(id)
if err != nil {
return nil, err
}

return c.EnvironmentClient(reg.AccountId)
}

// CatalogClient creates a Rancher client scoped to a Catalog's API
func (c *Config) CatalogClient() (*catalog.RancherClient, error) {

url := c.APIURL + "-catalog/schemas"
Expand Down
2 changes: 1 addition & 1 deletion builtin/providers/rancher/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
SecretKey: d.Get("secret_key").(string),
}

err := config.CreateClient()
_, err := config.GlobalClient()

return config, err
}
24 changes: 18 additions & 6 deletions builtin/providers/rancher/resource_rancher_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func resourceRancherEnvironment() *schema.Resource {

func resourceRancherEnvironmentCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Creating Environment: %s", d.Id())
client := meta.(*Config)
client, err := meta.(*Config).GlobalClient()
if err != nil {
return err
}

name := d.Get("name").(string)
description := d.Get("description").(string)
Expand Down Expand Up @@ -87,7 +90,10 @@ func resourceRancherEnvironmentCreate(d *schema.ResourceData, meta interface{})

func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Refreshing Environment: %s", d.Id())
client := meta.(*Config)
client, err := meta.(*Config).GlobalClient()
if err != nil {
return err
}

env, err := client.Project.ById(d.Id())
if err != nil {
Expand All @@ -110,13 +116,16 @@ func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) er

d.Set("description", env.Description)
d.Set("name", env.Name)
d.Set("orchestration", GetActiveOrchestration(env))
d.Set("orchestration", getActiveOrchestration(env))

return nil
}

func resourceRancherEnvironmentUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config)
client, err := meta.(*Config).GlobalClient()
if err != nil {
return err
}

name := d.Get("name").(string)
description := d.Get("description").(string)
Expand Down Expand Up @@ -145,7 +154,10 @@ func resourceRancherEnvironmentUpdate(d *schema.ResourceData, meta interface{})
func resourceRancherEnvironmentDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Deleting Environment: %s", d.Id())
id := d.Id()
client := meta.(*Config)
client, err := meta.(*Config).GlobalClient()
if err != nil {
return err
}

env, err := client.Project.ById(id)
if err != nil {
Expand Down Expand Up @@ -193,7 +205,7 @@ func setOrchestrationFields(orchestration string, data map[string]interface{}) {

// EnvironmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a Rancher Environment.
func EnvironmentStateRefreshFunc(client *Config, environmentID string) resource.StateRefreshFunc {
func EnvironmentStateRefreshFunc(client *rancherClient.RancherClient, environmentID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
env, err := client.Project.ById(environmentID)

Expand Down
15 changes: 12 additions & 3 deletions builtin/providers/rancher/resource_rancher_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func TestAccRancherEnvironment_disappears(t *testing.T) {

func testAccRancherEnvironmentDisappears(env *rancherClient.Project) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)
client, err := testAccProvider.Meta().(*Config).GlobalClient()
if err != nil {
return err
}
if err := client.Project.Delete(env); err != nil {
return fmt.Errorf("Error deleting Environment: %s", err)
}
Expand Down Expand Up @@ -96,7 +99,10 @@ func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project)
return fmt.Errorf("No App Name is set")
}

client := testAccProvider.Meta().(*Config)
client, err := testAccProvider.Meta().(*Config).GlobalClient()
if err != nil {
return err
}

foundEnv, err := client.Project.ById(rs.Primary.ID)
if err != nil {
Expand All @@ -114,7 +120,10 @@ func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project)
}

func testAccCheckRancherEnvironmentDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)
client, err := testAccProvider.Meta().(*Config).GlobalClient()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "rancher_environment" {
Expand Down
19 changes: 14 additions & 5 deletions builtin/providers/rancher/resource_rancher_registration_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,21 @@ func resourceRancherRegistrationTokenDelete(d *schema.ResourceData, meta interfa
}

func resourceRancherRegistrationTokenImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Config)
regT, err := client.RegistrationToken.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
envID, resourceID := splitID(d.Id())
d.SetId(resourceID)
if envID != "" {
d.Set("environment_id", envID)
} else {
client, err := meta.(*Config).GlobalClient()
if err != nil {
return []*schema.ResourceData{}, err
}
token, err := client.RegistrationToken.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}
d.Set("environment_id", token.AccountId)
}
d.Set("environment_id", regT.AccountId)
return []*schema.ResourceData{d}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.Re
return fmt.Errorf("No App Name is set")
}

client, _ := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
client, err := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
if err != nil {
return err
}

foundRegT, err := client.RegistrationToken.ById(rs.Primary.ID)
if err != nil {
Expand All @@ -154,12 +157,16 @@ func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.Re
}

func testAccCheckRancherRegistrationTokenDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)

for _, rs := range s.RootModule().Resources {
if rs.Type != "rancher_registration_token" {
continue
}
client, err := testAccProvider.Meta().(*Config).GlobalClient()
if err != nil {
return err
}

regT, err := client.RegistrationToken.ById(rs.Primary.ID)

if err == nil {
Expand Down
19 changes: 14 additions & 5 deletions builtin/providers/rancher/resource_rancher_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,21 @@ func resourceRancherRegistryDelete(d *schema.ResourceData, meta interface{}) err
}

func resourceRancherRegistryImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Config)
reg, err := client.Registry.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
envID, resourceID := splitID(d.Id())
d.SetId(resourceID)
if envID != "" {
d.Set("environment_id", envID)
} else {
client, err := meta.(*Config).GlobalClient()
if err != nil {
return []*schema.ResourceData{}, err
}
registry, err := client.Registry.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}
d.Set("environment_id", registry.AccountId)
}
d.Set("environment_id", reg.AccountId)
return []*schema.ResourceData{d}, nil
}

Expand Down
19 changes: 14 additions & 5 deletions builtin/providers/rancher/resource_rancher_registry_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,21 @@ func resourceRancherRegistryCredentialDelete(d *schema.ResourceData, meta interf
}

func resourceRancherRegistryCredentialImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Config)
regC, err := client.RegistryCredential.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
regID, resourceID := splitID(d.Id())
d.SetId(resourceID)
if regID != "" {
d.Set("registry_id", regID)
} else {
client, err := meta.(*Config).GlobalClient()
if err != nil {
return []*schema.ResourceData{}, err
}
cred, err := client.RegistryCredential.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}
d.Set("registry_id", cred.RegistryId)
}
d.Set("environment_id", regC.AccountId)
return []*schema.ResourceData{d}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.Re
return fmt.Errorf("No App Name is set")
}

client, _ := testAccProvider.Meta().(*Config).RegistryClient(rs.Primary.Attributes["registry_id"])
client, err := testAccProvider.Meta().(*Config).RegistryClient(rs.Primary.Attributes["registry_id"])
if err != nil {
return err
}

foundReg, err := client.RegistryCredential.ById(rs.Primary.ID)
if err != nil {
Expand All @@ -147,12 +150,15 @@ func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.Re
}

func testAccCheckRancherRegistryCredentialDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)

for _, rs := range s.RootModule().Resources {
if rs.Type != "rancher_registry_credential" {
continue
}
client, err := testAccProvider.Meta().(*Config).GlobalClient()
if err != nil {
return err
}

reg, err := client.RegistryCredential.ById(rs.Primary.ID)

if err == nil {
Expand Down
12 changes: 9 additions & 3 deletions builtin/providers/rancher/resource_rancher_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) re
return fmt.Errorf("No App Name is set")
}

client, _ := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
client, err := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
if err != nil {
return err
}

foundReg, err := client.Registry.ById(rs.Primary.ID)
if err != nil {
Expand All @@ -156,12 +159,15 @@ func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) re
}

func testAccCheckRancherRegistryDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)

for _, rs := range s.RootModule().Resources {
if rs.Type != "rancher_registry" {
continue
}
client, err := testAccProvider.Meta().(*Config).GlobalClient()
if err != nil {
return err
}

reg, err := client.Registry.ById(rs.Primary.ID)

if err == nil {
Expand Down
28 changes: 20 additions & 8 deletions builtin/providers/rancher/resource_rancher_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,13 @@ func resourceRancherStackRead(d *schema.ResourceData, meta interface{}) error {
dockerCompose := strings.Replace(config.DockerComposeConfig, "\r", "", -1)
rancherCompose := strings.Replace(config.RancherComposeConfig, "\r", "", -1)

catalogId := d.Get("catalog_id")
if catalogId == "" {
catalogID := d.Get("catalog_id")
if catalogID == "" {
d.Set("docker_compose", dockerCompose)
d.Set("rancher_compose", rancherCompose)
} else {
d.Set("docker_compose", "")
d.Set("rancher_compose", "")
}
d.Set("rendered_docker_compose", dockerCompose)
d.Set("rendered_rancher_compose", rancherCompose)
Expand Down Expand Up @@ -203,7 +206,7 @@ func resourceRancherStackUpdate(d *schema.ResourceData, meta interface{}) error
}

var newStack rancherClient.Environment
if err := client.Update("environment", &stack.Resource, data, &newStack); err != nil {
if err = client.Update("environment", &stack.Resource, data, &newStack); err != nil {
return err
}

Expand Down Expand Up @@ -334,12 +337,21 @@ func resourceRancherStackDelete(d *schema.ResourceData, meta interface{}) error
}

func resourceRancherStackImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Config)
stack, err := client.Environment.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
envID, resourceID := splitID(d.Id())
d.SetId(resourceID)
if envID != "" {
d.Set("environment_id", envID)
} else {
client, err := meta.(*Config).GlobalClient()
if err != nil {
return []*schema.ResourceData{}, err
}
stack, err := client.Environment.ById(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}
d.Set("environment_id", stack.AccountId)
}
d.Set("environment_id", stack.AccountId)
return []*schema.ResourceData{d}, nil
}

Expand Down
Loading

0 comments on commit 1efd5ff

Please sign in to comment.