1package securityinsight
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// AlertRulesClient is the API spec for Microsoft.SecurityInsights (Azure Security Insights) resource provider
19type AlertRulesClient struct {
20	BaseClient
21}
22
23// NewAlertRulesClient creates an instance of the AlertRulesClient client.
24func NewAlertRulesClient(subscriptionID string) AlertRulesClient {
25	return NewAlertRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewAlertRulesClientWithBaseURI creates an instance of the AlertRulesClient 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 NewAlertRulesClientWithBaseURI(baseURI string, subscriptionID string) AlertRulesClient {
31	return AlertRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate creates or updates the alert rule.
35// Parameters:
36// resourceGroupName - the name of the resource group within the user's subscription. The name is case
37// insensitive.
38// operationalInsightsResourceProvider - the namespace of workspaces resource provider-
39// Microsoft.OperationalInsights.
40// workspaceName - the name of the workspace.
41// ruleID - alert rule ID
42// alertRule - the alert rule
43func (client AlertRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string, ruleID string, alertRule BasicAlertRule) (result AlertRuleModel, err error) {
44	if tracing.IsEnabled() {
45		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.CreateOrUpdate")
46		defer func() {
47			sc := -1
48			if result.Response.Response != nil {
49				sc = result.Response.Response.StatusCode
50			}
51			tracing.EndSpan(ctx, sc, err)
52		}()
53	}
54	if err := validation.Validate([]validation.Validation{
55		{TargetValue: client.SubscriptionID,
56			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}}},
57		{TargetValue: resourceGroupName,
58			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
59				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
60				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
61		{TargetValue: workspaceName,
62			Constraints: []validation.Constraint{{Target: "workspaceName", Name: validation.MaxLength, Rule: 90, Chain: nil},
63				{Target: "workspaceName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
64		return result, validation.NewError("securityinsight.AlertRulesClient", "CreateOrUpdate", err.Error())
65	}
66
67	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, operationalInsightsResourceProvider, workspaceName, ruleID, alertRule)
68	if err != nil {
69		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
70		return
71	}
72
73	resp, err := client.CreateOrUpdateSender(req)
74	if err != nil {
75		result.Response = autorest.Response{Response: resp}
76		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "CreateOrUpdate", resp, "Failure sending request")
77		return
78	}
79
80	result, err = client.CreateOrUpdateResponder(resp)
81	if err != nil {
82		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "CreateOrUpdate", resp, "Failure responding to request")
83		return
84	}
85
86	return
87}
88
89// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
90func (client AlertRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string, ruleID string, alertRule BasicAlertRule) (*http.Request, error) {
91	pathParameters := map[string]interface{}{
92		"operationalInsightsResourceProvider": autorest.Encode("path", operationalInsightsResourceProvider),
93		"resourceGroupName":                   autorest.Encode("path", resourceGroupName),
94		"ruleId":                              autorest.Encode("path", ruleID),
95		"subscriptionId":                      autorest.Encode("path", client.SubscriptionID),
96		"workspaceName":                       autorest.Encode("path", workspaceName),
97	}
98
99	const APIVersion = "2019-01-01-preview"
100	queryParameters := map[string]interface{}{
101		"api-version": APIVersion,
102	}
103
104	preparer := autorest.CreatePreparer(
105		autorest.AsContentType("application/json; charset=utf-8"),
106		autorest.AsPut(),
107		autorest.WithBaseURL(client.BaseURI),
108		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{operationalInsightsResourceProvider}/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRules/{ruleId}", pathParameters),
109		autorest.WithJSON(alertRule),
110		autorest.WithQueryParameters(queryParameters))
111	return preparer.Prepare((&http.Request{}).WithContext(ctx))
112}
113
114// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
115// http.Response Body if it receives an error.
116func (client AlertRulesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
117	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
118}
119
120// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
121// closes the http.Response Body.
122func (client AlertRulesClient) CreateOrUpdateResponder(resp *http.Response) (result AlertRuleModel, err error) {
123	err = autorest.Respond(
124		resp,
125		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
126		autorest.ByUnmarshallingJSON(&result),
127		autorest.ByClosing())
128	result.Response = autorest.Response{Response: resp}
129	return
130}
131
132// Delete delete the alert rule.
133// Parameters:
134// resourceGroupName - the name of the resource group within the user's subscription. The name is case
135// insensitive.
136// operationalInsightsResourceProvider - the namespace of workspaces resource provider-
137// Microsoft.OperationalInsights.
138// workspaceName - the name of the workspace.
139// ruleID - alert rule ID
140func (client AlertRulesClient) Delete(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string, ruleID string) (result autorest.Response, err error) {
141	if tracing.IsEnabled() {
142		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Delete")
143		defer func() {
144			sc := -1
145			if result.Response != nil {
146				sc = result.Response.StatusCode
147			}
148			tracing.EndSpan(ctx, sc, err)
149		}()
150	}
151	if err := validation.Validate([]validation.Validation{
152		{TargetValue: client.SubscriptionID,
153			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}}},
154		{TargetValue: resourceGroupName,
155			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
156				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
157				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
158		{TargetValue: workspaceName,
159			Constraints: []validation.Constraint{{Target: "workspaceName", Name: validation.MaxLength, Rule: 90, Chain: nil},
160				{Target: "workspaceName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
161		return result, validation.NewError("securityinsight.AlertRulesClient", "Delete", err.Error())
162	}
163
164	req, err := client.DeletePreparer(ctx, resourceGroupName, operationalInsightsResourceProvider, workspaceName, ruleID)
165	if err != nil {
166		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "Delete", nil, "Failure preparing request")
167		return
168	}
169
170	resp, err := client.DeleteSender(req)
171	if err != nil {
172		result.Response = resp
173		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "Delete", resp, "Failure sending request")
174		return
175	}
176
177	result, err = client.DeleteResponder(resp)
178	if err != nil {
179		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "Delete", resp, "Failure responding to request")
180		return
181	}
182
183	return
184}
185
186// DeletePreparer prepares the Delete request.
187func (client AlertRulesClient) DeletePreparer(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string, ruleID string) (*http.Request, error) {
188	pathParameters := map[string]interface{}{
189		"operationalInsightsResourceProvider": autorest.Encode("path", operationalInsightsResourceProvider),
190		"resourceGroupName":                   autorest.Encode("path", resourceGroupName),
191		"ruleId":                              autorest.Encode("path", ruleID),
192		"subscriptionId":                      autorest.Encode("path", client.SubscriptionID),
193		"workspaceName":                       autorest.Encode("path", workspaceName),
194	}
195
196	const APIVersion = "2019-01-01-preview"
197	queryParameters := map[string]interface{}{
198		"api-version": APIVersion,
199	}
200
201	preparer := autorest.CreatePreparer(
202		autorest.AsDelete(),
203		autorest.WithBaseURL(client.BaseURI),
204		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{operationalInsightsResourceProvider}/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRules/{ruleId}", pathParameters),
205		autorest.WithQueryParameters(queryParameters))
206	return preparer.Prepare((&http.Request{}).WithContext(ctx))
207}
208
209// DeleteSender sends the Delete request. The method will close the
210// http.Response Body if it receives an error.
211func (client AlertRulesClient) DeleteSender(req *http.Request) (*http.Response, error) {
212	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
213}
214
215// DeleteResponder handles the response to the Delete request. The method always
216// closes the http.Response Body.
217func (client AlertRulesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
218	err = autorest.Respond(
219		resp,
220		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
221		autorest.ByClosing())
222	result.Response = resp
223	return
224}
225
226// Get gets the alert rule.
227// Parameters:
228// resourceGroupName - the name of the resource group within the user's subscription. The name is case
229// insensitive.
230// operationalInsightsResourceProvider - the namespace of workspaces resource provider-
231// Microsoft.OperationalInsights.
232// workspaceName - the name of the workspace.
233// ruleID - alert rule ID
234func (client AlertRulesClient) Get(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string, ruleID string) (result AlertRuleModel, err error) {
235	if tracing.IsEnabled() {
236		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Get")
237		defer func() {
238			sc := -1
239			if result.Response.Response != nil {
240				sc = result.Response.Response.StatusCode
241			}
242			tracing.EndSpan(ctx, sc, err)
243		}()
244	}
245	if err := validation.Validate([]validation.Validation{
246		{TargetValue: client.SubscriptionID,
247			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}}},
248		{TargetValue: resourceGroupName,
249			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
250				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
251				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
252		{TargetValue: workspaceName,
253			Constraints: []validation.Constraint{{Target: "workspaceName", Name: validation.MaxLength, Rule: 90, Chain: nil},
254				{Target: "workspaceName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
255		return result, validation.NewError("securityinsight.AlertRulesClient", "Get", err.Error())
256	}
257
258	req, err := client.GetPreparer(ctx, resourceGroupName, operationalInsightsResourceProvider, workspaceName, ruleID)
259	if err != nil {
260		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "Get", nil, "Failure preparing request")
261		return
262	}
263
264	resp, err := client.GetSender(req)
265	if err != nil {
266		result.Response = autorest.Response{Response: resp}
267		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "Get", resp, "Failure sending request")
268		return
269	}
270
271	result, err = client.GetResponder(resp)
272	if err != nil {
273		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "Get", resp, "Failure responding to request")
274		return
275	}
276
277	return
278}
279
280// GetPreparer prepares the Get request.
281func (client AlertRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string, ruleID string) (*http.Request, error) {
282	pathParameters := map[string]interface{}{
283		"operationalInsightsResourceProvider": autorest.Encode("path", operationalInsightsResourceProvider),
284		"resourceGroupName":                   autorest.Encode("path", resourceGroupName),
285		"ruleId":                              autorest.Encode("path", ruleID),
286		"subscriptionId":                      autorest.Encode("path", client.SubscriptionID),
287		"workspaceName":                       autorest.Encode("path", workspaceName),
288	}
289
290	const APIVersion = "2019-01-01-preview"
291	queryParameters := map[string]interface{}{
292		"api-version": APIVersion,
293	}
294
295	preparer := autorest.CreatePreparer(
296		autorest.AsGet(),
297		autorest.WithBaseURL(client.BaseURI),
298		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{operationalInsightsResourceProvider}/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRules/{ruleId}", pathParameters),
299		autorest.WithQueryParameters(queryParameters))
300	return preparer.Prepare((&http.Request{}).WithContext(ctx))
301}
302
303// GetSender sends the Get request. The method will close the
304// http.Response Body if it receives an error.
305func (client AlertRulesClient) GetSender(req *http.Request) (*http.Response, error) {
306	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
307}
308
309// GetResponder handles the response to the Get request. The method always
310// closes the http.Response Body.
311func (client AlertRulesClient) GetResponder(resp *http.Response) (result AlertRuleModel, err error) {
312	err = autorest.Respond(
313		resp,
314		azure.WithErrorUnlessStatusCode(http.StatusOK),
315		autorest.ByUnmarshallingJSON(&result),
316		autorest.ByClosing())
317	result.Response = autorest.Response{Response: resp}
318	return
319}
320
321// List gets all alert rules.
322// Parameters:
323// resourceGroupName - the name of the resource group within the user's subscription. The name is case
324// insensitive.
325// operationalInsightsResourceProvider - the namespace of workspaces resource provider-
326// Microsoft.OperationalInsights.
327// workspaceName - the name of the workspace.
328func (client AlertRulesClient) List(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string) (result AlertRulesListPage, err error) {
329	if tracing.IsEnabled() {
330		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.List")
331		defer func() {
332			sc := -1
333			if result.arl.Response.Response != nil {
334				sc = result.arl.Response.Response.StatusCode
335			}
336			tracing.EndSpan(ctx, sc, err)
337		}()
338	}
339	if err := validation.Validate([]validation.Validation{
340		{TargetValue: client.SubscriptionID,
341			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}}},
342		{TargetValue: resourceGroupName,
343			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
344				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
345				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
346		{TargetValue: workspaceName,
347			Constraints: []validation.Constraint{{Target: "workspaceName", Name: validation.MaxLength, Rule: 90, Chain: nil},
348				{Target: "workspaceName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
349		return result, validation.NewError("securityinsight.AlertRulesClient", "List", err.Error())
350	}
351
352	result.fn = client.listNextResults
353	req, err := client.ListPreparer(ctx, resourceGroupName, operationalInsightsResourceProvider, workspaceName)
354	if err != nil {
355		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "List", nil, "Failure preparing request")
356		return
357	}
358
359	resp, err := client.ListSender(req)
360	if err != nil {
361		result.arl.Response = autorest.Response{Response: resp}
362		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "List", resp, "Failure sending request")
363		return
364	}
365
366	result.arl, err = client.ListResponder(resp)
367	if err != nil {
368		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "List", resp, "Failure responding to request")
369		return
370	}
371	if result.arl.hasNextLink() && result.arl.IsEmpty() {
372		err = result.NextWithContext(ctx)
373		return
374	}
375
376	return
377}
378
379// ListPreparer prepares the List request.
380func (client AlertRulesClient) ListPreparer(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string) (*http.Request, error) {
381	pathParameters := map[string]interface{}{
382		"operationalInsightsResourceProvider": autorest.Encode("path", operationalInsightsResourceProvider),
383		"resourceGroupName":                   autorest.Encode("path", resourceGroupName),
384		"subscriptionId":                      autorest.Encode("path", client.SubscriptionID),
385		"workspaceName":                       autorest.Encode("path", workspaceName),
386	}
387
388	const APIVersion = "2019-01-01-preview"
389	queryParameters := map[string]interface{}{
390		"api-version": APIVersion,
391	}
392
393	preparer := autorest.CreatePreparer(
394		autorest.AsGet(),
395		autorest.WithBaseURL(client.BaseURI),
396		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{operationalInsightsResourceProvider}/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRules", pathParameters),
397		autorest.WithQueryParameters(queryParameters))
398	return preparer.Prepare((&http.Request{}).WithContext(ctx))
399}
400
401// ListSender sends the List request. The method will close the
402// http.Response Body if it receives an error.
403func (client AlertRulesClient) ListSender(req *http.Request) (*http.Response, error) {
404	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
405}
406
407// ListResponder handles the response to the List request. The method always
408// closes the http.Response Body.
409func (client AlertRulesClient) ListResponder(resp *http.Response) (result AlertRulesList, err error) {
410	err = autorest.Respond(
411		resp,
412		azure.WithErrorUnlessStatusCode(http.StatusOK),
413		autorest.ByUnmarshallingJSON(&result),
414		autorest.ByClosing())
415	result.Response = autorest.Response{Response: resp}
416	return
417}
418
419// listNextResults retrieves the next set of results, if any.
420func (client AlertRulesClient) listNextResults(ctx context.Context, lastResults AlertRulesList) (result AlertRulesList, err error) {
421	req, err := lastResults.alertRulesListPreparer(ctx)
422	if err != nil {
423		return result, autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "listNextResults", nil, "Failure preparing next results request")
424	}
425	if req == nil {
426		return
427	}
428	resp, err := client.ListSender(req)
429	if err != nil {
430		result.Response = autorest.Response{Response: resp}
431		return result, autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "listNextResults", resp, "Failure sending next results request")
432	}
433	result, err = client.ListResponder(resp)
434	if err != nil {
435		err = autorest.NewErrorWithError(err, "securityinsight.AlertRulesClient", "listNextResults", resp, "Failure responding to next results request")
436	}
437	return
438}
439
440// ListComplete enumerates all values, automatically crossing page boundaries as required.
441func (client AlertRulesClient) ListComplete(ctx context.Context, resourceGroupName string, operationalInsightsResourceProvider string, workspaceName string) (result AlertRulesListIterator, err error) {
442	if tracing.IsEnabled() {
443		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.List")
444		defer func() {
445			sc := -1
446			if result.Response().Response.Response != nil {
447				sc = result.page.Response().Response.Response.StatusCode
448			}
449			tracing.EndSpan(ctx, sc, err)
450		}()
451	}
452	result.page, err = client.List(ctx, resourceGroupName, operationalInsightsResourceProvider, workspaceName)
453	return
454}
455