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// SecureScoreControlsClient is the API spec for Microsoft.Security (Azure Security Center) resource provider
19type SecureScoreControlsClient struct {
20	BaseClient
21}
22
23// NewSecureScoreControlsClient creates an instance of the SecureScoreControlsClient client.
24func NewSecureScoreControlsClient(subscriptionID string, ascLocation string) SecureScoreControlsClient {
25	return NewSecureScoreControlsClientWithBaseURI(DefaultBaseURI, subscriptionID, ascLocation)
26}
27
28// NewSecureScoreControlsClientWithBaseURI creates an instance of the SecureScoreControlsClient client using a custom
29// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
30// stack).
31func NewSecureScoreControlsClientWithBaseURI(baseURI string, subscriptionID string, ascLocation string) SecureScoreControlsClient {
32	return SecureScoreControlsClient{NewWithBaseURI(baseURI, subscriptionID, ascLocation)}
33}
34
35// List get all security controls within a scope
36// Parameters:
37// expand - oData expand. Optional.
38func (client SecureScoreControlsClient) List(ctx context.Context, expand ExpandControlsEnum) (result SecureScoreControlListPage, err error) {
39	if tracing.IsEnabled() {
40		ctx = tracing.StartSpan(ctx, fqdn+"/SecureScoreControlsClient.List")
41		defer func() {
42			sc := -1
43			if result.sscl.Response.Response != nil {
44				sc = result.sscl.Response.Response.StatusCode
45			}
46			tracing.EndSpan(ctx, sc, err)
47		}()
48	}
49	if err := validation.Validate([]validation.Validation{
50		{TargetValue: client.SubscriptionID,
51			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 {
52		return result, validation.NewError("security.SecureScoreControlsClient", "List", err.Error())
53	}
54
55	result.fn = client.listNextResults
56	req, err := client.ListPreparer(ctx, expand)
57	if err != nil {
58		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "List", nil, "Failure preparing request")
59		return
60	}
61
62	resp, err := client.ListSender(req)
63	if err != nil {
64		result.sscl.Response = autorest.Response{Response: resp}
65		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "List", resp, "Failure sending request")
66		return
67	}
68
69	result.sscl, err = client.ListResponder(resp)
70	if err != nil {
71		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "List", resp, "Failure responding to request")
72		return
73	}
74	if result.sscl.hasNextLink() && result.sscl.IsEmpty() {
75		err = result.NextWithContext(ctx)
76		return
77	}
78
79	return
80}
81
82// ListPreparer prepares the List request.
83func (client SecureScoreControlsClient) ListPreparer(ctx context.Context, expand ExpandControlsEnum) (*http.Request, error) {
84	pathParameters := map[string]interface{}{
85		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
86	}
87
88	const APIVersion = "2020-01-01-preview"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92	if len(string(expand)) > 0 {
93		queryParameters["$expand"] = autorest.Encode("query", expand)
94	}
95
96	preparer := autorest.CreatePreparer(
97		autorest.AsGet(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Security/secureScoreControls", pathParameters),
100		autorest.WithQueryParameters(queryParameters))
101	return preparer.Prepare((&http.Request{}).WithContext(ctx))
102}
103
104// ListSender sends the List request. The method will close the
105// http.Response Body if it receives an error.
106func (client SecureScoreControlsClient) ListSender(req *http.Request) (*http.Response, error) {
107	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
108}
109
110// ListResponder handles the response to the List request. The method always
111// closes the http.Response Body.
112func (client SecureScoreControlsClient) ListResponder(resp *http.Response) (result SecureScoreControlList, err error) {
113	err = autorest.Respond(
114		resp,
115		azure.WithErrorUnlessStatusCode(http.StatusOK),
116		autorest.ByUnmarshallingJSON(&result),
117		autorest.ByClosing())
118	result.Response = autorest.Response{Response: resp}
119	return
120}
121
122// listNextResults retrieves the next set of results, if any.
123func (client SecureScoreControlsClient) listNextResults(ctx context.Context, lastResults SecureScoreControlList) (result SecureScoreControlList, err error) {
124	req, err := lastResults.secureScoreControlListPreparer(ctx)
125	if err != nil {
126		return result, autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "listNextResults", nil, "Failure preparing next results request")
127	}
128	if req == nil {
129		return
130	}
131	resp, err := client.ListSender(req)
132	if err != nil {
133		result.Response = autorest.Response{Response: resp}
134		return result, autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "listNextResults", resp, "Failure sending next results request")
135	}
136	result, err = client.ListResponder(resp)
137	if err != nil {
138		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "listNextResults", resp, "Failure responding to next results request")
139	}
140	return
141}
142
143// ListComplete enumerates all values, automatically crossing page boundaries as required.
144func (client SecureScoreControlsClient) ListComplete(ctx context.Context, expand ExpandControlsEnum) (result SecureScoreControlListIterator, err error) {
145	if tracing.IsEnabled() {
146		ctx = tracing.StartSpan(ctx, fqdn+"/SecureScoreControlsClient.List")
147		defer func() {
148			sc := -1
149			if result.Response().Response.Response != nil {
150				sc = result.page.Response().Response.Response.StatusCode
151			}
152			tracing.EndSpan(ctx, sc, err)
153		}()
154	}
155	result.page, err = client.List(ctx, expand)
156	return
157}
158
159// ListBySecureScore get all security controls for a specific initiative within a scope
160// Parameters:
161// secureScoreName - the initiative name. For the ASC Default initiative, use 'ascScore' as in the sample
162// request below.
163// expand - oData expand. Optional.
164func (client SecureScoreControlsClient) ListBySecureScore(ctx context.Context, secureScoreName string, expand ExpandControlsEnum) (result SecureScoreControlListPage, err error) {
165	if tracing.IsEnabled() {
166		ctx = tracing.StartSpan(ctx, fqdn+"/SecureScoreControlsClient.ListBySecureScore")
167		defer func() {
168			sc := -1
169			if result.sscl.Response.Response != nil {
170				sc = result.sscl.Response.Response.StatusCode
171			}
172			tracing.EndSpan(ctx, sc, err)
173		}()
174	}
175	if err := validation.Validate([]validation.Validation{
176		{TargetValue: client.SubscriptionID,
177			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 {
178		return result, validation.NewError("security.SecureScoreControlsClient", "ListBySecureScore", err.Error())
179	}
180
181	result.fn = client.listBySecureScoreNextResults
182	req, err := client.ListBySecureScorePreparer(ctx, secureScoreName, expand)
183	if err != nil {
184		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "ListBySecureScore", nil, "Failure preparing request")
185		return
186	}
187
188	resp, err := client.ListBySecureScoreSender(req)
189	if err != nil {
190		result.sscl.Response = autorest.Response{Response: resp}
191		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "ListBySecureScore", resp, "Failure sending request")
192		return
193	}
194
195	result.sscl, err = client.ListBySecureScoreResponder(resp)
196	if err != nil {
197		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "ListBySecureScore", resp, "Failure responding to request")
198		return
199	}
200	if result.sscl.hasNextLink() && result.sscl.IsEmpty() {
201		err = result.NextWithContext(ctx)
202		return
203	}
204
205	return
206}
207
208// ListBySecureScorePreparer prepares the ListBySecureScore request.
209func (client SecureScoreControlsClient) ListBySecureScorePreparer(ctx context.Context, secureScoreName string, expand ExpandControlsEnum) (*http.Request, error) {
210	pathParameters := map[string]interface{}{
211		"secureScoreName": autorest.Encode("path", secureScoreName),
212		"subscriptionId":  autorest.Encode("path", client.SubscriptionID),
213	}
214
215	const APIVersion = "2020-01-01-preview"
216	queryParameters := map[string]interface{}{
217		"api-version": APIVersion,
218	}
219	if len(string(expand)) > 0 {
220		queryParameters["$expand"] = autorest.Encode("query", expand)
221	}
222
223	preparer := autorest.CreatePreparer(
224		autorest.AsGet(),
225		autorest.WithBaseURL(client.BaseURI),
226		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Security/secureScores/{secureScoreName}/secureScoreControls", pathParameters),
227		autorest.WithQueryParameters(queryParameters))
228	return preparer.Prepare((&http.Request{}).WithContext(ctx))
229}
230
231// ListBySecureScoreSender sends the ListBySecureScore request. The method will close the
232// http.Response Body if it receives an error.
233func (client SecureScoreControlsClient) ListBySecureScoreSender(req *http.Request) (*http.Response, error) {
234	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
235}
236
237// ListBySecureScoreResponder handles the response to the ListBySecureScore request. The method always
238// closes the http.Response Body.
239func (client SecureScoreControlsClient) ListBySecureScoreResponder(resp *http.Response) (result SecureScoreControlList, err error) {
240	err = autorest.Respond(
241		resp,
242		azure.WithErrorUnlessStatusCode(http.StatusOK),
243		autorest.ByUnmarshallingJSON(&result),
244		autorest.ByClosing())
245	result.Response = autorest.Response{Response: resp}
246	return
247}
248
249// listBySecureScoreNextResults retrieves the next set of results, if any.
250func (client SecureScoreControlsClient) listBySecureScoreNextResults(ctx context.Context, lastResults SecureScoreControlList) (result SecureScoreControlList, err error) {
251	req, err := lastResults.secureScoreControlListPreparer(ctx)
252	if err != nil {
253		return result, autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "listBySecureScoreNextResults", nil, "Failure preparing next results request")
254	}
255	if req == nil {
256		return
257	}
258	resp, err := client.ListBySecureScoreSender(req)
259	if err != nil {
260		result.Response = autorest.Response{Response: resp}
261		return result, autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "listBySecureScoreNextResults", resp, "Failure sending next results request")
262	}
263	result, err = client.ListBySecureScoreResponder(resp)
264	if err != nil {
265		err = autorest.NewErrorWithError(err, "security.SecureScoreControlsClient", "listBySecureScoreNextResults", resp, "Failure responding to next results request")
266	}
267	return
268}
269
270// ListBySecureScoreComplete enumerates all values, automatically crossing page boundaries as required.
271func (client SecureScoreControlsClient) ListBySecureScoreComplete(ctx context.Context, secureScoreName string, expand ExpandControlsEnum) (result SecureScoreControlListIterator, err error) {
272	if tracing.IsEnabled() {
273		ctx = tracing.StartSpan(ctx, fqdn+"/SecureScoreControlsClient.ListBySecureScore")
274		defer func() {
275			sc := -1
276			if result.Response().Response.Response != nil {
277				sc = result.page.Response().Response.Response.StatusCode
278			}
279			tracing.EndSpan(ctx, sc, err)
280		}()
281	}
282	result.page, err = client.ListBySecureScore(ctx, secureScoreName, expand)
283	return
284}
285