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// ServerAzureADAdministratorsClient is the the Azure SQL Database management API provides a RESTful set of web
19// services that interact with Azure SQL Database services to manage your databases. The API enables you to create,
20// retrieve, update, and delete databases.
21type ServerAzureADAdministratorsClient struct {
22	BaseClient
23}
24
25// NewServerAzureADAdministratorsClient creates an instance of the ServerAzureADAdministratorsClient client.
26func NewServerAzureADAdministratorsClient(subscriptionID string) ServerAzureADAdministratorsClient {
27	return NewServerAzureADAdministratorsClientWithBaseURI(DefaultBaseURI, subscriptionID)
28}
29
30// NewServerAzureADAdministratorsClientWithBaseURI creates an instance of the ServerAzureADAdministratorsClient client
31// using a custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign
32// clouds, Azure stack).
33func NewServerAzureADAdministratorsClientWithBaseURI(baseURI string, subscriptionID string) ServerAzureADAdministratorsClient {
34	return ServerAzureADAdministratorsClient{NewWithBaseURI(baseURI, subscriptionID)}
35}
36
37// CreateOrUpdate creates or updates an existing Azure Active Directory administrator.
38// Parameters:
39// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
40// from the Azure Resource Manager API or the portal.
41// serverName - the name of the server.
42// parameters - the requested Azure Active Directory administrator Resource state.
43func (client ServerAzureADAdministratorsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, parameters ServerAzureADAdministrator) (result ServerAzureADAdministratorsCreateOrUpdateFuture, err error) {
44	if tracing.IsEnabled() {
45		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAzureADAdministratorsClient.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.AdministratorProperties", Name: validation.Null, Rule: false,
57				Chain: []validation.Constraint{{Target: "parameters.AdministratorProperties.AdministratorType", Name: validation.Null, Rule: true, Chain: nil},
58					{Target: "parameters.AdministratorProperties.Login", Name: validation.Null, Rule: true, Chain: nil},
59					{Target: "parameters.AdministratorProperties.Sid", Name: validation.Null, Rule: true, Chain: nil},
60				}}}}}); err != nil {
61		return result, validation.NewError("sql.ServerAzureADAdministratorsClient", "CreateOrUpdate", err.Error())
62	}
63
64	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, parameters)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "CreateOrUpdate", nil, "Failure preparing request")
67		return
68	}
69
70	result, err = client.CreateOrUpdateSender(req)
71	if err != nil {
72		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "CreateOrUpdate", nil, "Failure sending request")
73		return
74	}
75
76	return
77}
78
79// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
80func (client ServerAzureADAdministratorsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, parameters ServerAzureADAdministrator) (*http.Request, error) {
81	pathParameters := map[string]interface{}{
82		"administratorName": autorest.Encode("path", "ActiveDirectory"),
83		"resourceGroupName": autorest.Encode("path", resourceGroupName),
84		"serverName":        autorest.Encode("path", serverName),
85		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
86	}
87
88	const APIVersion = "2019-06-01-preview"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92
93	preparer := autorest.CreatePreparer(
94		autorest.AsContentType("application/json; charset=utf-8"),
95		autorest.AsPut(),
96		autorest.WithBaseURL(client.BaseURI),
97		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/administrators/{administratorName}", pathParameters),
98		autorest.WithJSON(parameters),
99		autorest.WithQueryParameters(queryParameters))
100	return preparer.Prepare((&http.Request{}).WithContext(ctx))
101}
102
103// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
104// http.Response Body if it receives an error.
105func (client ServerAzureADAdministratorsClient) CreateOrUpdateSender(req *http.Request) (future ServerAzureADAdministratorsCreateOrUpdateFuture, err error) {
106	var resp *http.Response
107	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
108	if err != nil {
109		return
110	}
111	var azf azure.Future
112	azf, err = azure.NewFutureFromResponse(resp)
113	future.FutureAPI = &azf
114	future.Result = future.result
115	return
116}
117
118// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
119// closes the http.Response Body.
120func (client ServerAzureADAdministratorsClient) CreateOrUpdateResponder(resp *http.Response) (result ServerAzureADAdministrator, err error) {
121	err = autorest.Respond(
122		resp,
123		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
124		autorest.ByUnmarshallingJSON(&result),
125		autorest.ByClosing())
126	result.Response = autorest.Response{Response: resp}
127	return
128}
129
130// Delete deletes the Azure Active Directory administrator with the given name.
131// Parameters:
132// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
133// from the Azure Resource Manager API or the portal.
134// serverName - the name of the server.
135func (client ServerAzureADAdministratorsClient) Delete(ctx context.Context, resourceGroupName string, serverName string) (result ServerAzureADAdministratorsDeleteFuture, err error) {
136	if tracing.IsEnabled() {
137		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAzureADAdministratorsClient.Delete")
138		defer func() {
139			sc := -1
140			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
141				sc = result.FutureAPI.Response().StatusCode
142			}
143			tracing.EndSpan(ctx, sc, err)
144		}()
145	}
146	req, err := client.DeletePreparer(ctx, resourceGroupName, serverName)
147	if err != nil {
148		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "Delete", nil, "Failure preparing request")
149		return
150	}
151
152	result, err = client.DeleteSender(req)
153	if err != nil {
154		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "Delete", nil, "Failure sending request")
155		return
156	}
157
158	return
159}
160
161// DeletePreparer prepares the Delete request.
162func (client ServerAzureADAdministratorsClient) DeletePreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
163	pathParameters := map[string]interface{}{
164		"administratorName": autorest.Encode("path", "ActiveDirectory"),
165		"resourceGroupName": autorest.Encode("path", resourceGroupName),
166		"serverName":        autorest.Encode("path", serverName),
167		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
168	}
169
170	const APIVersion = "2019-06-01-preview"
171	queryParameters := map[string]interface{}{
172		"api-version": APIVersion,
173	}
174
175	preparer := autorest.CreatePreparer(
176		autorest.AsDelete(),
177		autorest.WithBaseURL(client.BaseURI),
178		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/administrators/{administratorName}", pathParameters),
179		autorest.WithQueryParameters(queryParameters))
180	return preparer.Prepare((&http.Request{}).WithContext(ctx))
181}
182
183// DeleteSender sends the Delete request. The method will close the
184// http.Response Body if it receives an error.
185func (client ServerAzureADAdministratorsClient) DeleteSender(req *http.Request) (future ServerAzureADAdministratorsDeleteFuture, err error) {
186	var resp *http.Response
187	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
188	if err != nil {
189		return
190	}
191	var azf azure.Future
192	azf, err = azure.NewFutureFromResponse(resp)
193	future.FutureAPI = &azf
194	future.Result = future.result
195	return
196}
197
198// DeleteResponder handles the response to the Delete request. The method always
199// closes the http.Response Body.
200func (client ServerAzureADAdministratorsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
201	err = autorest.Respond(
202		resp,
203		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
204		autorest.ByClosing())
205	result.Response = resp
206	return
207}
208
209// Get gets a Azure Active Directory administrator.
210// Parameters:
211// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
212// from the Azure Resource Manager API or the portal.
213// serverName - the name of the server.
214func (client ServerAzureADAdministratorsClient) Get(ctx context.Context, resourceGroupName string, serverName string) (result ServerAzureADAdministrator, err error) {
215	if tracing.IsEnabled() {
216		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAzureADAdministratorsClient.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)
226	if err != nil {
227		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "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.ServerAzureADAdministratorsClient", "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.ServerAzureADAdministratorsClient", "Get", resp, "Failure responding to request")
241		return
242	}
243
244	return
245}
246
247// GetPreparer prepares the Get request.
248func (client ServerAzureADAdministratorsClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
249	pathParameters := map[string]interface{}{
250		"administratorName": autorest.Encode("path", "ActiveDirectory"),
251		"resourceGroupName": autorest.Encode("path", resourceGroupName),
252		"serverName":        autorest.Encode("path", serverName),
253		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
254	}
255
256	const APIVersion = "2019-06-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}/administrators/{administratorName}", 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 ServerAzureADAdministratorsClient) 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 ServerAzureADAdministratorsClient) GetResponder(resp *http.Response) (result ServerAzureADAdministrator, 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 a list of Azure Active Directory administrators 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.
292func (client ServerAzureADAdministratorsClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string) (result AdministratorListResultPage, err error) {
293	if tracing.IsEnabled() {
294		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAzureADAdministratorsClient.ListByServer")
295		defer func() {
296			sc := -1
297			if result.alr.Response.Response != nil {
298				sc = result.alr.Response.Response.StatusCode
299			}
300			tracing.EndSpan(ctx, sc, err)
301		}()
302	}
303	result.fn = client.listByServerNextResults
304	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName)
305	if err != nil {
306		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "ListByServer", nil, "Failure preparing request")
307		return
308	}
309
310	resp, err := client.ListByServerSender(req)
311	if err != nil {
312		result.alr.Response = autorest.Response{Response: resp}
313		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "ListByServer", resp, "Failure sending request")
314		return
315	}
316
317	result.alr, err = client.ListByServerResponder(resp)
318	if err != nil {
319		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "ListByServer", resp, "Failure responding to request")
320		return
321	}
322	if result.alr.hasNextLink() && result.alr.IsEmpty() {
323		err = result.NextWithContext(ctx)
324		return
325	}
326
327	return
328}
329
330// ListByServerPreparer prepares the ListByServer request.
331func (client ServerAzureADAdministratorsClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
332	pathParameters := map[string]interface{}{
333		"resourceGroupName": autorest.Encode("path", resourceGroupName),
334		"serverName":        autorest.Encode("path", serverName),
335		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
336	}
337
338	const APIVersion = "2019-06-01-preview"
339	queryParameters := map[string]interface{}{
340		"api-version": APIVersion,
341	}
342
343	preparer := autorest.CreatePreparer(
344		autorest.AsGet(),
345		autorest.WithBaseURL(client.BaseURI),
346		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/administrators", pathParameters),
347		autorest.WithQueryParameters(queryParameters))
348	return preparer.Prepare((&http.Request{}).WithContext(ctx))
349}
350
351// ListByServerSender sends the ListByServer request. The method will close the
352// http.Response Body if it receives an error.
353func (client ServerAzureADAdministratorsClient) ListByServerSender(req *http.Request) (*http.Response, error) {
354	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
355}
356
357// ListByServerResponder handles the response to the ListByServer request. The method always
358// closes the http.Response Body.
359func (client ServerAzureADAdministratorsClient) ListByServerResponder(resp *http.Response) (result AdministratorListResult, err error) {
360	err = autorest.Respond(
361		resp,
362		azure.WithErrorUnlessStatusCode(http.StatusOK),
363		autorest.ByUnmarshallingJSON(&result),
364		autorest.ByClosing())
365	result.Response = autorest.Response{Response: resp}
366	return
367}
368
369// listByServerNextResults retrieves the next set of results, if any.
370func (client ServerAzureADAdministratorsClient) listByServerNextResults(ctx context.Context, lastResults AdministratorListResult) (result AdministratorListResult, err error) {
371	req, err := lastResults.administratorListResultPreparer(ctx)
372	if err != nil {
373		return result, autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "listByServerNextResults", nil, "Failure preparing next results request")
374	}
375	if req == nil {
376		return
377	}
378	resp, err := client.ListByServerSender(req)
379	if err != nil {
380		result.Response = autorest.Response{Response: resp}
381		return result, autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "listByServerNextResults", resp, "Failure sending next results request")
382	}
383	result, err = client.ListByServerResponder(resp)
384	if err != nil {
385		err = autorest.NewErrorWithError(err, "sql.ServerAzureADAdministratorsClient", "listByServerNextResults", resp, "Failure responding to next results request")
386	}
387	return
388}
389
390// ListByServerComplete enumerates all values, automatically crossing page boundaries as required.
391func (client ServerAzureADAdministratorsClient) ListByServerComplete(ctx context.Context, resourceGroupName string, serverName string) (result AdministratorListResultIterator, err error) {
392	if tracing.IsEnabled() {
393		ctx = tracing.StartSpan(ctx, fqdn+"/ServerAzureADAdministratorsClient.ListByServer")
394		defer func() {
395			sc := -1
396			if result.Response().Response.Response != nil {
397				sc = result.page.Response().Response.Response.StatusCode
398			}
399			tracing.EndSpan(ctx, sc, err)
400		}()
401	}
402	result.page, err = client.ListByServer(ctx, resourceGroupName, serverName)
403	return
404}
405