1package mysql
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// TopQueryStatisticsClient is the the Microsoft Azure management API provides create, read, update, and delete
30// functionality for Azure MySQL resources including servers, databases, firewall rules, VNET rules, log files and
31// configurations with new business model.
32type TopQueryStatisticsClient struct {
33	BaseClient
34}
35
36// NewTopQueryStatisticsClient creates an instance of the TopQueryStatisticsClient client.
37func NewTopQueryStatisticsClient(subscriptionID string) TopQueryStatisticsClient {
38	return NewTopQueryStatisticsClientWithBaseURI(DefaultBaseURI, subscriptionID)
39}
40
41// NewTopQueryStatisticsClientWithBaseURI creates an instance of the TopQueryStatisticsClient client using a custom
42// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
43// stack).
44func NewTopQueryStatisticsClientWithBaseURI(baseURI string, subscriptionID string) TopQueryStatisticsClient {
45	return TopQueryStatisticsClient{NewWithBaseURI(baseURI, subscriptionID)}
46}
47
48// Get retrieve the query statistic for specified identifier.
49// Parameters:
50// resourceGroupName - the name of the resource group. The name is case insensitive.
51// serverName - the name of the server.
52// queryStatisticID - the Query Statistic identifier.
53func (client TopQueryStatisticsClient) Get(ctx context.Context, resourceGroupName string, serverName string, queryStatisticID string) (result QueryStatistic, err error) {
54	if tracing.IsEnabled() {
55		ctx = tracing.StartSpan(ctx, fqdn+"/TopQueryStatisticsClient.Get")
56		defer func() {
57			sc := -1
58			if result.Response.Response != nil {
59				sc = result.Response.Response.StatusCode
60			}
61			tracing.EndSpan(ctx, sc, err)
62		}()
63	}
64	if err := validation.Validate([]validation.Validation{
65		{TargetValue: client.SubscriptionID,
66			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
67		{TargetValue: resourceGroupName,
68			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
69				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
70				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
71		return result, validation.NewError("mysql.TopQueryStatisticsClient", "Get", err.Error())
72	}
73
74	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, queryStatisticID)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "Get", nil, "Failure preparing request")
77		return
78	}
79
80	resp, err := client.GetSender(req)
81	if err != nil {
82		result.Response = autorest.Response{Response: resp}
83		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "Get", resp, "Failure sending request")
84		return
85	}
86
87	result, err = client.GetResponder(resp)
88	if err != nil {
89		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "Get", resp, "Failure responding to request")
90		return
91	}
92
93	return
94}
95
96// GetPreparer prepares the Get request.
97func (client TopQueryStatisticsClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, queryStatisticID string) (*http.Request, error) {
98	pathParameters := map[string]interface{}{
99		"queryStatisticId":  autorest.Encode("path", queryStatisticID),
100		"resourceGroupName": autorest.Encode("path", resourceGroupName),
101		"serverName":        autorest.Encode("path", serverName),
102		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
103	}
104
105	const APIVersion = "2018-06-01"
106	queryParameters := map[string]interface{}{
107		"api-version": APIVersion,
108	}
109
110	preparer := autorest.CreatePreparer(
111		autorest.AsGet(),
112		autorest.WithBaseURL(client.BaseURI),
113		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMySQL/servers/{serverName}/topQueryStatistics/{queryStatisticId}", pathParameters),
114		autorest.WithQueryParameters(queryParameters))
115	return preparer.Prepare((&http.Request{}).WithContext(ctx))
116}
117
118// GetSender sends the Get request. The method will close the
119// http.Response Body if it receives an error.
120func (client TopQueryStatisticsClient) GetSender(req *http.Request) (*http.Response, error) {
121	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
122}
123
124// GetResponder handles the response to the Get request. The method always
125// closes the http.Response Body.
126func (client TopQueryStatisticsClient) GetResponder(resp *http.Response) (result QueryStatistic, err error) {
127	err = autorest.Respond(
128		resp,
129		azure.WithErrorUnlessStatusCode(http.StatusOK),
130		autorest.ByUnmarshallingJSON(&result),
131		autorest.ByClosing())
132	result.Response = autorest.Response{Response: resp}
133	return
134}
135
136// ListByServer retrieve the Query-Store top queries for specified metric and aggregation.
137// Parameters:
138// resourceGroupName - the name of the resource group. The name is case insensitive.
139// serverName - the name of the server.
140// parameters - the required parameters for retrieving top query statistics.
141func (client TopQueryStatisticsClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string, parameters TopQueryStatisticsInput) (result TopQueryStatisticsResultListPage, err error) {
142	if tracing.IsEnabled() {
143		ctx = tracing.StartSpan(ctx, fqdn+"/TopQueryStatisticsClient.ListByServer")
144		defer func() {
145			sc := -1
146			if result.tqsrl.Response.Response != nil {
147				sc = result.tqsrl.Response.Response.StatusCode
148			}
149			tracing.EndSpan(ctx, sc, err)
150		}()
151	}
152	if err := validation.Validate([]validation.Validation{
153		{TargetValue: client.SubscriptionID,
154			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
155		{TargetValue: resourceGroupName,
156			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
157				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
158				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
159		{TargetValue: parameters,
160			Constraints: []validation.Constraint{{Target: "parameters.TopQueryStatisticsInputProperties", Name: validation.Null, Rule: true,
161				Chain: []validation.Constraint{{Target: "parameters.TopQueryStatisticsInputProperties.NumberOfTopQueries", Name: validation.Null, Rule: true, Chain: nil},
162					{Target: "parameters.TopQueryStatisticsInputProperties.AggregationFunction", Name: validation.Null, Rule: true, Chain: nil},
163					{Target: "parameters.TopQueryStatisticsInputProperties.ObservedMetric", Name: validation.Null, Rule: true, Chain: nil},
164					{Target: "parameters.TopQueryStatisticsInputProperties.ObservationStartTime", Name: validation.Null, Rule: true, Chain: nil},
165					{Target: "parameters.TopQueryStatisticsInputProperties.ObservationEndTime", Name: validation.Null, Rule: true, Chain: nil},
166					{Target: "parameters.TopQueryStatisticsInputProperties.AggregationWindow", Name: validation.Null, Rule: true, Chain: nil},
167				}}}}}); err != nil {
168		return result, validation.NewError("mysql.TopQueryStatisticsClient", "ListByServer", err.Error())
169	}
170
171	result.fn = client.listByServerNextResults
172	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName, parameters)
173	if err != nil {
174		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "ListByServer", nil, "Failure preparing request")
175		return
176	}
177
178	resp, err := client.ListByServerSender(req)
179	if err != nil {
180		result.tqsrl.Response = autorest.Response{Response: resp}
181		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "ListByServer", resp, "Failure sending request")
182		return
183	}
184
185	result.tqsrl, err = client.ListByServerResponder(resp)
186	if err != nil {
187		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "ListByServer", resp, "Failure responding to request")
188		return
189	}
190	if result.tqsrl.hasNextLink() && result.tqsrl.IsEmpty() {
191		err = result.NextWithContext(ctx)
192		return
193	}
194
195	return
196}
197
198// ListByServerPreparer prepares the ListByServer request.
199func (client TopQueryStatisticsClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string, parameters TopQueryStatisticsInput) (*http.Request, error) {
200	pathParameters := map[string]interface{}{
201		"resourceGroupName": autorest.Encode("path", resourceGroupName),
202		"serverName":        autorest.Encode("path", serverName),
203		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
204	}
205
206	const APIVersion = "2018-06-01"
207	queryParameters := map[string]interface{}{
208		"api-version": APIVersion,
209	}
210
211	preparer := autorest.CreatePreparer(
212		autorest.AsContentType("application/json; charset=utf-8"),
213		autorest.AsGet(),
214		autorest.WithBaseURL(client.BaseURI),
215		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMySQL/servers/{serverName}/topQueryStatistics", pathParameters),
216		autorest.WithJSON(parameters),
217		autorest.WithQueryParameters(queryParameters))
218	return preparer.Prepare((&http.Request{}).WithContext(ctx))
219}
220
221// ListByServerSender sends the ListByServer request. The method will close the
222// http.Response Body if it receives an error.
223func (client TopQueryStatisticsClient) ListByServerSender(req *http.Request) (*http.Response, error) {
224	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
225}
226
227// ListByServerResponder handles the response to the ListByServer request. The method always
228// closes the http.Response Body.
229func (client TopQueryStatisticsClient) ListByServerResponder(resp *http.Response) (result TopQueryStatisticsResultList, err error) {
230	err = autorest.Respond(
231		resp,
232		azure.WithErrorUnlessStatusCode(http.StatusOK),
233		autorest.ByUnmarshallingJSON(&result),
234		autorest.ByClosing())
235	result.Response = autorest.Response{Response: resp}
236	return
237}
238
239// listByServerNextResults retrieves the next set of results, if any.
240func (client TopQueryStatisticsClient) listByServerNextResults(ctx context.Context, lastResults TopQueryStatisticsResultList) (result TopQueryStatisticsResultList, err error) {
241	req, err := lastResults.topQueryStatisticsResultListPreparer(ctx)
242	if err != nil {
243		return result, autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "listByServerNextResults", nil, "Failure preparing next results request")
244	}
245	if req == nil {
246		return
247	}
248	resp, err := client.ListByServerSender(req)
249	if err != nil {
250		result.Response = autorest.Response{Response: resp}
251		return result, autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "listByServerNextResults", resp, "Failure sending next results request")
252	}
253	result, err = client.ListByServerResponder(resp)
254	if err != nil {
255		err = autorest.NewErrorWithError(err, "mysql.TopQueryStatisticsClient", "listByServerNextResults", resp, "Failure responding to next results request")
256	}
257	return
258}
259
260// ListByServerComplete enumerates all values, automatically crossing page boundaries as required.
261func (client TopQueryStatisticsClient) ListByServerComplete(ctx context.Context, resourceGroupName string, serverName string, parameters TopQueryStatisticsInput) (result TopQueryStatisticsResultListIterator, err error) {
262	if tracing.IsEnabled() {
263		ctx = tracing.StartSpan(ctx, fqdn+"/TopQueryStatisticsClient.ListByServer")
264		defer func() {
265			sc := -1
266			if result.Response().Response.Response != nil {
267				sc = result.page.Response().Response.Response.StatusCode
268			}
269			tracing.EndSpan(ctx, sc, err)
270		}()
271	}
272	result.page, err = client.ListByServer(ctx, resourceGroupName, serverName, parameters)
273	return
274}
275