diff --git a/vsphere/resource_vsphere_virtual_disk.go b/vsphere/resource_vsphere_virtual_disk.go index 0255dcac7..49a3fec20 100644 --- a/vsphere/resource_vsphere_virtual_disk.go +++ b/vsphere/resource_vsphere_virtual_disk.go @@ -4,8 +4,10 @@ package vsphere import ( + "encoding/json" "fmt" "log" + "strconv" "strings" "errors" @@ -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") } @@ -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: []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 } diff --git a/website/docs/r/virtual_disk.html.markdown b/website/docs/r/virtual_disk.html.markdown index a80385488..4879a047b 100644 --- a/website/docs/r/virtual_disk.html.markdown +++ b/website/docs/r/virtual_disk.html.markdown @@ -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. \ No newline at end of file