Skip to content

Commit 3d2e722

Browse files
authored
Merge pull request #1717 from Mierdin/master
Implement 'licenses' field in compute_image resource
2 parents 5be332e + d7c1bf9 commit 3d2e722

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-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

+60
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ func TestAccComputeImage_basic(t *testing.T) {
3636
})
3737
}
3838

39+
func TestAccComputeImage_withLicense(t *testing.T) {
40+
t.Parallel()
41+
42+
var image compute.Image
43+
44+
resource.Test(t, resource.TestCase{
45+
PreCheck: func() { testAccPreCheck(t) },
46+
Providers: testAccProviders,
47+
CheckDestroy: testAccCheckComputeImageDestroy,
48+
Steps: []resource.TestStep{
49+
resource.TestStep{
50+
Config: testAccComputeImage_license("image-test-" + acctest.RandString(10)),
51+
Check: resource.ComposeTestCheckFunc(
52+
testAccCheckComputeImageExists(
53+
"google_compute_image.foobar", &image),
54+
testAccCheckComputeImageDescription(&image, "description-test"),
55+
testAccCheckComputeImageFamily(&image, "family-test"),
56+
testAccCheckComputeImageContainsLabel(&image, "my-label", "my-label-value"),
57+
testAccCheckComputeImageContainsLabel(&image, "empty-label", ""),
58+
testAccCheckComputeImageContainsLicense(&image, "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"),
59+
testAccCheckComputeImageHasComputedFingerprint(&image, "google_compute_image.foobar"),
60+
),
61+
},
62+
},
63+
})
64+
}
65+
3966
func TestAccComputeImage_update(t *testing.T) {
4067
t.Parallel()
4168

@@ -184,6 +211,19 @@ func testAccCheckComputeImageContainsLabel(image *compute.Image, key string, val
184211
}
185212
}
186213

214+
func testAccCheckComputeImageContainsLicense(image *compute.Image, expectedLicense string) resource.TestCheckFunc {
215+
return func(s *terraform.State) error {
216+
217+
for _, thisLicense := range image.Licenses {
218+
if thisLicense == expectedLicense {
219+
return nil
220+
}
221+
}
222+
223+
return fmt.Errorf("Expected license '%s' was not found", expectedLicense)
224+
}
225+
}
226+
187227
func testAccCheckComputeImageDoesNotContainLabel(image *compute.Image, key string) resource.TestCheckFunc {
188228
return func(s *terraform.State) error {
189229
if v, ok := image.Labels[key]; ok {
@@ -242,6 +282,26 @@ resource "google_compute_image" "foobar" {
242282
}`, name)
243283
}
244284

285+
func testAccComputeImage_license(name string) string {
286+
return fmt.Sprintf(`
287+
resource "google_compute_image" "foobar" {
288+
name = "%s"
289+
description = "description-test"
290+
family = "family-test"
291+
raw_disk {
292+
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
293+
}
294+
create_timeout = 5
295+
labels = {
296+
my-label = "my-label-value"
297+
empty-label = ""
298+
}
299+
licenses = [
300+
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
301+
]
302+
}`, name)
303+
}
304+
245305
func testAccComputeImage_update(name string) string {
246306
return fmt.Sprintf(`
247307
resource "google_compute_image" "foobar" {

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)