Skip to content

Commit

Permalink
Image: add basic auth support to download source image
Browse files Browse the repository at this point in the history
  • Loading branch information
binchenX authored and Bin Chen committed Feb 1, 2021
1 parent 661136c commit fb9e88b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
20 changes: 19 additions & 1 deletion openstack/images_image_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := http.DefaultClient
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)
}

defer resp.Body.Close()

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

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

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

"local_file_path": {
Type: schema.TypeString,
Optional: true,
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/images_image_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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`.

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

Expand Down

0 comments on commit fb9e88b

Please sign in to comment.