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