Skip to content

Commit 8941c5e

Browse files
committed
Implement 'licenses' field in compute_image resource
Signed-off-by: Matt Oswalt <[email protected]>
1 parent cf44511 commit 8941c5e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

google/resource_compute_image.go

+22
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ func resourceComputeImage() *schema.Resource {
106106
Set: schema.HashString,
107107
},
108108

109+
"licenses": &schema.Schema{
110+
Type: schema.TypeList,
111+
Optional: true,
112+
ForceNew: true,
113+
Elem: &schema.Schema{Type: schema.TypeString},
114+
},
115+
109116
"label_fingerprint": &schema.Schema{
110117
Type: schema.TypeString,
111118
Computed: true,
@@ -158,6 +165,11 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
158165
image.Labels = expandLabels(d)
159166
}
160167

168+
// Load up the licenses for this image if specified
169+
if _, ok := d.GetOk("licenses"); ok {
170+
image.Licenses = licenses(d)
171+
}
172+
161173
// Read create timeout
162174
var createTimeout int
163175
if v, ok := d.GetOk("create_timeout"); ok {
@@ -213,6 +225,7 @@ func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error {
213225
d.Set("family", image.Family)
214226
d.Set("self_link", image.SelfLink)
215227
d.Set("labels", image.Labels)
228+
d.Set("licenses", image.Licenses)
216229
d.Set("label_fingerprint", image.LabelFingerprint)
217230
d.Set("project", project)
218231

@@ -287,3 +300,12 @@ func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error
287300
d.SetId("")
288301
return nil
289302
}
303+
304+
func licenses(d *schema.ResourceData) []string {
305+
licensesCount := d.Get("licenses.#").(int)
306+
data := make([]string, licensesCount)
307+
for i := 0; i < licensesCount; i++ {
308+
data[i] = d.Get(fmt.Sprintf("licenses.%d", i)).(string)
309+
}
310+
return data
311+
}

google/resource_compute_image_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func TestAccComputeImage_basic(t *testing.T) {
2929
testAccCheckComputeImageFamily(&image, "family-test"),
3030
testAccCheckComputeImageContainsLabel(&image, "my-label", "my-label-value"),
3131
testAccCheckComputeImageContainsLabel(&image, "empty-label", ""),
32+
testAccCheckComputeImageContainsLicense(&image, "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"),
3233
testAccCheckComputeImageHasComputedFingerprint(&image, "google_compute_image.foobar"),
3334
),
3435
},
@@ -184,6 +185,19 @@ func testAccCheckComputeImageContainsLabel(image *compute.Image, key string, val
184185
}
185186
}
186187

188+
func testAccCheckComputeImageContainsLicense(image *compute.Image, expectedLicense string) resource.TestCheckFunc {
189+
return func(s *terraform.State) error {
190+
191+
for _, thisLicense := range image.Licenses {
192+
if thisLicense == expectedLicense {
193+
return nil
194+
}
195+
}
196+
197+
return fmt.Errorf("Expected license '%s' was not found", expectedLicense)
198+
}
199+
}
200+
187201
func testAccCheckComputeImageDoesNotContainLabel(image *compute.Image, key string) resource.TestCheckFunc {
188202
return func(s *terraform.State) error {
189203
if v, ok := image.Labels[key]; ok {
@@ -239,6 +253,9 @@ resource "google_compute_image" "foobar" {
239253
my-label = "my-label-value"
240254
empty-label = ""
241255
}
256+
licenses = [
257+
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
258+
]
242259
}`, name)
243260
}
244261

@@ -256,6 +273,9 @@ resource "google_compute_image" "foobar" {
256273
empty-label = "oh-look-theres-a-label-now"
257274
new-field = "only-shows-up-when-updated"
258275
}
276+
licenses = [
277+
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
278+
]
259279
}`, name)
260280
}
261281

website/docs/r/compute_image.html.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ resource "google_compute_image" "bootable-image" {
2222
raw_disk {
2323
source = "https://storage.googleapis.com/my-bucket/my-disk-image-tarball.tar.gz"
2424
}
25+
26+
licenses = [
27+
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
28+
]
2529
}
2630
2731
resource "google_compute_instance" "vm" {
@@ -67,6 +71,9 @@ The following arguments are supported: (Note that one of either source_disk or
6771
Changing this forces a new resource to be created. Structure is documented
6872
below.
6973

74+
* `licenses` - (Optional) A list of license URIs to apply to this image. Changing this
75+
forces a new resource to be created.
76+
7077
* `create_timeout` - (Deprecated) Configurable timeout in minutes for creating images. Default is 4 minutes.
7178

7279
The `raw_disk` block supports:

0 commit comments

Comments
 (0)