@@ -33,14 +33,16 @@ type DatastoreDriver struct {
3333 driver * VCenterDriver
3434}
3535
36+ // NewDatastore creates a new Datastore object.
3637func (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.
4446func (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.
7984func (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.
94102func (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.
109118func (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.
117127func (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.
122133func (ds * DatastoreDriver ) Name () string {
123134 return ds .ds .Name ()
124135}
125136
137+ // Reference retrieves the reference of a datastore.
126138func (ds * DatastoreDriver ) Reference () types.ManagedObjectReference {
127139 return ds .ds .Reference ()
128140}
129141
142+ // ResolvePath resolves a path in a datastore.
130143func (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.
135148func (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.
170185func (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.
185201func (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.
194211func (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.
205221func 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.
218236func (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.
229249func (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 }
0 commit comments