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