From a024176da9b6418b23faf3e464f4444699891f1d Mon Sep 17 00:00:00 2001 From: vbauzysvmware <41415459+vbauzysvmware@users.noreply.github.com> Date: Wed, 23 Oct 2019 13:06:42 +0300 Subject: [PATCH] Add QueryDisks (#255) * Added QueryDisks Signed-off-by: Vaidotas Bauzys --- CHANGELOG.md | 1 + govcd/disk.go | 29 +++++++++++++++++++++-- govcd/disk_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b7db26d..fb0345254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/govcd/disk.go b/govcd/disk.go index 0e3998b64..1e46cd2aa 100644 --- a/govcd/disk.go +++ b/govcd/disk.go @@ -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 == "" { @@ -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 @@ -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 diff --git a/govcd/disk_test.go b/govcd/disk_test.go index 50c854729..9df5ce4f2 100644 --- a/govcd/disk_test.go +++ b/govcd/disk_test.go @@ -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())