Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'licenses' field in compute_image resource #1717

Merged
merged 2 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions google/resource_compute_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func resourceComputeImage() *schema.Resource {
Set: schema.HashString,
},

"licenses": &schema.Schema{
Type: schema.TypeList,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does order matter for licenses? Is it reasonable to expect someone to try and reference each individual license using interpolations?

Copy link
Contributor Author

@Mierdin Mierdin Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither the CLI or API docs seem to give an indication that order matters. Does your previous comment about using a Set instead of a List impact the order? I haven't delved into the details there yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is nice. So Google isn't expecting the order to be meaningful, but I'd say it might still be intuitive to keep this as a TypeList in case the user wants to be able to refer to it using interpolations. (I'm assuming they won't be able to do this if it's an unordered TypeSet). So not necessarily for compatibility reasons, but for UX reasons, might be useful to keep it as-is. Let me know if you disagree, I'm not married to it.

Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"label_fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
60 changes: 60 additions & 0 deletions google/resource_compute_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/compute_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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:
Expand Down