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/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// ElasticPoolsClient is the the Azure SQL Database management API provides a RESTful set of web services that interact
19// with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update, and
20// delete databases.
21type ElasticPoolsClient struct {
22	BaseClient
23}
24
25// NewElasticPoolsClient creates an instance of the ElasticPoolsClient client.
26func NewElasticPoolsClient(subscriptionID string) ElasticPoolsClient {
27	return NewElasticPoolsClientWithBaseURI(DefaultBaseURI, subscriptionID)
28}
29
30// NewElasticPoolsClientWithBaseURI creates an instance of the ElasticPoolsClient client using a custom endpoint.  Use
31// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
32func NewElasticPoolsClientWithBaseURI(baseURI string, subscriptionID string) ElasticPoolsClient {
33	return ElasticPoolsClient{NewWithBaseURI(baseURI, subscriptionID)}
34}
35
36// CreateOrUpdate creates or updates an elastic pool.
37// Parameters:
38// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
39// from the Azure Resource Manager API or the portal.
40// serverName - the name of the server.
41// elasticPoolName - the name of the elastic pool.
42// parameters - the elastic pool parameters.
43func (client ElasticPoolsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string, parameters ElasticPool) (result ElasticPoolsCreateOrUpdateFuture, err error) {
44	if tracing.IsEnabled() {
45		ctx = tracing.StartSpan(ctx, fqdn+"/ElasticPoolsClient.CreateOrUpdate")
46		defer func() {
47			sc := -1
48			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
49				sc = result.FutureAPI.Response().StatusCode
50			}
51			tracing.EndSpan(ctx, sc, err)
52		}()
53	}
54	if err := validation.Validate([]validation.Validation{
55		{TargetValue: parameters,
56			Constraints: []validation.Constraint{{Target: "parameters.Sku", Name: validation.Null, Rule: false,
57				Chain: []validation.Constraint{{Target: "parameters.Sku.Name", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
58		return result, validation.NewError("sql.ElasticPoolsClient", "CreateOrUpdate", err.Error())
59	}
60
61	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, elasticPoolName, parameters)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "CreateOrUpdate", nil, "Failure preparing request")
64		return
65	}
66
67	result, err = client.CreateOrUpdateSender(req)
68	if err != nil {
69		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "CreateOrUpdate", nil, "Failure sending request")
70		return
71	}
72
73	return
74}
75
76// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
77func (client ElasticPoolsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string, parameters ElasticPool) (*http.Request, error) {
78	pathParameters := map[string]interface{}{
79		"elasticPoolName":   autorest.Encode("path", elasticPoolName),
80		"resourceGroupName": autorest.Encode("path", resourceGroupName),
81		"serverName":        autorest.Encode("path", serverName),
82		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
83	}
84
85	const APIVersion = "2017-10-01-preview"
86	queryParameters := map[string]interface{}{
87		"api-version": APIVersion,
88	}
89
90	parameters.Kind = nil
91	preparer := autorest.CreatePreparer(
92		autorest.AsContentType("application/json; charset=utf-8"),
93		autorest.AsPut(),
94		autorest.WithBaseURL(client.BaseURI),
95		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/elasticPools/{elasticPoolName}", pathParameters),
96		autorest.WithJSON(parameters),
97		autorest.WithQueryParameters(queryParameters))
98	return preparer.Prepare((&http.Request{}).WithContext(ctx))
99}
100
101// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
102// http.Response Body if it receives an error.
103func (client ElasticPoolsClient) CreateOrUpdateSender(req *http.Request) (future ElasticPoolsCreateOrUpdateFuture, err error) {
104	var resp *http.Response
105	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
106	if err != nil {
107		return
108	}
109	var azf azure.Future
110	azf, err = azure.NewFutureFromResponse(resp)
111	future.FutureAPI = &azf
112	future.Result = future.result
113	return
114}
115
116// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
117// closes the http.Response Body.
118func (client ElasticPoolsClient) CreateOrUpdateResponder(resp *http.Response) (result ElasticPool, err error) {
119	err = autorest.Respond(
120		resp,
121		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
122		autorest.ByUnmarshallingJSON(&result),
123		autorest.ByClosing())
124	result.Response = autorest.Response{Response: resp}
125	return
126}
127
128// Delete deletes an elastic pool.
129// Parameters:
130// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
131// from the Azure Resource Manager API or the portal.
132// serverName - the name of the server.
133// elasticPoolName - the name of the elastic pool.
134func (client ElasticPoolsClient) Delete(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string) (result ElasticPoolsDeleteFuture, err error) {
135	if tracing.IsEnabled() {
136		ctx = tracing.StartSpan(ctx, fqdn+"/ElasticPoolsClient.Delete")
137		defer func() {
138			sc := -1
139			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
140				sc = result.FutureAPI.Response().StatusCode
141			}
142			tracing.EndSpan(ctx, sc, err)
143		}()
144	}
145	req, err := client.DeletePreparer(ctx, resourceGroupName, serverName, elasticPoolName)
146	if err != nil {
147		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Delete", nil, "Failure preparing request")
148		return
149	}
150
151	result, err = client.DeleteSender(req)
152	if err != nil {
153		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Delete", nil, "Failure sending request")
154		return
155	}
156
157	return
158}
159
160// DeletePreparer prepares the Delete request.
161func (client ElasticPoolsClient) DeletePreparer(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string) (*http.Request, error) {
162	pathParameters := map[string]interface{}{
163		"elasticPoolName":   autorest.Encode("path", elasticPoolName),
164		"resourceGroupName": autorest.Encode("path", resourceGroupName),
165		"serverName":        autorest.Encode("path", serverName),
166		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
167	}
168
169	const APIVersion = "2017-10-01-preview"
170	queryParameters := map[string]interface{}{
171		"api-version": APIVersion,
172	}
173
174	preparer := autorest.CreatePreparer(
175		autorest.AsDelete(),
176		autorest.WithBaseURL(client.BaseURI),
177		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/elasticPools/{elasticPoolName}", pathParameters),
178		autorest.WithQueryParameters(queryParameters))
179	return preparer.Prepare((&http.Request{}).WithContext(ctx))
180}
181
182// DeleteSender sends the Delete request. The method will close the
183// http.Response Body if it receives an error.
184func (client ElasticPoolsClient) DeleteSender(req *http.Request) (future ElasticPoolsDeleteFuture, err error) {
185	var resp *http.Response
186	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
187	if err != nil {
188		return
189	}
190	var azf azure.Future
191	azf, err = azure.NewFutureFromResponse(resp)
192	future.FutureAPI = &azf
193	future.Result = future.result
194	return
195}
196
197// DeleteResponder handles the response to the Delete request. The method always
198// closes the http.Response Body.
199func (client ElasticPoolsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
200	err = autorest.Respond(
201		resp,
202		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
203		autorest.ByClosing())
204	result.Response = resp
205	return
206}
207
208// Get gets an elastic pool.
209// Parameters:
210// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
211// from the Azure Resource Manager API or the portal.
212// serverName - the name of the server.
213// elasticPoolName - the name of the elastic pool.
214func (client ElasticPoolsClient) Get(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string) (result ElasticPool, err error) {
215	if tracing.IsEnabled() {
216		ctx = tracing.StartSpan(ctx, fqdn+"/ElasticPoolsClient.Get")
217		defer func() {
218			sc := -1
219			if result.Response.Response != nil {
220				sc = result.Response.Response.StatusCode
221			}
222			tracing.EndSpan(ctx, sc, err)
223		}()
224	}
225	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, elasticPoolName)
226	if err != nil {
227		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Get", nil, "Failure preparing request")
228		return
229	}
230
231	resp, err := client.GetSender(req)
232	if err != nil {
233		result.Response = autorest.Response{Response: resp}
234		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Get", resp, "Failure sending request")
235		return
236	}
237
238	result, err = client.GetResponder(resp)
239	if err != nil {
240		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Get", resp, "Failure responding to request")
241		return
242	}
243
244	return
245}
246
247// GetPreparer prepares the Get request.
248func (client ElasticPoolsClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string) (*http.Request, error) {
249	pathParameters := map[string]interface{}{
250		"elasticPoolName":   autorest.Encode("path", elasticPoolName),
251		"resourceGroupName": autorest.Encode("path", resourceGroupName),
252		"serverName":        autorest.Encode("path", serverName),
253		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
254	}
255
256	const APIVersion = "2017-10-01-preview"
257	queryParameters := map[string]interface{}{
258		"api-version": APIVersion,
259	}
260
261	preparer := autorest.CreatePreparer(
262		autorest.AsGet(),
263		autorest.WithBaseURL(client.BaseURI),
264		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/elasticPools/{elasticPoolName}", pathParameters),
265		autorest.WithQueryParameters(queryParameters))
266	return preparer.Prepare((&http.Request{}).WithContext(ctx))
267}
268
269// GetSender sends the Get request. The method will close the
270// http.Response Body if it receives an error.
271func (client ElasticPoolsClient) GetSender(req *http.Request) (*http.Response, error) {
272	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
273}
274
275// GetResponder handles the response to the Get request. The method always
276// closes the http.Response Body.
277func (client ElasticPoolsClient) GetResponder(resp *http.Response) (result ElasticPool, err error) {
278	err = autorest.Respond(
279		resp,
280		azure.WithErrorUnlessStatusCode(http.StatusOK),
281		autorest.ByUnmarshallingJSON(&result),
282		autorest.ByClosing())
283	result.Response = autorest.Response{Response: resp}
284	return
285}
286
287// ListByServer gets all elastic pools in a server.
288// Parameters:
289// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
290// from the Azure Resource Manager API or the portal.
291// serverName - the name of the server.
292// skip - the number of elements in the collection to skip.
293func (client ElasticPoolsClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string, skip *int32) (result ElasticPoolListResultPage, err error) {
294	if tracing.IsEnabled() {
295		ctx = tracing.StartSpan(ctx, fqdn+"/ElasticPoolsClient.ListByServer")
296		defer func() {
297			sc := -1
298			if result.eplr.Response.Response != nil {
299				sc = result.eplr.Response.Response.StatusCode
300			}
301			tracing.EndSpan(ctx, sc, err)
302		}()
303	}
304	result.fn = client.listByServerNextResults
305	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName, skip)
306	if err != nil {
307		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "ListByServer", nil, "Failure preparing request")
308		return
309	}
310
311	resp, err := client.ListByServerSender(req)
312	if err != nil {
313		result.eplr.Response = autorest.Response{Response: resp}
314		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "ListByServer", resp, "Failure sending request")
315		return
316	}
317
318	result.eplr, err = client.ListByServerResponder(resp)
319	if err != nil {
320		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "ListByServer", resp, "Failure responding to request")
321		return
322	}
323	if result.eplr.hasNextLink() && result.eplr.IsEmpty() {
324		err = result.NextWithContext(ctx)
325		return
326	}
327
328	return
329}
330
331// ListByServerPreparer prepares the ListByServer request.
332func (client ElasticPoolsClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string, skip *int32) (*http.Request, error) {
333	pathParameters := map[string]interface{}{
334		"resourceGroupName": autorest.Encode("path", resourceGroupName),
335		"serverName":        autorest.Encode("path", serverName),
336		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
337	}
338
339	const APIVersion = "2017-10-01-preview"
340	queryParameters := map[string]interface{}{
341		"api-version": APIVersion,
342	}
343	if skip != nil {
344		queryParameters["$skip"] = autorest.Encode("query", *skip)
345	}
346
347	preparer := autorest.CreatePreparer(
348		autorest.AsGet(),
349		autorest.WithBaseURL(client.BaseURI),
350		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/elasticPools", pathParameters),
351		autorest.WithQueryParameters(queryParameters))
352	return preparer.Prepare((&http.Request{}).WithContext(ctx))
353}
354
355// ListByServerSender sends the ListByServer request. The method will close the
356// http.Response Body if it receives an error.
357func (client ElasticPoolsClient) ListByServerSender(req *http.Request) (*http.Response, error) {
358	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
359}
360
361// ListByServerResponder handles the response to the ListByServer request. The method always
362// closes the http.Response Body.
363func (client ElasticPoolsClient) ListByServerResponder(resp *http.Response) (result ElasticPoolListResult, err error) {
364	err = autorest.Respond(
365		resp,
366		azure.WithErrorUnlessStatusCode(http.StatusOK),
367		autorest.ByUnmarshallingJSON(&result),
368		autorest.ByClosing())
369	result.Response = autorest.Response{Response: resp}
370	return
371}
372
373// listByServerNextResults retrieves the next set of results, if any.
374func (client ElasticPoolsClient) listByServerNextResults(ctx context.Context, lastResults ElasticPoolListResult) (result ElasticPoolListResult, err error) {
375	req, err := lastResults.elasticPoolListResultPreparer(ctx)
376	if err != nil {
377		return result, autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "listByServerNextResults", nil, "Failure preparing next results request")
378	}
379	if req == nil {
380		return
381	}
382	resp, err := client.ListByServerSender(req)
383	if err != nil {
384		result.Response = autorest.Response{Response: resp}
385		return result, autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "listByServerNextResults", resp, "Failure sending next results request")
386	}
387	result, err = client.ListByServerResponder(resp)
388	if err != nil {
389		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "listByServerNextResults", resp, "Failure responding to next results request")
390	}
391	return
392}
393
394// ListByServerComplete enumerates all values, automatically crossing page boundaries as required.
395func (client ElasticPoolsClient) ListByServerComplete(ctx context.Context, resourceGroupName string, serverName string, skip *int32) (result ElasticPoolListResultIterator, err error) {
396	if tracing.IsEnabled() {
397		ctx = tracing.StartSpan(ctx, fqdn+"/ElasticPoolsClient.ListByServer")
398		defer func() {
399			sc := -1
400			if result.Response().Response.Response != nil {
401				sc = result.page.Response().Response.Response.StatusCode
402			}
403			tracing.EndSpan(ctx, sc, err)
404		}()
405	}
406	result.page, err = client.ListByServer(ctx, resourceGroupName, serverName, skip)
407	return
408}
409
410// Update updates an elastic pool.
411// Parameters:
412// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
413// from the Azure Resource Manager API or the portal.
414// serverName - the name of the server.
415// elasticPoolName - the name of the elastic pool.
416// parameters - the elastic pool update parameters.
417func (client ElasticPoolsClient) Update(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string, parameters ElasticPoolUpdate) (result ElasticPoolsUpdateFuture, err error) {
418	if tracing.IsEnabled() {
419		ctx = tracing.StartSpan(ctx, fqdn+"/ElasticPoolsClient.Update")
420		defer func() {
421			sc := -1
422			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
423				sc = result.FutureAPI.Response().StatusCode
424			}
425			tracing.EndSpan(ctx, sc, err)
426		}()
427	}
428	req, err := client.UpdatePreparer(ctx, resourceGroupName, serverName, elasticPoolName, parameters)
429	if err != nil {
430		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Update", nil, "Failure preparing request")
431		return
432	}
433
434	result, err = client.UpdateSender(req)
435	if err != nil {
436		err = autorest.NewErrorWithError(err, "sql.ElasticPoolsClient", "Update", nil, "Failure sending request")
437		return
438	}
439
440	return
441}
442
443// UpdatePreparer prepares the Update request.
444func (client ElasticPoolsClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, elasticPoolName string, parameters ElasticPoolUpdate) (*http.Request, error) {
445	pathParameters := map[string]interface{}{
446		"elasticPoolName":   autorest.Encode("path", elasticPoolName),
447		"resourceGroupName": autorest.Encode("path", resourceGroupName),
448		"serverName":        autorest.Encode("path", serverName),
449		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
450	}
451
452	const APIVersion = "2017-10-01-preview"
453	queryParameters := map[string]interface{}{
454		"api-version": APIVersion,
455	}
456
457	preparer := autorest.CreatePreparer(
458		autorest.AsContentType("application/json; charset=utf-8"),
459		autorest.AsPatch(),
460		autorest.WithBaseURL(client.BaseURI),
461		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/elasticPools/{elasticPoolName}", pathParameters),
462		autorest.WithJSON(parameters),
463		autorest.WithQueryParameters(queryParameters))
464	return preparer.Prepare((&http.Request{}).WithContext(ctx))
465}
466
467// UpdateSender sends the Update request. The method will close the
468// http.Response Body if it receives an error.
469func (client ElasticPoolsClient) UpdateSender(req *http.Request) (future ElasticPoolsUpdateFuture, err error) {
470	var resp *http.Response
471	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
472	if err != nil {
473		return
474	}
475	var azf azure.Future
476	azf, err = azure.NewFutureFromResponse(resp)
477	future.FutureAPI = &azf
478	future.Result = future.result
479	return
480}
481
482// UpdateResponder handles the response to the Update request. The method always
483// closes the http.Response Body.
484func (client ElasticPoolsClient) UpdateResponder(resp *http.Response) (result ElasticPool, err error) {
485	err = autorest.Respond(
486		resp,
487		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
488		autorest.ByUnmarshallingJSON(&result),
489		autorest.ByClosing())
490	result.Response = autorest.Response{Response: resp}
491	return
492}
493