mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-03 01:01:06 +00:00
providers/google: compute_instance supports metadata
This commit is contained in:
parent
d42c75315c
commit
0f0c34b674
@ -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{
|
"tags": &schema.Schema{
|
||||||
Type: schema.TypeSet,
|
Type: schema.TypeSet,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
@ -155,6 +163,23 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
|||||||
networks = append(networks, &iface)
|
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
|
// Calculate the tags
|
||||||
var tags *compute.Tags
|
var tags *compute.Tags
|
||||||
if v := d.Get("tags"); v != nil {
|
if v := d.Get("tags"); v != nil {
|
||||||
@ -171,11 +196,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
|||||||
Description: d.Get("description").(string),
|
Description: d.Get("description").(string),
|
||||||
Disks: disks,
|
Disks: disks,
|
||||||
MachineType: machineType.SelfLink,
|
MachineType: machineType.SelfLink,
|
||||||
/*
|
Metadata: metadata,
|
||||||
Metadata: &compute.Metadata{
|
|
||||||
Items: metadata,
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
Name: d.Get("name").(string),
|
Name: d.Get("name").(string),
|
||||||
NetworkInterfaces: networks,
|
NetworkInterfaces: networks,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
|
@ -22,7 +22,8 @@ func TestAccComputeInstance_basic(t *testing.T) {
|
|||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckComputeInstanceExists(
|
testAccCheckComputeInstanceExists(
|
||||||
"google_compute_instance.foobar", &instance),
|
"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 {
|
return func(s *terraform.State) error {
|
||||||
if instance.Tags == nil {
|
if instance.Tags == nil {
|
||||||
return fmt.Errorf("no tags")
|
return fmt.Errorf("no tags")
|
||||||
@ -106,4 +131,8 @@ resource "google_compute_instance" "foobar" {
|
|||||||
network {
|
network {
|
||||||
source = "default"
|
source = "default"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metadata {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
||||||
}`
|
}`
|
||||||
|
Loading…
Reference in New Issue
Block a user