From 64311ba77498baeb4b2d74c9f36c99502f3a9495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 31 Jan 2017 13:05:59 +0100 Subject: [PATCH] Rancher: reset Id for unknown resources When switching from one Rancher server to another, we want Terraform to recreate Rancher resources. This currently leads to ugly `EOF` errors. This patch resets resource Ids when they can't be found in the Rancher API. --- builtin/providers/rancher/resource_rancher_environment.go | 6 ++++++ .../rancher/resource_rancher_registration_token.go | 6 ++++++ builtin/providers/rancher/resource_rancher_registry.go | 6 ++++++ .../rancher/resource_rancher_registry_credential.go | 6 ++++++ builtin/providers/rancher/resource_rancher_stack.go | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/builtin/providers/rancher/resource_rancher_environment.go b/builtin/providers/rancher/resource_rancher_environment.go index 4d599b97af1a..6cbc7af7e122 100644 --- a/builtin/providers/rancher/resource_rancher_environment.go +++ b/builtin/providers/rancher/resource_rancher_environment.go @@ -94,6 +94,12 @@ func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) er return err } + if env == nil { + log.Printf("[INFO] Environment %s not found", d.Id()) + d.SetId("") + return nil + } + log.Printf("[INFO] Environment Name: %s", env.Name) d.Set("description", env.Description) diff --git a/builtin/providers/rancher/resource_rancher_registration_token.go b/builtin/providers/rancher/resource_rancher_registration_token.go index ec2cd60d8426..f35b022cdb13 100644 --- a/builtin/providers/rancher/resource_rancher_registration_token.go +++ b/builtin/providers/rancher/resource_rancher_registration_token.go @@ -108,6 +108,12 @@ func resourceRancherRegistrationTokenRead(d *schema.ResourceData, meta interface return err } + if regT == nil { + log.Printf("[INFO] RegistrationToken %s not found", d.Id()) + d.SetId("") + return nil + } + log.Printf("[INFO] RegistrationToken Name: %s", regT.Name) d.Set("description", regT.Description) diff --git a/builtin/providers/rancher/resource_rancher_registry.go b/builtin/providers/rancher/resource_rancher_registry.go index e17decfa5b15..ba452917a07a 100644 --- a/builtin/providers/rancher/resource_rancher_registry.go +++ b/builtin/providers/rancher/resource_rancher_registry.go @@ -100,6 +100,12 @@ func resourceRancherRegistryRead(d *schema.ResourceData, meta interface{}) error return err } + if registry == nil { + log.Printf("[INFO] Registry %s not found", d.Id()) + d.SetId("") + return nil + } + log.Printf("[INFO] Registry Name: %s", registry.Name) d.Set("description", registry.Description) diff --git a/builtin/providers/rancher/resource_rancher_registry_credential.go b/builtin/providers/rancher/resource_rancher_registry_credential.go index 49c3cd257e49..8187246aa811 100644 --- a/builtin/providers/rancher/resource_rancher_registry_credential.go +++ b/builtin/providers/rancher/resource_rancher_registry_credential.go @@ -114,6 +114,12 @@ func resourceRancherRegistryCredentialRead(d *schema.ResourceData, meta interfac return err } + if registryCred == nil { + log.Printf("[INFO] RegistryCredential %s not found", d.Id()) + d.SetId("") + return nil + } + log.Printf("[INFO] RegistryCredential Name: %s", registryCred.Name) d.Set("description", registryCred.Description) diff --git a/builtin/providers/rancher/resource_rancher_stack.go b/builtin/providers/rancher/resource_rancher_stack.go index ccdb4c059d83..88bb4bc7283e 100644 --- a/builtin/providers/rancher/resource_rancher_stack.go +++ b/builtin/providers/rancher/resource_rancher_stack.go @@ -132,6 +132,12 @@ func resourceRancherStackRead(d *schema.ResourceData, meta interface{}) error { return err } + if stack == nil { + log.Printf("[INFO] Stack %s not found", d.Id()) + d.SetId("") + return nil + } + if stack.State == "removed" { log.Printf("[INFO] Stack %s was removed on %v", d.Id(), stack.Removed) d.SetId("")