[TF] GKE Cluster - fix import on some convenience fields (#3146)

Signed-off-by: Modular Magician <magic-modules@google.com>
This commit is contained in:
The Magician 2019-02-28 13:08:55 -08:00 committed by emily
parent 1496810ca6
commit 77c086de1c
3 changed files with 83 additions and 52 deletions

View File

@ -601,11 +601,6 @@ func resourceContainerCluster() *schema.Resource {
} }
} }
func cidrOrSizeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// If the user specified a size and the API returned a full cidr block, suppress.
return strings.HasPrefix(new, "/") && strings.HasSuffix(old, new)
}
func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) error { func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config) config := meta.(*Config)
@ -1295,11 +1290,15 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
name := fmt.Sprintf("%s/nodePools/%s", containerClusterFullName(project, location, clusterName), "default-pool") name := fmt.Sprintf("%s/nodePools/%s", containerClusterFullName(project, location, clusterName), "default-pool")
op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(name).Do() op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(name).Do()
if err != nil { if err != nil {
return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) if !isGoogleApiErrorWithCode(err, 404) {
} return errwrap.Wrapf("Error deleting default node pool: {{err}}", err)
err = containerOperationWait(config, op, project, location, "removing default node pool", timeoutInMinutes) }
if err != nil { log.Printf("[WARN] Container cluster %q default node pool already removed, no change", d.Id())
return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) } else {
err = containerOperationWait(config, op, project, location, "removing default node pool", timeoutInMinutes)
if err != nil {
return errwrap.Wrapf("Error deleting default node pool: {{err}}", err)
}
} }
} }
@ -1751,29 +1750,55 @@ func flattenMasterAuthorizedNetworksConfig(c *containerBeta.MasterAuthorizedNetw
} }
func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/") config := meta.(*Config)
parts := strings.Split(d.Id(), "/")
var project, location, clusterName string
switch len(parts) { switch len(parts) {
case 2: case 2:
if loc := parts[0]; isZone(loc) { location = parts[0]
d.Set("zone", loc) clusterName = parts[1]
} else {
d.Set("region", loc)
}
d.Set("name", parts[1])
case 3: case 3:
d.Set("project", parts[0]) project = parts[0]
if loc := parts[1]; isZone(loc) { location = parts[1]
d.Set("zone", loc) clusterName = parts[2]
} else {
d.Set("region", loc)
}
d.Set("name", parts[2])
default: default:
return nil, fmt.Errorf("Invalid container cluster specifier. Expecting {zone}/{name} or {project}/{zone}/{name}") return nil, fmt.Errorf("Invalid container cluster specifier. Expecting {zone}/{name} or {project}/{zone}/{name}")
} }
d.SetId(parts[len(parts)-1]) if len(project) > 0 {
d.Set("project", project)
} else {
var err error
project, err = getProject(d, config)
if err != nil {
return nil, err
}
}
if isZone(location) {
d.Set("zone", location)
} else {
d.Set("region", location)
}
d.Set("name", clusterName)
d.SetId(clusterName)
// Try to determine remove_default_node_pool from presence of default
// node pool; if still present and user has it set to true, the pool
// will get removed on next apply.
nodePool := fmt.Sprintf("%s/nodePools/%s", containerClusterFullName(project, location, clusterName), "default-pool")
_, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(nodePool).Do()
if err != nil && isGoogleApiErrorWithCode(err, 404) {
d.Set("remove_default_node_pool", true)
} else {
d.Set("remove_default_node_pool", false)
if err != nil {
log.Printf("[WARN] Unable to import value for remove_default_node_pool, got error while trying to get default node pool: %s", err)
}
}
return []*schema.ResourceData{d}, nil return []*schema.ResourceData{d}, nil
} }
@ -1803,6 +1828,11 @@ func extractNodePoolInformationFromCluster(d *schema.ResourceData, config *Confi
}, nil }, nil
} }
func cidrOrSizeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// If the user specified a size and the API returned a full cidr block, suppress.
return strings.HasPrefix(new, "/") && strings.HasSuffix(old, new)
}
// We want to suppress diffs for empty or default client certificate configs, i.e: // We want to suppress diffs for empty or default client certificate configs, i.e:
// [{ "issue_client_certificate": true}] --> [] // [{ "issue_client_certificate": true}] --> []
// [] -> [{ "issue_client_certificate": true}] // [] -> [{ "issue_client_certificate": true}]

View File

@ -4,9 +4,8 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"regexp" "regexp"
"testing"
"strconv" "strconv"
"testing"
"github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
@ -207,11 +206,10 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
), ),
}, },
{ {
ResourceName: "google_container_cluster.with_network_policy_enabled", ResourceName: "google_container_cluster.with_network_policy_enabled",
ImportStateIdPrefix: "us-central1-a/", ImportStateIdPrefix: "us-central1-a/",
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool"},
}, },
{ {
Config: testAccContainerCluster_removeNetworkPolicy(clusterName), Config: testAccContainerCluster_removeNetworkPolicy(clusterName),
@ -221,11 +219,10 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
), ),
}, },
{ {
ResourceName: "google_container_cluster.with_network_policy_enabled", ResourceName: "google_container_cluster.with_network_policy_enabled",
ImportStateIdPrefix: "us-central1-a/", ImportStateIdPrefix: "us-central1-a/",
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool"},
}, },
{ {
Config: testAccContainerCluster_withNetworkPolicyDisabled(clusterName), Config: testAccContainerCluster_withNetworkPolicyDisabled(clusterName),
@ -235,11 +232,10 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
), ),
}, },
{ {
ResourceName: "google_container_cluster.with_network_policy_enabled", ResourceName: "google_container_cluster.with_network_policy_enabled",
ImportStateIdPrefix: "us-central1-a/", ImportStateIdPrefix: "us-central1-a/",
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool"},
}, },
{ {
Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName), Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName),
@ -249,11 +245,10 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
), ),
}, },
{ {
ResourceName: "google_container_cluster.with_network_policy_enabled", ResourceName: "google_container_cluster.with_network_policy_enabled",
ImportStateIdPrefix: "us-central1-a/", ImportStateIdPrefix: "us-central1-a/",
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool"},
}, },
{ {
Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName), Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName),
@ -1028,11 +1023,10 @@ func TestAccContainerCluster_withDefaultNodePoolRemoved(t *testing.T) {
"google_container_cluster.with_default_node_pool_removed", "node_pool.#", "0"), "google_container_cluster.with_default_node_pool_removed", "node_pool.#", "0"),
}, },
{ {
ResourceName: "google_container_cluster.with_default_node_pool_removed", ResourceName: "google_container_cluster.with_default_node_pool_removed",
ImportStateIdPrefix: "us-central1-a/", ImportStateIdPrefix: "us-central1-a/",
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"remove_default_node_pool"},
}, },
}, },
}) })

View File

@ -577,3 +577,10 @@ $ terraform import google_container_cluster.mycluster my-gcp-project/us-east1-a/
$ terraform import google_container_cluster.mycluster us-east1-a/my-cluster $ terraform import google_container_cluster.mycluster us-east1-a/my-cluster
``` ```
~> **Note:** This resource has several fields that control Terraform-specific behavior and aren't present in the API. If they are set in config and you import a cluster, Terraform may need to perform an update immediately after import. Some of these updates are no-ops, and some may modify your cluster.
For example:
- `min_master_version` will not be set on import and will show a no-op diff if set in config.
- `remove_default_node_pool`: If the default node pool exists at import, this value will be set to false in state (or true if non-existant). If set to true in config but the node pool exists, a follow-up diff/apply will delete the default node pool.