forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_vsphere_vmfs_disks.go
88 lines (76 loc) · 2.46 KB
/
data_source_vsphere_vmfs_disks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package vsphere
import (
"context"
"fmt"
"regexp"
"sort"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
func dataSourceVSphereVmfsDisks() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereVmfsDisksRead,
Schema: map[string]*schema.Schema{
"host_system_id": {
Type: schema.TypeString,
Description: "The managed object ID of the host to search for disks on.",
Required: true,
},
"rescan": {
Type: schema.TypeBool,
Description: "Rescan the system for disks before querying. This may lengthen the time it takes to gather information.",
Optional: true,
},
"filter": {
Type: schema.TypeString,
Description: "A regular expression to filter the disks against. Only disks with canonical names that match will be included.",
Optional: true,
ValidateFunc: validation.ValidateRegexp,
},
"disks": {
Type: schema.TypeList,
Description: "The names of the disks discovered by the search.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
hsID := d.Get("host_system_id").(string)
ss, err := hostStorageSystemFromHostSystemID(client, hsID)
if err != nil {
return fmt.Errorf("error loading host storage system: %s", err)
}
if d.Get("rescan").(bool) {
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
if err := ss.RescanAllHba(ctx); err != nil {
return err
}
}
var hss mo.HostStorageSystem
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
if err := ss.Properties(ctx, ss.Reference(), nil, &hss); err != nil {
return fmt.Errorf("error querying storage system properties: %s", err)
}
d.SetId(time.Now().UTC().String())
var disks []string
for _, sl := range hss.StorageDeviceInfo.ScsiLun {
if hsd, ok := sl.(*types.HostScsiDisk); ok {
if matched, _ := regexp.MatchString(d.Get("filter").(string), hsd.CanonicalName); matched {
disks = append(disks, hsd.CanonicalName)
}
}
}
sort.Strings(disks)
if err := d.Set("disks", disks); err != nil {
return fmt.Errorf("error saving results to state: %s", err)
}
return nil
}