Skip to content

Commit

Permalink
fix: Import of vsphere_virtual_disk imports the wrong vmdk_path (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zxinyu08 authored Feb 12, 2024
1 parent d613fae commit c6b790f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
24 changes: 21 additions & 3 deletions vsphere/resource_vsphere_virtual_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package vsphere

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

"errors"
Expand Down Expand Up @@ -497,7 +499,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 @@ -525,10 +541,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.

0 comments on commit c6b790f

Please sign in to comment.