1package security
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// ConnectorsClient is the API spec for Microsoft.Security (Azure Security Center) resource provider
19type ConnectorsClient struct {
20	BaseClient
21}
22
23// NewConnectorsClient creates an instance of the ConnectorsClient client.
24func NewConnectorsClient(subscriptionID string, ascLocation string) ConnectorsClient {
25	return NewConnectorsClientWithBaseURI(DefaultBaseURI, subscriptionID, ascLocation)
26}
27
28// NewConnectorsClientWithBaseURI creates an instance of the ConnectorsClient 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 NewConnectorsClientWithBaseURI(baseURI string, subscriptionID string, ascLocation string) ConnectorsClient {
31	return ConnectorsClient{NewWithBaseURI(baseURI, subscriptionID, ascLocation)}
32}
33
34// CreateOrUpdate create a cloud account connector or update an existing one. Connect to your cloud account. For AWS,
35// use either account credentials or role-based authentication. For GCP, use account organization credentials.
36// Parameters:
37// connectorName - name of the cloud account connector
38// connectorSetting - settings for the cloud account connector
39func (client ConnectorsClient) CreateOrUpdate(ctx context.Context, connectorName string, connectorSetting ConnectorSetting) (result ConnectorSetting, err error) {
40	if tracing.IsEnabled() {
41		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectorsClient.CreateOrUpdate")
42		defer func() {
43			sc := -1
44			if result.Response.Response != nil {
45				sc = result.Response.Response.StatusCode
46			}
47			tracing.EndSpan(ctx, sc, err)
48		}()
49	}
50	if err := validation.Validate([]validation.Validation{
51		{TargetValue: client.SubscriptionID,
52			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.Pattern, Rule: `^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`, Chain: nil}}}}); err != nil {
53		return result, validation.NewError("security.ConnectorsClient", "CreateOrUpdate", err.Error())
54	}
55
56	req, err := client.CreateOrUpdatePreparer(ctx, connectorName, connectorSetting)
57	if err != nil {
58		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "CreateOrUpdate", nil, "Failure preparing request")
59		return
60	}
61
62	resp, err := client.CreateOrUpdateSender(req)
63	if err != nil {
64		result.Response = autorest.Response{Response: resp}
65		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "CreateOrUpdate", resp, "Failure sending request")
66		return
67	}
68
69	result, err = client.CreateOrUpdateResponder(resp)
70	if err != nil {
71		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "CreateOrUpdate", resp, "Failure responding to request")
72		return
73	}
74
75	return
76}
77
78// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
79func (client ConnectorsClient) CreateOrUpdatePreparer(ctx context.Context, connectorName string, connectorSetting ConnectorSetting) (*http.Request, error) {
80	pathParameters := map[string]interface{}{
81		"connectorName":  autorest.Encode("path", connectorName),
82		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
83	}
84
85	const APIVersion = "2020-01-01-preview"
86	queryParameters := map[string]interface{}{
87		"api-version": APIVersion,
88	}
89
90	preparer := autorest.CreatePreparer(
91		autorest.AsContentType("application/json; charset=utf-8"),
92		autorest.AsPut(),
93		autorest.WithBaseURL(client.BaseURI),
94		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Security/connectors/{connectorName}", pathParameters),
95		autorest.WithJSON(connectorSetting),
96		autorest.WithQueryParameters(queryParameters))
97	return preparer.Prepare((&http.Request{}).WithContext(ctx))
98}
99
100// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
101// http.Response Body if it receives an error.
102func (client ConnectorsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
103	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
104}
105
106// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
107// closes the http.Response Body.
108func (client ConnectorsClient) CreateOrUpdateResponder(resp *http.Response) (result ConnectorSetting, err error) {
109	err = autorest.Respond(
110		resp,
111		azure.WithErrorUnlessStatusCode(http.StatusOK),
112		autorest.ByUnmarshallingJSON(&result),
113		autorest.ByClosing())
114	result.Response = autorest.Response{Response: resp}
115	return
116}
117
118// Delete delete a cloud account connector from a subscription
119// Parameters:
120// connectorName - name of the cloud account connector
121func (client ConnectorsClient) Delete(ctx context.Context, connectorName string) (result autorest.Response, err error) {
122	if tracing.IsEnabled() {
123		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectorsClient.Delete")
124		defer func() {
125			sc := -1
126			if result.Response != nil {
127				sc = result.Response.StatusCode
128			}
129			tracing.EndSpan(ctx, sc, err)
130		}()
131	}
132	if err := validation.Validate([]validation.Validation{
133		{TargetValue: client.SubscriptionID,
134			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.Pattern, Rule: `^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`, Chain: nil}}}}); err != nil {
135		return result, validation.NewError("security.ConnectorsClient", "Delete", err.Error())
136	}
137
138	req, err := client.DeletePreparer(ctx, connectorName)
139	if err != nil {
140		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "Delete", nil, "Failure preparing request")
141		return
142	}
143
144	resp, err := client.DeleteSender(req)
145	if err != nil {
146		result.Response = resp
147		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "Delete", resp, "Failure sending request")
148		return
149	}
150
151	result, err = client.DeleteResponder(resp)
152	if err != nil {
153		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "Delete", resp, "Failure responding to request")
154		return
155	}
156
157	return
158}
159
160// DeletePreparer prepares the Delete request.
161func (client ConnectorsClient) DeletePreparer(ctx context.Context, connectorName string) (*http.Request, error) {
162	pathParameters := map[string]interface{}{
163		"connectorName":  autorest.Encode("path", connectorName),
164		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
165	}
166
167	const APIVersion = "2020-01-01-preview"
168	queryParameters := map[string]interface{}{
169		"api-version": APIVersion,
170	}
171
172	preparer := autorest.CreatePreparer(
173		autorest.AsDelete(),
174		autorest.WithBaseURL(client.BaseURI),
175		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Security/connectors/{connectorName}", pathParameters),
176		autorest.WithQueryParameters(queryParameters))
177	return preparer.Prepare((&http.Request{}).WithContext(ctx))
178}
179
180// DeleteSender sends the Delete request. The method will close the
181// http.Response Body if it receives an error.
182func (client ConnectorsClient) DeleteSender(req *http.Request) (*http.Response, error) {
183	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
184}
185
186// DeleteResponder handles the response to the Delete request. The method always
187// closes the http.Response Body.
188func (client ConnectorsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
189	err = autorest.Respond(
190		resp,
191		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
192		autorest.ByClosing())
193	result.Response = resp
194	return
195}
196
197// Get details of a specific cloud account connector
198// Parameters:
199// connectorName - name of the cloud account connector
200func (client ConnectorsClient) Get(ctx context.Context, connectorName string) (result ConnectorSetting, err error) {
201	if tracing.IsEnabled() {
202		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectorsClient.Get")
203		defer func() {
204			sc := -1
205			if result.Response.Response != nil {
206				sc = result.Response.Response.StatusCode
207			}
208			tracing.EndSpan(ctx, sc, err)
209		}()
210	}
211	if err := validation.Validate([]validation.Validation{
212		{TargetValue: client.SubscriptionID,
213			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.Pattern, Rule: `^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`, Chain: nil}}}}); err != nil {
214		return result, validation.NewError("security.ConnectorsClient", "Get", err.Error())
215	}
216
217	req, err := client.GetPreparer(ctx, connectorName)
218	if err != nil {
219		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "Get", nil, "Failure preparing request")
220		return
221	}
222
223	resp, err := client.GetSender(req)
224	if err != nil {
225		result.Response = autorest.Response{Response: resp}
226		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "Get", resp, "Failure sending request")
227		return
228	}
229
230	result, err = client.GetResponder(resp)
231	if err != nil {
232		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "Get", resp, "Failure responding to request")
233		return
234	}
235
236	return
237}
238
239// GetPreparer prepares the Get request.
240func (client ConnectorsClient) GetPreparer(ctx context.Context, connectorName string) (*http.Request, error) {
241	pathParameters := map[string]interface{}{
242		"connectorName":  autorest.Encode("path", connectorName),
243		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
244	}
245
246	const APIVersion = "2020-01-01-preview"
247	queryParameters := map[string]interface{}{
248		"api-version": APIVersion,
249	}
250
251	preparer := autorest.CreatePreparer(
252		autorest.AsGet(),
253		autorest.WithBaseURL(client.BaseURI),
254		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Security/connectors/{connectorName}", pathParameters),
255		autorest.WithQueryParameters(queryParameters))
256	return preparer.Prepare((&http.Request{}).WithContext(ctx))
257}
258
259// GetSender sends the Get request. The method will close the
260// http.Response Body if it receives an error.
261func (client ConnectorsClient) GetSender(req *http.Request) (*http.Response, error) {
262	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
263}
264
265// GetResponder handles the response to the Get request. The method always
266// closes the http.Response Body.
267func (client ConnectorsClient) GetResponder(resp *http.Response) (result ConnectorSetting, err error) {
268	err = autorest.Respond(
269		resp,
270		azure.WithErrorUnlessStatusCode(http.StatusOK),
271		autorest.ByUnmarshallingJSON(&result),
272		autorest.ByClosing())
273	result.Response = autorest.Response{Response: resp}
274	return
275}
276
277// List cloud accounts connectors of a subscription
278func (client ConnectorsClient) List(ctx context.Context) (result ConnectorSettingListPage, err error) {
279	if tracing.IsEnabled() {
280		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectorsClient.List")
281		defer func() {
282			sc := -1
283			if result.csl.Response.Response != nil {
284				sc = result.csl.Response.Response.StatusCode
285			}
286			tracing.EndSpan(ctx, sc, err)
287		}()
288	}
289	if err := validation.Validate([]validation.Validation{
290		{TargetValue: client.SubscriptionID,
291			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.Pattern, Rule: `^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`, Chain: nil}}}}); err != nil {
292		return result, validation.NewError("security.ConnectorsClient", "List", err.Error())
293	}
294
295	result.fn = client.listNextResults
296	req, err := client.ListPreparer(ctx)
297	if err != nil {
298		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "List", nil, "Failure preparing request")
299		return
300	}
301
302	resp, err := client.ListSender(req)
303	if err != nil {
304		result.csl.Response = autorest.Response{Response: resp}
305		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "List", resp, "Failure sending request")
306		return
307	}
308
309	result.csl, err = client.ListResponder(resp)
310	if err != nil {
311		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "List", resp, "Failure responding to request")
312		return
313	}
314	if result.csl.hasNextLink() && result.csl.IsEmpty() {
315		err = result.NextWithContext(ctx)
316		return
317	}
318
319	return
320}
321
322// ListPreparer prepares the List request.
323func (client ConnectorsClient) ListPreparer(ctx context.Context) (*http.Request, error) {
324	pathParameters := map[string]interface{}{
325		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
326	}
327
328	const APIVersion = "2020-01-01-preview"
329	queryParameters := map[string]interface{}{
330		"api-version": APIVersion,
331	}
332
333	preparer := autorest.CreatePreparer(
334		autorest.AsGet(),
335		autorest.WithBaseURL(client.BaseURI),
336		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Security/connectors", pathParameters),
337		autorest.WithQueryParameters(queryParameters))
338	return preparer.Prepare((&http.Request{}).WithContext(ctx))
339}
340
341// ListSender sends the List request. The method will close the
342// http.Response Body if it receives an error.
343func (client ConnectorsClient) ListSender(req *http.Request) (*http.Response, error) {
344	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
345}
346
347// ListResponder handles the response to the List request. The method always
348// closes the http.Response Body.
349func (client ConnectorsClient) ListResponder(resp *http.Response) (result ConnectorSettingList, err error) {
350	err = autorest.Respond(
351		resp,
352		azure.WithErrorUnlessStatusCode(http.StatusOK),
353		autorest.ByUnmarshallingJSON(&result),
354		autorest.ByClosing())
355	result.Response = autorest.Response{Response: resp}
356	return
357}
358
359// listNextResults retrieves the next set of results, if any.
360func (client ConnectorsClient) listNextResults(ctx context.Context, lastResults ConnectorSettingList) (result ConnectorSettingList, err error) {
361	req, err := lastResults.connectorSettingListPreparer(ctx)
362	if err != nil {
363		return result, autorest.NewErrorWithError(err, "security.ConnectorsClient", "listNextResults", nil, "Failure preparing next results request")
364	}
365	if req == nil {
366		return
367	}
368	resp, err := client.ListSender(req)
369	if err != nil {
370		result.Response = autorest.Response{Response: resp}
371		return result, autorest.NewErrorWithError(err, "security.ConnectorsClient", "listNextResults", resp, "Failure sending next results request")
372	}
373	result, err = client.ListResponder(resp)
374	if err != nil {
375		err = autorest.NewErrorWithError(err, "security.ConnectorsClient", "listNextResults", resp, "Failure responding to next results request")
376	}
377	return
378}
379
380// ListComplete enumerates all values, automatically crossing page boundaries as required.
381func (client ConnectorsClient) ListComplete(ctx context.Context) (result ConnectorSettingListIterator, err error) {
382	if tracing.IsEnabled() {
383		ctx = tracing.StartSpan(ctx, fqdn+"/ConnectorsClient.List")
384		defer func() {
385			sc := -1
386			if result.Response().Response.Response != nil {
387				sc = result.page.Response().Response.Response.StatusCode
388			}
389			tracing.EndSpan(ctx, sc, err)
390		}()
391	}
392	result.page, err = client.List(ctx)
393	return
394}
395