mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
* Fix some spec names. * Closes #11567 .Handle deleted/purged resources from Rancher.
This commit is contained in:
parent
9acb86a182
commit
f0dd9b23a3
@ -100,6 +100,12 @@ func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) er
|
|||||||
return nil
|
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)
|
log.Printf("[INFO] Environment Name: %s", env.Name)
|
||||||
|
|
||||||
d.Set("description", env.Description)
|
d.Set("description", env.Description)
|
||||||
|
@ -3,13 +3,14 @@ package rancher
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
rancherClient "github.com/rancher/go-rancher/client"
|
rancherClient "github.com/rancher/go-rancher/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccRancherEnvironment(t *testing.T) {
|
func TestAccRancherEnvironment_basic(t *testing.T) {
|
||||||
var environment rancherClient.Project
|
var environment rancherClient.Project
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
@ -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 {
|
func testAccCheckRancherEnvironmentExists(n string, env *rancherClient.Project) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
|
@ -118,6 +118,12 @@ func resourceRancherRegistrationTokenRead(d *schema.ResourceData, meta interface
|
|||||||
return nil
|
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)
|
log.Printf("[INFO] RegistrationToken Name: %s", regT.Name)
|
||||||
|
|
||||||
d.Set("description", regT.Description)
|
d.Set("description", regT.Description)
|
||||||
|
@ -3,13 +3,14 @@ package rancher
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
rancherClient "github.com/rancher/go-rancher/client"
|
rancherClient "github.com/rancher/go-rancher/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccRancherRegistrationToken(t *testing.T) {
|
func TestAccRancherRegistrationToken_basic(t *testing.T) {
|
||||||
var registrationToken rancherClient.RegistrationToken
|
var registrationToken rancherClient.RegistrationToken
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
@ -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", ®istrationToken),
|
||||||
|
testAccRancherRegistrationTokenDisappears(®istrationToken),
|
||||||
|
),
|
||||||
|
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 {
|
func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.RegistrationToken) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
@ -59,7 +136,7 @@ func testAccCheckRancherRegistrationTokenExists(n string, regT *rancherClient.Re
|
|||||||
return fmt.Errorf("No App Name is set")
|
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)
|
foundRegT, err := client.RegistrationToken.ById(rs.Primary.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -106,6 +106,12 @@ func resourceRancherRegistryRead(d *schema.ResourceData, meta interface{}) error
|
|||||||
return nil
|
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)
|
log.Printf("[INFO] Registry Name: %s", registry.Name)
|
||||||
|
|
||||||
d.Set("description", registry.Description)
|
d.Set("description", registry.Description)
|
||||||
|
@ -120,6 +120,12 @@ func resourceRancherRegistryCredentialRead(d *schema.ResourceData, meta interfac
|
|||||||
return nil
|
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)
|
log.Printf("[INFO] RegistryCredential Name: %s", registryCred.Name)
|
||||||
|
|
||||||
d.Set("description", registryCred.Description)
|
d.Set("description", registryCred.Description)
|
||||||
|
@ -3,13 +3,14 @@ package rancher
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
rancherClient "github.com/rancher/go-rancher/client"
|
rancherClient "github.com/rancher/go-rancher/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccRancherRegistryCredential(t *testing.T) {
|
func TestAccRancherRegistryCredential_basic(t *testing.T) {
|
||||||
var registry rancherClient.RegistryCredential
|
var registry rancherClient.RegistryCredential
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
@ -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", ®istry),
|
||||||
|
testAccRancherRegistryCredentialDisappears(®istry),
|
||||||
|
),
|
||||||
|
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 {
|
func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.RegistryCredential) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
@ -51,7 +129,7 @@ func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.Re
|
|||||||
return fmt.Errorf("No App Name is set")
|
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)
|
foundReg, err := client.RegistryCredential.ById(rs.Primary.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -59,7 +137,7 @@ func testAccCheckRancherRegistryCredentialExists(n string, reg *rancherClient.Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
if foundReg.Resource.Id != rs.Primary.ID {
|
if foundReg.Resource.Id != rs.Primary.ID {
|
||||||
return fmt.Errorf("Environment not found")
|
return fmt.Errorf("RegistryCredential not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
*reg = *foundReg
|
*reg = *foundReg
|
||||||
|
@ -3,13 +3,14 @@ package rancher
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
rancherClient "github.com/rancher/go-rancher/client"
|
rancherClient "github.com/rancher/go-rancher/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccRancherRegistry(t *testing.T) {
|
func TestAccRancherRegistry_basic(t *testing.T) {
|
||||||
var registry rancherClient.Registry
|
var registry rancherClient.Registry
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
@ -48,6 +49,83 @@ func TestAccRancherRegistry(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAccRancherRegistry_disappears(t *testing.T) {
|
||||||
|
var registry rancherClient.Registry
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
Providers: testAccProviders,
|
||||||
|
CheckDestroy: testAccCheckRancherRegistryDestroy,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
resource.TestStep{
|
||||||
|
Config: testAccRancherRegistryConfig,
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testAccCheckRancherRegistryExists("rancher_registry.foo", ®istry),
|
||||||
|
testAccRancherRegistryDisappears(®istry),
|
||||||
|
),
|
||||||
|
ExpectNonEmptyPlan: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccRancherRegistryDisappears(reg *rancherClient.Registry) 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.Registry.ActionDeactivate(reg); e != nil {
|
||||||
|
return fmt.Errorf("Error deactivating Registry: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stateConf := &resource.StateChangeConf{
|
||||||
|
Pending: []string{"active", "inactive", "deactivating"},
|
||||||
|
Target: []string{"inactive"},
|
||||||
|
Refresh: RegistryStateRefreshFunc(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 deactivated: %s", reg.Id, waitErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update resource to reflect its state
|
||||||
|
reg, err = client.Registry.ById(reg.Id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to refresh state of deactivated registry (%s): %s", reg.Id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Remove
|
||||||
|
if _, err := client.Registry.ActionRemove(reg); err != nil {
|
||||||
|
return fmt.Errorf("Error removing Registry: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stateConf = &resource.StateChangeConf{
|
||||||
|
Pending: []string{"inactive", "removed", "removing"},
|
||||||
|
Target: []string{"removed"},
|
||||||
|
Refresh: RegistryStateRefreshFunc(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 testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) resource.TestCheckFunc {
|
func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
@ -60,7 +138,7 @@ func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) re
|
|||||||
return fmt.Errorf("No App Name is set")
|
return fmt.Errorf("No App Name is set")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := testAccProvider.Meta().(*Config)
|
client, _ := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
|
||||||
|
|
||||||
foundReg, err := client.Registry.ById(rs.Primary.ID)
|
foundReg, err := client.Registry.ById(rs.Primary.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -68,7 +146,7 @@ func testAccCheckRancherRegistryExists(n string, reg *rancherClient.Registry) re
|
|||||||
}
|
}
|
||||||
|
|
||||||
if foundReg.Resource.Id != rs.Primary.ID {
|
if foundReg.Resource.Id != rs.Primary.ID {
|
||||||
return fmt.Errorf("Environment not found")
|
return fmt.Errorf("Registry not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
*reg = *foundReg
|
*reg = *foundReg
|
||||||
|
@ -138,7 +138,7 @@ func resourceRancherStackRead(d *schema.ResourceData, meta interface{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if stack.State == "removed" {
|
if removed(stack.State) {
|
||||||
log.Printf("[INFO] Stack %s was removed on %v", d.Id(), stack.Removed)
|
log.Printf("[INFO] Stack %s was removed on %v", d.Id(), stack.Removed)
|
||||||
d.SetId("")
|
d.SetId("")
|
||||||
return nil
|
return nil
|
||||||
|
@ -3,6 +3,7 @@ package rancher
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
@ -114,6 +115,56 @@ func TestAccRancherStack_catalog(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAccRancherStack_disappears(t *testing.T) {
|
||||||
|
var stack rancherClient.Environment
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
Providers: testAccProviders,
|
||||||
|
CheckDestroy: testAccCheckRancherStackDestroy,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
resource.TestStep{
|
||||||
|
Config: testAccRancherStackConfig,
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testAccCheckRancherStackExists("rancher_stack.foo", &stack),
|
||||||
|
testAccRancherStackDisappears(&stack),
|
||||||
|
),
|
||||||
|
ExpectNonEmptyPlan: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccRancherStackDisappears(stack *rancherClient.Environment) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
client, err := testAccProvider.Meta().(*Config).EnvironmentClient(stack.AccountId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.Environment.Delete(stack); err != nil {
|
||||||
|
return fmt.Errorf("Error deleting Stack: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stateConf := &resource.StateChangeConf{
|
||||||
|
Pending: []string{"active", "removed", "removing"},
|
||||||
|
Target: []string{"removed"},
|
||||||
|
Refresh: StackStateRefreshFunc(client, stack.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 stack (%s) to be removed: %s", stack.Id, waitErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testAccCheckRancherStackExists(n string, stack *rancherClient.Environment) resource.TestCheckFunc {
|
func testAccCheckRancherStackExists(n string, stack *rancherClient.Environment) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
@ -126,7 +177,7 @@ func testAccCheckRancherStackExists(n string, stack *rancherClient.Environment)
|
|||||||
return fmt.Errorf("No App Name is set")
|
return fmt.Errorf("No App Name is set")
|
||||||
}
|
}
|
||||||
|
|
||||||
client := testAccProvider.Meta().(*Config)
|
client, _ := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"])
|
||||||
|
|
||||||
foundStack, err := client.Environment.ById(rs.Primary.ID)
|
foundStack, err := client.Environment.ById(rs.Primary.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2,6 +2,11 @@ package rancher
|
|||||||
|
|
||||||
import "github.com/rancher/go-rancher/client"
|
import "github.com/rancher/go-rancher/client"
|
||||||
|
|
||||||
|
const (
|
||||||
|
REMOVED = "removed"
|
||||||
|
PURGED = "purged"
|
||||||
|
)
|
||||||
|
|
||||||
// GetActiveOrchestration get the name of the active orchestration for a environment
|
// GetActiveOrchestration get the name of the active orchestration for a environment
|
||||||
func GetActiveOrchestration(project *client.Project) string {
|
func GetActiveOrchestration(project *client.Project) string {
|
||||||
orch := "cattle"
|
orch := "cattle"
|
||||||
@ -17,3 +22,7 @@ func GetActiveOrchestration(project *client.Project) string {
|
|||||||
|
|
||||||
return orch
|
return orch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removed(state string) bool {
|
||||||
|
return state == REMOVED || state == PURGED
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user