forked from vmware/go-vcloud-director
/
query.go
88 lines (66 loc) · 3.13 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcd
import (
"fmt"
"net/http"
"github.com/jlmorris3827/go-vcloud-director/v2/types/v56"
)
type Results struct {
Results *types.QueryResultRecordsType
client *Client
}
func NewResults(cli *Client) *Results {
return &Results{
Results: new(types.QueryResultRecordsType),
client: cli,
}
}
func (vcdCli *VCDClient) Query(params map[string]string) (Results, error) {
req := vcdCli.Client.NewRequest(params, http.MethodGet, vcdCli.QueryHREF, nil)
req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+vcdCli.Client.APIVersion)
return getResult(&vcdCli.Client, req)
}
func (vdc *Vdc) Query(params map[string]string) (Results, error) {
queryUrl := vdc.client.VCDHREF
queryUrl.Path += "/query"
req := vdc.client.NewRequest(params, http.MethodGet, queryUrl, nil)
req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+vdc.client.APIVersion)
return getResult(vdc.client, req)
}
// 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.NewRequestWitNotEncodedParamsWithApiVersion(params, notEncodedParams, http.MethodGet, queryUlr, nil, apiVersion)
req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+apiVersion)
return getResult(client, req)
}
func (vcdCli *VCDClient) QueryWithNotEncodedParams(params map[string]string, notEncodedParams map[string]string) (Results, error) {
return vcdCli.Client.QueryWithNotEncodedParams(params, notEncodedParams)
}
func (vdc *Vdc) QueryWithNotEncodedParams(params map[string]string, notEncodedParams map[string]string) (Results, error) {
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 {
return Results{}, fmt.Errorf("error retrieving query: %s", err)
}
results := NewResults(client)
if err = decodeBody(types.BodyTypeXML, resp, results.Results); err != nil {
return Results{}, fmt.Errorf("error decoding query results: %s", err)
}
return *results, nil
}