providers/google: compute_instance supports metadata

This commit is contained in:
Mitchell Hashimoto 2014-08-25 15:25:45 -07:00
parent da03be8941
commit 7f3bb1a45a
2 changed files with 60 additions and 10 deletions

View File

@ -69,6 +69,14 @@ func resourceComputeInstance() *schema.Resource {
},
},
"metadata": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeMap,
},
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
@ -155,6 +163,23 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
networks = append(networks, &iface)
}
// Calculate the metadata
var metadata *compute.Metadata
if v := d.Get("metadata").([]interface{}); len(v) > 0 {
m := new(compute.Metadata)
m.Items = make([]*compute.MetadataItems, 0, len(v))
for _, v := range v {
for k, v := range v.(map[string]interface{}) {
m.Items = append(m.Items, &compute.MetadataItems{
Key: k,
Value: v.(string),
})
}
}
metadata = m
}
// Calculate the tags
var tags *compute.Tags
if v := d.Get("tags"); v != nil {
@ -168,14 +193,10 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
// Create the instance information
instance := compute.Instance{
Description: d.Get("description").(string),
Disks: disks,
MachineType: machineType.SelfLink,
/*
Metadata: &compute.Metadata{
Items: metadata,
},
*/
Description: d.Get("description").(string),
Disks: disks,
MachineType: machineType.SelfLink,
Metadata: metadata,
Name: d.Get("name").(string),
NetworkInterfaces: networks,
Tags: tags,

View File

@ -22,7 +22,8 @@ func TestAccComputeInstance_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceTag(instance, "foo"),
testAccCheckComputeInstanceTag(&instance, "foo"),
testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
),
},
},
@ -76,7 +77,31 @@ func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) res
}
}
func testAccCheckComputeInstanceExists(instance *compute.Instance, n string) resource.TestCheckFunc {
func testAccCheckComputeInstanceMetadata(
instance *compute.Instance,
k string, v string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instance.Metadata == nil {
return fmt.Errorf("no metadata")
}
for _, item := range instance.Metadata.Items {
if k != item.Key {
continue
}
if v == item.Value {
return nil
}
return fmt.Errorf("bad value for %s: %s", k, item.Value)
}
return fmt.Errorf("metadata not found: %s", k)
}
}
func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instance.Tags == nil {
return fmt.Errorf("no tags")
@ -106,4 +131,8 @@ resource "google_compute_instance" "foobar" {
network {
source = "default"
}
metadata {
foo = "bar"
}
}`