1package automation
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// VariableClient is the automation Client
19type VariableClient struct {
20	BaseClient
21}
22
23// NewVariableClient creates an instance of the VariableClient client.
24func NewVariableClient(subscriptionID string) VariableClient {
25	return NewVariableClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewVariableClientWithBaseURI creates an instance of the VariableClient client using a custom endpoint.  Use this
29// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewVariableClientWithBaseURI(baseURI string, subscriptionID string) VariableClient {
31	return VariableClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate create a variable.
35// Parameters:
36// resourceGroupName - name of an Azure Resource group.
37// automationAccountName - the name of the automation account.
38// variableName - the variable name.
39// parameters - the parameters supplied to the create or update variable operation.
40func (client VariableClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string, parameters VariableCreateOrUpdateParameters) (result Variable, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/VariableClient.CreateOrUpdate")
43		defer func() {
44			sc := -1
45			if result.Response.Response != nil {
46				sc = result.Response.Response.StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: resourceGroupName,
53			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
54				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
55				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}},
56		{TargetValue: parameters,
57			Constraints: []validation.Constraint{{Target: "parameters.Name", Name: validation.Null, Rule: true, Chain: nil},
58				{Target: "parameters.VariableCreateOrUpdateProperties", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
59		return result, validation.NewError("automation.VariableClient", "CreateOrUpdate", err.Error())
60	}
61
62	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, automationAccountName, variableName, parameters)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "automation.VariableClient", "CreateOrUpdate", nil, "Failure preparing request")
65		return
66	}
67
68	resp, err := client.CreateOrUpdateSender(req)
69	if err != nil {
70		result.Response = autorest.Response{Response: resp}
71		err = autorest.NewErrorWithError(err, "automation.VariableClient", "CreateOrUpdate", resp, "Failure sending request")
72		return
73	}
74
75	result, err = client.CreateOrUpdateResponder(resp)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "automation.VariableClient", "CreateOrUpdate", resp, "Failure responding to request")
78		return
79	}
80
81	return
82}
83
84// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
85func (client VariableClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string, parameters VariableCreateOrUpdateParameters) (*http.Request, error) {
86	pathParameters := map[string]interface{}{
87		"automationAccountName": autorest.Encode("path", automationAccountName),
88		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
89		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
90		"variableName":          autorest.Encode("path", variableName),
91	}
92
93	const APIVersion = "2015-10-31"
94	queryParameters := map[string]interface{}{
95		"api-version": APIVersion,
96	}
97
98	preparer := autorest.CreatePreparer(
99		autorest.AsContentType("application/json; charset=utf-8"),
100		autorest.AsPut(),
101		autorest.WithBaseURL(client.BaseURI),
102		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/variables/{variableName}", pathParameters),
103		autorest.WithJSON(parameters),
104		autorest.WithQueryParameters(queryParameters))
105	return preparer.Prepare((&http.Request{}).WithContext(ctx))
106}
107
108// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
109// http.Response Body if it receives an error.
110func (client VariableClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
111	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
112}
113
114// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
115// closes the http.Response Body.
116func (client VariableClient) CreateOrUpdateResponder(resp *http.Response) (result Variable, err error) {
117	err = autorest.Respond(
118		resp,
119		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
120		autorest.ByUnmarshallingJSON(&result),
121		autorest.ByClosing())
122	result.Response = autorest.Response{Response: resp}
123	return
124}
125
126// Delete delete the variable.
127// Parameters:
128// resourceGroupName - name of an Azure Resource group.
129// automationAccountName - the name of the automation account.
130// variableName - the name of variable.
131func (client VariableClient) Delete(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string) (result autorest.Response, err error) {
132	if tracing.IsEnabled() {
133		ctx = tracing.StartSpan(ctx, fqdn+"/VariableClient.Delete")
134		defer func() {
135			sc := -1
136			if result.Response != nil {
137				sc = result.Response.StatusCode
138			}
139			tracing.EndSpan(ctx, sc, err)
140		}()
141	}
142	if err := validation.Validate([]validation.Validation{
143		{TargetValue: resourceGroupName,
144			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
145				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
146				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
147		return result, validation.NewError("automation.VariableClient", "Delete", err.Error())
148	}
149
150	req, err := client.DeletePreparer(ctx, resourceGroupName, automationAccountName, variableName)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Delete", nil, "Failure preparing request")
153		return
154	}
155
156	resp, err := client.DeleteSender(req)
157	if err != nil {
158		result.Response = resp
159		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Delete", resp, "Failure sending request")
160		return
161	}
162
163	result, err = client.DeleteResponder(resp)
164	if err != nil {
165		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Delete", resp, "Failure responding to request")
166		return
167	}
168
169	return
170}
171
172// DeletePreparer prepares the Delete request.
173func (client VariableClient) DeletePreparer(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string) (*http.Request, error) {
174	pathParameters := map[string]interface{}{
175		"automationAccountName": autorest.Encode("path", automationAccountName),
176		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
177		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
178		"variableName":          autorest.Encode("path", variableName),
179	}
180
181	const APIVersion = "2015-10-31"
182	queryParameters := map[string]interface{}{
183		"api-version": APIVersion,
184	}
185
186	preparer := autorest.CreatePreparer(
187		autorest.AsDelete(),
188		autorest.WithBaseURL(client.BaseURI),
189		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/variables/{variableName}", pathParameters),
190		autorest.WithQueryParameters(queryParameters))
191	return preparer.Prepare((&http.Request{}).WithContext(ctx))
192}
193
194// DeleteSender sends the Delete request. The method will close the
195// http.Response Body if it receives an error.
196func (client VariableClient) DeleteSender(req *http.Request) (*http.Response, error) {
197	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
198}
199
200// DeleteResponder handles the response to the Delete request. The method always
201// closes the http.Response Body.
202func (client VariableClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
203	err = autorest.Respond(
204		resp,
205		azure.WithErrorUnlessStatusCode(http.StatusOK),
206		autorest.ByClosing())
207	result.Response = resp
208	return
209}
210
211// Get retrieve the variable identified by variable name.
212// Parameters:
213// resourceGroupName - name of an Azure Resource group.
214// automationAccountName - the name of the automation account.
215// variableName - the name of variable.
216func (client VariableClient) Get(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string) (result Variable, err error) {
217	if tracing.IsEnabled() {
218		ctx = tracing.StartSpan(ctx, fqdn+"/VariableClient.Get")
219		defer func() {
220			sc := -1
221			if result.Response.Response != nil {
222				sc = result.Response.Response.StatusCode
223			}
224			tracing.EndSpan(ctx, sc, err)
225		}()
226	}
227	if err := validation.Validate([]validation.Validation{
228		{TargetValue: resourceGroupName,
229			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
230				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
231				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
232		return result, validation.NewError("automation.VariableClient", "Get", err.Error())
233	}
234
235	req, err := client.GetPreparer(ctx, resourceGroupName, automationAccountName, variableName)
236	if err != nil {
237		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Get", nil, "Failure preparing request")
238		return
239	}
240
241	resp, err := client.GetSender(req)
242	if err != nil {
243		result.Response = autorest.Response{Response: resp}
244		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Get", resp, "Failure sending request")
245		return
246	}
247
248	result, err = client.GetResponder(resp)
249	if err != nil {
250		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Get", resp, "Failure responding to request")
251		return
252	}
253
254	return
255}
256
257// GetPreparer prepares the Get request.
258func (client VariableClient) GetPreparer(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string) (*http.Request, error) {
259	pathParameters := map[string]interface{}{
260		"automationAccountName": autorest.Encode("path", automationAccountName),
261		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
262		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
263		"variableName":          autorest.Encode("path", variableName),
264	}
265
266	const APIVersion = "2015-10-31"
267	queryParameters := map[string]interface{}{
268		"api-version": APIVersion,
269	}
270
271	preparer := autorest.CreatePreparer(
272		autorest.AsGet(),
273		autorest.WithBaseURL(client.BaseURI),
274		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/variables/{variableName}", pathParameters),
275		autorest.WithQueryParameters(queryParameters))
276	return preparer.Prepare((&http.Request{}).WithContext(ctx))
277}
278
279// GetSender sends the Get request. The method will close the
280// http.Response Body if it receives an error.
281func (client VariableClient) GetSender(req *http.Request) (*http.Response, error) {
282	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
283}
284
285// GetResponder handles the response to the Get request. The method always
286// closes the http.Response Body.
287func (client VariableClient) GetResponder(resp *http.Response) (result Variable, err error) {
288	err = autorest.Respond(
289		resp,
290		azure.WithErrorUnlessStatusCode(http.StatusOK),
291		autorest.ByUnmarshallingJSON(&result),
292		autorest.ByClosing())
293	result.Response = autorest.Response{Response: resp}
294	return
295}
296
297// ListByAutomationAccount retrieve a list of variables.
298// Parameters:
299// resourceGroupName - name of an Azure Resource group.
300// automationAccountName - the name of the automation account.
301func (client VariableClient) ListByAutomationAccount(ctx context.Context, resourceGroupName string, automationAccountName string) (result VariableListResultPage, err error) {
302	if tracing.IsEnabled() {
303		ctx = tracing.StartSpan(ctx, fqdn+"/VariableClient.ListByAutomationAccount")
304		defer func() {
305			sc := -1
306			if result.vlr.Response.Response != nil {
307				sc = result.vlr.Response.Response.StatusCode
308			}
309			tracing.EndSpan(ctx, sc, err)
310		}()
311	}
312	if err := validation.Validate([]validation.Validation{
313		{TargetValue: resourceGroupName,
314			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
315				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
316				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
317		return result, validation.NewError("automation.VariableClient", "ListByAutomationAccount", err.Error())
318	}
319
320	result.fn = client.listByAutomationAccountNextResults
321	req, err := client.ListByAutomationAccountPreparer(ctx, resourceGroupName, automationAccountName)
322	if err != nil {
323		err = autorest.NewErrorWithError(err, "automation.VariableClient", "ListByAutomationAccount", nil, "Failure preparing request")
324		return
325	}
326
327	resp, err := client.ListByAutomationAccountSender(req)
328	if err != nil {
329		result.vlr.Response = autorest.Response{Response: resp}
330		err = autorest.NewErrorWithError(err, "automation.VariableClient", "ListByAutomationAccount", resp, "Failure sending request")
331		return
332	}
333
334	result.vlr, err = client.ListByAutomationAccountResponder(resp)
335	if err != nil {
336		err = autorest.NewErrorWithError(err, "automation.VariableClient", "ListByAutomationAccount", resp, "Failure responding to request")
337		return
338	}
339	if result.vlr.hasNextLink() && result.vlr.IsEmpty() {
340		err = result.NextWithContext(ctx)
341		return
342	}
343
344	return
345}
346
347// ListByAutomationAccountPreparer prepares the ListByAutomationAccount request.
348func (client VariableClient) ListByAutomationAccountPreparer(ctx context.Context, resourceGroupName string, automationAccountName string) (*http.Request, error) {
349	pathParameters := map[string]interface{}{
350		"automationAccountName": autorest.Encode("path", automationAccountName),
351		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
352		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
353	}
354
355	const APIVersion = "2015-10-31"
356	queryParameters := map[string]interface{}{
357		"api-version": APIVersion,
358	}
359
360	preparer := autorest.CreatePreparer(
361		autorest.AsGet(),
362		autorest.WithBaseURL(client.BaseURI),
363		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/variables", pathParameters),
364		autorest.WithQueryParameters(queryParameters))
365	return preparer.Prepare((&http.Request{}).WithContext(ctx))
366}
367
368// ListByAutomationAccountSender sends the ListByAutomationAccount request. The method will close the
369// http.Response Body if it receives an error.
370func (client VariableClient) ListByAutomationAccountSender(req *http.Request) (*http.Response, error) {
371	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
372}
373
374// ListByAutomationAccountResponder handles the response to the ListByAutomationAccount request. The method always
375// closes the http.Response Body.
376func (client VariableClient) ListByAutomationAccountResponder(resp *http.Response) (result VariableListResult, err error) {
377	err = autorest.Respond(
378		resp,
379		azure.WithErrorUnlessStatusCode(http.StatusOK),
380		autorest.ByUnmarshallingJSON(&result),
381		autorest.ByClosing())
382	result.Response = autorest.Response{Response: resp}
383	return
384}
385
386// listByAutomationAccountNextResults retrieves the next set of results, if any.
387func (client VariableClient) listByAutomationAccountNextResults(ctx context.Context, lastResults VariableListResult) (result VariableListResult, err error) {
388	req, err := lastResults.variableListResultPreparer(ctx)
389	if err != nil {
390		return result, autorest.NewErrorWithError(err, "automation.VariableClient", "listByAutomationAccountNextResults", nil, "Failure preparing next results request")
391	}
392	if req == nil {
393		return
394	}
395	resp, err := client.ListByAutomationAccountSender(req)
396	if err != nil {
397		result.Response = autorest.Response{Response: resp}
398		return result, autorest.NewErrorWithError(err, "automation.VariableClient", "listByAutomationAccountNextResults", resp, "Failure sending next results request")
399	}
400	result, err = client.ListByAutomationAccountResponder(resp)
401	if err != nil {
402		err = autorest.NewErrorWithError(err, "automation.VariableClient", "listByAutomationAccountNextResults", resp, "Failure responding to next results request")
403	}
404	return
405}
406
407// ListByAutomationAccountComplete enumerates all values, automatically crossing page boundaries as required.
408func (client VariableClient) ListByAutomationAccountComplete(ctx context.Context, resourceGroupName string, automationAccountName string) (result VariableListResultIterator, err error) {
409	if tracing.IsEnabled() {
410		ctx = tracing.StartSpan(ctx, fqdn+"/VariableClient.ListByAutomationAccount")
411		defer func() {
412			sc := -1
413			if result.Response().Response.Response != nil {
414				sc = result.page.Response().Response.Response.StatusCode
415			}
416			tracing.EndSpan(ctx, sc, err)
417		}()
418	}
419	result.page, err = client.ListByAutomationAccount(ctx, resourceGroupName, automationAccountName)
420	return
421}
422
423// Update update a variable.
424// Parameters:
425// resourceGroupName - name of an Azure Resource group.
426// automationAccountName - the name of the automation account.
427// variableName - the variable name.
428// parameters - the parameters supplied to the update variable operation.
429func (client VariableClient) Update(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string, parameters VariableUpdateParameters) (result Variable, err error) {
430	if tracing.IsEnabled() {
431		ctx = tracing.StartSpan(ctx, fqdn+"/VariableClient.Update")
432		defer func() {
433			sc := -1
434			if result.Response.Response != nil {
435				sc = result.Response.Response.StatusCode
436			}
437			tracing.EndSpan(ctx, sc, err)
438		}()
439	}
440	if err := validation.Validate([]validation.Validation{
441		{TargetValue: resourceGroupName,
442			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
443				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
444				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
445		return result, validation.NewError("automation.VariableClient", "Update", err.Error())
446	}
447
448	req, err := client.UpdatePreparer(ctx, resourceGroupName, automationAccountName, variableName, parameters)
449	if err != nil {
450		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Update", nil, "Failure preparing request")
451		return
452	}
453
454	resp, err := client.UpdateSender(req)
455	if err != nil {
456		result.Response = autorest.Response{Response: resp}
457		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Update", resp, "Failure sending request")
458		return
459	}
460
461	result, err = client.UpdateResponder(resp)
462	if err != nil {
463		err = autorest.NewErrorWithError(err, "automation.VariableClient", "Update", resp, "Failure responding to request")
464		return
465	}
466
467	return
468}
469
470// UpdatePreparer prepares the Update request.
471func (client VariableClient) UpdatePreparer(ctx context.Context, resourceGroupName string, automationAccountName string, variableName string, parameters VariableUpdateParameters) (*http.Request, error) {
472	pathParameters := map[string]interface{}{
473		"automationAccountName": autorest.Encode("path", automationAccountName),
474		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
475		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
476		"variableName":          autorest.Encode("path", variableName),
477	}
478
479	const APIVersion = "2015-10-31"
480	queryParameters := map[string]interface{}{
481		"api-version": APIVersion,
482	}
483
484	preparer := autorest.CreatePreparer(
485		autorest.AsContentType("application/json; charset=utf-8"),
486		autorest.AsPatch(),
487		autorest.WithBaseURL(client.BaseURI),
488		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/variables/{variableName}", pathParameters),
489		autorest.WithJSON(parameters),
490		autorest.WithQueryParameters(queryParameters))
491	return preparer.Prepare((&http.Request{}).WithContext(ctx))
492}
493
494// UpdateSender sends the Update request. The method will close the
495// http.Response Body if it receives an error.
496func (client VariableClient) UpdateSender(req *http.Request) (*http.Response, error) {
497	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
498}
499
500// UpdateResponder handles the response to the Update request. The method always
501// closes the http.Response Body.
502func (client VariableClient) UpdateResponder(resp *http.Response) (result Variable, err error) {
503	err = autorest.Respond(
504		resp,
505		azure.WithErrorUnlessStatusCode(http.StatusOK),
506		autorest.ByUnmarshallingJSON(&result),
507		autorest.ByClosing())
508	result.Response = autorest.Response{Response: resp}
509	return
510}
511