Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix queries for vCD 10 #293

Merged
merged 12 commits into from
Apr 2, 2020
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ NOTES:
* Improved testinf function `deleteVapp()` to avoid deletion errors during test suite run
[#297](https://github.com/vmware/go-vcloud-director/pull/297)

BUGS FIXED:
* Fix issue in Queries with vCD 10 version, which do not return network pool or provider VDC[#293](https://github.com/vmware/go-vcloud-director/pull/293)

## 2.6.0 (March 13, 2010)

Expand Down
5 changes: 3 additions & 2 deletions govcd/adminorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ func isCatalogFromSameOrg(adminOrg *AdminOrg, catalogName string) (bool, error)
func (adminOrg *AdminOrg) FindAdminCatalogRecords(name string) ([]*types.AdminCatalogRecord, error) {
util.Logger.Printf("[DEBUG] FindAdminCatalogRecords with name: %s and org name: %s", name, adminOrg.AdminOrg.Name)
results, err := adminOrg.client.QueryWithNotEncodedParams(nil, map[string]string{
"type": "adminCatalog",
"filter": fmt.Sprintf("name==%s;orgName==%s", url.QueryEscape(name), url.QueryEscape(adminOrg.AdminOrg.Name)),
"type": "adminCatalog",
"filter": fmt.Sprintf("name==%s;orgName==%s", url.QueryEscape(name), url.QueryEscape(adminOrg.AdminOrg.Name)),
"filterEncoded": "true",
})
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions govcd/adminvdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ var vdcVersionedFuncsV97 = vdcVersionedFuncs{
UpdateVdcAsync: updateVdcAsyncV97,
}

// TODO: add a wrapper function to use newest available method when version is higher than currently handled
// VDC function mapping by vDC version
var vdcVersionedFuncsByVcdVersion = map[string]vdcVersionedFuncs{
"vdc9.0": vdcVersionedFuncsV90,
"vdc9.1": vdcVersionedFuncsV90,
"vdc9.5": vdcVersionedFuncsV90,
"vdc9.7": vdcVersionedFuncsV97,
"vdc10.0": vdcVersionedFuncsV97,
"vdc10.1": vdcVersionedFuncsV97,
}

// GetAdminVdcByName function uses a valid VDC name and returns a admin VDC object.
Expand Down
2 changes: 2 additions & 0 deletions govcd/api_vcd_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var apiVersionToVcdVersion = map[string]string{
"31.0": "9.5",
"32.0": "9.7",
"33.0": "10.0",
"34.0": "10.1",
}

// vcdVersionToApiVersion gets the max supported API version from vCD version
Expand All @@ -42,6 +43,7 @@ var vcdVersionToApiVersion = map[string]string{
"9.5": "31.0",
"9.7": "32.0",
"10.0": "33.0",
"10.1": "34.0",
}

// to make vcdVersionToApiVersion used
Expand Down
4 changes: 2 additions & 2 deletions govcd/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (vdc *Vdc) QueryDisk(diskName string) (DiskRecord, error) {
typeMedia = "adminDisk"
}

results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": "name==" + url.QueryEscape(diskName)})
results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": "name==" + url.QueryEscape(diskName), "filterEncoded": "true"})
if err != nil {
return DiskRecord{}, fmt.Errorf("error querying disk %s", err)
}
Expand Down Expand Up @@ -362,7 +362,7 @@ func (vdc *Vdc) QueryDisks(diskName string) (*[]*types.DiskRecordType, error) {
typeMedia = "adminDisk"
}

results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": "name==" + url.QueryEscape(diskName)})
results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": "name==" + url.QueryEscape(diskName), "filterEncoded": "true"})
if err != nil {
return nil, fmt.Errorf("error querying disks %s", err)
}
Expand Down
8 changes: 5 additions & 3 deletions govcd/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func queryMediaWithFilter(vdc *Vdc, filter string) ([]*types.MediaRecordType, er
typeMedia = "adminMedia"
}

results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": filter})
results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia, "filter": filter, "filterEncoded": "true"})
if err != nil {
return nil, fmt.Errorf("error querying medias %s", err)
}
Expand Down Expand Up @@ -487,7 +487,8 @@ func (catalog *Catalog) GetMediaById(mediaId string) (*Media, error) {
}

results, err := catalog.client.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia,
"filter": fmt.Sprintf("catalogName==%s", url.QueryEscape(catalog.Catalog.Name))})
"filter": fmt.Sprintf("catalogName==%s", url.QueryEscape(catalog.Catalog.Name)),
"filterEncoded": "true"})
if err != nil {
return nil, fmt.Errorf("error querying medias %s", err)
}
Expand Down Expand Up @@ -572,7 +573,8 @@ func (catalog *Catalog) QueryMedia(mediaName string) (*MediaRecord, error) {
results, err := catalog.client.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia,
"filter": fmt.Sprintf("name==%s;catalogName==%s",
url.QueryEscape(mediaName),
url.QueryEscape(catalog.Catalog.Name))})
url.QueryEscape(catalog.Catalog.Name)),
"filterEncoded": "true"})
if err != nil {
return nil, fmt.Errorf("error querying medias %s", err)
}
Expand Down
10 changes: 6 additions & 4 deletions govcd/orgvdcnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ func (vdc *Vdc) CreateOrgVDCNetwork(networkConfig *types.OrgVDCNetwork) (Task, e
func (vdc *Vdc) GetNetworkList() ([]*types.QueryResultOrgVdcNetworkRecordType, error) {
// Find the list of networks with the wanted name
result, err := vdc.client.QueryWithNotEncodedParams(nil, map[string]string{
"type": "orgVdcNetwork",
"filter": fmt.Sprintf("vdc==%s", url.QueryEscape(vdc.Vdc.ID)),
"type": "orgVdcNetwork",
"filter": fmt.Sprintf("vdc==%s", url.QueryEscape(vdc.Vdc.ID)),
"filterEncoded": "true",
})
if err != nil {
return nil, fmt.Errorf("[findEdgeGatewayConnection] error returning the list of networks for VDC: %s", err)
Expand All @@ -215,8 +216,9 @@ func (vdc *Vdc) FindEdgeGatewayNameByNetwork(networkName string) (string, error)

// Find the list of networks with the wanted name
result, err := vdc.client.QueryWithNotEncodedParams(nil, map[string]string{
"type": "orgVdcNetwork",
"filter": fmt.Sprintf("name==%s;vdc==%s", url.QueryEscape(networkName), url.QueryEscape(vdc.Vdc.ID)),
"type": "orgVdcNetwork",
"filter": fmt.Sprintf("name==%s;vdc==%s", url.QueryEscape(networkName), url.QueryEscape(vdc.Vdc.ID)),
"filterEncoded": "true",
})
if err != nil {
return "", fmt.Errorf("[findEdgeGatewayConnection] error returning the list of networks for VDC: %s", err)
Expand Down
17 changes: 15 additions & 2 deletions govcd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ func (vdc *Vdc) Query(params map[string]string) (Results, error) {

// QueryWithNotEncodedParams uses Query API to search for requested data
func (client *Client) QueryWithNotEncodedParams(params map[string]string, notEncodedParams map[string]string) (Results, error) {
return client.QueryWithNotEncodedParamsWithApiVersion(params, notEncodedParams, client.APIVersion)
}

// QueryWithNotEncodedParams uses Query API to search for requested data
func (client *Client) QueryWithNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, apiVersion string) (Results, error) {
queryUlr := client.VCDHREF
queryUlr.Path += "/query"

req := client.NewRequestWitNotEncodedParams(params, notEncodedParams, http.MethodGet, queryUlr, nil)
req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+client.APIVersion)
req := client.NewRequestWitNotEncodedParamsWithApiVersion(params, notEncodedParams, http.MethodGet, queryUlr, nil, apiVersion)
req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+apiVersion)

return getResult(client, req)
}
Expand All @@ -59,6 +64,14 @@ func (vdc *Vdc) QueryWithNotEncodedParams(params map[string]string, notEncodedPa
return vdc.client.QueryWithNotEncodedParams(params, notEncodedParams)
}

func (vcdCli *VCDClient) QueryWithNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, apiVersion string) (Results, error) {
return vcdCli.Client.QueryWithNotEncodedParamsWithApiVersion(params, notEncodedParams, apiVersion)
}

func (vdc *Vdc) QueryWithNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, apiVersion string) (Results, error) {
return vdc.client.QueryWithNotEncodedParamsWithApiVersion(params, notEncodedParams, apiVersion)
}

func getResult(client *Client, request *http.Request) (Results, error) {
resp, err := checkResp(client.Http.Do(request))
if err != nil {
Expand Down
29 changes: 17 additions & 12 deletions govcd/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,9 @@ func QueryDistributedPortGroup(vcdCli *VCDClient, name string) ([]*types.PortGro
// Find a list of Port groups matching the filter parameter.
func QueryPortGroups(vcdCli *VCDClient, filter string) ([]*types.PortGroupRecordType, error) {
results, err := vcdCli.QueryWithNotEncodedParams(nil, map[string]string{
"type": "portgroup",
"filter": filter,
"type": "portgroup",
"filter": filter,
"filterEncoded": "true",
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -695,8 +696,9 @@ func GetStorageProfileByHref(vcdClient *VCDClient, url string) (*types.VdcStorag
// QueryProviderVdcStorageProfileByName finds a provider VDC storage profile by name
func QueryProviderVdcStorageProfileByName(vcdCli *VCDClient, name string) ([]*types.QueryResultProviderVdcStorageProfileRecordType, error) {
results, err := vcdCli.QueryWithNotEncodedParams(nil, map[string]string{
"type": "providerVdcStorageProfile",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"type": "providerVdcStorageProfile",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"filterEncoded": "true",
})
if err != nil {
return nil, err
Expand All @@ -708,8 +710,9 @@ func QueryProviderVdcStorageProfileByName(vcdCli *VCDClient, name string) ([]*ty
// QueryNetworkPoolByName finds a network pool by name
func QueryNetworkPoolByName(vcdCli *VCDClient, name string) ([]*types.QueryResultNetworkPoolRecordType, error) {
results, err := vcdCli.QueryWithNotEncodedParams(nil, map[string]string{
"type": "networkPool",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"type": "networkPool",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"filterEncoded": "true",
})
if err != nil {
return nil, err
Expand All @@ -720,10 +723,11 @@ func QueryNetworkPoolByName(vcdCli *VCDClient, name string) ([]*types.QueryResul

// QueryProviderVdcByName finds a provider VDC by name
func QueryProviderVdcByName(vcdCli *VCDClient, name string) ([]*types.QueryResultVMWProviderVdcRecordType, error) {
results, err := vcdCli.QueryWithNotEncodedParams(nil, map[string]string{
"type": "providerVdc",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
})
results, err := vcdCli.QueryWithNotEncodedParamsWithApiVersion(nil, map[string]string{
"type": "providerVdc",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"filterEncoded": "true",
}, vcdCli.Client.GetSpecificApiVersionOnCondition(">= 31.0", "31.0"))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -784,8 +788,9 @@ func GetNetworkPoolByHREF(client *VCDClient, href string) (*types.VMWNetworkPool
// QueryOrgVdcNetworkByName finds a org VDC network by name which has edge gateway as reference
func QueryOrgVdcNetworkByName(vcdCli *VCDClient, name string) ([]*types.QueryResultOrgVdcNetworkRecordType, error) {
results, err := vcdCli.QueryWithNotEncodedParams(nil, map[string]string{
"type": "orgVdcNetwork",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"type": "orgVdcNetwork",
"filter": fmt.Sprintf("name==%s", url.QueryEscape(name)),
"filterEncoded": "true",
})
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion govcd/vdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ func (vdc *Vdc) QueryVM(vappName, vmName string) (VMRecord, error) {
}

results, err := vdc.QueryWithNotEncodedParams(nil, map[string]string{"type": typeMedia,
"filter": "name==" + url.QueryEscape(vmName) + ";containerName==" + url.QueryEscape(vappName)})
"filter": "name==" + url.QueryEscape(vmName) + ";containerName==" + url.QueryEscape(vappName),
"filterEncoded": "true"})
if err != nil {
return VMRecord{}, fmt.Errorf("error querying vm %s", err)
}
Expand Down