github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/query.go (about)

     1  /*
     2   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    12  )
    13  
    14  type Results struct {
    15  	Results *types.QueryResultRecordsType
    16  	client  *Client
    17  }
    18  
    19  func NewResults(cli *Client) *Results {
    20  	return &Results{
    21  		Results: new(types.QueryResultRecordsType),
    22  		client:  cli,
    23  	}
    24  }
    25  
    26  func (vcdClient *VCDClient) Query(params map[string]string) (Results, error) {
    27  
    28  	req := vcdClient.Client.NewRequest(params, http.MethodGet, vcdClient.QueryHREF, nil)
    29  	req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+vcdClient.Client.APIVersion)
    30  
    31  	return getResult(&vcdClient.Client, req)
    32  }
    33  
    34  func (vdc *Vdc) Query(params map[string]string) (Results, error) {
    35  	queryUrl := vdc.client.VCDHREF
    36  	queryUrl.Path += "/query"
    37  	req := vdc.client.NewRequest(params, http.MethodGet, queryUrl, nil)
    38  	req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+vdc.client.APIVersion)
    39  
    40  	return getResult(vdc.client, req)
    41  }
    42  
    43  // QueryWithNotEncodedParams uses Query API to search for requested data
    44  func (client *Client) QueryWithNotEncodedParams(params map[string]string, notEncodedParams map[string]string) (Results, error) {
    45  	return client.QueryWithNotEncodedParamsWithApiVersion(params, notEncodedParams, client.APIVersion)
    46  }
    47  
    48  func (client *Client) QueryWithNotEncodedParamsWithHeaders(params map[string]string, notEncodedParams map[string]string, headers map[string]string) (Results, error) {
    49  	return client.QueryWithNotEncodedParamsWithApiVersionWithHeaders(params, notEncodedParams, client.APIVersion, headers)
    50  }
    51  
    52  // QueryWithNotEncodedParams uses Query API to search for requested data
    53  func (client *Client) QueryWithNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, apiVersion string) (Results, error) {
    54  	return client.QueryWithNotEncodedParamsWithApiVersionWithHeaders(params, notEncodedParams, apiVersion, nil)
    55  }
    56  
    57  func (client *Client) QueryWithNotEncodedParamsWithApiVersionWithHeaders(params map[string]string, notEncodedParams map[string]string, apiVersion string, headers map[string]string) (Results, error) {
    58  	queryUrl := client.VCDHREF
    59  	queryUrl.Path += "/query"
    60  
    61  	req := client.NewRequestWitNotEncodedParamsWithApiVersion(params, notEncodedParams, http.MethodGet, queryUrl, nil, apiVersion)
    62  	req.Header.Add("Accept", "vnd.vmware.vcloud.org+xml;version="+apiVersion)
    63  
    64  	for k, v := range headers {
    65  		req.Header.Add(k, v)
    66  	}
    67  
    68  	return getResult(client, req)
    69  }
    70  
    71  func (vcdClient *VCDClient) QueryWithNotEncodedParams(params map[string]string, notEncodedParams map[string]string) (Results, error) {
    72  	return vcdClient.Client.QueryWithNotEncodedParams(params, notEncodedParams)
    73  }
    74  
    75  func (vdc *Vdc) QueryWithNotEncodedParams(params map[string]string, notEncodedParams map[string]string) (Results, error) {
    76  	return vdc.client.QueryWithNotEncodedParams(params, notEncodedParams)
    77  }
    78  
    79  func (vcdClient *VCDClient) QueryWithNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, apiVersion string) (Results, error) {
    80  	return vcdClient.Client.QueryWithNotEncodedParamsWithApiVersion(params, notEncodedParams, apiVersion)
    81  }
    82  
    83  func (vdc *Vdc) QueryWithNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, apiVersion string) (Results, error) {
    84  	return vdc.client.QueryWithNotEncodedParamsWithApiVersion(params, notEncodedParams, apiVersion)
    85  }
    86  
    87  func getResult(client *Client, request *http.Request) (Results, error) {
    88  	resp, err := checkResp(client.Http.Do(request))
    89  	if err != nil {
    90  		return Results{}, fmt.Errorf("error retrieving query: %s", err)
    91  	}
    92  
    93  	results := NewResults(client)
    94  
    95  	if err = decodeBody(types.BodyTypeXML, resp, results.Results); err != nil {
    96  		return Results{}, fmt.Errorf("error decoding query results: %s", err)
    97  	}
    98  
    99  	return *results, nil
   100  }