Skip to content

Commit

Permalink
Review fixes:
Browse files Browse the repository at this point in the history
- Set max items in node config schema
- Fill missing node config fields
- Put test helpers above than test vars
  • Loading branch information
aadidenko committed Jul 28, 2017
1 parent 67faa70 commit 7a34f6b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 39 deletions.
1 change: 1 addition & 0 deletions google/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var schemaNodeConfig = &schema.Schema{
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"machine_type": {
Expand Down
3 changes: 0 additions & 3 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,6 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
}
if v, ok := d.GetOk("node_config"); ok {
nodeConfigs := v.([]interface{})
if len(nodeConfigs) > 1 {
return fmt.Errorf("Cannot specify more than one node_config.")
}
nodeConfig := nodeConfigs[0].(map[string]interface{})

cluster.NodeConfig = &container.NodeConfig{}
Expand Down
40 changes: 37 additions & 3 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e

if v, ok := d.GetOk("node_config"); ok {
nodeConfigs := v.([]interface{})
if len(nodeConfigs) > 1 {
return fmt.Errorf("Cannot specify more than one node_config.")
}
nodeConfig := nodeConfigs[0].(map[string]interface{})

nodePool.Config = &container.NodeConfig{}
Expand All @@ -104,6 +101,10 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e
nodePool.Config.DiskSizeGb = int64(v.(int))
}

if v, ok = nodeConfig["local_ssd_count"]; ok {
nodePool.Config.LocalSsdCount = int64(v.(int))
}

if v, ok := nodeConfig["oauth_scopes"]; ok {
scopesList := v.([]interface{})
scopes := []string{}
Expand All @@ -113,6 +114,39 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e

nodePool.Config.OauthScopes = scopes
}

if v, ok = nodeConfig["service_account"]; ok {
nodePool.Config.ServiceAccount = v.(string)
}

if v, ok = nodeConfig["metadata"]; ok {
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
nodePool.Config.Metadata = m
}

if v, ok = nodeConfig["image_type"]; ok {
nodePool.Config.ImageType = v.(string)
}

if v, ok = nodeConfig["labels"]; ok {
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
nodePool.Config.Labels = m
}

if v, ok := nodeConfig["tags"]; ok {
tagsList := v.([]interface{})
tags := []string{}
for _, v := range tagsList {
tags = append(tags, v.(string))
}
nodePool.Config.Tags = tags
}
}

req := &container.CreateNodePoolRequest{
Expand Down
66 changes: 33 additions & 33 deletions google/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ func testAccCheckContainerNodePoolMatches(n string) resource.TestCheckFunc {
}
}

func nodepoolCheckMatch(attributes map[string]string, attr string, gcp interface{}) string {
if gcpList, ok := gcp.([]string); ok {
return nodepoolCheckListMatch(attributes, attr, gcpList)
}
tf := attributes[attr]
if tf != gcp {
return nodepoolMatchError(attr, tf, gcp)
}
return ""
}

func nodepoolCheckListMatch(attributes map[string]string, attr string, gcpList []string) string {
num, err := strconv.Atoi(attributes[attr+".#"])
if err != nil {
return fmt.Sprintf("Error in number conversion for attribute %s: %s", attr, err)
}
if num != len(gcpList) {
return fmt.Sprintf("NodePool has mismatched %s size.\nTF Size: %d\nGCP Size: %d", attr, num, len(gcpList))
}

for i, gcp := range gcpList {
if tf := attributes[fmt.Sprintf("%s.%d", attr, i)]; tf != gcp {
return nodepoolMatchError(fmt.Sprintf("%s[%d]", attr, i), tf, gcp)
}
}

return ""
}

func nodepoolMatchError(attr, tf string, gcp interface{}) string {
return fmt.Sprintf("NodePool has mismatched %s.\nTF State: %+v\nGCP State: %+v", attr, tf, gcp)
}

var testAccContainerNodePool_basic = fmt.Sprintf(`
resource "google_container_cluster" "cluster" {
name = "tf-cluster-nodepool-test-%s"
Expand Down Expand Up @@ -190,36 +223,3 @@ resource "google_container_node_pool" "np_with_node_config_scope_alias" {
oauth_scopes = ["compute-rw", "storage-ro", "logging-write", "monitoring"]
}
}`, acctest.RandString(10), acctest.RandString(10))

func nodepoolCheckMatch(attributes map[string]string, attr string, gcp interface{}) string {
if gcpList, ok := gcp.([]string); ok {
return nodepoolCheckListMatch(attributes, attr, gcpList)
}
tf := attributes[attr]
if tf != gcp {
return nodepoolMatchError(attr, tf, gcp)
}
return ""
}

func nodepoolCheckListMatch(attributes map[string]string, attr string, gcpList []string) string {
num, err := strconv.Atoi(attributes[attr+".#"])
if err != nil {
return fmt.Sprintf("Error in number conversion for attribute %s: %s", attr, err)
}
if num != len(gcpList) {
return fmt.Sprintf("NodePool has mismatched %s size.\nTF Size: %d\nGCP Size: %d", attr, num, len(gcpList))
}

for i, gcp := range gcpList {
if tf := attributes[fmt.Sprintf("%s.%d", attr, i)]; tf != gcp {
return nodepoolMatchError(fmt.Sprintf("%s[%d]", attr, i), tf, gcp)
}
}

return ""
}

func nodepoolMatchError(attr, tf string, gcp interface{}) string {
return fmt.Sprintf("NodePool has mismatched %s.\nTF State: %+v\nGCP State: %+v", attr, tf, gcp)
}

0 comments on commit 7a34f6b

Please sign in to comment.