Skip to content

Commit 397ef9b

Browse files
tenthirtyamlbajolet-hashicorp
authored andcommitted
chore: driver godoc
Adds or updates the Go Doc comments for the driver functions. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 6b4a3c9 commit 397ef9b

File tree

12 files changed

+181
-19
lines changed

12 files changed

+181
-19
lines changed

builder/vsphere/driver/cluster.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type Cluster struct {
1010
cluster *object.ClusterComputeResource
1111
}
1212

13+
// FindCluster locates a cluster within the vCenter environment by its name.
14+
// Returns a Cluster object or an error if not found or if the retrieval
15+
// process fails.
1316
func (d *VCenterDriver) FindCluster(name string) (*Cluster, error) {
1417
c, err := d.finder.ClusterComputeResource(d.ctx, name)
1518
if err != nil {

builder/vsphere/driver/datastore.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ type DatastoreDriver struct {
3333
driver *VCenterDriver
3434
}
3535

36+
// NewDatastore creates a new Datastore object.
3637
func (d *VCenterDriver) NewDatastore(ref *types.ManagedObjectReference) Datastore {
3738
return &DatastoreDriver{
3839
ds: object.NewDatastore(d.client.Client, *ref),
3940
driver: d,
4041
}
4142
}
4243

43-
// If name is an empty string, then resolve host's one
44+
// FindDatastore locates a datastore by its name and an optional host.
45+
// Returns a Datastore object or an error if the datastore is not found.
4446
func (d *VCenterDriver) FindDatastore(name string, host string) (Datastore, error) {
4547
if name == "" {
4648
h, err := d.FindHost(host)
@@ -76,6 +78,9 @@ func (d *VCenterDriver) FindDatastore(name string, host string) (Datastore, erro
7678
}, nil
7779
}
7880

81+
// GetDatastoreName retrieves the name of a datastore by its ID.
82+
// Returns the name of the datastore or an error if the retrieval process
83+
// fails.
7984
func (d *VCenterDriver) GetDatastoreName(id string) (string, error) {
8085
obj := types.ManagedObjectReference{
8186
Type: "Datastore",
@@ -91,6 +96,9 @@ func (d *VCenterDriver) GetDatastoreName(id string) (string, error) {
9196
return me.Name, nil
9297
}
9398

99+
// Info retrieves properties of the datastore object with optional filters
100+
// specified as parameters. If no parameters are provided, all properties are
101+
// returned.
94102
func (ds *DatastoreDriver) Info(params ...string) (*mo.Datastore, error) {
95103
var p []string
96104
if len(params) == 0 {
@@ -106,6 +114,7 @@ func (ds *DatastoreDriver) Info(params ...string) (*mo.Datastore, error) {
106114
return &info, nil
107115
}
108116

117+
// DirExists checks if a directory exists in a datastore.
109118
func (ds *DatastoreDriver) DirExists(filepath string) bool {
110119
_, err := ds.ds.Stat(ds.driver.ctx, filepath)
111120
if _, ok := err.(object.DatastoreNoSuchDirectoryError); ok {
@@ -114,24 +123,28 @@ func (ds *DatastoreDriver) DirExists(filepath string) bool {
114123
return true
115124
}
116125

126+
// FileExists checks if a file exists in a datastore.
117127
func (ds *DatastoreDriver) FileExists(path string) bool {
118128
_, err := ds.ds.Stat(ds.driver.ctx, path)
119129
return err == nil
120130
}
121131

132+
// Name retrieves the name of a datastore.
122133
func (ds *DatastoreDriver) Name() string {
123134
return ds.ds.Name()
124135
}
125136

137+
// Reference retrieves the reference of a datastore.
126138
func (ds *DatastoreDriver) Reference() types.ManagedObjectReference {
127139
return ds.ds.Reference()
128140
}
129141

142+
// ResolvePath resolves a path in a datastore.
130143
func (ds *DatastoreDriver) ResolvePath(path string) string {
131144
return ds.ds.Path(path)
132145
}
133146

134-
// The file ID isn't available via the API, so we use DatastoreBrowser to search
147+
// GetDatastoreFilePath retrieves the full path of a file in a specified datastore directory by its datastore ID and name.
135148
func (d *VCenterDriver) GetDatastoreFilePath(datastoreID, dir, filename string) (string, error) {
136149
ref := types.ManagedObjectReference{Type: "Datastore", Value: datastoreID}
137150
ds := object.NewDatastore(d.vimClient, ref)
@@ -167,6 +180,8 @@ func (d *VCenterDriver) GetDatastoreFilePath(datastoreID, dir, filename string)
167180
return res.File[0].GetFileInfo().Path, nil
168181
}
169182

183+
// UploadFile uploads a file from the local source path to the destination path
184+
// in the datastore, with optional host context.
170185
func (ds *DatastoreDriver) UploadFile(src, dst, host string, setHost bool) error {
171186
p := soap.DefaultUpload
172187
ctx := ds.driver.ctx
@@ -182,6 +197,7 @@ func (ds *DatastoreDriver) UploadFile(src, dst, host string, setHost bool) error
182197
return ds.ds.UploadFile(ctx, src, dst, &p)
183198
}
184199

200+
// Delete deletes a file from a datastore by a path.
185201
func (ds *DatastoreDriver) Delete(path string) error {
186202
dc, err := ds.driver.finder.Datacenter(ds.driver.ctx, ds.ds.DatacenterPath)
187203
if err != nil {
@@ -191,6 +207,7 @@ func (ds *DatastoreDriver) Delete(path string) error {
191207
return fm.Delete(ds.driver.ctx, path)
192208
}
193209

210+
// MakeDirectory creates a directory in a datastore by a path.
194211
func (ds *DatastoreDriver) MakeDirectory(path string) error {
195212
dc, err := ds.driver.finder.Datacenter(ds.driver.ctx, ds.ds.DatacenterPath)
196213
if err != nil {
@@ -200,8 +217,7 @@ func (ds *DatastoreDriver) MakeDirectory(path string) error {
200217
return fm.FileManager.MakeDirectory(ds.driver.ctx, path, dc, true)
201218
}
202219

203-
// Cuts out the datastore prefix
204-
// Example: "[datastore1] file.ext" --> "file.ext"
220+
// RemoveDatastorePrefix removes the datastore prefix from a path.
205221
func RemoveDatastorePrefix(path string) string {
206222
res := object.DatastorePath{}
207223
if hadPrefix := res.FromString(path); hadPrefix {
@@ -215,6 +231,8 @@ type DatastoreIsoPath struct {
215231
path string
216232
}
217233

234+
// Validate checks if the path matches the expected datastore ISO path format.
235+
// Returns true if valid, otherwise false.
218236
func (d *DatastoreIsoPath) Validate() bool {
219237
// Matches:
220238
// [datastore] /dir/subdir/file
@@ -226,11 +244,12 @@ func (d *DatastoreIsoPath) Validate() bool {
226244
return matched
227245
}
228246

247+
// GetFilePath removes the datastore name from the path and returns the trimmed
248+
// file path portion of the datastore ISO path.
229249
func (d *DatastoreIsoPath) GetFilePath() string {
230250
filePath := d.path
231251
parts := strings.Split(d.path, "]")
232252
if len(parts) > 1 {
233-
// removes datastore name from path
234253
filePath = parts[1]
235254
filePath = strings.TrimSpace(filePath)
236255
}

builder/vsphere/driver/disk.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ type StorageConfig struct {
2222
Storage []Disk
2323
}
2424

25+
// AddStorageDevices adds virtual storage devices to an existing device list
26+
// based on the configuration. Adds a new controller for each controller type
27+
// specified in the configuration and adds virtual disks to the controller.
2528
func (c *StorageConfig) AddStorageDevices(existingDevices object.VirtualDeviceList) ([]types.BaseVirtualDeviceConfigSpec, error) {
2629
newDevices := object.VirtualDeviceList{}
2730

28-
// Create new controller based on existing devices list and add it to the new devices list.
2931
var controllers []types.BaseVirtualController
3032
for _, controllerType := range c.DiskControllerType {
3133
var device types.BaseVirtualDevice
@@ -71,7 +73,8 @@ func (c *StorageConfig) AddStorageDevices(existingDevices object.VirtualDeviceLi
7173
return newDevices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
7274
}
7375

74-
// Returns the first virtual disk found in the devices list.
76+
// findDisk scans a list of virtual devices and retrieves a single virtual disk
77+
// if exactly one is found. Returns an error if no disk or multiple disks are found.
7578
// TODO: Add support for multiple disks.
7679
func findDisk(devices object.VirtualDeviceList) (*types.VirtualDisk, error) {
7780
var disks []*types.VirtualDisk
@@ -90,6 +93,6 @@ func findDisk(devices object.VirtualDeviceList) (*types.VirtualDisk, error) {
9093
// Single disk found.
9194
return disks[0], nil
9295
}
93-
// More than one disk found.
96+
// Multiple disks found.
9497
return nil, errors.New("more than one virtual disk found, only a single disk is allowed")
9598
}

builder/vsphere/driver/driver.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ func (d *VCenterDriver) Cleanup() (error, error) {
134134
return d.restClient.client.Logout(d.ctx), d.client.SessionManager.Logout(d.ctx)
135135
}
136136

137-
// The rest.Client requires vCenter.
138-
// RestClient is to modularize the rest.Client session and use it only when is necessary.
139-
// This will allow users without vCenter Server to use the other features that do not use the rest.Client.
140-
// To use the client login/logout must be done to create an authenticated session.
137+
// RestClient manages RESTful interactions with vCenter, handling client initialization and credential storage.
141138
type RestClient struct {
142139
client *rest.Client
143140
credentials *url.Userinfo

builder/vsphere/driver/folder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ func (d *VCenterDriver) NewFolder(ref *types.ManagedObjectReference) *Folder {
2626
}
2727
}
2828

29+
// FindFolder locates or creates a folder structure within a datastore based
30+
// on the provided folder name. Returns a pointer to the Folder object or an
31+
// error if the operation fails.
2932
func (d *VCenterDriver) FindFolder(name string) (*Folder, error) {
3033
if name != "" {
31-
// create folders if they don't exist
34+
// If the folder does not exist, create it.
3235
parent := ""
3336
parentFolder, err := d.finder.Folder(d.ctx, path.Join(d.datacenter.InventoryPath, "vm"))
3437
if err != nil {
@@ -59,6 +62,8 @@ func (d *VCenterDriver) FindFolder(name string) (*Folder, error) {
5962
}, nil
6063
}
6164

65+
// Info retrieves properties of the folder object with optional filters specified
66+
// as parameters. If no parameters are provided, all properties are returned.
6267
func (f *Folder) Info(params ...string) (*mo.Folder, error) {
6368
var p []string
6469
if len(params) == 0 {
@@ -74,6 +79,8 @@ func (f *Folder) Info(params ...string) (*mo.Folder, error) {
7479
return &info, nil
7580
}
7681

82+
// Path constructs and returns the full hierarchical path of the folder,
83+
// starting from the datacenter level or an error.
7784
func (f *Folder) Path() (string, error) {
7885
info, err := f.Info("name", "parent")
7986
if err != nil {

builder/vsphere/driver/host.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ type Host struct {
1414
host *object.HostSystem
1515
}
1616

17+
// NewHost creates and initializes a new Host object using a
18+
// ManagedObjectReference and the VCenterDriver instance.
1719
func (d *VCenterDriver) NewHost(ref *types.ManagedObjectReference) *Host {
1820
return &Host{
1921
host: object.NewHostSystem(d.client.Client, *ref),
2022
driver: d,
2123
}
2224
}
2325

26+
// FindHost locates a host within the vCenter environment by its name. Returns
27+
// a Host object or an error if not found or if the retrieval process fails.
2428
func (d *VCenterDriver) FindHost(name string) (*Host, error) {
2529
h, err := d.finder.HostSystem(d.ctx, name)
2630
if err != nil {
@@ -32,6 +36,8 @@ func (d *VCenterDriver) FindHost(name string) (*Host, error) {
3236
}, nil
3337
}
3438

39+
// Info retrieves properties of the host object with optional filters specified
40+
// as parameters. If no parameters are provided, all properties are returned.
3541
func (h *Host) Info(params ...string) (*mo.HostSystem, error) {
3642
var p []string
3743
if len(params) == 0 {

builder/vsphere/driver/library.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type Library struct {
1717
library *library.Library
1818
}
1919

20+
// FindContentLibraryByName retrieves a content library by its name. Returns a
21+
// Library object or an error if the library is not found.
2022
func (d *VCenterDriver) FindContentLibraryByName(name string) (*Library, error) {
2123
lm := library.NewManager(d.restClient.client)
2224
l, err := lm.GetLibraryByName(d.ctx, name)
@@ -29,6 +31,9 @@ func (d *VCenterDriver) FindContentLibraryByName(name string) (*Library, error)
2931
}, nil
3032
}
3133

34+
// FindContentLibraryItem retrieves a content library item by its name within
35+
// the specified library ID. Returns the library item if found or an error if
36+
// the item is not found or the retrieval process fails.
3237
func (d *VCenterDriver) FindContentLibraryItem(libraryId string, name string) (*library.Item, error) {
3338
lm := library.NewManager(d.restClient.client)
3439
items, err := lm.GetLibraryItems(d.ctx, libraryId)
@@ -43,6 +48,10 @@ func (d *VCenterDriver) FindContentLibraryItem(libraryId string, name string) (*
4348
return nil, fmt.Errorf("content library item %s not found", name)
4449
}
4550

51+
// FindContentLibraryItemUUID retrieves the UUID of a content library item
52+
//
53+
// based on the given library ID and item name. Returns the UUID if found or
54+
// an error if the item is not found or the retrieval process fails.
4655
func (d *VCenterDriver) FindContentLibraryItemUUID(libraryId string, name string) (string, error) {
4756
item, err := d.FindContentLibraryItem(libraryId, name)
4857
if err != nil {
@@ -51,6 +60,10 @@ func (d *VCenterDriver) FindContentLibraryItemUUID(libraryId string, name string
5160
return item.ID, nil
5261
}
5362

63+
// FindContentLibraryFileDatastorePath checks if the provided ISO path belongs
64+
// to a content library and retrieves its datastore path. Returns the datastore
65+
// path if the ISO path is a content library path or an error if the path is
66+
// not identified as a content library path or if the retrieval process fails.
5467
func (d *VCenterDriver) FindContentLibraryFileDatastorePath(isoPath string) (string, error) {
5568
log.Printf("Check if ISO path is a Content Library path")
5669
err := d.restClient.Login(d.ctx)
@@ -98,6 +111,8 @@ func (d *VCenterDriver) FindContentLibraryFileDatastorePath(isoPath string) (str
98111
return path.Join(libItemDir, isoFilePath), nil
99112
}
100113

114+
// UpdateContentLibraryItem updates the metadata of a content library item,
115+
// such as its name and description. Returns an error if the update fails.
101116
func (d *VCenterDriver) UpdateContentLibraryItem(item *library.Item, name string, description string) error {
102117
lm := library.NewManager(d.restClient.client)
103118
item.Patch(&library.Item{
@@ -112,6 +127,8 @@ type LibraryFilePath struct {
112127
path string
113128
}
114129

130+
// Validate checks the format of the LibraryFilePath and returns an error if
131+
// the path is not in the expected format.
115132
func (l *LibraryFilePath) Validate() error {
116133
l.path = strings.TrimLeft(l.path, "/")
117134
parts := strings.Split(l.path, "/")
@@ -121,14 +138,17 @@ func (l *LibraryFilePath) Validate() error {
121138
return nil
122139
}
123140

141+
// GetLibraryName retrieves the library name from the content library file path.
124142
func (l *LibraryFilePath) GetLibraryName() string {
125143
return strings.Split(l.path, "/")[0]
126144
}
127145

146+
// GetLibraryItemName retrieves the library item name from the content library file path.
128147
func (l *LibraryFilePath) GetLibraryItemName() string {
129148
return strings.Split(l.path, "/")[1]
130149
}
131150

151+
// GetFileName retrieves the file name from the content library file path.
132152
func (l *LibraryFilePath) GetFileName() string {
133153
return strings.Split(l.path, "/")[2]
134154
}

builder/vsphere/driver/network.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ type Network struct {
1616
network object.NetworkReference
1717
}
1818

19+
// NewNetwork creates and initializes a new Network object using the provided
20+
// ManagedObjectReference.
1921
func (d *VCenterDriver) NewNetwork(ref *types.ManagedObjectReference) *Network {
2022
return &Network{
2123
network: object.NewNetwork(d.client.Client, *ref),
2224
driver: d,
2325
}
2426
}
2527

28+
// FindNetwork locates a network by its name within the vCenter context.
29+
// Returns a Network object or an error if the network is not found.
2630
func (d *VCenterDriver) FindNetwork(name string) (*Network, error) {
2731
n, err := d.finder.Network(d.ctx, name)
2832
if err != nil {
@@ -34,6 +38,8 @@ func (d *VCenterDriver) FindNetwork(name string) (*Network, error) {
3438
}, nil
3539
}
3640

41+
// FindNetworks retrieves a list of networks in the vCenter matching the
42+
// provided name and returns them as Network objects.
3743
func (d *VCenterDriver) FindNetworks(name string) ([]*Network, error) {
3844
ns, err := d.finder.NetworkList(d.ctx, name)
3945
if err != nil {
@@ -49,6 +55,9 @@ func (d *VCenterDriver) FindNetworks(name string) ([]*Network, error) {
4955
return networks, nil
5056
}
5157

58+
// Info retrieves the properties of the network object with optional filters
59+
// specified as parameters. If no parameters are provided, all properties are
60+
// returned.
5261
func (n *Network) Info(params ...string) (*mo.Network, error) {
5362
var p []string
5463
if len(params) == 0 {
@@ -75,6 +84,7 @@ type MultipleNetworkFoundError struct {
7584
append string
7685
}
7786

87+
// Error returns a formatted error message for the MultipleNetworkFoundError.
7888
func (e *MultipleNetworkFoundError) Error() string {
7989
return fmt.Sprintf("'%s' resolves to more than one network name; %s", e.path, e.append)
8090
}

0 commit comments

Comments
 (0)