1package security
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/tracing"
25	"net/http"
26)
27
28// CompliancesClient is the API spec for Microsoft.Security (Azure Security Center) resource provider
29type CompliancesClient struct {
30	BaseClient
31}
32
33// NewCompliancesClient creates an instance of the CompliancesClient client.
34func NewCompliancesClient(subscriptionID string, ascLocation string) CompliancesClient {
35	return NewCompliancesClientWithBaseURI(DefaultBaseURI, subscriptionID, ascLocation)
36}
37
38// NewCompliancesClientWithBaseURI creates an instance of the CompliancesClient client using a custom endpoint.  Use
39// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
40func NewCompliancesClientWithBaseURI(baseURI string, subscriptionID string, ascLocation string) CompliancesClient {
41	return CompliancesClient{NewWithBaseURI(baseURI, subscriptionID, ascLocation)}
42}
43
44// Get details of a specific Compliance.
45// Parameters:
46// scope - scope of the query, can be subscription (/subscriptions/0b06d9ea-afe6-4779-bd59-30e5c2d9d13f) or
47// management group (/providers/Microsoft.Management/managementGroups/mgName).
48// complianceName - name of the Compliance
49func (client CompliancesClient) Get(ctx context.Context, scope string, complianceName string) (result Compliance, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/CompliancesClient.Get")
52		defer func() {
53			sc := -1
54			if result.Response.Response != nil {
55				sc = result.Response.Response.StatusCode
56			}
57			tracing.EndSpan(ctx, sc, err)
58		}()
59	}
60	req, err := client.GetPreparer(ctx, scope, complianceName)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "Get", nil, "Failure preparing request")
63		return
64	}
65
66	resp, err := client.GetSender(req)
67	if err != nil {
68		result.Response = autorest.Response{Response: resp}
69		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "Get", resp, "Failure sending request")
70		return
71	}
72
73	result, err = client.GetResponder(resp)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "Get", resp, "Failure responding to request")
76		return
77	}
78
79	return
80}
81
82// GetPreparer prepares the Get request.
83func (client CompliancesClient) GetPreparer(ctx context.Context, scope string, complianceName string) (*http.Request, error) {
84	pathParameters := map[string]interface{}{
85		"complianceName": autorest.Encode("path", complianceName),
86		"scope":          scope,
87	}
88
89	const APIVersion = "2017-08-01-preview"
90	queryParameters := map[string]interface{}{
91		"api-version": APIVersion,
92	}
93
94	preparer := autorest.CreatePreparer(
95		autorest.AsGet(),
96		autorest.WithBaseURL(client.BaseURI),
97		autorest.WithPathParameters("/{scope}/providers/Microsoft.Security/compliances/{complianceName}", pathParameters),
98		autorest.WithQueryParameters(queryParameters))
99	return preparer.Prepare((&http.Request{}).WithContext(ctx))
100}
101
102// GetSender sends the Get request. The method will close the
103// http.Response Body if it receives an error.
104func (client CompliancesClient) GetSender(req *http.Request) (*http.Response, error) {
105	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
106}
107
108// GetResponder handles the response to the Get request. The method always
109// closes the http.Response Body.
110func (client CompliancesClient) GetResponder(resp *http.Response) (result Compliance, err error) {
111	err = autorest.Respond(
112		resp,
113		azure.WithErrorUnlessStatusCode(http.StatusOK),
114		autorest.ByUnmarshallingJSON(&result),
115		autorest.ByClosing())
116	result.Response = autorest.Response{Response: resp}
117	return
118}
119
120// List the Compliance scores of the specific management group.
121// Parameters:
122// scope - scope of the query, can be subscription (/subscriptions/0b06d9ea-afe6-4779-bd59-30e5c2d9d13f) or
123// management group (/providers/Microsoft.Management/managementGroups/mgName).
124func (client CompliancesClient) List(ctx context.Context, scope string) (result ComplianceListPage, err error) {
125	if tracing.IsEnabled() {
126		ctx = tracing.StartSpan(ctx, fqdn+"/CompliancesClient.List")
127		defer func() {
128			sc := -1
129			if result.cl.Response.Response != nil {
130				sc = result.cl.Response.Response.StatusCode
131			}
132			tracing.EndSpan(ctx, sc, err)
133		}()
134	}
135	result.fn = client.listNextResults
136	req, err := client.ListPreparer(ctx, scope)
137	if err != nil {
138		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "List", nil, "Failure preparing request")
139		return
140	}
141
142	resp, err := client.ListSender(req)
143	if err != nil {
144		result.cl.Response = autorest.Response{Response: resp}
145		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "List", resp, "Failure sending request")
146		return
147	}
148
149	result.cl, err = client.ListResponder(resp)
150	if err != nil {
151		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "List", resp, "Failure responding to request")
152		return
153	}
154	if result.cl.hasNextLink() && result.cl.IsEmpty() {
155		err = result.NextWithContext(ctx)
156		return
157	}
158
159	return
160}
161
162// ListPreparer prepares the List request.
163func (client CompliancesClient) ListPreparer(ctx context.Context, scope string) (*http.Request, error) {
164	pathParameters := map[string]interface{}{
165		"scope": scope,
166	}
167
168	const APIVersion = "2017-08-01-preview"
169	queryParameters := map[string]interface{}{
170		"api-version": APIVersion,
171	}
172
173	preparer := autorest.CreatePreparer(
174		autorest.AsGet(),
175		autorest.WithBaseURL(client.BaseURI),
176		autorest.WithPathParameters("/{scope}/providers/Microsoft.Security/compliances", pathParameters),
177		autorest.WithQueryParameters(queryParameters))
178	return preparer.Prepare((&http.Request{}).WithContext(ctx))
179}
180
181// ListSender sends the List request. The method will close the
182// http.Response Body if it receives an error.
183func (client CompliancesClient) ListSender(req *http.Request) (*http.Response, error) {
184	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
185}
186
187// ListResponder handles the response to the List request. The method always
188// closes the http.Response Body.
189func (client CompliancesClient) ListResponder(resp *http.Response) (result ComplianceList, err error) {
190	err = autorest.Respond(
191		resp,
192		azure.WithErrorUnlessStatusCode(http.StatusOK),
193		autorest.ByUnmarshallingJSON(&result),
194		autorest.ByClosing())
195	result.Response = autorest.Response{Response: resp}
196	return
197}
198
199// listNextResults retrieves the next set of results, if any.
200func (client CompliancesClient) listNextResults(ctx context.Context, lastResults ComplianceList) (result ComplianceList, err error) {
201	req, err := lastResults.complianceListPreparer(ctx)
202	if err != nil {
203		return result, autorest.NewErrorWithError(err, "security.CompliancesClient", "listNextResults", nil, "Failure preparing next results request")
204	}
205	if req == nil {
206		return
207	}
208	resp, err := client.ListSender(req)
209	if err != nil {
210		result.Response = autorest.Response{Response: resp}
211		return result, autorest.NewErrorWithError(err, "security.CompliancesClient", "listNextResults", resp, "Failure sending next results request")
212	}
213	result, err = client.ListResponder(resp)
214	if err != nil {
215		err = autorest.NewErrorWithError(err, "security.CompliancesClient", "listNextResults", resp, "Failure responding to next results request")
216	}
217	return
218}
219
220// ListComplete enumerates all values, automatically crossing page boundaries as required.
221func (client CompliancesClient) ListComplete(ctx context.Context, scope string) (result ComplianceListIterator, err error) {
222	if tracing.IsEnabled() {
223		ctx = tracing.StartSpan(ctx, fqdn+"/CompliancesClient.List")
224		defer func() {
225			sc := -1
226			if result.Response().Response.Response != nil {
227				sc = result.page.Response().Response.Response.StatusCode
228			}
229			tracing.EndSpan(ctx, sc, err)
230		}()
231	}
232	result.page, err = client.List(ctx, scope)
233	return
234}
235