From 5d7e156982dcd2ea2d743f0298bae6263e95d53a Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Mon, 31 Aug 2015 09:06:25 -0500 Subject: [PATCH] provider/google: Misc. cleanups for tests to pass --- .../google/resource_compute_instance.go | 3 +- ...resource_compute_instance_template_test.go | 4 +- .../google/resource_compute_instance_test.go | 4 +- .../resource_compute_project_metadata.go | 45 ++++++++++--------- .../resource_compute_project_metadata_test.go | 8 ++-- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 8233815b3..63d2260a0 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -809,9 +809,10 @@ func resourceInstanceMetadata(d *schema.ResourceData) (*compute.Metadata, error) if len(mdMap) > 0 { m.Items = make([]*compute.MetadataItems, 0, len(mdMap)) for key, val := range mdMap { + v := val.(string) m.Items = append(m.Items, &compute.MetadataItems{ Key: key, - Value: val.(string), + Value: &v, }) } diff --git a/builtin/providers/google/resource_compute_instance_template_test.go b/builtin/providers/google/resource_compute_instance_template_test.go index e1688d9ea..769ea68a7 100644 --- a/builtin/providers/google/resource_compute_instance_template_test.go +++ b/builtin/providers/google/resource_compute_instance_template_test.go @@ -132,11 +132,11 @@ func testAccCheckComputeInstanceTemplateMetadata( continue } - if v == item.Value { + if item.Value != nil && v == *item.Value { return nil } - return fmt.Errorf("bad value for %s: %s", k, item.Value) + return fmt.Errorf("bad value for %s: %s", k, *item.Value) } return fmt.Errorf("metadata not found: %s", k) diff --git a/builtin/providers/google/resource_compute_instance_test.go b/builtin/providers/google/resource_compute_instance_test.go index 3ae487a18..394e66dbf 100644 --- a/builtin/providers/google/resource_compute_instance_test.go +++ b/builtin/providers/google/resource_compute_instance_test.go @@ -332,11 +332,11 @@ func testAccCheckComputeInstanceMetadata( continue } - if v == item.Value { + if item.Value != nil && v == *item.Value { return nil } - return fmt.Errorf("bad value for %s: %s", k, item.Value) + return fmt.Errorf("bad value for %s: %s", k, *item.Value) } return fmt.Errorf("metadata not found: %s", k) diff --git a/builtin/providers/google/resource_compute_project_metadata.go b/builtin/providers/google/resource_compute_project_metadata.go index ff0906dad..850ade3dc 100644 --- a/builtin/providers/google/resource_compute_project_metadata.go +++ b/builtin/providers/google/resource_compute_project_metadata.go @@ -5,25 +5,25 @@ import ( "log" "time" -// "github.com/hashicorp/terraform/helper/hashcode" + // "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" -// "google.golang.org/api/googleapi" + // "google.golang.org/api/googleapi" ) func resourceComputeProjectMetadata() *schema.Resource { return &schema.Resource{ Create: resourceComputeProjectMetadataCreate, - Read: resourceComputeProjectMetadataRead, + Read: resourceComputeProjectMetadataRead, Update: resourceComputeProjectMetadataUpdate, Delete: resourceComputeProjectMetadataDelete, SchemaVersion: 0, Schema: map[string]*schema.Schema{ - "metadata": &schema.Schema { - Elem: schema.TypeString, - Type: schema.TypeMap, + "metadata": &schema.Schema{ + Elem: schema.TypeString, + Type: schema.TypeMap, Required: true, }, }, @@ -74,24 +74,25 @@ func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface newMDMap := d.Get("metadata").(map[string]interface{}) // Ensure that we aren't overwriting entries that already exist - for _, kv := range(md.Items) { + for _, kv := range md.Items { if _, ok := newMDMap[kv.Key]; ok { return fmt.Errorf("Error, key '%s' already exists in project '%s'", kv.Key, config.Project) } } // Append new metadata to existing metadata - for key, val := range(newMDMap) { - md.Items = append(md.Items, &compute.MetadataItems { + for key, val := range newMDMap { + v := val.(string) + md.Items = append(md.Items, &compute.MetadataItems{ Key: key, - Value: val.(string), + Value: &v, }) } op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do() if err != nil { - return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err); + return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err) } log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink) @@ -126,12 +127,12 @@ func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{} newMD := make(map[string]interface{}) - for _, kv := range(md.Items) { + for _, kv := range md.Items { newMD[kv.Key] = kv.Value } if err = d.Set("metadata", newMD); err != nil { - return fmt.Errorf("Error setting metadata: %s", err); + return fmt.Errorf("Error setting metadata: %s", err) } d.SetId("common_metadata") @@ -160,36 +161,38 @@ func resourceComputeProjectMetadataUpdate(d *schema.ResourceData, meta interface curMDMap := make(map[string]string) // Load metadata on server into map - for _, kv := range(md.Items) { - // If the server state has a key that we had in our old + for _, kv := range md.Items { + // If the server state has a key that we had in our old // state, but not in our new state, we should delete it _, okOld := oMDMap[kv.Key] _, okNew := nMDMap[kv.Key] if okOld && !okNew { continue } else { - curMDMap[kv.Key] = kv.Value + if kv.Value != nil { + curMDMap[kv.Key] = *kv.Value + } } } // Insert new metadata into existing metadata (overwriting when needed) - for key, val := range(nMDMap) { + for key, val := range nMDMap { curMDMap[key] = val.(string) } // Reformat old metadata into a list md.Items = nil - for key, val := range(curMDMap) { - md.Items = append(md.Items, &compute.MetadataItems { + for key, val := range curMDMap { + md.Items = append(md.Items, &compute.MetadataItems{ Key: key, - Value: val, + Value: &val, }) } op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do() if err != nil { - return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err); + return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err) } log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink) diff --git a/builtin/providers/google/resource_compute_project_metadata_test.go b/builtin/providers/google/resource_compute_project_metadata_test.go index adcb25459..264443386 100644 --- a/builtin/providers/google/resource_compute_project_metadata_test.go +++ b/builtin/providers/google/resource_compute_project_metadata_test.go @@ -150,13 +150,13 @@ func testAccCheckComputeProjectMetadataContains(project *compute.Project, key st return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err) } - for _, kv := range(project.CommonInstanceMetadata.Items) { + for _, kv := range project.CommonInstanceMetadata.Items { if kv.Key == key { - if (kv.Value == value) { + if kv.Value != nil && *kv.Value == value { return nil } else { return fmt.Errorf("Error, key value mismatch, wanted (%s, %s), got (%s, %s)", - key, value, kv.Key, kv.Value); + key, value, kv.Key, *kv.Value) } } } @@ -174,7 +174,7 @@ func testAccCheckComputeProjectMetadataSize(project *compute.Project, size int) } if size > len(project.CommonInstanceMetadata.Items) { - return fmt.Errorf("Error, expected at least %d metadata items, got %d", size, + return fmt.Errorf("Error, expected at least %d metadata items, got %d", size, len(project.CommonInstanceMetadata.Items)) }