Skip to content

Commit

Permalink
r/vmfs_datastore: Add import feature
Browse files Browse the repository at this point in the history
Add an easy peasy import feature. MoRefs can be fetched via a special
part of the vSphere web client that basically allows you to walk the
ServiceInstance root object, which allows you to fetch datastores and a
number of other managed object IDs.
  • Loading branch information
vancluever committed Sep 1, 2017
1 parent c617982 commit a5b4034
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions vsphere/resource_vsphere_vmfs_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func resourceVSphereVmfsDatastore() *schema.Resource {
Read: resourceVSphereVmfsDatastoreRead,
Update: resourceVSphereVmfsDatastoreUpdate,
Delete: resourceVSphereVmfsDatastoreDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereVmfsDatastoreImport,
},
Schema: s,
}
}
Expand Down Expand Up @@ -328,3 +331,25 @@ func resourceVSphereVmfsDatastoreDelete(d *schema.ResourceData, meta interface{}

return nil
}

func resourceVSphereVmfsDatastoreImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// We support importing a MoRef - so we need to load the datastore and check
// to make sure 1) it exists, and 2) it's a VMFS datastore. If it is, we are
// good to go (rest of the stuff will be handled by read on refresh).
client := meta.(*govmomi.Client)
id := d.Id()
ds, err := datastoreFromID(client, id)
if err != nil {
return nil, fmt.Errorf("cannot find datastore: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
t, err := ds.Type(ctx)
if err != nil {
return nil, fmt.Errorf("error fetching datastore type: %s", err)
}
if t != types.HostFileSystemVolumeFileSystemTypeVMFS {
return nil, fmt.Errorf("datastore ID %q is not a VMFS datastore", id)
}
return []*schema.ResourceData{d}, nil
}
26 changes: 26 additions & 0 deletions vsphere/resource_vsphere_vmfs_datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ func TestAccResourceVSphereVmfsDatastore(t *testing.T) {
},
},
},
{
"import",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccResourceVSphereVmfsDatastorePreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereVmfsDatastoreExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereVmfsDatastoreConfigStaticSingle(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereVmfsDatastoreExists(true),
),
},
{
Config: testAccResourceVSphereVmfsDatastoreConfigStaticSingle(),
ImportState: true,
ResourceName: "vsphere_vmfs_datastore.datastore",
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"host_system_id"},
},
},
},
},
}

for _, tc := range testAccResourceVSphereVmfsDatastoreCases {
Expand Down

0 comments on commit a5b4034

Please sign in to comment.