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// ConnectionClient is the automation Client
30type ConnectionClient struct {
31	BaseClient
32}
33
34// NewConnectionClient creates an instance of the ConnectionClient client.
35func NewConnectionClient(subscriptionID string) ConnectionClient {
36	return NewConnectionClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewConnectionClientWithBaseURI creates an instance of the ConnectionClient 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 NewConnectionClientWithBaseURI(baseURI string, subscriptionID string) ConnectionClient {
42	return ConnectionClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// CreateOrUpdate create or update a connection.
46// Parameters:
47// resourceGroupName - name of an Azure Resource group.
48// automationAccountName - the name of the automation account.
49// connectionName - the parameters supplied to the create or update connection operation.
50// parameters - the parameters supplied to the create or update connection operation.
51func (client ConnectionClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string, parameters ConnectionCreateOrUpdateParameters) (result Connection, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectionClient.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.ConnectionCreateOrUpdateProperties", Name: validation.Null, Rule: true,
70					Chain: []validation.Constraint{{Target: "parameters.ConnectionCreateOrUpdateProperties.ConnectionType", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
71		return result, validation.NewError("automation.ConnectionClient", "CreateOrUpdate", err.Error())
72	}
73
74	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, automationAccountName, connectionName, parameters)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "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.ConnectionClient", "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.ConnectionClient", "CreateOrUpdate", resp, "Failure responding to request")
90	}
91
92	return
93}
94
95// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
96func (client ConnectionClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string, parameters ConnectionCreateOrUpdateParameters) (*http.Request, error) {
97	pathParameters := map[string]interface{}{
98		"automationAccountName": autorest.Encode("path", automationAccountName),
99		"connectionName":        autorest.Encode("path", connectionName),
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}/connections/{connectionName}", 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 ConnectionClient) 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 ConnectionClient) CreateOrUpdateResponder(resp *http.Response) (result Connection, 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 connection.
139// Parameters:
140// resourceGroupName - name of an Azure Resource group.
141// automationAccountName - the name of the automation account.
142// connectionName - the name of connection.
143func (client ConnectionClient) Delete(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string) (result Connection, err error) {
144	if tracing.IsEnabled() {
145		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectionClient.Delete")
146		defer func() {
147			sc := -1
148			if result.Response.Response != nil {
149				sc = result.Response.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.ConnectionClient", "Delete", err.Error())
160	}
161
162	req, err := client.DeletePreparer(ctx, resourceGroupName, automationAccountName, connectionName)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Delete", nil, "Failure preparing request")
165		return
166	}
167
168	resp, err := client.DeleteSender(req)
169	if err != nil {
170		result.Response = autorest.Response{Response: resp}
171		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "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.ConnectionClient", "Delete", resp, "Failure responding to request")
178	}
179
180	return
181}
182
183// DeletePreparer prepares the Delete request.
184func (client ConnectionClient) DeletePreparer(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string) (*http.Request, error) {
185	pathParameters := map[string]interface{}{
186		"automationAccountName": autorest.Encode("path", automationAccountName),
187		"connectionName":        autorest.Encode("path", connectionName),
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}/connections/{connectionName}", 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 ConnectionClient) 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 ConnectionClient) DeleteResponder(resp *http.Response) (result Connection, err error) {
214	err = autorest.Respond(
215		resp,
216		client.ByInspecting(),
217		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
218		autorest.ByUnmarshallingJSON(&result),
219		autorest.ByClosing())
220	result.Response = autorest.Response{Response: resp}
221	return
222}
223
224// Get retrieve the connection identified by connection name.
225// Parameters:
226// resourceGroupName - name of an Azure Resource group.
227// automationAccountName - the name of the automation account.
228// connectionName - the name of connection.
229func (client ConnectionClient) Get(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string) (result Connection, err error) {
230	if tracing.IsEnabled() {
231		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectionClient.Get")
232		defer func() {
233			sc := -1
234			if result.Response.Response != nil {
235				sc = result.Response.Response.StatusCode
236			}
237			tracing.EndSpan(ctx, sc, err)
238		}()
239	}
240	if err := validation.Validate([]validation.Validation{
241		{TargetValue: resourceGroupName,
242			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
243				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
244				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
245		return result, validation.NewError("automation.ConnectionClient", "Get", err.Error())
246	}
247
248	req, err := client.GetPreparer(ctx, resourceGroupName, automationAccountName, connectionName)
249	if err != nil {
250		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Get", nil, "Failure preparing request")
251		return
252	}
253
254	resp, err := client.GetSender(req)
255	if err != nil {
256		result.Response = autorest.Response{Response: resp}
257		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Get", resp, "Failure sending request")
258		return
259	}
260
261	result, err = client.GetResponder(resp)
262	if err != nil {
263		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Get", resp, "Failure responding to request")
264	}
265
266	return
267}
268
269// GetPreparer prepares the Get request.
270func (client ConnectionClient) GetPreparer(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string) (*http.Request, error) {
271	pathParameters := map[string]interface{}{
272		"automationAccountName": autorest.Encode("path", automationAccountName),
273		"connectionName":        autorest.Encode("path", connectionName),
274		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
275		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
276	}
277
278	const APIVersion = "2015-10-31"
279	queryParameters := map[string]interface{}{
280		"api-version": APIVersion,
281	}
282
283	preparer := autorest.CreatePreparer(
284		autorest.AsGet(),
285		autorest.WithBaseURL(client.BaseURI),
286		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/connections/{connectionName}", pathParameters),
287		autorest.WithQueryParameters(queryParameters))
288	return preparer.Prepare((&http.Request{}).WithContext(ctx))
289}
290
291// GetSender sends the Get request. The method will close the
292// http.Response Body if it receives an error.
293func (client ConnectionClient) GetSender(req *http.Request) (*http.Response, error) {
294	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
295}
296
297// GetResponder handles the response to the Get request. The method always
298// closes the http.Response Body.
299func (client ConnectionClient) GetResponder(resp *http.Response) (result Connection, err error) {
300	err = autorest.Respond(
301		resp,
302		client.ByInspecting(),
303		azure.WithErrorUnlessStatusCode(http.StatusOK),
304		autorest.ByUnmarshallingJSON(&result),
305		autorest.ByClosing())
306	result.Response = autorest.Response{Response: resp}
307	return
308}
309
310// ListByAutomationAccount retrieve a list of connections.
311// Parameters:
312// resourceGroupName - name of an Azure Resource group.
313// automationAccountName - the name of the automation account.
314func (client ConnectionClient) ListByAutomationAccount(ctx context.Context, resourceGroupName string, automationAccountName string) (result ConnectionListResultPage, err error) {
315	if tracing.IsEnabled() {
316		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectionClient.ListByAutomationAccount")
317		defer func() {
318			sc := -1
319			if result.clr.Response.Response != nil {
320				sc = result.clr.Response.Response.StatusCode
321			}
322			tracing.EndSpan(ctx, sc, err)
323		}()
324	}
325	if err := validation.Validate([]validation.Validation{
326		{TargetValue: resourceGroupName,
327			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
328				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
329				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
330		return result, validation.NewError("automation.ConnectionClient", "ListByAutomationAccount", err.Error())
331	}
332
333	result.fn = client.listByAutomationAccountNextResults
334	req, err := client.ListByAutomationAccountPreparer(ctx, resourceGroupName, automationAccountName)
335	if err != nil {
336		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "ListByAutomationAccount", nil, "Failure preparing request")
337		return
338	}
339
340	resp, err := client.ListByAutomationAccountSender(req)
341	if err != nil {
342		result.clr.Response = autorest.Response{Response: resp}
343		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "ListByAutomationAccount", resp, "Failure sending request")
344		return
345	}
346
347	result.clr, err = client.ListByAutomationAccountResponder(resp)
348	if err != nil {
349		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "ListByAutomationAccount", resp, "Failure responding to request")
350	}
351
352	return
353}
354
355// ListByAutomationAccountPreparer prepares the ListByAutomationAccount request.
356func (client ConnectionClient) ListByAutomationAccountPreparer(ctx context.Context, resourceGroupName string, automationAccountName string) (*http.Request, error) {
357	pathParameters := map[string]interface{}{
358		"automationAccountName": autorest.Encode("path", automationAccountName),
359		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
360		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
361	}
362
363	const APIVersion = "2015-10-31"
364	queryParameters := map[string]interface{}{
365		"api-version": APIVersion,
366	}
367
368	preparer := autorest.CreatePreparer(
369		autorest.AsGet(),
370		autorest.WithBaseURL(client.BaseURI),
371		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/connections", pathParameters),
372		autorest.WithQueryParameters(queryParameters))
373	return preparer.Prepare((&http.Request{}).WithContext(ctx))
374}
375
376// ListByAutomationAccountSender sends the ListByAutomationAccount request. The method will close the
377// http.Response Body if it receives an error.
378func (client ConnectionClient) ListByAutomationAccountSender(req *http.Request) (*http.Response, error) {
379	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
380}
381
382// ListByAutomationAccountResponder handles the response to the ListByAutomationAccount request. The method always
383// closes the http.Response Body.
384func (client ConnectionClient) ListByAutomationAccountResponder(resp *http.Response) (result ConnectionListResult, err error) {
385	err = autorest.Respond(
386		resp,
387		client.ByInspecting(),
388		azure.WithErrorUnlessStatusCode(http.StatusOK),
389		autorest.ByUnmarshallingJSON(&result),
390		autorest.ByClosing())
391	result.Response = autorest.Response{Response: resp}
392	return
393}
394
395// listByAutomationAccountNextResults retrieves the next set of results, if any.
396func (client ConnectionClient) listByAutomationAccountNextResults(ctx context.Context, lastResults ConnectionListResult) (result ConnectionListResult, err error) {
397	req, err := lastResults.connectionListResultPreparer(ctx)
398	if err != nil {
399		return result, autorest.NewErrorWithError(err, "automation.ConnectionClient", "listByAutomationAccountNextResults", nil, "Failure preparing next results request")
400	}
401	if req == nil {
402		return
403	}
404	resp, err := client.ListByAutomationAccountSender(req)
405	if err != nil {
406		result.Response = autorest.Response{Response: resp}
407		return result, autorest.NewErrorWithError(err, "automation.ConnectionClient", "listByAutomationAccountNextResults", resp, "Failure sending next results request")
408	}
409	result, err = client.ListByAutomationAccountResponder(resp)
410	if err != nil {
411		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "listByAutomationAccountNextResults", resp, "Failure responding to next results request")
412	}
413	return
414}
415
416// ListByAutomationAccountComplete enumerates all values, automatically crossing page boundaries as required.
417func (client ConnectionClient) ListByAutomationAccountComplete(ctx context.Context, resourceGroupName string, automationAccountName string) (result ConnectionListResultIterator, err error) {
418	if tracing.IsEnabled() {
419		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectionClient.ListByAutomationAccount")
420		defer func() {
421			sc := -1
422			if result.Response().Response.Response != nil {
423				sc = result.page.Response().Response.Response.StatusCode
424			}
425			tracing.EndSpan(ctx, sc, err)
426		}()
427	}
428	result.page, err = client.ListByAutomationAccount(ctx, resourceGroupName, automationAccountName)
429	return
430}
431
432// Update update a connection.
433// Parameters:
434// resourceGroupName - name of an Azure Resource group.
435// automationAccountName - the name of the automation account.
436// connectionName - the parameters supplied to the update a connection operation.
437// parameters - the parameters supplied to the update a connection operation.
438func (client ConnectionClient) Update(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string, parameters ConnectionUpdateParameters) (result Connection, err error) {
439	if tracing.IsEnabled() {
440		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectionClient.Update")
441		defer func() {
442			sc := -1
443			if result.Response.Response != nil {
444				sc = result.Response.Response.StatusCode
445			}
446			tracing.EndSpan(ctx, sc, err)
447		}()
448	}
449	if err := validation.Validate([]validation.Validation{
450		{TargetValue: resourceGroupName,
451			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
452				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
453				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
454		return result, validation.NewError("automation.ConnectionClient", "Update", err.Error())
455	}
456
457	req, err := client.UpdatePreparer(ctx, resourceGroupName, automationAccountName, connectionName, parameters)
458	if err != nil {
459		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Update", nil, "Failure preparing request")
460		return
461	}
462
463	resp, err := client.UpdateSender(req)
464	if err != nil {
465		result.Response = autorest.Response{Response: resp}
466		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Update", resp, "Failure sending request")
467		return
468	}
469
470	result, err = client.UpdateResponder(resp)
471	if err != nil {
472		err = autorest.NewErrorWithError(err, "automation.ConnectionClient", "Update", resp, "Failure responding to request")
473	}
474
475	return
476}
477
478// UpdatePreparer prepares the Update request.
479func (client ConnectionClient) UpdatePreparer(ctx context.Context, resourceGroupName string, automationAccountName string, connectionName string, parameters ConnectionUpdateParameters) (*http.Request, error) {
480	pathParameters := map[string]interface{}{
481		"automationAccountName": autorest.Encode("path", automationAccountName),
482		"connectionName":        autorest.Encode("path", connectionName),
483		"resourceGroupName":     autorest.Encode("path", resourceGroupName),
484		"subscriptionId":        autorest.Encode("path", client.SubscriptionID),
485	}
486
487	const APIVersion = "2015-10-31"
488	queryParameters := map[string]interface{}{
489		"api-version": APIVersion,
490	}
491
492	preparer := autorest.CreatePreparer(
493		autorest.AsContentType("application/json; charset=utf-8"),
494		autorest.AsPatch(),
495		autorest.WithBaseURL(client.BaseURI),
496		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/connections/{connectionName}", pathParameters),
497		autorest.WithJSON(parameters),
498		autorest.WithQueryParameters(queryParameters))
499	return preparer.Prepare((&http.Request{}).WithContext(ctx))
500}
501
502// UpdateSender sends the Update request. The method will close the
503// http.Response Body if it receives an error.
504func (client ConnectionClient) UpdateSender(req *http.Request) (*http.Response, error) {
505	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
506}
507
508// UpdateResponder handles the response to the Update request. The method always
509// closes the http.Response Body.
510func (client ConnectionClient) UpdateResponder(resp *http.Response) (result Connection, err error) {
511	err = autorest.Respond(
512		resp,
513		client.ByInspecting(),
514		azure.WithErrorUnlessStatusCode(http.StatusOK),
515		autorest.ByUnmarshallingJSON(&result),
516		autorest.ByClosing())
517	result.Response = autorest.Response{Response: resp}
518	return
519}
520