1package dtl
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// SecretsClient is the the DevTest Labs Client.
19type SecretsClient struct {
20	BaseClient
21}
22
23// NewSecretsClient creates an instance of the SecretsClient client.
24func NewSecretsClient(subscriptionID string) SecretsClient {
25	return NewSecretsClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewSecretsClientWithBaseURI creates an instance of the SecretsClient client using a custom endpoint.  Use this when
29// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewSecretsClientWithBaseURI(baseURI string, subscriptionID string) SecretsClient {
31	return SecretsClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate create or replace an existing secret.
35// Parameters:
36// resourceGroupName - the name of the resource group.
37// labName - the name of the lab.
38// userName - the name of the user profile.
39// name - the name of the secret.
40// secret - a secret.
41func (client SecretsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, labName string, userName string, name string, secret Secret) (result Secret, err error) {
42	if tracing.IsEnabled() {
43		ctx = tracing.StartSpan(ctx, fqdn+"/SecretsClient.CreateOrUpdate")
44		defer func() {
45			sc := -1
46			if result.Response.Response != nil {
47				sc = result.Response.Response.StatusCode
48			}
49			tracing.EndSpan(ctx, sc, err)
50		}()
51	}
52	if err := validation.Validate([]validation.Validation{
53		{TargetValue: secret,
54			Constraints: []validation.Constraint{{Target: "secret.SecretProperties", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
55		return result, validation.NewError("dtl.SecretsClient", "CreateOrUpdate", err.Error())
56	}
57
58	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, labName, userName, name, secret)
59	if err != nil {
60		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "CreateOrUpdate", nil, "Failure preparing request")
61		return
62	}
63
64	resp, err := client.CreateOrUpdateSender(req)
65	if err != nil {
66		result.Response = autorest.Response{Response: resp}
67		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "CreateOrUpdate", resp, "Failure sending request")
68		return
69	}
70
71	result, err = client.CreateOrUpdateResponder(resp)
72	if err != nil {
73		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "CreateOrUpdate", resp, "Failure responding to request")
74		return
75	}
76
77	return
78}
79
80// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
81func (client SecretsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, labName string, userName string, name string, secret Secret) (*http.Request, error) {
82	pathParameters := map[string]interface{}{
83		"labName":           autorest.Encode("path", labName),
84		"name":              autorest.Encode("path", name),
85		"resourceGroupName": autorest.Encode("path", resourceGroupName),
86		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
87		"userName":          autorest.Encode("path", userName),
88	}
89
90	const APIVersion = "2016-05-15"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsContentType("application/json; charset=utf-8"),
97		autorest.AsPut(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/users/{userName}/secrets/{name}", pathParameters),
100		autorest.WithJSON(secret),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
106// http.Response Body if it receives an error.
107func (client SecretsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
109}
110
111// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
112// closes the http.Response Body.
113func (client SecretsClient) CreateOrUpdateResponder(resp *http.Response) (result Secret, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// Delete delete secret.
124// Parameters:
125// resourceGroupName - the name of the resource group.
126// labName - the name of the lab.
127// userName - the name of the user profile.
128// name - the name of the secret.
129func (client SecretsClient) Delete(ctx context.Context, resourceGroupName string, labName string, userName string, name string) (result autorest.Response, err error) {
130	if tracing.IsEnabled() {
131		ctx = tracing.StartSpan(ctx, fqdn+"/SecretsClient.Delete")
132		defer func() {
133			sc := -1
134			if result.Response != nil {
135				sc = result.Response.StatusCode
136			}
137			tracing.EndSpan(ctx, sc, err)
138		}()
139	}
140	req, err := client.DeletePreparer(ctx, resourceGroupName, labName, userName, name)
141	if err != nil {
142		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "Delete", nil, "Failure preparing request")
143		return
144	}
145
146	resp, err := client.DeleteSender(req)
147	if err != nil {
148		result.Response = resp
149		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "Delete", resp, "Failure sending request")
150		return
151	}
152
153	result, err = client.DeleteResponder(resp)
154	if err != nil {
155		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "Delete", resp, "Failure responding to request")
156		return
157	}
158
159	return
160}
161
162// DeletePreparer prepares the Delete request.
163func (client SecretsClient) DeletePreparer(ctx context.Context, resourceGroupName string, labName string, userName string, name string) (*http.Request, error) {
164	pathParameters := map[string]interface{}{
165		"labName":           autorest.Encode("path", labName),
166		"name":              autorest.Encode("path", name),
167		"resourceGroupName": autorest.Encode("path", resourceGroupName),
168		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
169		"userName":          autorest.Encode("path", userName),
170	}
171
172	const APIVersion = "2016-05-15"
173	queryParameters := map[string]interface{}{
174		"api-version": APIVersion,
175	}
176
177	preparer := autorest.CreatePreparer(
178		autorest.AsDelete(),
179		autorest.WithBaseURL(client.BaseURI),
180		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/users/{userName}/secrets/{name}", pathParameters),
181		autorest.WithQueryParameters(queryParameters))
182	return preparer.Prepare((&http.Request{}).WithContext(ctx))
183}
184
185// DeleteSender sends the Delete request. The method will close the
186// http.Response Body if it receives an error.
187func (client SecretsClient) DeleteSender(req *http.Request) (*http.Response, error) {
188	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
189}
190
191// DeleteResponder handles the response to the Delete request. The method always
192// closes the http.Response Body.
193func (client SecretsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
194	err = autorest.Respond(
195		resp,
196		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
197		autorest.ByClosing())
198	result.Response = resp
199	return
200}
201
202// Get get secret.
203// Parameters:
204// resourceGroupName - the name of the resource group.
205// labName - the name of the lab.
206// userName - the name of the user profile.
207// name - the name of the secret.
208// expand - specify the $expand query. Example: 'properties($select=value)'
209func (client SecretsClient) Get(ctx context.Context, resourceGroupName string, labName string, userName string, name string, expand string) (result Secret, err error) {
210	if tracing.IsEnabled() {
211		ctx = tracing.StartSpan(ctx, fqdn+"/SecretsClient.Get")
212		defer func() {
213			sc := -1
214			if result.Response.Response != nil {
215				sc = result.Response.Response.StatusCode
216			}
217			tracing.EndSpan(ctx, sc, err)
218		}()
219	}
220	req, err := client.GetPreparer(ctx, resourceGroupName, labName, userName, name, expand)
221	if err != nil {
222		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "Get", nil, "Failure preparing request")
223		return
224	}
225
226	resp, err := client.GetSender(req)
227	if err != nil {
228		result.Response = autorest.Response{Response: resp}
229		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "Get", resp, "Failure sending request")
230		return
231	}
232
233	result, err = client.GetResponder(resp)
234	if err != nil {
235		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "Get", resp, "Failure responding to request")
236		return
237	}
238
239	return
240}
241
242// GetPreparer prepares the Get request.
243func (client SecretsClient) GetPreparer(ctx context.Context, resourceGroupName string, labName string, userName string, name string, expand string) (*http.Request, error) {
244	pathParameters := map[string]interface{}{
245		"labName":           autorest.Encode("path", labName),
246		"name":              autorest.Encode("path", name),
247		"resourceGroupName": autorest.Encode("path", resourceGroupName),
248		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
249		"userName":          autorest.Encode("path", userName),
250	}
251
252	const APIVersion = "2016-05-15"
253	queryParameters := map[string]interface{}{
254		"api-version": APIVersion,
255	}
256	if len(expand) > 0 {
257		queryParameters["$expand"] = autorest.Encode("query", expand)
258	}
259
260	preparer := autorest.CreatePreparer(
261		autorest.AsGet(),
262		autorest.WithBaseURL(client.BaseURI),
263		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/users/{userName}/secrets/{name}", pathParameters),
264		autorest.WithQueryParameters(queryParameters))
265	return preparer.Prepare((&http.Request{}).WithContext(ctx))
266}
267
268// GetSender sends the Get request. The method will close the
269// http.Response Body if it receives an error.
270func (client SecretsClient) GetSender(req *http.Request) (*http.Response, error) {
271	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
272}
273
274// GetResponder handles the response to the Get request. The method always
275// closes the http.Response Body.
276func (client SecretsClient) GetResponder(resp *http.Response) (result Secret, err error) {
277	err = autorest.Respond(
278		resp,
279		azure.WithErrorUnlessStatusCode(http.StatusOK),
280		autorest.ByUnmarshallingJSON(&result),
281		autorest.ByClosing())
282	result.Response = autorest.Response{Response: resp}
283	return
284}
285
286// List list secrets in a given user profile.
287// Parameters:
288// resourceGroupName - the name of the resource group.
289// labName - the name of the lab.
290// userName - the name of the user profile.
291// expand - specify the $expand query. Example: 'properties($select=value)'
292// filter - the filter to apply to the operation.
293// top - the maximum number of resources to return from the operation.
294// orderby - the ordering expression for the results, using OData notation.
295func (client SecretsClient) List(ctx context.Context, resourceGroupName string, labName string, userName string, expand string, filter string, top *int32, orderby string) (result ResponseWithContinuationSecretPage, err error) {
296	if tracing.IsEnabled() {
297		ctx = tracing.StartSpan(ctx, fqdn+"/SecretsClient.List")
298		defer func() {
299			sc := -1
300			if result.rwcs.Response.Response != nil {
301				sc = result.rwcs.Response.Response.StatusCode
302			}
303			tracing.EndSpan(ctx, sc, err)
304		}()
305	}
306	result.fn = client.listNextResults
307	req, err := client.ListPreparer(ctx, resourceGroupName, labName, userName, expand, filter, top, orderby)
308	if err != nil {
309		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "List", nil, "Failure preparing request")
310		return
311	}
312
313	resp, err := client.ListSender(req)
314	if err != nil {
315		result.rwcs.Response = autorest.Response{Response: resp}
316		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "List", resp, "Failure sending request")
317		return
318	}
319
320	result.rwcs, err = client.ListResponder(resp)
321	if err != nil {
322		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "List", resp, "Failure responding to request")
323		return
324	}
325	if result.rwcs.hasNextLink() && result.rwcs.IsEmpty() {
326		err = result.NextWithContext(ctx)
327		return
328	}
329
330	return
331}
332
333// ListPreparer prepares the List request.
334func (client SecretsClient) ListPreparer(ctx context.Context, resourceGroupName string, labName string, userName string, expand string, filter string, top *int32, orderby string) (*http.Request, error) {
335	pathParameters := map[string]interface{}{
336		"labName":           autorest.Encode("path", labName),
337		"resourceGroupName": autorest.Encode("path", resourceGroupName),
338		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
339		"userName":          autorest.Encode("path", userName),
340	}
341
342	const APIVersion = "2016-05-15"
343	queryParameters := map[string]interface{}{
344		"api-version": APIVersion,
345	}
346	if len(expand) > 0 {
347		queryParameters["$expand"] = autorest.Encode("query", expand)
348	}
349	if len(filter) > 0 {
350		queryParameters["$filter"] = autorest.Encode("query", filter)
351	}
352	if top != nil {
353		queryParameters["$top"] = autorest.Encode("query", *top)
354	}
355	if len(orderby) > 0 {
356		queryParameters["$orderby"] = autorest.Encode("query", orderby)
357	}
358
359	preparer := autorest.CreatePreparer(
360		autorest.AsGet(),
361		autorest.WithBaseURL(client.BaseURI),
362		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/users/{userName}/secrets", pathParameters),
363		autorest.WithQueryParameters(queryParameters))
364	return preparer.Prepare((&http.Request{}).WithContext(ctx))
365}
366
367// ListSender sends the List request. The method will close the
368// http.Response Body if it receives an error.
369func (client SecretsClient) ListSender(req *http.Request) (*http.Response, error) {
370	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
371}
372
373// ListResponder handles the response to the List request. The method always
374// closes the http.Response Body.
375func (client SecretsClient) ListResponder(resp *http.Response) (result ResponseWithContinuationSecret, err error) {
376	err = autorest.Respond(
377		resp,
378		azure.WithErrorUnlessStatusCode(http.StatusOK),
379		autorest.ByUnmarshallingJSON(&result),
380		autorest.ByClosing())
381	result.Response = autorest.Response{Response: resp}
382	return
383}
384
385// listNextResults retrieves the next set of results, if any.
386func (client SecretsClient) listNextResults(ctx context.Context, lastResults ResponseWithContinuationSecret) (result ResponseWithContinuationSecret, err error) {
387	req, err := lastResults.responseWithContinuationSecretPreparer(ctx)
388	if err != nil {
389		return result, autorest.NewErrorWithError(err, "dtl.SecretsClient", "listNextResults", nil, "Failure preparing next results request")
390	}
391	if req == nil {
392		return
393	}
394	resp, err := client.ListSender(req)
395	if err != nil {
396		result.Response = autorest.Response{Response: resp}
397		return result, autorest.NewErrorWithError(err, "dtl.SecretsClient", "listNextResults", resp, "Failure sending next results request")
398	}
399	result, err = client.ListResponder(resp)
400	if err != nil {
401		err = autorest.NewErrorWithError(err, "dtl.SecretsClient", "listNextResults", resp, "Failure responding to next results request")
402	}
403	return
404}
405
406// ListComplete enumerates all values, automatically crossing page boundaries as required.
407func (client SecretsClient) ListComplete(ctx context.Context, resourceGroupName string, labName string, userName string, expand string, filter string, top *int32, orderby string) (result ResponseWithContinuationSecretIterator, err error) {
408	if tracing.IsEnabled() {
409		ctx = tracing.StartSpan(ctx, fqdn+"/SecretsClient.List")
410		defer func() {
411			sc := -1
412			if result.Response().Response.Response != nil {
413				sc = result.page.Response().Response.Response.StatusCode
414			}
415			tracing.EndSpan(ctx, sc, err)
416		}()
417	}
418	result.page, err = client.List(ctx, resourceGroupName, labName, userName, expand, filter, top, orderby)
419	return
420}
421