Skip to content

Commit

Permalink
Merge pull request #2 from vmware/master
Browse files Browse the repository at this point in the history
Add QueryDisks (vmware#255)
  • Loading branch information
funky81 authored Oct 24, 2019
2 parents c0e647c + a024176 commit 9f7cdf3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ which use the proxied NSX-V API of advanced edge gateway for handling firewall r
* Added methods `VDC.GetDiskByHref` `VDC.GetDisksByName` and related `GetDiskById`
* Added new methods `Catalog.QueryMedia`, `Catalog.GetMediaByName`, `Catalog.GetMediaById`, `Catalog.GetMediaByNameOrId`, `AdminCatalog.QueryMedia`, `AdminCatalog.GetMediaByName`, `AdminCatalog.GetMediaById`, `AdminCatalog.GetMediaByNameOrId`, `MediaRecord.Refresh`, `MediaRecord.Delete`, `MediaRecord.GetMetadata`, `MediaRecord.AddMetadata`, `MediaRecord.AddMetadataAsync`, `MediaRecord.DeleteMetadata`, `MediaRecord.DeleteMetadataAsync`, `Media.GetMetadata`, `Media.AddMetadata`, `Media.AddMetadataAsync`, `Media.DeleteMetadata`, `Media.DeleteMetadataAsync` [#245](https://github.com/vmware/go-vcloud-director/pull/245)
* Deprecated methods `Vdc.FindMediaImage`, `MediaItem`, `RemoveMediaImageIfExists`, `MediaItem.Delete`, `FindMediaAsCatalogItem`, `*MediaItem.Refresh`, `MediaItem.GetMetadata`, `MediaItem.AddMetadata`, `MediaItem.AddMetadataAsync`, `MediaItem.DeleteMetadata`, `MediaItem.DeleteMetadataAsync` [#245](https://github.com/vmware/go-vcloud-director/pull/245)
* Added method `VDC.QueryDisks` [#255](https://github.com/vmware/go-vcloud-director/pull/255)

IMPROVEMENTS:

Expand Down
29 changes: 27 additions & 2 deletions govcd/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func FindDiskByHREF(client *Client, href string) (*Disk, error) {

}

// Find independent disk using disk name. Returns VMRecord query return type
// QueryDisk find independent disk using disk name. Returns DiskRecord type
func (vdc *Vdc) QueryDisk(diskName string) (DiskRecord, error) {

if diskName == "" {
Expand All @@ -331,7 +331,7 @@ func (vdc *Vdc) QueryDisk(diskName string) (DiskRecord, error) {

results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": "name==" + url.QueryEscape(diskName)})
if err != nil {
return DiskRecord{}, fmt.Errorf("error querying disk %#v", err)
return DiskRecord{}, fmt.Errorf("error querying disk %s", err)
}

diskResults := results.Results.DiskRecord
Expand All @@ -350,6 +350,31 @@ func (vdc *Vdc) QueryDisk(diskName string) (DiskRecord, error) {
return *newDisk, nil
}

// QueryDisks find independent disks using disk name. Returns list of DiskRecordType
func (vdc *Vdc) QueryDisks(diskName string) (*[]*types.DiskRecordType, error) {

if diskName == "" {
return nil, fmt.Errorf("disk name can't be empty")
}

typeMedia := "disk"
if vdc.client.IsSysAdmin {
typeMedia = "adminDisk"
}

results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": "name==" + url.QueryEscape(diskName)})
if err != nil {
return nil, fmt.Errorf("error querying disks %s", err)
}

diskResults := results.Results.DiskRecord
if vdc.client.IsSysAdmin {
diskResults = results.Results.AdminDiskRecord
}

return &diskResults, nil
}

// GetDiskByHref finds a Disk by HREF
// On success, returns a pointer to the Disk structure and a nil error
// On failure, returns a nil pointer and an error
Expand Down
57 changes: 57 additions & 0 deletions govcd/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,63 @@ func (vcd *TestVCD) Test_QueryDisk(check *C) {

}

// Test query disk
func (vcd *TestVCD) Test_QueryDisks(check *C) {

if vcd.config.VCD.Disk.Size <= 0 {
check.Skip("skipping test because disk size is 0")
}

fmt.Printf("Running: %s\n", check.TestName())

name := "TestQueryDisks"

// Create disk
diskCreateParamsDisk := &types.Disk{
Name: name,
Size: vcd.config.VCD.Disk.Size,
Description: name,
}

diskCreateParams := &types.DiskCreateParams{
Disk: diskCreateParamsDisk,
}

task, err := vcd.vdc.CreateDisk(diskCreateParams)
check.Assert(err, IsNil)

check.Assert(task.Task.Owner.Type, Equals, types.MimeDisk)
diskHREF := task.Task.Owner.HREF

PrependToCleanupList(diskHREF, "disk", "", check.TestName())

// Wait for disk creation complete
err = task.WaitTaskCompletion()
check.Assert(err, IsNil)

// create second disk with same name
task, err = vcd.vdc.CreateDisk(diskCreateParams)
check.Assert(err, IsNil)

check.Assert(task.Task.Owner.Type, Equals, types.MimeDisk)
diskHREF = task.Task.Owner.HREF

PrependToCleanupList(diskHREF, "disk", "", check.TestName())

// Wait for disk creation complete
err = task.WaitTaskCompletion()
check.Assert(err, IsNil)

// Verify created disk
check.Assert(diskHREF, Not(Equals), "")
diskRecords, err := vcd.vdc.QueryDisks(name)

check.Assert(err, IsNil)
check.Assert(len(*diskRecords), Equals, 2)
check.Assert((*diskRecords)[0].Name, Equals, diskCreateParamsDisk.Name)
check.Assert((*diskRecords)[0].SizeB, Equals, int64(diskCreateParamsDisk.Size))
}

// Tests Disk list retrieval by name, by ID
func (vcd *TestVCD) Test_GetDisks(check *C) {
fmt.Printf("Running: %s\n", check.TestName())
Expand Down

0 comments on commit 9f7cdf3

Please sign in to comment.