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// AgentRegistrationInformationClient is the automation Client
30type AgentRegistrationInformationClient struct {
31	BaseClient
32}
33
34// NewAgentRegistrationInformationClient creates an instance of the AgentRegistrationInformationClient client.
35func NewAgentRegistrationInformationClient(subscriptionID string) AgentRegistrationInformationClient {
36	return NewAgentRegistrationInformationClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewAgentRegistrationInformationClientWithBaseURI creates an instance of the AgentRegistrationInformationClient
40// client using a custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI
41// (sovereign clouds, Azure stack).
42func NewAgentRegistrationInformationClientWithBaseURI(baseURI string, subscriptionID string) AgentRegistrationInformationClient {
43	return AgentRegistrationInformationClient{NewWithBaseURI(baseURI, subscriptionID)}
44}
45
46// Get retrieve the automation agent registration information.
47// Parameters:
48// resourceGroupName - name of an Azure Resource group.
49// automationAccountName - the name of the automation account.
50func (client AgentRegistrationInformationClient) Get(ctx context.Context, resourceGroupName string, automationAccountName string) (result AgentRegistration, err error) {
51	if tracing.IsEnabled() {
52		ctx = tracing.StartSpan(ctx, fqdn+"/AgentRegistrationInformationClient.Get")
53		defer func() {
54			sc := -1
55			if result.Response.Response != nil {
56				sc = result.Response.Response.StatusCode
57			}
58			tracing.EndSpan(ctx, sc, err)
59		}()
60	}
61	if err := validation.Validate([]validation.Validation{
62		{TargetValue: resourceGroupName,
63			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
64				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
65				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
66		return result, validation.NewError("automation.AgentRegistrationInformationClient", "Get", err.Error())
67	}
68
69	req, err := client.GetPreparer(ctx, resourceGroupName, automationAccountName)
70	if err != nil {
71		err = autorest.NewErrorWithError(err, "automation.AgentRegistrationInformationClient", "Get", nil, "Failure preparing request")
72		return
73	}
74
75	resp, err := client.GetSender(req)
76	if err != nil {
77		result.Response = autorest.Response{Response: resp}
78		err = autorest.NewErrorWithError(err, "automation.AgentRegistrationInformationClient", "Get", resp, "Failure sending request")
79		return
80	}
81
82	result, err = client.GetResponder(resp)
83	if err != nil {
84		err = autorest.NewErrorWithError(err, "automation.AgentRegistrationInformationClient", "Get", resp, "Failure responding to request")
85	}
86
87	return
88}
89
90// GetPreparer prepares the Get request.
91func (client AgentRegistrationInformationClient) GetPreparer(ctx context.Context, resourceGroupName string, automationAccountName string) (*http.Request, error) {
92	pathParameters := map[string]interface{}{
93		"automationAccountName": autorest.Encode("path", automationAccountName),
94		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
95		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
96	}
97
98	const APIVersion = "2015-10-31"
99	queryParameters := map[string]interface{}{
100		"api-version": APIVersion,
101	}
102
103	preparer := autorest.CreatePreparer(
104		autorest.AsGet(),
105		autorest.WithBaseURL(client.BaseURI),
106		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/agentRegistrationInformation", pathParameters),
107		autorest.WithQueryParameters(queryParameters))
108	return preparer.Prepare((&http.Request{}).WithContext(ctx))
109}
110
111// GetSender sends the Get request. The method will close the
112// http.Response Body if it receives an error.
113func (client AgentRegistrationInformationClient) GetSender(req *http.Request) (*http.Response, error) {
114	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
115}
116
117// GetResponder handles the response to the Get request. The method always
118// closes the http.Response Body.
119func (client AgentRegistrationInformationClient) GetResponder(resp *http.Response) (result AgentRegistration, err error) {
120	err = autorest.Respond(
121		resp,
122		client.ByInspecting(),
123		azure.WithErrorUnlessStatusCode(http.StatusOK),
124		autorest.ByUnmarshallingJSON(&result),
125		autorest.ByClosing())
126	result.Response = autorest.Response{Response: resp}
127	return
128}
129
130// RegenerateKey regenerate a primary or secondary agent registration key
131// Parameters:
132// resourceGroupName - name of an Azure Resource group.
133// automationAccountName - the name of the automation account.
134// parameters - the name of the agent registration key to be regenerated
135func (client AgentRegistrationInformationClient) RegenerateKey(ctx context.Context, resourceGroupName string, automationAccountName string, parameters AgentRegistrationRegenerateKeyParameter) (result AgentRegistration, err error) {
136	if tracing.IsEnabled() {
137		ctx = tracing.StartSpan(ctx, fqdn+"/AgentRegistrationInformationClient.RegenerateKey")
138		defer func() {
139			sc := -1
140			if result.Response.Response != nil {
141				sc = result.Response.Response.StatusCode
142			}
143			tracing.EndSpan(ctx, sc, err)
144		}()
145	}
146	if err := validation.Validate([]validation.Validation{
147		{TargetValue: resourceGroupName,
148			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
149				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
150				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
151		return result, validation.NewError("automation.AgentRegistrationInformationClient", "RegenerateKey", err.Error())
152	}
153
154	req, err := client.RegenerateKeyPreparer(ctx, resourceGroupName, automationAccountName, parameters)
155	if err != nil {
156		err = autorest.NewErrorWithError(err, "automation.AgentRegistrationInformationClient", "RegenerateKey", nil, "Failure preparing request")
157		return
158	}
159
160	resp, err := client.RegenerateKeySender(req)
161	if err != nil {
162		result.Response = autorest.Response{Response: resp}
163		err = autorest.NewErrorWithError(err, "automation.AgentRegistrationInformationClient", "RegenerateKey", resp, "Failure sending request")
164		return
165	}
166
167	result, err = client.RegenerateKeyResponder(resp)
168	if err != nil {
169		err = autorest.NewErrorWithError(err, "automation.AgentRegistrationInformationClient", "RegenerateKey", resp, "Failure responding to request")
170	}
171
172	return
173}
174
175// RegenerateKeyPreparer prepares the RegenerateKey request.
176func (client AgentRegistrationInformationClient) RegenerateKeyPreparer(ctx context.Context, resourceGroupName string, automationAccountName string, parameters AgentRegistrationRegenerateKeyParameter) (*http.Request, error) {
177	pathParameters := map[string]interface{}{
178		"automationAccountName": autorest.Encode("path", automationAccountName),
179		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
180		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
181	}
182
183	const APIVersion = "2015-10-31"
184	queryParameters := map[string]interface{}{
185		"api-version": APIVersion,
186	}
187
188	preparer := autorest.CreatePreparer(
189		autorest.AsContentType("application/json; charset=utf-8"),
190		autorest.AsPost(),
191		autorest.WithBaseURL(client.BaseURI),
192		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/agentRegistrationInformation/regenerateKey", pathParameters),
193		autorest.WithJSON(parameters),
194		autorest.WithQueryParameters(queryParameters))
195	return preparer.Prepare((&http.Request{}).WithContext(ctx))
196}
197
198// RegenerateKeySender sends the RegenerateKey request. The method will close the
199// http.Response Body if it receives an error.
200func (client AgentRegistrationInformationClient) RegenerateKeySender(req *http.Request) (*http.Response, error) {
201	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
202}
203
204// RegenerateKeyResponder handles the response to the RegenerateKey request. The method always
205// closes the http.Response Body.
206func (client AgentRegistrationInformationClient) RegenerateKeyResponder(resp *http.Response) (result AgentRegistration, err error) {
207	err = autorest.Respond(
208		resp,
209		client.ByInspecting(),
210		azure.WithErrorUnlessStatusCode(http.StatusOK),
211		autorest.ByUnmarshallingJSON(&result),
212		autorest.ByClosing())
213	result.Response = autorest.Response{Response: resp}
214	return
215}
216