1package databoxedge
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// RolesClient is the client for the Roles methods of the Databoxedge service.
18type RolesClient struct {
19	BaseClient
20}
21
22// NewRolesClient creates an instance of the RolesClient client.
23func NewRolesClient(subscriptionID string) RolesClient {
24	return NewRolesClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewRolesClientWithBaseURI creates an instance of the RolesClient client using a custom endpoint.  Use this when
28// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewRolesClientWithBaseURI(baseURI string, subscriptionID string) RolesClient {
30	return RolesClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// CreateOrUpdate create or update a role.
34// Parameters:
35// deviceName - the device name.
36// name - the role name.
37// role - the role properties.
38// resourceGroupName - the resource group name.
39func (client RolesClient) CreateOrUpdate(ctx context.Context, deviceName string, name string, role BasicRole, resourceGroupName string) (result RolesCreateOrUpdateFuture, err error) {
40	if tracing.IsEnabled() {
41		ctx = tracing.StartSpan(ctx, fqdn+"/RolesClient.CreateOrUpdate")
42		defer func() {
43			sc := -1
44			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
45				sc = result.FutureAPI.Response().StatusCode
46			}
47			tracing.EndSpan(ctx, sc, err)
48		}()
49	}
50	req, err := client.CreateOrUpdatePreparer(ctx, deviceName, name, role, resourceGroupName)
51	if err != nil {
52		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "CreateOrUpdate", nil, "Failure preparing request")
53		return
54	}
55
56	result, err = client.CreateOrUpdateSender(req)
57	if err != nil {
58		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "CreateOrUpdate", nil, "Failure sending request")
59		return
60	}
61
62	return
63}
64
65// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
66func (client RolesClient) CreateOrUpdatePreparer(ctx context.Context, deviceName string, name string, role BasicRole, resourceGroupName string) (*http.Request, error) {
67	pathParameters := map[string]interface{}{
68		"deviceName":        autorest.Encode("path", deviceName),
69		"name":              autorest.Encode("path", name),
70		"resourceGroupName": autorest.Encode("path", resourceGroupName),
71		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
72	}
73
74	const APIVersion = "2020-12-01"
75	queryParameters := map[string]interface{}{
76		"api-version": APIVersion,
77	}
78
79	preparer := autorest.CreatePreparer(
80		autorest.AsContentType("application/json; charset=utf-8"),
81		autorest.AsPut(),
82		autorest.WithBaseURL(client.BaseURI),
83		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataBoxEdge/dataBoxEdgeDevices/{deviceName}/roles/{name}", pathParameters),
84		autorest.WithJSON(role),
85		autorest.WithQueryParameters(queryParameters))
86	return preparer.Prepare((&http.Request{}).WithContext(ctx))
87}
88
89// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
90// http.Response Body if it receives an error.
91func (client RolesClient) CreateOrUpdateSender(req *http.Request) (future RolesCreateOrUpdateFuture, err error) {
92	var resp *http.Response
93	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
94	if err != nil {
95		return
96	}
97	var azf azure.Future
98	azf, err = azure.NewFutureFromResponse(resp)
99	future.FutureAPI = &azf
100	future.Result = future.result
101	return
102}
103
104// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
105// closes the http.Response Body.
106func (client RolesClient) CreateOrUpdateResponder(resp *http.Response) (result RoleModel, err error) {
107	err = autorest.Respond(
108		resp,
109		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
110		autorest.ByUnmarshallingJSON(&result),
111		autorest.ByClosing())
112	result.Response = autorest.Response{Response: resp}
113	return
114}
115
116// Delete deletes the role on the device.
117// Parameters:
118// deviceName - the device name.
119// name - the role name.
120// resourceGroupName - the resource group name.
121func (client RolesClient) Delete(ctx context.Context, deviceName string, name string, resourceGroupName string) (result RolesDeleteFuture, err error) {
122	if tracing.IsEnabled() {
123		ctx = tracing.StartSpan(ctx, fqdn+"/RolesClient.Delete")
124		defer func() {
125			sc := -1
126			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
127				sc = result.FutureAPI.Response().StatusCode
128			}
129			tracing.EndSpan(ctx, sc, err)
130		}()
131	}
132	req, err := client.DeletePreparer(ctx, deviceName, name, resourceGroupName)
133	if err != nil {
134		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "Delete", nil, "Failure preparing request")
135		return
136	}
137
138	result, err = client.DeleteSender(req)
139	if err != nil {
140		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "Delete", nil, "Failure sending request")
141		return
142	}
143
144	return
145}
146
147// DeletePreparer prepares the Delete request.
148func (client RolesClient) DeletePreparer(ctx context.Context, deviceName string, name string, resourceGroupName string) (*http.Request, error) {
149	pathParameters := map[string]interface{}{
150		"deviceName":        autorest.Encode("path", deviceName),
151		"name":              autorest.Encode("path", name),
152		"resourceGroupName": autorest.Encode("path", resourceGroupName),
153		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
154	}
155
156	const APIVersion = "2020-12-01"
157	queryParameters := map[string]interface{}{
158		"api-version": APIVersion,
159	}
160
161	preparer := autorest.CreatePreparer(
162		autorest.AsDelete(),
163		autorest.WithBaseURL(client.BaseURI),
164		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataBoxEdge/dataBoxEdgeDevices/{deviceName}/roles/{name}", pathParameters),
165		autorest.WithQueryParameters(queryParameters))
166	return preparer.Prepare((&http.Request{}).WithContext(ctx))
167}
168
169// DeleteSender sends the Delete request. The method will close the
170// http.Response Body if it receives an error.
171func (client RolesClient) DeleteSender(req *http.Request) (future RolesDeleteFuture, err error) {
172	var resp *http.Response
173	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
174	if err != nil {
175		return
176	}
177	var azf azure.Future
178	azf, err = azure.NewFutureFromResponse(resp)
179	future.FutureAPI = &azf
180	future.Result = future.result
181	return
182}
183
184// DeleteResponder handles the response to the Delete request. The method always
185// closes the http.Response Body.
186func (client RolesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
187	err = autorest.Respond(
188		resp,
189		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
190		autorest.ByClosing())
191	result.Response = resp
192	return
193}
194
195// Get gets a specific role by name.
196// Parameters:
197// deviceName - the device name.
198// name - the role name.
199// resourceGroupName - the resource group name.
200func (client RolesClient) Get(ctx context.Context, deviceName string, name string, resourceGroupName string) (result RoleModel, err error) {
201	if tracing.IsEnabled() {
202		ctx = tracing.StartSpan(ctx, fqdn+"/RolesClient.Get")
203		defer func() {
204			sc := -1
205			if result.Response.Response != nil {
206				sc = result.Response.Response.StatusCode
207			}
208			tracing.EndSpan(ctx, sc, err)
209		}()
210	}
211	req, err := client.GetPreparer(ctx, deviceName, name, resourceGroupName)
212	if err != nil {
213		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "Get", nil, "Failure preparing request")
214		return
215	}
216
217	resp, err := client.GetSender(req)
218	if err != nil {
219		result.Response = autorest.Response{Response: resp}
220		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "Get", resp, "Failure sending request")
221		return
222	}
223
224	result, err = client.GetResponder(resp)
225	if err != nil {
226		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "Get", resp, "Failure responding to request")
227		return
228	}
229
230	return
231}
232
233// GetPreparer prepares the Get request.
234func (client RolesClient) GetPreparer(ctx context.Context, deviceName string, name string, resourceGroupName string) (*http.Request, error) {
235	pathParameters := map[string]interface{}{
236		"deviceName":        autorest.Encode("path", deviceName),
237		"name":              autorest.Encode("path", name),
238		"resourceGroupName": autorest.Encode("path", resourceGroupName),
239		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
240	}
241
242	const APIVersion = "2020-12-01"
243	queryParameters := map[string]interface{}{
244		"api-version": APIVersion,
245	}
246
247	preparer := autorest.CreatePreparer(
248		autorest.AsGet(),
249		autorest.WithBaseURL(client.BaseURI),
250		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataBoxEdge/dataBoxEdgeDevices/{deviceName}/roles/{name}", pathParameters),
251		autorest.WithQueryParameters(queryParameters))
252	return preparer.Prepare((&http.Request{}).WithContext(ctx))
253}
254
255// GetSender sends the Get request. The method will close the
256// http.Response Body if it receives an error.
257func (client RolesClient) GetSender(req *http.Request) (*http.Response, error) {
258	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
259}
260
261// GetResponder handles the response to the Get request. The method always
262// closes the http.Response Body.
263func (client RolesClient) GetResponder(resp *http.Response) (result RoleModel, err error) {
264	err = autorest.Respond(
265		resp,
266		azure.WithErrorUnlessStatusCode(http.StatusOK),
267		autorest.ByUnmarshallingJSON(&result),
268		autorest.ByClosing())
269	result.Response = autorest.Response{Response: resp}
270	return
271}
272
273// ListByDataBoxEdgeDevice lists all the roles configured in a Data Box Edge/Data Box Gateway device.
274// Parameters:
275// deviceName - the device name.
276// resourceGroupName - the resource group name.
277func (client RolesClient) ListByDataBoxEdgeDevice(ctx context.Context, deviceName string, resourceGroupName string) (result RoleListPage, err error) {
278	if tracing.IsEnabled() {
279		ctx = tracing.StartSpan(ctx, fqdn+"/RolesClient.ListByDataBoxEdgeDevice")
280		defer func() {
281			sc := -1
282			if result.rl.Response.Response != nil {
283				sc = result.rl.Response.Response.StatusCode
284			}
285			tracing.EndSpan(ctx, sc, err)
286		}()
287	}
288	result.fn = client.listByDataBoxEdgeDeviceNextResults
289	req, err := client.ListByDataBoxEdgeDevicePreparer(ctx, deviceName, resourceGroupName)
290	if err != nil {
291		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "ListByDataBoxEdgeDevice", nil, "Failure preparing request")
292		return
293	}
294
295	resp, err := client.ListByDataBoxEdgeDeviceSender(req)
296	if err != nil {
297		result.rl.Response = autorest.Response{Response: resp}
298		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "ListByDataBoxEdgeDevice", resp, "Failure sending request")
299		return
300	}
301
302	result.rl, err = client.ListByDataBoxEdgeDeviceResponder(resp)
303	if err != nil {
304		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "ListByDataBoxEdgeDevice", resp, "Failure responding to request")
305		return
306	}
307	if result.rl.hasNextLink() && result.rl.IsEmpty() {
308		err = result.NextWithContext(ctx)
309		return
310	}
311
312	return
313}
314
315// ListByDataBoxEdgeDevicePreparer prepares the ListByDataBoxEdgeDevice request.
316func (client RolesClient) ListByDataBoxEdgeDevicePreparer(ctx context.Context, deviceName string, resourceGroupName string) (*http.Request, error) {
317	pathParameters := map[string]interface{}{
318		"deviceName":        autorest.Encode("path", deviceName),
319		"resourceGroupName": autorest.Encode("path", resourceGroupName),
320		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
321	}
322
323	const APIVersion = "2020-12-01"
324	queryParameters := map[string]interface{}{
325		"api-version": APIVersion,
326	}
327
328	preparer := autorest.CreatePreparer(
329		autorest.AsGet(),
330		autorest.WithBaseURL(client.BaseURI),
331		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataBoxEdge/dataBoxEdgeDevices/{deviceName}/roles", pathParameters),
332		autorest.WithQueryParameters(queryParameters))
333	return preparer.Prepare((&http.Request{}).WithContext(ctx))
334}
335
336// ListByDataBoxEdgeDeviceSender sends the ListByDataBoxEdgeDevice request. The method will close the
337// http.Response Body if it receives an error.
338func (client RolesClient) ListByDataBoxEdgeDeviceSender(req *http.Request) (*http.Response, error) {
339	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
340}
341
342// ListByDataBoxEdgeDeviceResponder handles the response to the ListByDataBoxEdgeDevice request. The method always
343// closes the http.Response Body.
344func (client RolesClient) ListByDataBoxEdgeDeviceResponder(resp *http.Response) (result RoleList, err error) {
345	err = autorest.Respond(
346		resp,
347		azure.WithErrorUnlessStatusCode(http.StatusOK),
348		autorest.ByUnmarshallingJSON(&result),
349		autorest.ByClosing())
350	result.Response = autorest.Response{Response: resp}
351	return
352}
353
354// listByDataBoxEdgeDeviceNextResults retrieves the next set of results, if any.
355func (client RolesClient) listByDataBoxEdgeDeviceNextResults(ctx context.Context, lastResults RoleList) (result RoleList, err error) {
356	req, err := lastResults.roleListPreparer(ctx)
357	if err != nil {
358		return result, autorest.NewErrorWithError(err, "databoxedge.RolesClient", "listByDataBoxEdgeDeviceNextResults", nil, "Failure preparing next results request")
359	}
360	if req == nil {
361		return
362	}
363	resp, err := client.ListByDataBoxEdgeDeviceSender(req)
364	if err != nil {
365		result.Response = autorest.Response{Response: resp}
366		return result, autorest.NewErrorWithError(err, "databoxedge.RolesClient", "listByDataBoxEdgeDeviceNextResults", resp, "Failure sending next results request")
367	}
368	result, err = client.ListByDataBoxEdgeDeviceResponder(resp)
369	if err != nil {
370		err = autorest.NewErrorWithError(err, "databoxedge.RolesClient", "listByDataBoxEdgeDeviceNextResults", resp, "Failure responding to next results request")
371	}
372	return
373}
374
375// ListByDataBoxEdgeDeviceComplete enumerates all values, automatically crossing page boundaries as required.
376func (client RolesClient) ListByDataBoxEdgeDeviceComplete(ctx context.Context, deviceName string, resourceGroupName string) (result RoleListIterator, err error) {
377	if tracing.IsEnabled() {
378		ctx = tracing.StartSpan(ctx, fqdn+"/RolesClient.ListByDataBoxEdgeDevice")
379		defer func() {
380			sc := -1
381			if result.Response().Response.Response != nil {
382				sc = result.page.Response().Response.Response.StatusCode
383			}
384			tracing.EndSpan(ctx, sc, err)
385		}()
386	}
387	result.page, err = client.ListByDataBoxEdgeDevice(ctx, deviceName, resourceGroupName)
388	return
389}
390