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

Image: add basic auth support to download source image #1157

Merged
merged 1 commit into from
Feb 8, 2021
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: 20 additions & 2 deletions openstack/images_image_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func resourceImagesImageV2FileProps(filename string) (int64, string, error) {
return filesize, filechecksum, nil
}

func resourceImagesImageV2File(d *schema.ResourceData) (string, error) {
func resourceImagesImageV2File(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
if filename := d.Get("local_file_path").(string); filename != "" {
return filename, nil
} else if furl := d.Get("image_source_url").(string); furl != "" {
Expand All @@ -100,10 +100,28 @@ func resourceImagesImageV2File(d *schema.ResourceData) (string, error) {
return "", fmt.Errorf("Error creating file %q: %s", filename, err)
}
defer file.Close()
resp, err := http.Get(furl)
client := &client.ProviderClient.HTTPClient
request, err := http.NewRequest("GET", furl, nil)
if err != nil {
return "", fmt.Errorf("Error create a new request")
}

username := d.Get("image_source_username").(string)
password := d.Get("image_source_password").(string)
if username != "" && password != "" {
request.SetBasicAuth(username, password)
}

resp, err := client.Do(request)
if err != nil {
return "", fmt.Errorf("Error downloading image from %q", furl)
}

// check for credential error among other errors
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Error downloading image from %q, statusCode is %d", furl, resp.StatusCode)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kayrus this is the required error check I mentioned.

defer resp.Body.Close()

if _, err = io.Copy(file, resp.Body); err != nil {
Expand Down
15 changes: 14 additions & 1 deletion openstack/resource_openstack_images_image_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ func resourceImagesImageV2() *schema.Resource {
ConflictsWith: []string{"local_file_path"},
},

"image_source_username": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"local_file_path"},
},

"image_source_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ConflictsWith: []string{"local_file_path"},
},

"local_file_path": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -257,7 +270,7 @@ func resourceImagesImageV2Create(d *schema.ResourceData, meta interface{}) error
var imgFile *os.File

// downloading/getting image file props
imgFilePath, err = resourceImagesImageV2File(d)
imgFilePath, err = resourceImagesImageV2File(imageClient, d)
if err != nil {
return fmt.Errorf("Error opening file for Image: %s", err)
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/images_image_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ description: |-

Manages a V2 Image resource within OpenStack Glance.

~> **Note:** All arguments including the source image URL password will be
stored in the raw state as plain-text. [Read more about sensitive data in
state](https://www.terraform.io/docs/language/state/sensitive-data.html).

## Example Usage

```hcl
Expand Down Expand Up @@ -48,6 +52,10 @@ The following arguments are supported:
being uploaded to Glance.
Conflicts with `local_file_path`.

* `image_source_username` - (Optional) The username of basic auth to download `image_source_url`.

* `image_source_password` - (Optional) The password of basic auth to download `image_source_url`.
binchenX marked this conversation as resolved.
Show resolved Hide resolved

* `min_disk_gb` - (Optional) Amount of disk space (in GB) required to boot image.
Defaults to 0.

Expand Down