1package kusto
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// DatabasePrincipalAssignmentsClient is the the Azure Kusto management API provides a RESTful set of web services that
19// interact with Azure Kusto services to manage your clusters and databases. The API enables you to create, update, and
20// delete clusters and databases.
21type DatabasePrincipalAssignmentsClient struct {
22	BaseClient
23}
24
25// NewDatabasePrincipalAssignmentsClient creates an instance of the DatabasePrincipalAssignmentsClient client.
26func NewDatabasePrincipalAssignmentsClient(subscriptionID string) DatabasePrincipalAssignmentsClient {
27	return NewDatabasePrincipalAssignmentsClientWithBaseURI(DefaultBaseURI, subscriptionID)
28}
29
30// NewDatabasePrincipalAssignmentsClientWithBaseURI creates an instance of the DatabasePrincipalAssignmentsClient
31// client using a custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI
32// (sovereign clouds, Azure stack).
33func NewDatabasePrincipalAssignmentsClientWithBaseURI(baseURI string, subscriptionID string) DatabasePrincipalAssignmentsClient {
34	return DatabasePrincipalAssignmentsClient{NewWithBaseURI(baseURI, subscriptionID)}
35}
36
37// CheckNameAvailability checks that the database principal assignment is valid and is not already in use.
38// Parameters:
39// resourceGroupName - the name of the resource group containing the Kusto cluster.
40// clusterName - the name of the Kusto cluster.
41// databaseName - the name of the database in the Kusto cluster.
42// principalAssignmentName - the name of the resource.
43func (client DatabasePrincipalAssignmentsClient) CheckNameAvailability(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName DatabasePrincipalAssignmentCheckNameRequest) (result CheckNameResult, err error) {
44	if tracing.IsEnabled() {
45		ctx = tracing.StartSpan(ctx, fqdn+"/DatabasePrincipalAssignmentsClient.CheckNameAvailability")
46		defer func() {
47			sc := -1
48			if result.Response.Response != nil {
49				sc = result.Response.Response.StatusCode
50			}
51			tracing.EndSpan(ctx, sc, err)
52		}()
53	}
54	if err := validation.Validate([]validation.Validation{
55		{TargetValue: principalAssignmentName,
56			Constraints: []validation.Constraint{{Target: "principalAssignmentName.Name", Name: validation.Null, Rule: true, Chain: nil},
57				{Target: "principalAssignmentName.Type", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
58		return result, validation.NewError("kusto.DatabasePrincipalAssignmentsClient", "CheckNameAvailability", err.Error())
59	}
60
61	req, err := client.CheckNameAvailabilityPreparer(ctx, resourceGroupName, clusterName, databaseName, principalAssignmentName)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "CheckNameAvailability", nil, "Failure preparing request")
64		return
65	}
66
67	resp, err := client.CheckNameAvailabilitySender(req)
68	if err != nil {
69		result.Response = autorest.Response{Response: resp}
70		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "CheckNameAvailability", resp, "Failure sending request")
71		return
72	}
73
74	result, err = client.CheckNameAvailabilityResponder(resp)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "CheckNameAvailability", resp, "Failure responding to request")
77		return
78	}
79
80	return
81}
82
83// CheckNameAvailabilityPreparer prepares the CheckNameAvailability request.
84func (client DatabasePrincipalAssignmentsClient) CheckNameAvailabilityPreparer(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName DatabasePrincipalAssignmentCheckNameRequest) (*http.Request, error) {
85	pathParameters := map[string]interface{}{
86		"clusterName":       autorest.Encode("path", clusterName),
87		"databaseName":      autorest.Encode("path", databaseName),
88		"resourceGroupName": autorest.Encode("path", resourceGroupName),
89		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
90	}
91
92	const APIVersion = "2020-02-15"
93	queryParameters := map[string]interface{}{
94		"api-version": APIVersion,
95	}
96
97	preparer := autorest.CreatePreparer(
98		autorest.AsContentType("application/json; charset=utf-8"),
99		autorest.AsPost(),
100		autorest.WithBaseURL(client.BaseURI),
101		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Kusto/clusters/{clusterName}/databases/{databaseName}/checkPrincipalAssignmentNameAvailability", pathParameters),
102		autorest.WithJSON(principalAssignmentName),
103		autorest.WithQueryParameters(queryParameters))
104	return preparer.Prepare((&http.Request{}).WithContext(ctx))
105}
106
107// CheckNameAvailabilitySender sends the CheckNameAvailability request. The method will close the
108// http.Response Body if it receives an error.
109func (client DatabasePrincipalAssignmentsClient) CheckNameAvailabilitySender(req *http.Request) (*http.Response, error) {
110	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
111}
112
113// CheckNameAvailabilityResponder handles the response to the CheckNameAvailability request. The method always
114// closes the http.Response Body.
115func (client DatabasePrincipalAssignmentsClient) CheckNameAvailabilityResponder(resp *http.Response) (result CheckNameResult, err error) {
116	err = autorest.Respond(
117		resp,
118		azure.WithErrorUnlessStatusCode(http.StatusOK),
119		autorest.ByUnmarshallingJSON(&result),
120		autorest.ByClosing())
121	result.Response = autorest.Response{Response: resp}
122	return
123}
124
125// CreateOrUpdate creates a Kusto cluster database principalAssignment.
126// Parameters:
127// resourceGroupName - the name of the resource group containing the Kusto cluster.
128// clusterName - the name of the Kusto cluster.
129// databaseName - the name of the database in the Kusto cluster.
130// principalAssignmentName - the name of the Kusto principalAssignment.
131// parameters - the Kusto principalAssignments parameters supplied for the operation.
132func (client DatabasePrincipalAssignmentsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName string, parameters DatabasePrincipalAssignment) (result DatabasePrincipalAssignmentsCreateOrUpdateFuture, err error) {
133	if tracing.IsEnabled() {
134		ctx = tracing.StartSpan(ctx, fqdn+"/DatabasePrincipalAssignmentsClient.CreateOrUpdate")
135		defer func() {
136			sc := -1
137			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
138				sc = result.FutureAPI.Response().StatusCode
139			}
140			tracing.EndSpan(ctx, sc, err)
141		}()
142	}
143	if err := validation.Validate([]validation.Validation{
144		{TargetValue: parameters,
145			Constraints: []validation.Constraint{{Target: "parameters.DatabasePrincipalProperties", Name: validation.Null, Rule: false,
146				Chain: []validation.Constraint{{Target: "parameters.DatabasePrincipalProperties.PrincipalID", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
147		return result, validation.NewError("kusto.DatabasePrincipalAssignmentsClient", "CreateOrUpdate", err.Error())
148	}
149
150	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, clusterName, databaseName, principalAssignmentName, parameters)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "CreateOrUpdate", nil, "Failure preparing request")
153		return
154	}
155
156	result, err = client.CreateOrUpdateSender(req)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "CreateOrUpdate", nil, "Failure sending request")
159		return
160	}
161
162	return
163}
164
165// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
166func (client DatabasePrincipalAssignmentsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName string, parameters DatabasePrincipalAssignment) (*http.Request, error) {
167	pathParameters := map[string]interface{}{
168		"clusterName":             autorest.Encode("path", clusterName),
169		"databaseName":            autorest.Encode("path", databaseName),
170		"principalAssignmentName": autorest.Encode("path", principalAssignmentName),
171		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
172		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
173	}
174
175	const APIVersion = "2020-02-15"
176	queryParameters := map[string]interface{}{
177		"api-version": APIVersion,
178	}
179
180	preparer := autorest.CreatePreparer(
181		autorest.AsContentType("application/json; charset=utf-8"),
182		autorest.AsPut(),
183		autorest.WithBaseURL(client.BaseURI),
184		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Kusto/clusters/{clusterName}/databases/{databaseName}/principalAssignments/{principalAssignmentName}", pathParameters),
185		autorest.WithJSON(parameters),
186		autorest.WithQueryParameters(queryParameters))
187	return preparer.Prepare((&http.Request{}).WithContext(ctx))
188}
189
190// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
191// http.Response Body if it receives an error.
192func (client DatabasePrincipalAssignmentsClient) CreateOrUpdateSender(req *http.Request) (future DatabasePrincipalAssignmentsCreateOrUpdateFuture, err error) {
193	var resp *http.Response
194	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
195	if err != nil {
196		return
197	}
198	var azf azure.Future
199	azf, err = azure.NewFutureFromResponse(resp)
200	future.FutureAPI = &azf
201	future.Result = future.result
202	return
203}
204
205// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
206// closes the http.Response Body.
207func (client DatabasePrincipalAssignmentsClient) CreateOrUpdateResponder(resp *http.Response) (result DatabasePrincipalAssignment, err error) {
208	err = autorest.Respond(
209		resp,
210		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
211		autorest.ByUnmarshallingJSON(&result),
212		autorest.ByClosing())
213	result.Response = autorest.Response{Response: resp}
214	return
215}
216
217// Delete deletes a Kusto principalAssignment.
218// Parameters:
219// resourceGroupName - the name of the resource group containing the Kusto cluster.
220// clusterName - the name of the Kusto cluster.
221// databaseName - the name of the database in the Kusto cluster.
222// principalAssignmentName - the name of the Kusto principalAssignment.
223func (client DatabasePrincipalAssignmentsClient) Delete(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName string) (result DatabasePrincipalAssignmentsDeleteFuture, err error) {
224	if tracing.IsEnabled() {
225		ctx = tracing.StartSpan(ctx, fqdn+"/DatabasePrincipalAssignmentsClient.Delete")
226		defer func() {
227			sc := -1
228			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
229				sc = result.FutureAPI.Response().StatusCode
230			}
231			tracing.EndSpan(ctx, sc, err)
232		}()
233	}
234	req, err := client.DeletePreparer(ctx, resourceGroupName, clusterName, databaseName, principalAssignmentName)
235	if err != nil {
236		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "Delete", nil, "Failure preparing request")
237		return
238	}
239
240	result, err = client.DeleteSender(req)
241	if err != nil {
242		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "Delete", nil, "Failure sending request")
243		return
244	}
245
246	return
247}
248
249// DeletePreparer prepares the Delete request.
250func (client DatabasePrincipalAssignmentsClient) DeletePreparer(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName string) (*http.Request, error) {
251	pathParameters := map[string]interface{}{
252		"clusterName":             autorest.Encode("path", clusterName),
253		"databaseName":            autorest.Encode("path", databaseName),
254		"principalAssignmentName": autorest.Encode("path", principalAssignmentName),
255		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
256		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
257	}
258
259	const APIVersion = "2020-02-15"
260	queryParameters := map[string]interface{}{
261		"api-version": APIVersion,
262	}
263
264	preparer := autorest.CreatePreparer(
265		autorest.AsDelete(),
266		autorest.WithBaseURL(client.BaseURI),
267		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Kusto/clusters/{clusterName}/databases/{databaseName}/principalAssignments/{principalAssignmentName}", pathParameters),
268		autorest.WithQueryParameters(queryParameters))
269	return preparer.Prepare((&http.Request{}).WithContext(ctx))
270}
271
272// DeleteSender sends the Delete request. The method will close the
273// http.Response Body if it receives an error.
274func (client DatabasePrincipalAssignmentsClient) DeleteSender(req *http.Request) (future DatabasePrincipalAssignmentsDeleteFuture, err error) {
275	var resp *http.Response
276	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
277	if err != nil {
278		return
279	}
280	var azf azure.Future
281	azf, err = azure.NewFutureFromResponse(resp)
282	future.FutureAPI = &azf
283	future.Result = future.result
284	return
285}
286
287// DeleteResponder handles the response to the Delete request. The method always
288// closes the http.Response Body.
289func (client DatabasePrincipalAssignmentsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
290	err = autorest.Respond(
291		resp,
292		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
293		autorest.ByClosing())
294	result.Response = resp
295	return
296}
297
298// Get gets a Kusto cluster database principalAssignment.
299// Parameters:
300// resourceGroupName - the name of the resource group containing the Kusto cluster.
301// clusterName - the name of the Kusto cluster.
302// databaseName - the name of the database in the Kusto cluster.
303// principalAssignmentName - the name of the Kusto principalAssignment.
304func (client DatabasePrincipalAssignmentsClient) Get(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName string) (result DatabasePrincipalAssignment, err error) {
305	if tracing.IsEnabled() {
306		ctx = tracing.StartSpan(ctx, fqdn+"/DatabasePrincipalAssignmentsClient.Get")
307		defer func() {
308			sc := -1
309			if result.Response.Response != nil {
310				sc = result.Response.Response.StatusCode
311			}
312			tracing.EndSpan(ctx, sc, err)
313		}()
314	}
315	req, err := client.GetPreparer(ctx, resourceGroupName, clusterName, databaseName, principalAssignmentName)
316	if err != nil {
317		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "Get", nil, "Failure preparing request")
318		return
319	}
320
321	resp, err := client.GetSender(req)
322	if err != nil {
323		result.Response = autorest.Response{Response: resp}
324		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "Get", resp, "Failure sending request")
325		return
326	}
327
328	result, err = client.GetResponder(resp)
329	if err != nil {
330		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "Get", resp, "Failure responding to request")
331		return
332	}
333
334	return
335}
336
337// GetPreparer prepares the Get request.
338func (client DatabasePrincipalAssignmentsClient) GetPreparer(ctx context.Context, resourceGroupName string, clusterName string, databaseName string, principalAssignmentName string) (*http.Request, error) {
339	pathParameters := map[string]interface{}{
340		"clusterName":             autorest.Encode("path", clusterName),
341		"databaseName":            autorest.Encode("path", databaseName),
342		"principalAssignmentName": autorest.Encode("path", principalAssignmentName),
343		"resourceGroupName":       autorest.Encode("path", resourceGroupName),
344		"subscriptionId":          autorest.Encode("path", client.SubscriptionID),
345	}
346
347	const APIVersion = "2020-02-15"
348	queryParameters := map[string]interface{}{
349		"api-version": APIVersion,
350	}
351
352	preparer := autorest.CreatePreparer(
353		autorest.AsGet(),
354		autorest.WithBaseURL(client.BaseURI),
355		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Kusto/clusters/{clusterName}/databases/{databaseName}/principalAssignments/{principalAssignmentName}", pathParameters),
356		autorest.WithQueryParameters(queryParameters))
357	return preparer.Prepare((&http.Request{}).WithContext(ctx))
358}
359
360// GetSender sends the Get request. The method will close the
361// http.Response Body if it receives an error.
362func (client DatabasePrincipalAssignmentsClient) GetSender(req *http.Request) (*http.Response, error) {
363	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
364}
365
366// GetResponder handles the response to the Get request. The method always
367// closes the http.Response Body.
368func (client DatabasePrincipalAssignmentsClient) GetResponder(resp *http.Response) (result DatabasePrincipalAssignment, err error) {
369	err = autorest.Respond(
370		resp,
371		azure.WithErrorUnlessStatusCode(http.StatusOK),
372		autorest.ByUnmarshallingJSON(&result),
373		autorest.ByClosing())
374	result.Response = autorest.Response{Response: resp}
375	return
376}
377
378// List lists all Kusto cluster database principalAssignments.
379// Parameters:
380// resourceGroupName - the name of the resource group containing the Kusto cluster.
381// clusterName - the name of the Kusto cluster.
382// databaseName - the name of the database in the Kusto cluster.
383func (client DatabasePrincipalAssignmentsClient) List(ctx context.Context, resourceGroupName string, clusterName string, databaseName string) (result DatabasePrincipalAssignmentListResult, err error) {
384	if tracing.IsEnabled() {
385		ctx = tracing.StartSpan(ctx, fqdn+"/DatabasePrincipalAssignmentsClient.List")
386		defer func() {
387			sc := -1
388			if result.Response.Response != nil {
389				sc = result.Response.Response.StatusCode
390			}
391			tracing.EndSpan(ctx, sc, err)
392		}()
393	}
394	req, err := client.ListPreparer(ctx, resourceGroupName, clusterName, databaseName)
395	if err != nil {
396		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "List", nil, "Failure preparing request")
397		return
398	}
399
400	resp, err := client.ListSender(req)
401	if err != nil {
402		result.Response = autorest.Response{Response: resp}
403		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "List", resp, "Failure sending request")
404		return
405	}
406
407	result, err = client.ListResponder(resp)
408	if err != nil {
409		err = autorest.NewErrorWithError(err, "kusto.DatabasePrincipalAssignmentsClient", "List", resp, "Failure responding to request")
410		return
411	}
412
413	return
414}
415
416// ListPreparer prepares the List request.
417func (client DatabasePrincipalAssignmentsClient) ListPreparer(ctx context.Context, resourceGroupName string, clusterName string, databaseName string) (*http.Request, error) {
418	pathParameters := map[string]interface{}{
419		"clusterName":       autorest.Encode("path", clusterName),
420		"databaseName":      autorest.Encode("path", databaseName),
421		"resourceGroupName": autorest.Encode("path", resourceGroupName),
422		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
423	}
424
425	const APIVersion = "2020-02-15"
426	queryParameters := map[string]interface{}{
427		"api-version": APIVersion,
428	}
429
430	preparer := autorest.CreatePreparer(
431		autorest.AsGet(),
432		autorest.WithBaseURL(client.BaseURI),
433		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Kusto/clusters/{clusterName}/databases/{databaseName}/principalAssignments", pathParameters),
434		autorest.WithQueryParameters(queryParameters))
435	return preparer.Prepare((&http.Request{}).WithContext(ctx))
436}
437
438// ListSender sends the List request. The method will close the
439// http.Response Body if it receives an error.
440func (client DatabasePrincipalAssignmentsClient) ListSender(req *http.Request) (*http.Response, error) {
441	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
442}
443
444// ListResponder handles the response to the List request. The method always
445// closes the http.Response Body.
446func (client DatabasePrincipalAssignmentsClient) ListResponder(resp *http.Response) (result DatabasePrincipalAssignmentListResult, err error) {
447	err = autorest.Respond(
448		resp,
449		azure.WithErrorUnlessStatusCode(http.StatusOK),
450		autorest.ByUnmarshallingJSON(&result),
451		autorest.ByClosing())
452	result.Response = autorest.Response{Response: resp}
453	return
454}
455