1package sql
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/tracing"
25	"net/http"
26)
27
28// ManagedInstanceKeysClient is the the Azure SQL Database management API provides a RESTful set of web services that
29// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
30// and delete databases.
31type ManagedInstanceKeysClient struct {
32	BaseClient
33}
34
35// NewManagedInstanceKeysClient creates an instance of the ManagedInstanceKeysClient client.
36func NewManagedInstanceKeysClient(subscriptionID string) ManagedInstanceKeysClient {
37	return NewManagedInstanceKeysClientWithBaseURI(DefaultBaseURI, subscriptionID)
38}
39
40// NewManagedInstanceKeysClientWithBaseURI creates an instance of the ManagedInstanceKeysClient client using a custom
41// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
42// stack).
43func NewManagedInstanceKeysClientWithBaseURI(baseURI string, subscriptionID string) ManagedInstanceKeysClient {
44	return ManagedInstanceKeysClient{NewWithBaseURI(baseURI, subscriptionID)}
45}
46
47// CreateOrUpdate creates or updates a managed instance key.
48// Parameters:
49// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
50// from the Azure Resource Manager API or the portal.
51// managedInstanceName - the name of the managed instance.
52// keyName - the name of the managed instance key to be operated on (updated or created).
53// parameters - the requested managed instance key resource state.
54func (client ManagedInstanceKeysClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, managedInstanceName string, keyName string, parameters ManagedInstanceKey) (result ManagedInstanceKeysCreateOrUpdateFuture, err error) {
55	if tracing.IsEnabled() {
56		ctx = tracing.StartSpan(ctx, fqdn+"/ManagedInstanceKeysClient.CreateOrUpdate")
57		defer func() {
58			sc := -1
59			if result.Response() != nil {
60				sc = result.Response().StatusCode
61			}
62			tracing.EndSpan(ctx, sc, err)
63		}()
64	}
65	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, managedInstanceName, keyName, parameters)
66	if err != nil {
67		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "CreateOrUpdate", nil, "Failure preparing request")
68		return
69	}
70
71	result, err = client.CreateOrUpdateSender(req)
72	if err != nil {
73		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "CreateOrUpdate", nil, "Failure sending request")
74		return
75	}
76
77	return
78}
79
80// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
81func (client ManagedInstanceKeysClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, managedInstanceName string, keyName string, parameters ManagedInstanceKey) (*http.Request, error) {
82	pathParameters := map[string]interface{}{
83		"keyName":             autorest.Encode("path", keyName),
84		"managedInstanceName": autorest.Encode("path", managedInstanceName),
85		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
86		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
87	}
88
89	const APIVersion = "2017-10-01-preview"
90	queryParameters := map[string]interface{}{
91		"api-version": APIVersion,
92	}
93
94	parameters.Kind = nil
95	preparer := autorest.CreatePreparer(
96		autorest.AsContentType("application/json; charset=utf-8"),
97		autorest.AsPut(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/managedInstances/{managedInstanceName}/keys/{keyName}", pathParameters),
100		autorest.WithJSON(parameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
106// http.Response Body if it receives an error.
107func (client ManagedInstanceKeysClient) CreateOrUpdateSender(req *http.Request) (future ManagedInstanceKeysCreateOrUpdateFuture, err error) {
108	var resp *http.Response
109	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
110	if err != nil {
111		return
112	}
113	var azf azure.Future
114	azf, err = azure.NewFutureFromResponse(resp)
115	future.FutureAPI = &azf
116	future.Result = func(client ManagedInstanceKeysClient) (mik ManagedInstanceKey, err error) {
117		var done bool
118		done, err = future.DoneWithContext(context.Background(), client)
119		if err != nil {
120			err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysCreateOrUpdateFuture", "Result", future.Response(), "Polling failure")
121			return
122		}
123		if !done {
124			err = azure.NewAsyncOpIncompleteError("sql.ManagedInstanceKeysCreateOrUpdateFuture")
125			return
126		}
127		sender := autorest.DecorateSender(client, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
128		if mik.Response.Response, err = future.GetResult(sender); err == nil && mik.Response.Response.StatusCode != http.StatusNoContent {
129			mik, err = client.CreateOrUpdateResponder(mik.Response.Response)
130			if err != nil {
131				err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysCreateOrUpdateFuture", "Result", mik.Response.Response, "Failure responding to request")
132			}
133		}
134		return
135	}
136	return
137}
138
139// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
140// closes the http.Response Body.
141func (client ManagedInstanceKeysClient) CreateOrUpdateResponder(resp *http.Response) (result ManagedInstanceKey, err error) {
142	err = autorest.Respond(
143		resp,
144		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
145		autorest.ByUnmarshallingJSON(&result),
146		autorest.ByClosing())
147	result.Response = autorest.Response{Response: resp}
148	return
149}
150
151// Delete deletes the managed instance key with the given name.
152// Parameters:
153// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
154// from the Azure Resource Manager API or the portal.
155// managedInstanceName - the name of the managed instance.
156// keyName - the name of the managed instance key to be deleted.
157func (client ManagedInstanceKeysClient) Delete(ctx context.Context, resourceGroupName string, managedInstanceName string, keyName string) (result ManagedInstanceKeysDeleteFuture, err error) {
158	if tracing.IsEnabled() {
159		ctx = tracing.StartSpan(ctx, fqdn+"/ManagedInstanceKeysClient.Delete")
160		defer func() {
161			sc := -1
162			if result.Response() != nil {
163				sc = result.Response().StatusCode
164			}
165			tracing.EndSpan(ctx, sc, err)
166		}()
167	}
168	req, err := client.DeletePreparer(ctx, resourceGroupName, managedInstanceName, keyName)
169	if err != nil {
170		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "Delete", nil, "Failure preparing request")
171		return
172	}
173
174	result, err = client.DeleteSender(req)
175	if err != nil {
176		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "Delete", nil, "Failure sending request")
177		return
178	}
179
180	return
181}
182
183// DeletePreparer prepares the Delete request.
184func (client ManagedInstanceKeysClient) DeletePreparer(ctx context.Context, resourceGroupName string, managedInstanceName string, keyName string) (*http.Request, error) {
185	pathParameters := map[string]interface{}{
186		"keyName":             autorest.Encode("path", keyName),
187		"managedInstanceName": autorest.Encode("path", managedInstanceName),
188		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
189		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
190	}
191
192	const APIVersion = "2017-10-01-preview"
193	queryParameters := map[string]interface{}{
194		"api-version": APIVersion,
195	}
196
197	preparer := autorest.CreatePreparer(
198		autorest.AsDelete(),
199		autorest.WithBaseURL(client.BaseURI),
200		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/managedInstances/{managedInstanceName}/keys/{keyName}", pathParameters),
201		autorest.WithQueryParameters(queryParameters))
202	return preparer.Prepare((&http.Request{}).WithContext(ctx))
203}
204
205// DeleteSender sends the Delete request. The method will close the
206// http.Response Body if it receives an error.
207func (client ManagedInstanceKeysClient) DeleteSender(req *http.Request) (future ManagedInstanceKeysDeleteFuture, err error) {
208	var resp *http.Response
209	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
210	if err != nil {
211		return
212	}
213	var azf azure.Future
214	azf, err = azure.NewFutureFromResponse(resp)
215	future.FutureAPI = &azf
216	future.Result = func(client ManagedInstanceKeysClient) (ar autorest.Response, err error) {
217		var done bool
218		done, err = future.DoneWithContext(context.Background(), client)
219		if err != nil {
220			err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysDeleteFuture", "Result", future.Response(), "Polling failure")
221			return
222		}
223		if !done {
224			err = azure.NewAsyncOpIncompleteError("sql.ManagedInstanceKeysDeleteFuture")
225			return
226		}
227		ar.Response = future.Response()
228		return
229	}
230	return
231}
232
233// DeleteResponder handles the response to the Delete request. The method always
234// closes the http.Response Body.
235func (client ManagedInstanceKeysClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
236	err = autorest.Respond(
237		resp,
238		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
239		autorest.ByClosing())
240	result.Response = resp
241	return
242}
243
244// Get gets a managed instance key.
245// Parameters:
246// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
247// from the Azure Resource Manager API or the portal.
248// managedInstanceName - the name of the managed instance.
249// keyName - the name of the managed instance key to be retrieved.
250func (client ManagedInstanceKeysClient) Get(ctx context.Context, resourceGroupName string, managedInstanceName string, keyName string) (result ManagedInstanceKey, err error) {
251	if tracing.IsEnabled() {
252		ctx = tracing.StartSpan(ctx, fqdn+"/ManagedInstanceKeysClient.Get")
253		defer func() {
254			sc := -1
255			if result.Response.Response != nil {
256				sc = result.Response.Response.StatusCode
257			}
258			tracing.EndSpan(ctx, sc, err)
259		}()
260	}
261	req, err := client.GetPreparer(ctx, resourceGroupName, managedInstanceName, keyName)
262	if err != nil {
263		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "Get", nil, "Failure preparing request")
264		return
265	}
266
267	resp, err := client.GetSender(req)
268	if err != nil {
269		result.Response = autorest.Response{Response: resp}
270		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "Get", resp, "Failure sending request")
271		return
272	}
273
274	result, err = client.GetResponder(resp)
275	if err != nil {
276		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "Get", resp, "Failure responding to request")
277		return
278	}
279
280	return
281}
282
283// GetPreparer prepares the Get request.
284func (client ManagedInstanceKeysClient) GetPreparer(ctx context.Context, resourceGroupName string, managedInstanceName string, keyName string) (*http.Request, error) {
285	pathParameters := map[string]interface{}{
286		"keyName":             autorest.Encode("path", keyName),
287		"managedInstanceName": autorest.Encode("path", managedInstanceName),
288		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
289		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
290	}
291
292	const APIVersion = "2017-10-01-preview"
293	queryParameters := map[string]interface{}{
294		"api-version": APIVersion,
295	}
296
297	preparer := autorest.CreatePreparer(
298		autorest.AsGet(),
299		autorest.WithBaseURL(client.BaseURI),
300		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/managedInstances/{managedInstanceName}/keys/{keyName}", pathParameters),
301		autorest.WithQueryParameters(queryParameters))
302	return preparer.Prepare((&http.Request{}).WithContext(ctx))
303}
304
305// GetSender sends the Get request. The method will close the
306// http.Response Body if it receives an error.
307func (client ManagedInstanceKeysClient) GetSender(req *http.Request) (*http.Response, error) {
308	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
309}
310
311// GetResponder handles the response to the Get request. The method always
312// closes the http.Response Body.
313func (client ManagedInstanceKeysClient) GetResponder(resp *http.Response) (result ManagedInstanceKey, err error) {
314	err = autorest.Respond(
315		resp,
316		azure.WithErrorUnlessStatusCode(http.StatusOK),
317		autorest.ByUnmarshallingJSON(&result),
318		autorest.ByClosing())
319	result.Response = autorest.Response{Response: resp}
320	return
321}
322
323// ListByInstance gets a list of managed instance keys.
324// Parameters:
325// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
326// from the Azure Resource Manager API or the portal.
327// managedInstanceName - the name of the managed instance.
328// filter - an OData filter expression that filters elements in the collection.
329func (client ManagedInstanceKeysClient) ListByInstance(ctx context.Context, resourceGroupName string, managedInstanceName string, filter string) (result ManagedInstanceKeyListResultPage, err error) {
330	if tracing.IsEnabled() {
331		ctx = tracing.StartSpan(ctx, fqdn+"/ManagedInstanceKeysClient.ListByInstance")
332		defer func() {
333			sc := -1
334			if result.miklr.Response.Response != nil {
335				sc = result.miklr.Response.Response.StatusCode
336			}
337			tracing.EndSpan(ctx, sc, err)
338		}()
339	}
340	result.fn = client.listByInstanceNextResults
341	req, err := client.ListByInstancePreparer(ctx, resourceGroupName, managedInstanceName, filter)
342	if err != nil {
343		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "ListByInstance", nil, "Failure preparing request")
344		return
345	}
346
347	resp, err := client.ListByInstanceSender(req)
348	if err != nil {
349		result.miklr.Response = autorest.Response{Response: resp}
350		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "ListByInstance", resp, "Failure sending request")
351		return
352	}
353
354	result.miklr, err = client.ListByInstanceResponder(resp)
355	if err != nil {
356		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "ListByInstance", resp, "Failure responding to request")
357		return
358	}
359	if result.miklr.hasNextLink() && result.miklr.IsEmpty() {
360		err = result.NextWithContext(ctx)
361		return
362	}
363
364	return
365}
366
367// ListByInstancePreparer prepares the ListByInstance request.
368func (client ManagedInstanceKeysClient) ListByInstancePreparer(ctx context.Context, resourceGroupName string, managedInstanceName string, filter string) (*http.Request, error) {
369	pathParameters := map[string]interface{}{
370		"managedInstanceName": autorest.Encode("path", managedInstanceName),
371		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
372		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
373	}
374
375	const APIVersion = "2017-10-01-preview"
376	queryParameters := map[string]interface{}{
377		"api-version": APIVersion,
378	}
379	if len(filter) > 0 {
380		queryParameters["$filter"] = autorest.Encode("query", filter)
381	}
382
383	preparer := autorest.CreatePreparer(
384		autorest.AsGet(),
385		autorest.WithBaseURL(client.BaseURI),
386		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/managedInstances/{managedInstanceName}/keys", pathParameters),
387		autorest.WithQueryParameters(queryParameters))
388	return preparer.Prepare((&http.Request{}).WithContext(ctx))
389}
390
391// ListByInstanceSender sends the ListByInstance request. The method will close the
392// http.Response Body if it receives an error.
393func (client ManagedInstanceKeysClient) ListByInstanceSender(req *http.Request) (*http.Response, error) {
394	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
395}
396
397// ListByInstanceResponder handles the response to the ListByInstance request. The method always
398// closes the http.Response Body.
399func (client ManagedInstanceKeysClient) ListByInstanceResponder(resp *http.Response) (result ManagedInstanceKeyListResult, err error) {
400	err = autorest.Respond(
401		resp,
402		azure.WithErrorUnlessStatusCode(http.StatusOK),
403		autorest.ByUnmarshallingJSON(&result),
404		autorest.ByClosing())
405	result.Response = autorest.Response{Response: resp}
406	return
407}
408
409// listByInstanceNextResults retrieves the next set of results, if any.
410func (client ManagedInstanceKeysClient) listByInstanceNextResults(ctx context.Context, lastResults ManagedInstanceKeyListResult) (result ManagedInstanceKeyListResult, err error) {
411	req, err := lastResults.managedInstanceKeyListResultPreparer(ctx)
412	if err != nil {
413		return result, autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "listByInstanceNextResults", nil, "Failure preparing next results request")
414	}
415	if req == nil {
416		return
417	}
418	resp, err := client.ListByInstanceSender(req)
419	if err != nil {
420		result.Response = autorest.Response{Response: resp}
421		return result, autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "listByInstanceNextResults", resp, "Failure sending next results request")
422	}
423	result, err = client.ListByInstanceResponder(resp)
424	if err != nil {
425		err = autorest.NewErrorWithError(err, "sql.ManagedInstanceKeysClient", "listByInstanceNextResults", resp, "Failure responding to next results request")
426	}
427	return
428}
429
430// ListByInstanceComplete enumerates all values, automatically crossing page boundaries as required.
431func (client ManagedInstanceKeysClient) ListByInstanceComplete(ctx context.Context, resourceGroupName string, managedInstanceName string, filter string) (result ManagedInstanceKeyListResultIterator, err error) {
432	if tracing.IsEnabled() {
433		ctx = tracing.StartSpan(ctx, fqdn+"/ManagedInstanceKeysClient.ListByInstance")
434		defer func() {
435			sc := -1
436			if result.Response().Response.Response != nil {
437				sc = result.page.Response().Response.Response.StatusCode
438			}
439			tracing.EndSpan(ctx, sc, err)
440		}()
441	}
442	result.page, err = client.ListByInstance(ctx, resourceGroupName, managedInstanceName, filter)
443	return
444}
445