Merge pull request #1717 from Mierdin/master

Implement 'licenses' field in compute_image resource
This commit is contained in:
Paddy 2018-07-11 21:15:35 -07:00 committed by GitHub
commit 3d2e72208e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 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

@ -36,6 +36,33 @@ func TestAccComputeImage_basic(t *testing.T) {
})
}
func TestAccComputeImage_withLicense(t *testing.T) {
t.Parallel()
var image compute.Image
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeImageDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeImage_license("image-test-" + acctest.RandString(10)),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeImageExists(
"google_compute_image.foobar", &image),
testAccCheckComputeImageDescription(&image, "description-test"),
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"),
),
},
},
})
}
func TestAccComputeImage_update(t *testing.T) {
t.Parallel()
@ -184,6 +211,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 {
@ -242,6 +282,26 @@ resource "google_compute_image" "foobar" {
}`, name)
}
func testAccComputeImage_license(name string) string {
return fmt.Sprintf(`
resource "google_compute_image" "foobar" {
name = "%s"
description = "description-test"
family = "family-test"
raw_disk {
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
}
create_timeout = 5
labels = {
my-label = "my-label-value"
empty-label = ""
}
licenses = [
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
]
}`, name)
}
func testAccComputeImage_update(name string) string {
return fmt.Sprintf(`
resource "google_compute_image" "foobar" {

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: