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