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