1package sql
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5//
6// Code generated by Microsoft (R) AutoRest Code Generator.
7// Changes may cause incorrect behavior and will be lost if the code is regenerated.
8
9import (
10	"context"
11	"github.com/Azure/go-autorest/autorest"
12	"github.com/Azure/go-autorest/autorest/azure"
13	"github.com/Azure/go-autorest/tracing"
14	"net/http"
15)
16
17// UsagesClient is the the Azure SQL Database management API provides a RESTful set of web services that interact with
18// Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update, and delete
19// databases.
20type UsagesClient struct {
21	BaseClient
22}
23
24// NewUsagesClient creates an instance of the UsagesClient client.
25func NewUsagesClient(subscriptionID string) UsagesClient {
26	return NewUsagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
27}
28
29// NewUsagesClientWithBaseURI creates an instance of the UsagesClient client using a custom endpoint.  Use this when
30// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
31func NewUsagesClientWithBaseURI(baseURI string, subscriptionID string) UsagesClient {
32	return UsagesClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// ListByInstancePool gets all instance pool usage metrics
36// Parameters:
37// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
38// from the Azure Resource Manager API or the portal.
39// instancePoolName - the name of the instance pool to be retrieved.
40// expandChildren - optional request parameter to include managed instance usages within the instance pool.
41func (client UsagesClient) ListByInstancePool(ctx context.Context, resourceGroupName string, instancePoolName string, expandChildren *bool) (result UsageListResultPage, err error) {
42	if tracing.IsEnabled() {
43		ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.ListByInstancePool")
44		defer func() {
45			sc := -1
46			if result.ulr.Response.Response != nil {
47				sc = result.ulr.Response.Response.StatusCode
48			}
49			tracing.EndSpan(ctx, sc, err)
50		}()
51	}
52	result.fn = client.listByInstancePoolNextResults
53	req, err := client.ListByInstancePoolPreparer(ctx, resourceGroupName, instancePoolName, expandChildren)
54	if err != nil {
55		err = autorest.NewErrorWithError(err, "sql.UsagesClient", "ListByInstancePool", nil, "Failure preparing request")
56		return
57	}
58
59	resp, err := client.ListByInstancePoolSender(req)
60	if err != nil {
61		result.ulr.Response = autorest.Response{Response: resp}
62		err = autorest.NewErrorWithError(err, "sql.UsagesClient", "ListByInstancePool", resp, "Failure sending request")
63		return
64	}
65
66	result.ulr, err = client.ListByInstancePoolResponder(resp)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "sql.UsagesClient", "ListByInstancePool", resp, "Failure responding to request")
69		return
70	}
71	if result.ulr.hasNextLink() && result.ulr.IsEmpty() {
72		err = result.NextWithContext(ctx)
73		return
74	}
75
76	return
77}
78
79// ListByInstancePoolPreparer prepares the ListByInstancePool request.
80func (client UsagesClient) ListByInstancePoolPreparer(ctx context.Context, resourceGroupName string, instancePoolName string, expandChildren *bool) (*http.Request, error) {
81	pathParameters := map[string]interface{}{
82		"instancePoolName":  autorest.Encode("path", instancePoolName),
83		"resourceGroupName": autorest.Encode("path", resourceGroupName),
84		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
85	}
86
87	const APIVersion = "2018-06-01-preview"
88	queryParameters := map[string]interface{}{
89		"api-version": APIVersion,
90	}
91	if expandChildren != nil {
92		queryParameters["expandChildren"] = autorest.Encode("query", *expandChildren)
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsGet(),
97		autorest.WithBaseURL(client.BaseURI),
98		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/instancePools/{instancePoolName}/usages", pathParameters),
99		autorest.WithQueryParameters(queryParameters))
100	return preparer.Prepare((&http.Request{}).WithContext(ctx))
101}
102
103// ListByInstancePoolSender sends the ListByInstancePool request. The method will close the
104// http.Response Body if it receives an error.
105func (client UsagesClient) ListByInstancePoolSender(req *http.Request) (*http.Response, error) {
106	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
107}
108
109// ListByInstancePoolResponder handles the response to the ListByInstancePool request. The method always
110// closes the http.Response Body.
111func (client UsagesClient) ListByInstancePoolResponder(resp *http.Response) (result UsageListResult, err error) {
112	err = autorest.Respond(
113		resp,
114		azure.WithErrorUnlessStatusCode(http.StatusOK),
115		autorest.ByUnmarshallingJSON(&result),
116		autorest.ByClosing())
117	result.Response = autorest.Response{Response: resp}
118	return
119}
120
121// listByInstancePoolNextResults retrieves the next set of results, if any.
122func (client UsagesClient) listByInstancePoolNextResults(ctx context.Context, lastResults UsageListResult) (result UsageListResult, err error) {
123	req, err := lastResults.usageListResultPreparer(ctx)
124	if err != nil {
125		return result, autorest.NewErrorWithError(err, "sql.UsagesClient", "listByInstancePoolNextResults", nil, "Failure preparing next results request")
126	}
127	if req == nil {
128		return
129	}
130	resp, err := client.ListByInstancePoolSender(req)
131	if err != nil {
132		result.Response = autorest.Response{Response: resp}
133		return result, autorest.NewErrorWithError(err, "sql.UsagesClient", "listByInstancePoolNextResults", resp, "Failure sending next results request")
134	}
135	result, err = client.ListByInstancePoolResponder(resp)
136	if err != nil {
137		err = autorest.NewErrorWithError(err, "sql.UsagesClient", "listByInstancePoolNextResults", resp, "Failure responding to next results request")
138	}
139	return
140}
141
142// ListByInstancePoolComplete enumerates all values, automatically crossing page boundaries as required.
143func (client UsagesClient) ListByInstancePoolComplete(ctx context.Context, resourceGroupName string, instancePoolName string, expandChildren *bool) (result UsageListResultIterator, err error) {
144	if tracing.IsEnabled() {
145		ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.ListByInstancePool")
146		defer func() {
147			sc := -1
148			if result.Response().Response.Response != nil {
149				sc = result.page.Response().Response.Response.StatusCode
150			}
151			tracing.EndSpan(ctx, sc, err)
152		}()
153	}
154	result.page, err = client.ListByInstancePool(ctx, resourceGroupName, instancePoolName, expandChildren)
155	return
156}
157