Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #11567. Handled Rancher deleted resources. #11607

Merged
merged 2 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions builtin/providers/rancher/resource_rancher_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) er
return nil
}

if removed(env.State) {
log.Printf("[INFO] Environment %s was removed on %v", d.Id(), env.Removed)
d.SetId("")
return nil
}

log.Printf("[INFO] Environment Name: %s", env.Name)

d.Set("description", env.Description)
Expand Down
47 changes: 46 additions & 1 deletion builtin/providers/rancher/resource_rancher_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package rancher
import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
rancherClient "github.com/rancher/go-rancher/client"
)

func TestAccRancherEnvironment(t *testing.T) {
func TestAccRancherEnvironment_basic(t *testing.T) {
var environment rancherClient.Project

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -39,6 +40,50 @@ func TestAccRancherEnvironment(t *testing.T) {
})
}

func TestAccRancherEnvironment_disappears(t *testing.T) {
var environment rancherClient.Project

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherEnvironmentDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherEnvironmentConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherEnvironmentExists("rancher_environment.foo", &environment),
testAccRancherEnvironmentDisappears(&environment),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccRancherEnvironmentDisappears(env *rancherClient.Project) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*Config)
if err := client.Project.Delete(env); err != nil {
return fmt.Errorf("Error deleting Environment: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"active", "removed", "removing"},
Target: []string{"removed"},
Refresh: EnvironmentStateRefreshFunc(client, env.Id),
Timeout: 10 * time.Minute,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

_, waitErr := stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf(
"Error waiting for environment (%s) to be removed: %s", env.Id, waitErr)
}
return nil
}
}

func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func resourceRancherRegistrationTokenRead(d *schema.ResourceData, meta interface
return nil
}

if removed(regT.State) {
log.Printf("[INFO] Registration Token %s was removed on %v", d.Id(), regT.Removed)
d.SetId("")
return nil
}

log.Printf("[INFO] RegistrationToken Name: %s", regT.Name)

d.Set("description", regT.Description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package rancher
import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
rancherClient "github.com/rancher/go-rancher/client"
)

func TestAccRancherRegistrationToken(t *testing.T) {
func TestAccRancherRegistrationToken_basic(t *testing.T) {
var registrationToken rancherClient.RegistrationToken

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -47,6 +48,82 @@ func TestAccRancherRegistrationToken(t *testing.T) {
})
}

func TestAccRancherRegistrationToken_disappears(t *testing.T) {
var registrationToken rancherClient.RegistrationToken

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherRegistrationTokenDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherRegistrationTokenConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistrationTokenExists("rancher_registration_token.foo", &registrationToken),
testAccRancherRegistrationTokenDisappears(&registrationToken),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccRancherRegistrationTokenDisappears(token *rancherClient.RegistrationToken) resource.TestCheckFunc {
return func(s *terraform.State) error {
client, err := testAccProvider.Meta().(*Config).EnvironmentClient(token.AccountId)
if err != nil {
return err
}

if _, e := client.RegistrationToken.ActionDeactivate(token); e != nil {
return fmt.Errorf("Error deactivating RegistrationToken: %s", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"active", "inactive", "deactivating"},
Target: []string{"inactive"},
Refresh: RegistrationTokenStateRefreshFunc(client, token.Id),
Timeout: 10 * time.Minute,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

_, waitErr := stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf(
"Error waiting for registration token (%s) to be deactivated: %s", token.Id, waitErr)
}

// Update resource to reflect its state
token, err = client.RegistrationToken.ById(token.Id)
if err != nil {
return fmt.Errorf("Failed to refresh state of deactivated registration token (%s): %s", token.Id, err)
}

// Step 2: Remove
if _, err := client.RegistrationToken.ActionRemove(token); err != nil {
return fmt.Errorf("Error removing RegistrationToken: %s", err)
}

stateConf = &resource.StateChangeConf{
Pending: []string{"inactive", "removed", "removing"},
Target: []string{"removed"},
Refresh: RegistrationTokenStateRefreshFunc(client, token.Id),
Timeout: 10 * time.Minute,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

_, waitErr = stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf(
"Error waiting for registration token (%s) to be removed: %s", token.Id, waitErr)
}

return nil
}
}

func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.RegistrationToken) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -59,7 +136,7 @@ func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.Re
return fmt.Errorf("No App Name is set")
}

client := testAccProvider.Meta().(*Config)
client, _ := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])

foundRegT, err := client.RegistrationToken.ById(rs.Primary.ID)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions builtin/providers/rancher/resource_rancher_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func resourceRancherRegistryRead(d *schema.ResourceData, meta interface{}) error
return nil
}

if removed(registry.State) {
log.Printf("[INFO] Registry %s was removed on %v", d.Id(), registry.Removed)
d.SetId("")
return nil
}

log.Printf("[INFO] Registry Name: %s", registry.Name)

d.Set("description", registry.Description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func resourceRancherRegistryCredentialRead(d *schema.ResourceData, meta interfac
return nil
}

if removed(registryCred.State) {
log.Printf("[INFO] Registry Credential %s was removed on %v", d.Id(), registryCred.Removed)
d.SetId("")
return nil
}

log.Printf("[INFO] RegistryCredential Name: %s", registryCred.Name)

d.Set("description", registryCred.Description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package rancher
import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
rancherClient "github.com/rancher/go-rancher/client"
)

func TestAccRancherRegistryCredential(t *testing.T) {
func TestAccRancherRegistryCredential_basic(t *testing.T) {
var registry rancherClient.RegistryCredential

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -39,6 +40,83 @@ func TestAccRancherRegistryCredential(t *testing.T) {
})
}

func TestAccRancherRegistryCredential_disappears(t *testing.T) {
var registry rancherClient.RegistryCredential

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancherRegistryCredentialDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancherRegistryCredentialConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancherRegistryCredentialExists("rancher_registry_credential.foo", &registry),
testAccRancherRegistryCredentialDisappears(&registry),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccRancherRegistryCredentialDisappears(reg *rancherClient.RegistryCredential) resource.TestCheckFunc {
return func(s *terraform.State) error {
client, err := testAccProvider.Meta().(*Config).EnvironmentClient(reg.AccountId)
if err != nil {
return err
}

// Step 1: Deactivate
if _, e := client.RegistryCredential.ActionDeactivate(reg); e != nil {
return fmt.Errorf("Error deactivating RegistryCredential: %s", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"active", "inactive", "deactivating"},
Target: []string{"inactive"},
Refresh: RegistryCredentialStateRefreshFunc(client, reg.Id),
Timeout: 10 * time.Minute,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

_, waitErr := stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf(
"Error waiting for registry credential (%s) to be deactivated: %s", reg.Id, waitErr)
}

// Update resource to reflect its state
reg, err = client.RegistryCredential.ById(reg.Id)
if err != nil {
return fmt.Errorf("Failed to refresh state of deactivated registry credential (%s): %s", reg.Id, err)
}

// Step 2: Remove
if _, err := client.RegistryCredential.ActionRemove(reg); err != nil {
return fmt.Errorf("Error removing RegistryCredential: %s", err)
}

stateConf = &resource.StateChangeConf{
Pending: []string{"inactive", "removed", "removing"},
Target: []string{"removed"},
Refresh: RegistryCredentialStateRefreshFunc(client, reg.Id),
Timeout: 10 * time.Minute,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

_, waitErr = stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf(
"Error waiting for registry (%s) to be removed: %s", reg.Id, waitErr)
}

return nil
}
}

func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.RegistryCredential) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -51,15 +129,15 @@ func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.Re
return fmt.Errorf("No App Name is set")
}

client := testAccProvider.Meta().(*Config)
client, _ := testAccProvider.Meta().(*Config).RegistryClient(rs.Primary.Attributes["registry_id"])

foundReg, err := client.RegistryCredential.ById(rs.Primary.ID)
if err != nil {
return err
}

if foundReg.Resource.Id != rs.Primary.ID {
return fmt.Errorf("Environment not found")
return fmt.Errorf("RegistryCredential not found")
}

*reg = *foundReg
Expand Down
Loading