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