Skip to content

Commit 7027a83

Browse files
authored
Add labels to google_compute_instance_template (hashicorp#515)
1 parent 077c18f commit 7027a83

3 files changed

+39
-0
lines changed

google/resource_compute_instance_template.go

+14
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ func resourceComputeInstanceTemplate() *schema.Resource {
324324
Type: schema.TypeString,
325325
Computed: true,
326326
},
327+
328+
"labels": &schema.Schema{
329+
Type: schema.TypeMap,
330+
Optional: true,
331+
ForceNew: true,
332+
Elem: &schema.Schema{Type: schema.TypeString},
333+
Set: schema.HashString,
334+
},
327335
},
328336
}
329337
}
@@ -576,6 +584,9 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
576584
instanceProperties.ServiceAccounts = serviceAccounts
577585

578586
instanceProperties.Tags = resourceInstanceTags(d)
587+
if _, ok := d.GetOk("labels"); ok {
588+
instanceProperties.Labels = expandLabels(d)
589+
}
579590

580591
var itName string
581592
if v, ok := d.GetOk("name"); ok {
@@ -743,6 +754,9 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
743754
return fmt.Errorf("Error setting tags_fingerprint: %s", err)
744755
}
745756
}
757+
if instanceTemplate.Properties.Labels != nil {
758+
d.Set("labels", instanceTemplate.Properties.Labels)
759+
}
746760
if err = d.Set("self_link", instanceTemplate.SelfLink); err != nil {
747761
return fmt.Errorf("Error setting self_link: %s", err)
748762
}

google/resource_compute_instance_template_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestAccComputeInstanceTemplate_basic(t *testing.T) {
2828
testAccCheckComputeInstanceTemplateTag(&instanceTemplate, "foo"),
2929
testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"),
3030
testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "projects/debian-cloud/global/images/debian-8-jessie-v20160803", true, true),
31+
testAccCheckComputeInstanceTemplateContainsLabel(&instanceTemplate, "my_label", "foobar"),
3132
),
3233
},
3334
},
@@ -412,6 +413,19 @@ func testAccCheckComputeInstanceTemplateNetworkIP(n, networkIP string, instanceT
412413
}
413414
}
414415

416+
func testAccCheckComputeInstanceTemplateContainsLabel(instanceTemplate *compute.InstanceTemplate, key string, value string) resource.TestCheckFunc {
417+
return func(s *terraform.State) error {
418+
v, ok := instanceTemplate.Properties.Labels[key]
419+
if !ok {
420+
return fmt.Errorf("Expected label with key '%s' not found", key)
421+
}
422+
if v != value {
423+
return fmt.Errorf("Incorrect label value for key '%s': expected '%s' but found '%s'", key, value, v)
424+
}
425+
return nil
426+
}
427+
}
428+
415429
var testAccComputeInstanceTemplate_basic = fmt.Sprintf(`
416430
resource "google_compute_instance_template" "foobar" {
417431
name = "instancet-test-%s"
@@ -441,6 +455,10 @@ resource "google_compute_instance_template" "foobar" {
441455
service_account {
442456
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
443457
}
458+
459+
labels {
460+
my_label = "foobar"
461+
}
444462
}`, acctest.RandString(10))
445463

446464
var testAccComputeInstanceTemplate_preemptible = fmt.Sprintf(`

website/docs/r/compute_instance_template.html.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ resource "google_compute_instance_template" "default" {
2424
2525
tags = ["foo", "bar"]
2626
27+
labels = {
28+
environment = "dev"
29+
}
30+
2731
instance_description = "description assigned to instances"
2832
machine_type = "n1-standard-1"
2933
can_ip_forward = false
@@ -133,6 +137,9 @@ The following arguments are supported:
133137
* `instance_description` - (Optional) A brief description to use for instances
134138
created from this template.
135139

140+
* `labels` - (Optional) A set of key/value label pairs to assign to instances
141+
created from this template,
142+
136143
* `metadata` - (Optional) Metadata key/value pairs to make available from
137144
within instances created from this template.
138145

0 commit comments

Comments
 (0)