provider/google: Misc. cleanups for tests to pass
This commit is contained in:
parent
75a78dfcd0
commit
5d7e156982
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue