Skip to content

Commit 1f9b0d3

Browse files
committed
Add support for using labels on compute_instance
1 parent a25461f commit 1f9b0d3

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

google/resource_compute_instance.go

+51
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,18 @@ func resourceComputeInstance() *schema.Resource {
343343
Computed: true,
344344
},
345345

346+
"labels": &schema.Schema{
347+
Type: schema.TypeMap,
348+
Optional: true,
349+
Elem: &schema.Schema{Type: schema.TypeString},
350+
Set: schema.HashString,
351+
},
352+
353+
"label_fingerprint": &schema.Schema{
354+
Type: schema.TypeString,
355+
Computed: true,
356+
},
357+
346358
"create_timeout": &schema.Schema{
347359
Type: schema.TypeInt,
348360
Optional: true,
@@ -676,6 +688,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
676688
Name: d.Get("name").(string),
677689
NetworkInterfaces: networkInterfaces,
678690
Tags: resourceInstanceTags(d),
691+
Labels: resourceInstanceLabels(d),
679692
ServiceAccounts: serviceAccounts,
680693
Scheduling: scheduling,
681694
}
@@ -845,6 +858,14 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
845858
d.Set("tags_fingerprint", instance.Tags.Fingerprint)
846859
}
847860

861+
if len(instance.Labels) > 0 {
862+
d.Set("labels", instance.Labels)
863+
}
864+
865+
if instance.LabelFingerprint != "" {
866+
d.Set("label_fingerprint", instance.LabelFingerprint)
867+
}
868+
848869
disksCount := d.Get("disk.#").(int)
849870
attachedDisksCount := d.Get("attached_disk.#").(int)
850871
disks := make([]map[string]interface{}, 0, disksCount)
@@ -977,6 +998,24 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
977998
d.SetPartial("tags")
978999
}
9791000

1001+
if d.HasChange("labels") {
1002+
labels := resourceInstanceLabels(d)
1003+
labelFingerprint := d.Get("label_fingerprint").(string)
1004+
req := compute.InstancesSetLabelsRequest{Labels: labels, LabelFingerprint: labelFingerprint}
1005+
1006+
op, err := config.clientCompute.Instances.SetLabels(project, zone, d.Id(), &req).Do()
1007+
if err != nil {
1008+
return fmt.Errorf("Error updating labels: %s", err)
1009+
}
1010+
1011+
opErr := computeOperationWaitZone(config, op, project, zone, "labels to update")
1012+
if opErr != nil {
1013+
return opErr
1014+
}
1015+
1016+
d.SetPartial("labels")
1017+
}
1018+
9801019
if d.HasChange("scheduling") {
9811020
prefix := "scheduling.0"
9821021
scheduling := &compute.Scheduling{}
@@ -1127,6 +1166,18 @@ func resourceInstanceMetadata(d *schema.ResourceData) (*compute.Metadata, error)
11271166
return m, nil
11281167
}
11291168

1169+
func resourceInstanceLabels(d *schema.ResourceData) map[string]string {
1170+
labels := map[string]string{}
1171+
if v, ok := d.GetOk("labels"); ok {
1172+
labelMap := v.(map[string]interface{})
1173+
for k, v := range labelMap {
1174+
vstr := v.(string)
1175+
labels[k] = vstr
1176+
}
1177+
}
1178+
return labels
1179+
}
1180+
11301181
func resourceInstanceTags(d *schema.ResourceData) *compute.Tags {
11311182
// Calculate the tags
11321183
var tags *compute.Tags

google/resource_compute_instance_test.go

+38-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestAccComputeInstance_basic1(t *testing.T) {
5151
testAccCheckComputeInstanceExists(
5252
"google_compute_instance.foobar", &instance),
5353
testAccCheckComputeInstanceTag(&instance, "foo"),
54+
testAccCheckComputeInstanceLabel(&instance, "my_key", "my_value"),
5455
testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
5556
testAccCheckComputeInstanceMetadata(&instance, "baz", "qux"),
5657
testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
@@ -385,6 +386,7 @@ func TestAccComputeInstance_update(t *testing.T) {
385386
"google_compute_instance.foobar", &instance),
386387
testAccCheckComputeInstanceMetadata(
387388
&instance, "bar", "baz"),
389+
testAccCheckComputeInstanceLabel(&instance, "only_me", "nothing_else"),
388390
testAccCheckComputeInstanceTag(&instance, "baz"),
389391
testAccCheckComputeInstanceAccessConfig(&instance),
390392
),
@@ -795,6 +797,24 @@ func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resour
795797
}
796798
}
797799

800+
func testAccCheckComputeInstanceLabel(instance *compute.Instance, key string, value string) resource.TestCheckFunc {
801+
return func(s *terraform.State) error {
802+
if instance.Labels == nil {
803+
return fmt.Errorf("no labels found on instance %s", instance.Name)
804+
}
805+
806+
v, ok := instance.Labels[key]
807+
if !ok {
808+
return fmt.Errorf("No label found with key %s on instance %s", key, instance.Name)
809+
}
810+
if v != value {
811+
return fmt.Errorf("Expected value '%s' but found value '%s' for label '%s' on instance %s", value, v, key, instance.Name)
812+
}
813+
814+
return nil
815+
}
816+
}
817+
798818
func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope string) resource.TestCheckFunc {
799819
return func(s *terraform.State) error {
800820
if count := len(instance.ServiceAccounts); count != 1 {
@@ -919,6 +939,11 @@ resource "google_compute_instance" "foobar" {
919939
create_timeout = 5
920940
921941
metadata_startup_script = "echo Hello"
942+
943+
labels {
944+
my_key = "my_value"
945+
my_other_key = "my_other_value"
946+
}
922947
}
923948
`, instance)
924949
}
@@ -1050,10 +1075,11 @@ resource "google_compute_instance" "foobar" {
10501075
func testAccComputeInstance_update(instance string) string {
10511076
return fmt.Sprintf(`
10521077
resource "google_compute_instance" "foobar" {
1053-
name = "%s"
1054-
machine_type = "n1-standard-1"
1055-
zone = "us-central1-a"
1056-
tags = ["baz"]
1078+
name = "%s"
1079+
machine_type = "n1-standard-1"
1080+
zone = "us-central1-a"
1081+
can_ip_forward = false
1082+
tags = ["baz"]
10571083
10581084
disk {
10591085
image = "debian-8-jessie-v20160803"
@@ -1067,6 +1093,14 @@ resource "google_compute_instance" "foobar" {
10671093
metadata {
10681094
bar = "baz"
10691095
}
1096+
1097+
create_timeout = 5
1098+
1099+
metadata_startup_script = "echo Hello"
1100+
1101+
labels {
1102+
only_me = "nothing_else"
1103+
}
10701104
}
10711105
`, instance)
10721106
}

website/docs/r/compute_instance.html.markdown

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ resource "google_compute_instance" "default" {
4646
foo = "bar"
4747
}
4848
49+
4950
metadata_startup_script = "echo hi > /test.txt"
5051
5152
service_account {
@@ -102,6 +103,8 @@ The following arguments are supported:
102103

103104
* `tags` - (Optional) A list of tags to attach to the instance.
104105

106+
* `labels` - (Optional) A set of key/value label pairs to assign to the instance.
107+
105108
* `create_timeout` - (Optional) Configurable timeout in minutes for creating instances. Default is 4 minutes.
106109
Changing this forces a new resource to be created.
107110

@@ -208,6 +211,8 @@ exported:
208211

209212
* `tags_fingerprint` - The unique fingerprint of the tags.
210213

214+
* `label_fingerprint` - The unique fingerprint of the labels.
215+
211216
* `network_interface.0.address` - The internal ip address of the instance, either manually or dynamically assigned.
212217

213218
* `network_interface.0.access_config.0.assigned_nat_ip` - If the instance has an access config, either the given external ip (in the `nat_ip` field) or the ephemeral (generated) ip (if you didn't provide one).

0 commit comments

Comments
 (0)