Skip to content

Commit

Permalink
Image storage location (#7943) (#14619)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Shubham Singh <[email protected]>
  • Loading branch information
modular-magician and shubhamkr619 authored May 16, 2023
1 parent 7dd0a58 commit cad555a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/7943.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
* Added support for storageLocation in Image resource
```
29 changes: 29 additions & 0 deletions google/resource_compute_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
38 changes: 38 additions & 0 deletions google/resource_compute_image_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions website/docs/r/compute_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ resource "google_compute_image" "example" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=image_basic_storage_location&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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

Expand All @@ -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).
Expand Down

0 comments on commit cad555a

Please sign in to comment.