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 5, 2021
1 parent 661136c commit 5985a10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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)
}

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
5 changes: 5 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,11 @@ 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`.
Warning: the password will be stored in the Terraform state as plain-text.

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

Expand Down

0 comments on commit 5985a10

Please sign in to comment.