Skip to content

Commit

Permalink
Add decompression flag to openstack_images_image_v2 resource (#1482)
Browse files Browse the repository at this point in the history
Some image are compressed and only filling Content-Type header resulting
if uploading a compressed image to openstack.
With this we automatically detects and uncompress images in this case.
  • Loading branch information
spnngl committed Mar 1, 2023
1 parent 87b5d8f commit fbf41d2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
19 changes: 12 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ FEATURES
* Updated Golangci-lint to `v1.51.2` ([#1488](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1488))
* Updated Terraform SDK to `v2.25.0` ([#1490](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1490))

FEATURES

IMPROVEMENTS

* Added `decompress` argument to `images_image_v2` resource ([#1482](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1482))
* Added `name_regex` argument to `openstack_images_image_v2` data source ([#1469](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1469))
* Added ability to provide a list of tags into `openstack_images_image_v2`, `openstack_images_image_ids_v2` data sources ([#1462](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1462)), ([#1468](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1468))
* Added `name_regex` attribute to `openstack_images_image_v2` data source ([#1469](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1469))
Expand All @@ -17,6 +20,8 @@ BUG FIXES

* Fixed `containerinfra_clustertemplate_v1` labels update ([#1455](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1455))

BUG FIXES

## 1.49.0 (26 October, 2022)

FEATURES
Expand Down Expand Up @@ -473,8 +478,8 @@ IMPROVEMENTS

BUG FIXES

* Fixed the bug with unchecked errors in initialization of Identity V3 client in `identity_auth_scope_v3` data source [[#878](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/878)]
* Fixed the bug with unchecked errors in initialization of Compute V2 client in `compute_floatingip_associate_v2` resource [[#878](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/878)]
* Fixed the bug with unchecked errors in initialization of Identity V3 client in `identity_auth_scope_v3` data source [[#878](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/878)]
* Fixed the bug with unchecked errors in initialization of Compute V2 client in `compute_floatingip_associate_v2` resource [[#878](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/878)]
* Fixed the bug with 404 errors handling while getting statuses tree in `openstack_lb_loadbalancer_v2` resource ([#883](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/883))
* Fixed the bug where is was unable to remove TLS references in `openstack_lb_listener_v2` resource ([#891](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/891))
* Fixed the bug where empty `scheduler_hints` list caused a panic in `openstack_compute_instance_v2` resource ([#885](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/885))
Expand Down Expand Up @@ -755,11 +760,11 @@ IMPROVEMENTS
* Added `transparent_vlan` to `openstack_networking_network_v2` data source ([#538](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/538))
* Added `max_retries` to the provider options ([#413](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/413))
* Added the ability to override catalog endpoints ([#501](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/501))
* Changed the `segments` attribute of the `openstack_networking_network_v2` to `TypeSet` [[#578](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/578)]
* Changed the `segments` attribute of the `openstack_networking_network_v2` to `TypeSet` [[#578](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/578)]

BUG FIXES

* `openstack_compute_interface_attach_v2` now correctly sets the `instance_id` [[#557](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/557)]
* `openstack_compute_interface_attach_v2` now correctly sets the `instance_id` [[#557](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/557)]
* `openstack_networking_port_v2` will now correctly set the `admin_state_up` to `true/UP` if left omitted ([#594](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/594))
* Fixed out of range panic in `openstack_compute_instance_v2` when no IP addresses were detected ([#539](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/539))

Expand Down Expand Up @@ -1008,7 +1013,7 @@ BUG FIXES

BACKWARDS INCOMPATIBILITIES / NOTES:

* The way IP addresses for `allowed_address_pairs` in the `openstack_networking_port_v2` resource are stored in the Terraform state has changed.
* The way IP addresses for `allowed_address_pairs` in the `openstack_networking_port_v2` resource are stored in the Terraform state has changed.
* The `external_gateway` argument in the `openstack_networking_router_v2` has been deprecated in favor of the more appropriately named `external_network_id`.

FEATURES
Expand Down Expand Up @@ -1102,7 +1107,7 @@ IMPROVEMENTS
* Added `status` field to `openstack_networking_network_v2` data source ([#105](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/105))
* `openstack_networking_router_v2` can now be imported ([#111](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/111))
* `openstack_networking_router_interface_v2` can now be imported ([#112](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/112))

BUG FIXES

* `openstack_lb_listener_v2`: Don't send `connection_limit` unless it has been set ([#90](https://github.com/terraform-provider-openstack/terraform-provider-openstack/issues/90))
Expand Down Expand Up @@ -1155,7 +1160,7 @@ BUG FIXES:

BACKWARDS INCOMPATIBILITIES / NOTES:

* You can now specify `region` in the provider block. All resources will inherit this region setting, or you can override it in the resource-level `region`. Make sure to do a `plan` before an `apply` to make sure the resource is not destroyed due to incorrectly determining the region! If you see this happening, either explicitly set the `region` in the resource or use `lifecycle.ignore_changes`.
* You can now specify `region` in the provider block. All resources will inherit this region setting, or you can override it in the resource-level `region`. Make sure to do a `plan` before an `apply` to make sure the resource is not destroyed due to incorrectly determining the region! If you see this happening, either explicitly set the `region` in the resource or use `lifecycle.ignore_changes`.
* `floating_ip` has been removed from `openstack_compute_instance_v2`. You must now use `openstack_compute_floatingip_associate_v2` to associate a Floating IP with an Instance.
* `volume` has been removed from `openstack_compute_instance_v2`. You must now use `openstack_compute_volume_attach_v2` to attach a Volume with an Instance.
* `member` has been removed from `openstack_lb_pool_v1`. You must now use `openstack_lb_member_v1` to add a LBaaS v1 Member to a Pool.
Expand Down
25 changes: 24 additions & 1 deletion openstack/images_image_v2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package openstack

import (
"compress/bzip2"
"compress/gzip"
"context"
"crypto/md5"
"encoding/hex"
Expand Down Expand Up @@ -127,7 +129,28 @@ func resourceImagesImageV2File(client *gophercloud.ServiceClient, d *schema.Reso

defer resp.Body.Close()

if _, err = io.Copy(file, resp.Body); err != nil {
reader := resp.Body
decompress := d.Get("decompress").(bool)

if decompress {
// If we're here "Content-Encoding" in not filled, we'll read
// "Content-Type" to select format
switch resp.Header.Get("Content-Type") {
case "gzip", "application/gzip":
reader, err = gzip.NewReader(resp.Body)
if err != nil {
return "", fmt.Errorf("Error decompressing gzip image: %s", err)
}
case "bzip2", "application/bzip2", "application/x-bzip2":
bz2Reader := bzip2.NewReader(resp.Body)
reader = io.NopCloser(bz2Reader)
default:
return "", fmt.Errorf("Error decompressing image, format %s is not supported", resp.Header.Get("Content-Type"))
}
defer reader.Close()
}

if _, err = io.Copy(file, reader); err != nil {
return "", fmt.Errorf("Error downloading image %q to file %q: %s", furl, filename, err)
}
return filename, nil
Expand Down
1 change: 1 addition & 0 deletions openstack/import_openstack_images_image_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestAccImagesImageV2_importBasic(t *testing.T) {
"image_cache_path",
"image_source_url",
"verify_checksum",
"decompress",
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions openstack/resource_openstack_images_image_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ func resourceImagesImageV2() *schema.Resource {
ConflictsWith: []string{"local_file_path", "verify_checksum"},
},

"decompress": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

// Computed-only
"checksum": {
Type: schema.TypeString,
Expand Down
1 change: 1 addition & 0 deletions openstack/resource_openstack_images_image_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func TestAccImagesImageV2_webdownload(t *testing.T) {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckGlanceImport(t)
t.Skip("OpenStack dev env seems to not support web-download anymore")
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckImagesImageV2Destroy,
Expand Down
7 changes: 6 additions & 1 deletion website/docs/r/images_image_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The following arguments are supported:

* `name` - (Required) The name of the image.

* `image_id` - (Optional) Unique ID (valid UUID) of image to create. Changing
* `image_id` - (Optional) Unique ID (valid UUID) of image to create. Changing
this creates a new image.

* `properties` - (Optional) A map of key/value pairs to set freeform
Expand Down Expand Up @@ -97,6 +97,11 @@ The following arguments are supported:
be used to let Openstack download the image directly from the remote source.
Conflicts with `local_file_path`. Defaults to false.

* `decompress` - (Optional) If true, this provider will decompress downloaded
image before uploading it to OpenStack. Decompression algorithm is chosen by
checking "Content-Type" header, supported algorithm are: gzip, bzip2.
Defaults to false. Changing this creates a new Image.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit fbf41d2

Please sign in to comment.