From cad555a5e988b3958ab8285fefc271145d6e1fa3 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 16 May 2023 10:35:44 -0700 Subject: [PATCH] Image storage location (#7943) (#14619) * update code to follow latest structure * storagelocation api adding default_from_api * updated the location api to use US * fix resource name * fix resource name and image name * updated the storage location option --------- Signed-off-by: Modular Magician Co-authored-by: Shubham Singh --- .changelog/7943.txt | 3 ++ google/resource_compute_image.go | 29 ++++++++++++++ .../resource_compute_image_generated_test.go | 38 +++++++++++++++++++ website/docs/r/compute_image.html.markdown | 24 ++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 .changelog/7943.txt diff --git a/.changelog/7943.txt b/.changelog/7943.txt new file mode 100644 index 00000000000..b2d8c2e74ec --- /dev/null +++ b/.changelog/7943.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +* Added support for storageLocation in Image resource +``` diff --git a/google/resource_compute_image.go b/google/resource_compute_image.go index ddd9450557f..e124bf7627d 100644 --- a/google/resource_compute_image.go +++ b/google/resource_compute_image.go @@ -214,6 +214,18 @@ In order to create an image, you must provide the full or partial URL of one of * The rawDisk.source URL * The sourceDisk URL`, }, + "storage_locations": { + Type: schema.TypeList, + Computed: true, + Optional: true, + ForceNew: true, + Description: `Cloud Storage bucket storage location of the image +(regional or multi-regional). +Reference link: https://cloud.google.com/compute/docs/reference/rest/v1/images`, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "archive_size_bytes": { Type: schema.TypeInt, Computed: true, @@ -274,6 +286,12 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error } else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { obj["description"] = descriptionProp } + storageLocationsProp, err := expandComputeImageStorageLocations(d.Get("storage_locations"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("storage_locations"); !tpgresource.IsEmptyValue(reflect.ValueOf(storageLocationsProp)) && (ok || !reflect.DeepEqual(v, storageLocationsProp)) { + obj["storageLocations"] = storageLocationsProp + } diskSizeGbProp, err := expandComputeImageDiskSizeGb(d.Get("disk_size_gb"), d, config) if err != nil { return err @@ -436,6 +454,9 @@ func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("description", flattenComputeImageDescription(res["description"], d, config)); err != nil { return fmt.Errorf("Error reading Image: %s", err) } + if err := d.Set("storage_locations", flattenComputeImageStorageLocations(res["storageLocations"], d, config)); err != nil { + return fmt.Errorf("Error reading Image: %s", err) + } if err := d.Set("disk_size_gb", flattenComputeImageDiskSizeGb(res["diskSizeGb"], d, config)); err != nil { return fmt.Errorf("Error reading Image: %s", err) } @@ -629,6 +650,10 @@ func flattenComputeImageDescription(v interface{}, d *schema.ResourceData, confi return v } +func flattenComputeImageStorageLocations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + func flattenComputeImageDiskSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { // Handles the string fixed64 format if strVal, ok := v.(string); ok { @@ -743,6 +768,10 @@ func expandComputeImageDescription(v interface{}, d tpgresource.TerraformResourc return v, nil } +func expandComputeImageStorageLocations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + func expandComputeImageDiskSizeGb(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { return v, nil } diff --git a/google/resource_compute_image_generated_test.go b/google/resource_compute_image_generated_test.go index 052bc215d21..7f1997c5369 100644 --- a/google/resource_compute_image_generated_test.go +++ b/google/resource_compute_image_generated_test.go @@ -109,6 +109,44 @@ resource "google_compute_image" "example" { `, context) } +func TestAccComputeImage_imageBasicStorageLocationExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": RandString(t, 10), + } + + VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckComputeImageDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeImage_imageBasicStorageLocationExample(context), + }, + { + ResourceName: "google_compute_image.example", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"raw_disk", "source_disk", "source_image", "source_snapshot"}, + }, + }, + }) +} + +func testAccComputeImage_imageBasicStorageLocationExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_image" "example" { + name = "tf-test-example-sl-image%{random_suffix}" + + raw_disk { + source = "https://storage.googleapis.com/bosh-gce-raw-stemcells/bosh-stemcell-97.98-google-kvm-ubuntu-xenial-go_agent-raw-1557960142.tar.gz" + } + storage_locations = ["us-central1"] +} +`, context) +} + func testAccCheckComputeImageDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { for name, rs := range s.RootModule().Resources { diff --git a/website/docs/r/compute_image.html.markdown b/website/docs/r/compute_image.html.markdown index 6426415ce38..43a4efe3e73 100644 --- a/website/docs/r/compute_image.html.markdown +++ b/website/docs/r/compute_image.html.markdown @@ -85,6 +85,24 @@ resource "google_compute_image" "example" { } } ``` + +## Example Usage - Image Basic Storage Location + + +```hcl +resource "google_compute_image" "example" { + name = "example-sl-image" + + raw_disk { + source = "https://storage.googleapis.com/bosh-gce-raw-stemcells/bosh-stemcell-97.98-google-kvm-ubuntu-xenial-go_agent-raw-1557960142.tar.gz" + } + storage_locations = ["us-central1"] +} +``` ## Argument Reference @@ -110,6 +128,12 @@ The following arguments are supported: An optional description of this resource. Provide this property when you create the resource. +* `storage_locations` - + (Optional) + Cloud Storage bucket storage location of the image + (regional or multi-regional). + Reference link: https://cloud.google.com/compute/docs/reference/rest/v1/images + * `disk_size_gb` - (Optional) Size of the image when restored onto a persistent disk (in GB).