Implement 'licenses' field in compute_image resource

Signed-off-by: Matt Oswalt <matt@keepingitclassless.net>
This commit is contained in:
Matt Oswalt 2018-06-28 09:49:34 -07:00
parent cf4451177e
commit 8941c5ef6e
No known key found for this signature in database
GPG Key ID: 1A5F742C896088D5
3 changed files with 49 additions and 0 deletions

View File

@ -106,6 +106,13 @@ func resourceComputeImage() *schema.Resource {
Set: schema.HashString,
},
"licenses": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"label_fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
@ -158,6 +165,11 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
image.Labels = expandLabels(d)
}
// Load up the licenses for this image if specified
if _, ok := d.GetOk("licenses"); ok {
image.Licenses = licenses(d)
}
// Read create timeout
var createTimeout int
if v, ok := d.GetOk("create_timeout"); ok {
@ -213,6 +225,7 @@ func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error {
d.Set("family", image.Family)
d.Set("self_link", image.SelfLink)
d.Set("labels", image.Labels)
d.Set("licenses", image.Licenses)
d.Set("label_fingerprint", image.LabelFingerprint)
d.Set("project", project)
@ -287,3 +300,12 @@ func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error
d.SetId("")
return nil
}
func licenses(d *schema.ResourceData) []string {
licensesCount := d.Get("licenses.#").(int)
data := make([]string, licensesCount)
for i := 0; i < licensesCount; i++ {
data[i] = d.Get(fmt.Sprintf("licenses.%d", i)).(string)
}
return data
}

View File

@ -29,6 +29,7 @@ func TestAccComputeImage_basic(t *testing.T) {
testAccCheckComputeImageFamily(&image, "family-test"),
testAccCheckComputeImageContainsLabel(&image, "my-label", "my-label-value"),
testAccCheckComputeImageContainsLabel(&image, "empty-label", ""),
testAccCheckComputeImageContainsLicense(&image, "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"),
testAccCheckComputeImageHasComputedFingerprint(&image, "google_compute_image.foobar"),
),
},
@ -184,6 +185,19 @@ func testAccCheckComputeImageContainsLabel(image *compute.Image, key string, val
}
}
func testAccCheckComputeImageContainsLicense(image *compute.Image, expectedLicense string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, thisLicense := range image.Licenses {
if thisLicense == expectedLicense {
return nil
}
}
return fmt.Errorf("Expected license '%s' was not found", expectedLicense)
}
}
func testAccCheckComputeImageDoesNotContainLabel(image *compute.Image, key string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if v, ok := image.Labels[key]; ok {
@ -239,6 +253,9 @@ resource "google_compute_image" "foobar" {
my-label = "my-label-value"
empty-label = ""
}
licenses = [
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
]
}`, name)
}
@ -256,6 +273,9 @@ resource "google_compute_image" "foobar" {
empty-label = "oh-look-theres-a-label-now"
new-field = "only-shows-up-when-updated"
}
licenses = [
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
]
}`, name)
}

View File

@ -22,6 +22,10 @@ resource "google_compute_image" "bootable-image" {
raw_disk {
source = "https://storage.googleapis.com/my-bucket/my-disk-image-tarball.tar.gz"
}
licenses = [
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
]
}
resource "google_compute_instance" "vm" {
@ -67,6 +71,9 @@ The following arguments are supported: (Note that one of either source_disk or
Changing this forces a new resource to be created. Structure is documented
below.
* `licenses` - (Optional) A list of license URIs to apply to this image. Changing this
forces a new resource to be created.
* `create_timeout` - (Deprecated) Configurable timeout in minutes for creating images. Default is 4 minutes.
The `raw_disk` block supports: