client.go raw

   1  //go:build go1.18
   2  // +build go1.18
   3  
   4  // Copyright (c) Microsoft Corporation. All rights reserved.
   5  // Licensed under the MIT License. See License.txt in the project root for license information.
   6  // Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
   7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
   8  
   9  package armresourcegraph
  10  
  11  import (
  12  	"context"
  13  	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
  14  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
  15  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
  16  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
  17  	"net/http"
  18  )
  19  
  20  // Client contains the methods for the ResourceGraphClient group.
  21  // Don't use this type directly, use NewClient() instead.
  22  type Client struct {
  23  	internal *arm.Client
  24  }
  25  
  26  // NewClient creates a new instance of Client with the specified values.
  27  //   - credential - used to authorize requests. Usually a credential from azidentity.
  28  //   - options - pass nil to accept the default values.
  29  func NewClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*Client, error) {
  30  	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
  31  	if err != nil {
  32  		return nil, err
  33  	}
  34  	client := &Client{
  35  		internal: cl,
  36  	}
  37  	return client, nil
  38  }
  39  
  40  // Resources - Queries the resources managed by Azure Resource Manager for scopes specified in the request.
  41  // If the operation fails it returns an *azcore.ResponseError type.
  42  //
  43  // Generated from API version 2021-06-01-preview
  44  //   - query - Request specifying query and its options.
  45  //   - options - ClientResourcesOptions contains the optional parameters for the Client.Resources method.
  46  func (client *Client) Resources(ctx context.Context, query QueryRequest, options *ClientResourcesOptions) (ClientResourcesResponse, error) {
  47  	var err error
  48  	const operationName = "Client.Resources"
  49  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
  50  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
  51  	defer func() { endSpan(err) }()
  52  	req, err := client.resourcesCreateRequest(ctx, query, options)
  53  	if err != nil {
  54  		return ClientResourcesResponse{}, err
  55  	}
  56  	httpResp, err := client.internal.Pipeline().Do(req)
  57  	if err != nil {
  58  		return ClientResourcesResponse{}, err
  59  	}
  60  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
  61  		err = runtime.NewResponseError(httpResp)
  62  		return ClientResourcesResponse{}, err
  63  	}
  64  	resp, err := client.resourcesHandleResponse(httpResp)
  65  	return resp, err
  66  }
  67  
  68  // resourcesCreateRequest creates the Resources request.
  69  func (client *Client) resourcesCreateRequest(ctx context.Context, query QueryRequest, options *ClientResourcesOptions) (*policy.Request, error) {
  70  	urlPath := "/providers/Microsoft.ResourceGraph/resources"
  71  	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
  72  	if err != nil {
  73  		return nil, err
  74  	}
  75  	reqQP := req.Raw().URL.Query()
  76  	reqQP.Set("api-version", "2021-06-01-preview")
  77  	req.Raw().URL.RawQuery = reqQP.Encode()
  78  	req.Raw().Header["Accept"] = []string{"application/json"}
  79  	if err := runtime.MarshalAsJSON(req, query); err != nil {
  80  		return nil, err
  81  	}
  82  	return req, nil
  83  }
  84  
  85  // resourcesHandleResponse handles the Resources response.
  86  func (client *Client) resourcesHandleResponse(resp *http.Response) (ClientResourcesResponse, error) {
  87  	result := ClientResourcesResponse{}
  88  	if err := runtime.UnmarshalAsJSON(resp, &result.QueryResponse); err != nil {
  89  		return ClientResourcesResponse{}, err
  90  	}
  91  	return result, nil
  92  }
  93  
  94  // ResourcesHistory - List all snapshots of a resource for a given time interval.
  95  // If the operation fails it returns an *azcore.ResponseError type.
  96  //
  97  // Generated from API version 2021-06-01-preview
  98  //   - request - Request specifying the query and its options.
  99  //   - options - ClientResourcesHistoryOptions contains the optional parameters for the Client.ResourcesHistory method.
 100  func (client *Client) ResourcesHistory(ctx context.Context, request ResourcesHistoryRequest, options *ClientResourcesHistoryOptions) (ClientResourcesHistoryResponse, error) {
 101  	var err error
 102  	const operationName = "Client.ResourcesHistory"
 103  	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
 104  	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
 105  	defer func() { endSpan(err) }()
 106  	req, err := client.resourcesHistoryCreateRequest(ctx, request, options)
 107  	if err != nil {
 108  		return ClientResourcesHistoryResponse{}, err
 109  	}
 110  	httpResp, err := client.internal.Pipeline().Do(req)
 111  	if err != nil {
 112  		return ClientResourcesHistoryResponse{}, err
 113  	}
 114  	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
 115  		err = runtime.NewResponseError(httpResp)
 116  		return ClientResourcesHistoryResponse{}, err
 117  	}
 118  	resp, err := client.resourcesHistoryHandleResponse(httpResp)
 119  	return resp, err
 120  }
 121  
 122  // resourcesHistoryCreateRequest creates the ResourcesHistory request.
 123  func (client *Client) resourcesHistoryCreateRequest(ctx context.Context, request ResourcesHistoryRequest, options *ClientResourcesHistoryOptions) (*policy.Request, error) {
 124  	urlPath := "/providers/Microsoft.ResourceGraph/resourcesHistory"
 125  	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
 126  	if err != nil {
 127  		return nil, err
 128  	}
 129  	reqQP := req.Raw().URL.Query()
 130  	reqQP.Set("api-version", "2021-06-01-preview")
 131  	req.Raw().URL.RawQuery = reqQP.Encode()
 132  	req.Raw().Header["Accept"] = []string{"application/json"}
 133  	if err := runtime.MarshalAsJSON(req, request); err != nil {
 134  		return nil, err
 135  	}
 136  	return req, nil
 137  }
 138  
 139  // resourcesHistoryHandleResponse handles the ResourcesHistory response.
 140  func (client *Client) resourcesHistoryHandleResponse(resp *http.Response) (ClientResourcesHistoryResponse, error) {
 141  	result := ClientResourcesHistoryResponse{}
 142  	if err := runtime.UnmarshalAsJSON(resp, &result.Interface); err != nil {
 143  		return ClientResourcesHistoryResponse{}, err
 144  	}
 145  	return result, nil
 146  }
 147