Skip to content

Commit d030d4d

Browse files
Image storage location (#7943) (#5644)
* 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]>
1 parent d8a19d4 commit d030d4d

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

.changelog/7943.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
* Added support for storageLocation in Image resource
3+
```

google-beta/resource_compute_image.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ In order to create an image, you must provide the full or partial URL of one of
214214
* The rawDisk.source URL
215215
* The sourceDisk URL`,
216216
},
217+
"storage_locations": {
218+
Type: schema.TypeList,
219+
Computed: true,
220+
Optional: true,
221+
ForceNew: true,
222+
Description: `Cloud Storage bucket storage location of the image
223+
(regional or multi-regional).
224+
Reference link: https://cloud.google.com/compute/docs/reference/rest/v1/images`,
225+
Elem: &schema.Schema{
226+
Type: schema.TypeString,
227+
},
228+
},
217229
"archive_size_bytes": {
218230
Type: schema.TypeInt,
219231
Computed: true,
@@ -274,6 +286,12 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
274286
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
275287
obj["description"] = descriptionProp
276288
}
289+
storageLocationsProp, err := expandComputeImageStorageLocations(d.Get("storage_locations"), d, config)
290+
if err != nil {
291+
return err
292+
} else if v, ok := d.GetOkExists("storage_locations"); !tpgresource.IsEmptyValue(reflect.ValueOf(storageLocationsProp)) && (ok || !reflect.DeepEqual(v, storageLocationsProp)) {
293+
obj["storageLocations"] = storageLocationsProp
294+
}
277295
diskSizeGbProp, err := expandComputeImageDiskSizeGb(d.Get("disk_size_gb"), d, config)
278296
if err != nil {
279297
return err
@@ -436,6 +454,9 @@ func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error {
436454
if err := d.Set("description", flattenComputeImageDescription(res["description"], d, config)); err != nil {
437455
return fmt.Errorf("Error reading Image: %s", err)
438456
}
457+
if err := d.Set("storage_locations", flattenComputeImageStorageLocations(res["storageLocations"], d, config)); err != nil {
458+
return fmt.Errorf("Error reading Image: %s", err)
459+
}
439460
if err := d.Set("disk_size_gb", flattenComputeImageDiskSizeGb(res["diskSizeGb"], d, config)); err != nil {
440461
return fmt.Errorf("Error reading Image: %s", err)
441462
}
@@ -629,6 +650,10 @@ func flattenComputeImageDescription(v interface{}, d *schema.ResourceData, confi
629650
return v
630651
}
631652

653+
func flattenComputeImageStorageLocations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
654+
return v
655+
}
656+
632657
func flattenComputeImageDiskSizeGb(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
633658
// Handles the string fixed64 format
634659
if strVal, ok := v.(string); ok {
@@ -743,6 +768,10 @@ func expandComputeImageDescription(v interface{}, d tpgresource.TerraformResourc
743768
return v, nil
744769
}
745770

771+
func expandComputeImageStorageLocations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
772+
return v, nil
773+
}
774+
746775
func expandComputeImageDiskSizeGb(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
747776
return v, nil
748777
}

google-beta/resource_compute_image_generated_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,44 @@ resource "google_compute_image" "example" {
109109
`, context)
110110
}
111111

112+
func TestAccComputeImage_imageBasicStorageLocationExample(t *testing.T) {
113+
t.Parallel()
114+
115+
context := map[string]interface{}{
116+
"random_suffix": RandString(t, 10),
117+
}
118+
119+
VcrTest(t, resource.TestCase{
120+
PreCheck: func() { acctest.AccTestPreCheck(t) },
121+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
122+
CheckDestroy: testAccCheckComputeImageDestroyProducer(t),
123+
Steps: []resource.TestStep{
124+
{
125+
Config: testAccComputeImage_imageBasicStorageLocationExample(context),
126+
},
127+
{
128+
ResourceName: "google_compute_image.example",
129+
ImportState: true,
130+
ImportStateVerify: true,
131+
ImportStateVerifyIgnore: []string{"raw_disk", "source_disk", "source_image", "source_snapshot"},
132+
},
133+
},
134+
})
135+
}
136+
137+
func testAccComputeImage_imageBasicStorageLocationExample(context map[string]interface{}) string {
138+
return Nprintf(`
139+
resource "google_compute_image" "example" {
140+
name = "tf-test-example-sl-image%{random_suffix}"
141+
142+
raw_disk {
143+
source = "https://storage.googleapis.com/bosh-gce-raw-stemcells/bosh-stemcell-97.98-google-kvm-ubuntu-xenial-go_agent-raw-1557960142.tar.gz"
144+
}
145+
storage_locations = ["us-central1"]
146+
}
147+
`, context)
148+
}
149+
112150
func testAccCheckComputeImageDestroyProducer(t *testing.T) func(s *terraform.State) error {
113151
return func(s *terraform.State) error {
114152
for name, rs := range s.RootModule().Resources {

website/docs/r/compute_image.html.markdown

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ resource "google_compute_image" "example" {
8585
}
8686
}
8787
```
88+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
89+
<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">
90+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
91+
</a>
92+
</div>
93+
## Example Usage - Image Basic Storage Location
94+
95+
96+
```hcl
97+
resource "google_compute_image" "example" {
98+
name = "example-sl-image"
99+
100+
raw_disk {
101+
source = "https://storage.googleapis.com/bosh-gce-raw-stemcells/bosh-stemcell-97.98-google-kvm-ubuntu-xenial-go_agent-raw-1557960142.tar.gz"
102+
}
103+
storage_locations = ["us-central1"]
104+
}
105+
```
88106

89107
## Argument Reference
90108

@@ -110,6 +128,12 @@ The following arguments are supported:
110128
An optional description of this resource. Provide this property when
111129
you create the resource.
112130

131+
* `storage_locations` -
132+
(Optional)
133+
Cloud Storage bucket storage location of the image
134+
(regional or multi-regional).
135+
Reference link: https://cloud.google.com/compute/docs/reference/rest/v1/images
136+
113137
* `disk_size_gb` -
114138
(Optional)
115139
Size of the image when restored onto a persistent disk (in GB).

0 commit comments

Comments
 (0)