1package sql
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/tracing"
14	"net/http"
15)
16
17// DatabaseVulnerabilityAssessmentScansClient is the the Azure SQL Database management API provides a RESTful set of
18// web services that interact with Azure SQL Database services to manage your databases. The API enables you to create,
19// retrieve, update, and delete databases.
20type DatabaseVulnerabilityAssessmentScansClient struct {
21	BaseClient
22}
23
24// NewDatabaseVulnerabilityAssessmentScansClient creates an instance of the DatabaseVulnerabilityAssessmentScansClient
25// client.
26func NewDatabaseVulnerabilityAssessmentScansClient(subscriptionID string) DatabaseVulnerabilityAssessmentScansClient {
27	return NewDatabaseVulnerabilityAssessmentScansClientWithBaseURI(DefaultBaseURI, subscriptionID)
28}
29
30// NewDatabaseVulnerabilityAssessmentScansClientWithBaseURI creates an instance of the
31// DatabaseVulnerabilityAssessmentScansClient client using a custom endpoint.  Use this when interacting with an Azure
32// cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
33func NewDatabaseVulnerabilityAssessmentScansClientWithBaseURI(baseURI string, subscriptionID string) DatabaseVulnerabilityAssessmentScansClient {
34	return DatabaseVulnerabilityAssessmentScansClient{NewWithBaseURI(baseURI, subscriptionID)}
35}
36
37// Export convert an existing scan result to a human readable format. If already exists nothing happens
38// Parameters:
39// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
40// from the Azure Resource Manager API or the portal.
41// serverName - the name of the server.
42// databaseName - the name of the scanned database.
43// scanID - the vulnerability assessment scan Id.
44func (client DatabaseVulnerabilityAssessmentScansClient) Export(ctx context.Context, resourceGroupName string, serverName string, databaseName string, scanID string) (result DatabaseVulnerabilityAssessmentScansExport, err error) {
45	if tracing.IsEnabled() {
46		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseVulnerabilityAssessmentScansClient.Export")
47		defer func() {
48			sc := -1
49			if result.Response.Response != nil {
50				sc = result.Response.Response.StatusCode
51			}
52			tracing.EndSpan(ctx, sc, err)
53		}()
54	}
55	req, err := client.ExportPreparer(ctx, resourceGroupName, serverName, databaseName, scanID)
56	if err != nil {
57		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "Export", nil, "Failure preparing request")
58		return
59	}
60
61	resp, err := client.ExportSender(req)
62	if err != nil {
63		result.Response = autorest.Response{Response: resp}
64		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "Export", resp, "Failure sending request")
65		return
66	}
67
68	result, err = client.ExportResponder(resp)
69	if err != nil {
70		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "Export", resp, "Failure responding to request")
71		return
72	}
73
74	return
75}
76
77// ExportPreparer prepares the Export request.
78func (client DatabaseVulnerabilityAssessmentScansClient) ExportPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, scanID string) (*http.Request, error) {
79	pathParameters := map[string]interface{}{
80		"databaseName":                autorest.Encode("path", databaseName),
81		"resourceGroupName":           autorest.Encode("path", resourceGroupName),
82		"scanId":                      autorest.Encode("path", scanID),
83		"serverName":                  autorest.Encode("path", serverName),
84		"subscriptionId":              autorest.Encode("path", client.SubscriptionID),
85		"vulnerabilityAssessmentName": autorest.Encode("path", "default"),
86	}
87
88	const APIVersion = "2017-10-01-preview"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92
93	preparer := autorest.CreatePreparer(
94		autorest.AsPost(),
95		autorest.WithBaseURL(client.BaseURI),
96		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/vulnerabilityAssessments/{vulnerabilityAssessmentName}/scans/{scanId}/export", pathParameters),
97		autorest.WithQueryParameters(queryParameters))
98	return preparer.Prepare((&http.Request{}).WithContext(ctx))
99}
100
101// ExportSender sends the Export request. The method will close the
102// http.Response Body if it receives an error.
103func (client DatabaseVulnerabilityAssessmentScansClient) ExportSender(req *http.Request) (*http.Response, error) {
104	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
105}
106
107// ExportResponder handles the response to the Export request. The method always
108// closes the http.Response Body.
109func (client DatabaseVulnerabilityAssessmentScansClient) ExportResponder(resp *http.Response) (result DatabaseVulnerabilityAssessmentScansExport, err error) {
110	err = autorest.Respond(
111		resp,
112		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
113		autorest.ByUnmarshallingJSON(&result),
114		autorest.ByClosing())
115	result.Response = autorest.Response{Response: resp}
116	return
117}
118
119// Get gets a vulnerability assessment scan record of a database.
120// Parameters:
121// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
122// from the Azure Resource Manager API or the portal.
123// serverName - the name of the server.
124// databaseName - the name of the database.
125// scanID - the vulnerability assessment scan Id of the scan to retrieve.
126func (client DatabaseVulnerabilityAssessmentScansClient) Get(ctx context.Context, resourceGroupName string, serverName string, databaseName string, scanID string) (result VulnerabilityAssessmentScanRecord, err error) {
127	if tracing.IsEnabled() {
128		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseVulnerabilityAssessmentScansClient.Get")
129		defer func() {
130			sc := -1
131			if result.Response.Response != nil {
132				sc = result.Response.Response.StatusCode
133			}
134			tracing.EndSpan(ctx, sc, err)
135		}()
136	}
137	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, databaseName, scanID)
138	if err != nil {
139		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "Get", nil, "Failure preparing request")
140		return
141	}
142
143	resp, err := client.GetSender(req)
144	if err != nil {
145		result.Response = autorest.Response{Response: resp}
146		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "Get", resp, "Failure sending request")
147		return
148	}
149
150	result, err = client.GetResponder(resp)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "Get", resp, "Failure responding to request")
153		return
154	}
155
156	return
157}
158
159// GetPreparer prepares the Get request.
160func (client DatabaseVulnerabilityAssessmentScansClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, scanID string) (*http.Request, error) {
161	pathParameters := map[string]interface{}{
162		"databaseName":                autorest.Encode("path", databaseName),
163		"resourceGroupName":           autorest.Encode("path", resourceGroupName),
164		"scanId":                      autorest.Encode("path", scanID),
165		"serverName":                  autorest.Encode("path", serverName),
166		"subscriptionId":              autorest.Encode("path", client.SubscriptionID),
167		"vulnerabilityAssessmentName": autorest.Encode("path", "default"),
168	}
169
170	const APIVersion = "2017-10-01-preview"
171	queryParameters := map[string]interface{}{
172		"api-version": APIVersion,
173	}
174
175	preparer := autorest.CreatePreparer(
176		autorest.AsGet(),
177		autorest.WithBaseURL(client.BaseURI),
178		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/vulnerabilityAssessments/{vulnerabilityAssessmentName}/scans/{scanId}", pathParameters),
179		autorest.WithQueryParameters(queryParameters))
180	return preparer.Prepare((&http.Request{}).WithContext(ctx))
181}
182
183// GetSender sends the Get request. The method will close the
184// http.Response Body if it receives an error.
185func (client DatabaseVulnerabilityAssessmentScansClient) GetSender(req *http.Request) (*http.Response, error) {
186	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
187}
188
189// GetResponder handles the response to the Get request. The method always
190// closes the http.Response Body.
191func (client DatabaseVulnerabilityAssessmentScansClient) GetResponder(resp *http.Response) (result VulnerabilityAssessmentScanRecord, err error) {
192	err = autorest.Respond(
193		resp,
194		azure.WithErrorUnlessStatusCode(http.StatusOK),
195		autorest.ByUnmarshallingJSON(&result),
196		autorest.ByClosing())
197	result.Response = autorest.Response{Response: resp}
198	return
199}
200
201// InitiateScan executes a Vulnerability Assessment database scan.
202// Parameters:
203// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
204// from the Azure Resource Manager API or the portal.
205// serverName - the name of the server.
206// databaseName - the name of the database.
207// scanID - the vulnerability assessment scan Id of the scan to retrieve.
208func (client DatabaseVulnerabilityAssessmentScansClient) InitiateScan(ctx context.Context, resourceGroupName string, serverName string, databaseName string, scanID string) (result DatabaseVulnerabilityAssessmentScansInitiateScanFuture, err error) {
209	if tracing.IsEnabled() {
210		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseVulnerabilityAssessmentScansClient.InitiateScan")
211		defer func() {
212			sc := -1
213			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
214				sc = result.FutureAPI.Response().StatusCode
215			}
216			tracing.EndSpan(ctx, sc, err)
217		}()
218	}
219	req, err := client.InitiateScanPreparer(ctx, resourceGroupName, serverName, databaseName, scanID)
220	if err != nil {
221		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "InitiateScan", nil, "Failure preparing request")
222		return
223	}
224
225	result, err = client.InitiateScanSender(req)
226	if err != nil {
227		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "InitiateScan", nil, "Failure sending request")
228		return
229	}
230
231	return
232}
233
234// InitiateScanPreparer prepares the InitiateScan request.
235func (client DatabaseVulnerabilityAssessmentScansClient) InitiateScanPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, scanID string) (*http.Request, error) {
236	pathParameters := map[string]interface{}{
237		"databaseName":                autorest.Encode("path", databaseName),
238		"resourceGroupName":           autorest.Encode("path", resourceGroupName),
239		"scanId":                      autorest.Encode("path", scanID),
240		"serverName":                  autorest.Encode("path", serverName),
241		"subscriptionId":              autorest.Encode("path", client.SubscriptionID),
242		"vulnerabilityAssessmentName": autorest.Encode("path", "default"),
243	}
244
245	const APIVersion = "2017-10-01-preview"
246	queryParameters := map[string]interface{}{
247		"api-version": APIVersion,
248	}
249
250	preparer := autorest.CreatePreparer(
251		autorest.AsPost(),
252		autorest.WithBaseURL(client.BaseURI),
253		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/vulnerabilityAssessments/{vulnerabilityAssessmentName}/scans/{scanId}/initiateScan", pathParameters),
254		autorest.WithQueryParameters(queryParameters))
255	return preparer.Prepare((&http.Request{}).WithContext(ctx))
256}
257
258// InitiateScanSender sends the InitiateScan request. The method will close the
259// http.Response Body if it receives an error.
260func (client DatabaseVulnerabilityAssessmentScansClient) InitiateScanSender(req *http.Request) (future DatabaseVulnerabilityAssessmentScansInitiateScanFuture, err error) {
261	var resp *http.Response
262	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
263	if err != nil {
264		return
265	}
266	var azf azure.Future
267	azf, err = azure.NewFutureFromResponse(resp)
268	future.FutureAPI = &azf
269	future.Result = future.result
270	return
271}
272
273// InitiateScanResponder handles the response to the InitiateScan request. The method always
274// closes the http.Response Body.
275func (client DatabaseVulnerabilityAssessmentScansClient) InitiateScanResponder(resp *http.Response) (result autorest.Response, err error) {
276	err = autorest.Respond(
277		resp,
278		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
279		autorest.ByClosing())
280	result.Response = resp
281	return
282}
283
284// ListByDatabase lists the vulnerability assessment scans of a database.
285// Parameters:
286// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
287// from the Azure Resource Manager API or the portal.
288// serverName - the name of the server.
289// databaseName - the name of the database.
290func (client DatabaseVulnerabilityAssessmentScansClient) ListByDatabase(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result VulnerabilityAssessmentScanRecordListResultPage, err error) {
291	if tracing.IsEnabled() {
292		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseVulnerabilityAssessmentScansClient.ListByDatabase")
293		defer func() {
294			sc := -1
295			if result.vasrlr.Response.Response != nil {
296				sc = result.vasrlr.Response.Response.StatusCode
297			}
298			tracing.EndSpan(ctx, sc, err)
299		}()
300	}
301	result.fn = client.listByDatabaseNextResults
302	req, err := client.ListByDatabasePreparer(ctx, resourceGroupName, serverName, databaseName)
303	if err != nil {
304		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "ListByDatabase", nil, "Failure preparing request")
305		return
306	}
307
308	resp, err := client.ListByDatabaseSender(req)
309	if err != nil {
310		result.vasrlr.Response = autorest.Response{Response: resp}
311		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "ListByDatabase", resp, "Failure sending request")
312		return
313	}
314
315	result.vasrlr, err = client.ListByDatabaseResponder(resp)
316	if err != nil {
317		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "ListByDatabase", resp, "Failure responding to request")
318		return
319	}
320	if result.vasrlr.hasNextLink() && result.vasrlr.IsEmpty() {
321		err = result.NextWithContext(ctx)
322		return
323	}
324
325	return
326}
327
328// ListByDatabasePreparer prepares the ListByDatabase request.
329func (client DatabaseVulnerabilityAssessmentScansClient) ListByDatabasePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (*http.Request, error) {
330	pathParameters := map[string]interface{}{
331		"databaseName":                autorest.Encode("path", databaseName),
332		"resourceGroupName":           autorest.Encode("path", resourceGroupName),
333		"serverName":                  autorest.Encode("path", serverName),
334		"subscriptionId":              autorest.Encode("path", client.SubscriptionID),
335		"vulnerabilityAssessmentName": autorest.Encode("path", "default"),
336	}
337
338	const APIVersion = "2017-10-01-preview"
339	queryParameters := map[string]interface{}{
340		"api-version": APIVersion,
341	}
342
343	preparer := autorest.CreatePreparer(
344		autorest.AsGet(),
345		autorest.WithBaseURL(client.BaseURI),
346		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/vulnerabilityAssessments/{vulnerabilityAssessmentName}/scans", pathParameters),
347		autorest.WithQueryParameters(queryParameters))
348	return preparer.Prepare((&http.Request{}).WithContext(ctx))
349}
350
351// ListByDatabaseSender sends the ListByDatabase request. The method will close the
352// http.Response Body if it receives an error.
353func (client DatabaseVulnerabilityAssessmentScansClient) ListByDatabaseSender(req *http.Request) (*http.Response, error) {
354	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
355}
356
357// ListByDatabaseResponder handles the response to the ListByDatabase request. The method always
358// closes the http.Response Body.
359func (client DatabaseVulnerabilityAssessmentScansClient) ListByDatabaseResponder(resp *http.Response) (result VulnerabilityAssessmentScanRecordListResult, err error) {
360	err = autorest.Respond(
361		resp,
362		azure.WithErrorUnlessStatusCode(http.StatusOK),
363		autorest.ByUnmarshallingJSON(&result),
364		autorest.ByClosing())
365	result.Response = autorest.Response{Response: resp}
366	return
367}
368
369// listByDatabaseNextResults retrieves the next set of results, if any.
370func (client DatabaseVulnerabilityAssessmentScansClient) listByDatabaseNextResults(ctx context.Context, lastResults VulnerabilityAssessmentScanRecordListResult) (result VulnerabilityAssessmentScanRecordListResult, err error) {
371	req, err := lastResults.vulnerabilityAssessmentScanRecordListResultPreparer(ctx)
372	if err != nil {
373		return result, autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "listByDatabaseNextResults", nil, "Failure preparing next results request")
374	}
375	if req == nil {
376		return
377	}
378	resp, err := client.ListByDatabaseSender(req)
379	if err != nil {
380		result.Response = autorest.Response{Response: resp}
381		return result, autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "listByDatabaseNextResults", resp, "Failure sending next results request")
382	}
383	result, err = client.ListByDatabaseResponder(resp)
384	if err != nil {
385		err = autorest.NewErrorWithError(err, "sql.DatabaseVulnerabilityAssessmentScansClient", "listByDatabaseNextResults", resp, "Failure responding to next results request")
386	}
387	return
388}
389
390// ListByDatabaseComplete enumerates all values, automatically crossing page boundaries as required.
391func (client DatabaseVulnerabilityAssessmentScansClient) ListByDatabaseComplete(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result VulnerabilityAssessmentScanRecordListResultIterator, err error) {
392	if tracing.IsEnabled() {
393		ctx = tracing.StartSpan(ctx, fqdn+"/DatabaseVulnerabilityAssessmentScansClient.ListByDatabase")
394		defer func() {
395			sc := -1
396			if result.Response().Response.Response != nil {
397				sc = result.page.Response().Response.Response.StatusCode
398			}
399			tracing.EndSpan(ctx, sc, err)
400		}()
401	}
402	result.page, err = client.ListByDatabase(ctx, resourceGroupName, serverName, databaseName)
403	return
404}
405