providers/google: support tags on compute_instance

This commit is contained in:
Mitchell Hashimoto 2014-08-25 15:10:30 -07:00
parent 5bf258809f
commit da03be8941
2 changed files with 40 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"code.google.com/p/google-api-go-client/compute/v1" "code.google.com/p/google-api-go-client/compute/v1"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
@ -67,6 +68,15 @@ func resourceComputeInstance() *schema.Resource {
}, },
}, },
}, },
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
}, },
} }
} }
@ -145,6 +155,17 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
networks = append(networks, &iface) networks = append(networks, &iface)
} }
// Calculate the tags
var tags *compute.Tags
if v := d.Get("tags"); v != nil {
vs := v.(*schema.Set).List()
tags = new(compute.Tags)
tags.Items = make([]string, len(vs))
for i, v := range v.(*schema.Set).List() {
tags.Items[i] = v.(string)
}
}
// Create the instance information // Create the instance information
instance := compute.Instance{ instance := compute.Instance{
Description: d.Get("description").(string), Description: d.Get("description").(string),
@ -157,6 +178,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
*/ */
Name: d.Get("name").(string), Name: d.Get("name").(string),
NetworkInterfaces: networks, NetworkInterfaces: networks,
Tags: tags,
/* /*
ServiceAccounts: []*compute.ServiceAccount{ ServiceAccounts: []*compute.ServiceAccount{
&compute.ServiceAccount{ &compute.ServiceAccount{
@ -168,9 +190,6 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
}, },
}, },
}, },
Tags: &compute.Tags{
Items: c.Tags,
},
*/ */
} }

View File

@ -22,6 +22,7 @@ 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"),
), ),
}, },
}, },
@ -75,11 +76,28 @@ func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) res
} }
} }
func testAccCheckComputeInstanceExists(instance *compute.Instance, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instance.Tags == nil {
return fmt.Errorf("no tags")
}
for _, k := range instance.Tags.Items {
if k == n {
return nil
}
}
return fmt.Errorf("tag not found: %s", n)
}
}
const testAccComputeInstance_basic = ` const testAccComputeInstance_basic = `
resource "google_compute_instance" "foobar" { resource "google_compute_instance" "foobar" {
name = "terraform-test" name = "terraform-test"
machine_type = "n1-standard-1" machine_type = "n1-standard-1"
zone = "us-central1-a" zone = "us-central1-a"
tags = ["foo", "bar"]
disk { disk {
source = "debian-7-wheezy-v20140814" source = "debian-7-wheezy-v20140814"