Skip to content

Commit

Permalink
add initial node count back but make it deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow committed Aug 17, 2017
1 parent e75d407 commit 7220e5b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
39 changes: 31 additions & 8 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"google.golang.org/api/container/v1"
)

Expand All @@ -29,7 +30,7 @@ func resourceContainerCluster() *schema.Resource {
Delete: schema.DefaultTimeout(10 * time.Minute),
},

SchemaVersion: 2,
SchemaVersion: 1,
MigrateState: resourceContainerClusterMigrateState,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -236,9 +237,18 @@ func resourceContainerCluster() *schema.Resource {
ForceNew: true, // TODO(danawillow): Add ability to add/remove nodePools
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"initial_node_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Deprecated: "Use node_count instead",
},

"node_count": {
Type: schema.TypeInt,
Required: true,
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(1),
},

"name": {
Expand Down Expand Up @@ -374,7 +384,19 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
for i := 0; i < nodePoolsCount; i++ {
prefix := fmt.Sprintf("node_pool.%d", i)

nodeCount := d.Get(prefix + ".node_count").(int)
nodeCount := 0
if initialNodeCount, ok := d.GetOk(prefix + ".initial_node_count"); ok {
nodeCount = initialNodeCount.(int)
}
if nc, ok := d.GetOk(prefix + ".node_count"); ok {
if nodeCount != 0 {
return fmt.Errorf("Cannot set both initial_node_count and node_count on node pool %d", i)
}
nodeCount = nc.(int)
}
if nodeCount == 0 {
return fmt.Errorf("Node pool %d cannot be set with 0 node count", i)
}

name, err := generateNodePoolName(prefix, d)
if err != nil {
Expand Down Expand Up @@ -727,10 +749,11 @@ func flattenClusterNodePools(d *schema.ResourceData, config *Config, c []*contai
size += int(igm.TargetSize)
}
nodePool := map[string]interface{}{
"name": np.Name,
"name_prefix": d.Get(fmt.Sprintf("node_pool.%d.name_prefix", i)),
"node_config": flattenClusterNodeConfig(np.Config),
"node_count": size / len(np.InstanceGroupUrls),
"name": np.Name,
"name_prefix": d.Get(fmt.Sprintf("node_pool.%d.name_prefix", i)),
"initial_node_count": np.InitialNodeCount,
"node_count": size / len(np.InstanceGroupUrls),
"node_config": flattenClusterNodeConfig(np.Config),
}
nodePools = append(nodePools, nodePool)
}
Expand Down
23 changes: 0 additions & 23 deletions google/resource_container_cluster_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ func resourceContainerClusterMigrateState(
case 0:
log.Println("[INFO] Found Container Cluster State v0; migrating to v1")
return migrateClusterStateV0toV1(is)
case 1:
log.Println("[INFO] Found Container Cluster State v1; migrating to v2")
return migrateClusterStateV1toV2(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
Expand Down Expand Up @@ -71,23 +68,3 @@ func migrateClusterStateV0toV1(is *terraform.InstanceState) (*terraform.Instance
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}

func migrateClusterStateV1toV2(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

for k, v := range is.Attributes {
if !strings.HasPrefix(k, "node_pool.") {
continue
}
if !strings.HasSuffix(k, ".initial_node_count") {
continue
}

is.Attributes[strings.Replace(k, "initial_node_count", "node_count", 1)] = v

delete(is.Attributes, k)
}

log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
14 changes: 0 additions & 14 deletions google/resource_container_cluster_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ func TestContainerClusterMigrateState(t *testing.T) {
},
Meta: &Config{},
},
"rename node_pool.initial_node_count to node_pool.node_count": {
StateVersion: 1,
Attributes: map[string]string{
"node_pool.#": "2",
"node_pool.0.initial_node_count": "3",
"node_pool.1.initial_node_count": "2",
},
Expected: map[string]string{
"node_pool.#": "2",
"node_pool.0.node_count": "3",
"node_pool.1.node_count": "2",
},
Meta: &Config{},
},
}

for tn, tc := range cases {
Expand Down
2 changes: 1 addition & 1 deletion google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ resource "google_container_cluster" "with_node_pool" {
node_pool {
name = "tf-cluster-nodepool-test-%s"
node_count = 2
initial_node_count = 2
}
}`, acctest.RandString(10), acctest.RandString(10))

Expand Down

0 comments on commit 7220e5b

Please sign in to comment.