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

fix: import of r/vsphere_virtual_disk imports the wrong vmdk_path #1762

Merged
merged 1 commit into from
Feb 12, 2024
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
24 changes: 21 additions & 3 deletions vsphere/resource_vsphere_virtual_disk.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package vsphere

import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"

"errors"
Expand Down Expand Up @@ -484,7 +486,21 @@ func searchForDirectory(client *govmomi.Client, datacenter string, datastore str

func resourceVSphereVirtualDiskImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Client).vimClient
p := d.Id()
var data map[string]string
if err := json.Unmarshal([]byte(d.Id()), &data); err != nil {
return nil, err
}
createDirectories, ok := data["create_directories"]
if ok {
createDirectoriesBool, _ := strconv.ParseBool(createDirectories)
log.Printf("[INFO] Set create_directories during import: %v", createDirectoriesBool)
_ = d.Set("create_directories", createDirectoriesBool)
}

p, ok := data["virtual_disk_path"]
if !ok {
return nil, errors.New("missing virtual_disk_path in input data")
}
if !strings.HasPrefix(p, "/") {
return nil, errors.New("ID must start with a trailing slash")
}
Expand Down Expand Up @@ -512,10 +528,12 @@ func resourceVSphereVirtualDiskImport(d *schema.ResourceData, meta interface{})
return nil, fmt.Errorf("Invalid datastore path '%s'", di.Name)
}

//addrParts[2] is in form: [<datastore>]path/to/vmdk
vmdkPath := strings.Split(addrParts[2], "]")[1]
_ = d.Set("datacenter", dc.Name())
_ = d.Set("datastore", dp.Datastore)
_ = d.Set("vmdk_path", dp.Path)
d.SetId(dp.Path)
_ = d.Set("vmdk_path", vmdkPath)
d.SetId(vmdkPath)

return []*schema.ResourceData{d}, nil
}
5 changes: 3 additions & 2 deletions website/docs/r/virtual_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ via supplying the full datastore path to the virtual disk. An example is below:
[docs-import]: https://www.terraform.io/docs/import/index.html

```
terraform import vsphere_virtual_disk.virtual_disk "/dc-01/[datastore-01] foo/bar.vmdk"
terraform import vsphere_virtual_disk.virtual_disk \
'{"virtual_disk_path": "/dc-01/[datastore-01]foo/bar.vmdk", \ "create_directories": "true"}'
```

The above would import the virtual disk located at `foo/bar.vmdk` in the `datastore-01`
datastore of the `dc-01` datacenter.
datastore of the `dc-01` datacenter with `create_directories` set as `true`.

~> **NOTE:** Import is not supported if using the **deprecated** `adapter_type` field.