1package securityinsight
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/autorest/validation"
25	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// AlertRuleTemplatesClient is the API spec for Microsoft.SecurityInsights (Azure Security Insights) resource provider
30type AlertRuleTemplatesClient struct {
31	BaseClient
32}
33
34// NewAlertRuleTemplatesClient creates an instance of the AlertRuleTemplatesClient client.
35func NewAlertRuleTemplatesClient(subscriptionID string) AlertRuleTemplatesClient {
36	return NewAlertRuleTemplatesClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewAlertRuleTemplatesClientWithBaseURI creates an instance of the AlertRuleTemplatesClient client using a custom
40// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
41// stack).
42func NewAlertRuleTemplatesClientWithBaseURI(baseURI string, subscriptionID string) AlertRuleTemplatesClient {
43	return AlertRuleTemplatesClient{NewWithBaseURI(baseURI, subscriptionID)}
44}
45
46// Get gets the alert rule template.
47// Parameters:
48// resourceGroupName - the name of the resource group within the user's subscription. The name is case
49// insensitive.
50// workspaceName - the name of the workspace.
51// alertRuleTemplateID - alert rule template ID
52func (client AlertRuleTemplatesClient) Get(ctx context.Context, resourceGroupName string, workspaceName string, alertRuleTemplateID string) (result AlertRuleTemplateModel, err error) {
53	if tracing.IsEnabled() {
54		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRuleTemplatesClient.Get")
55		defer func() {
56			sc := -1
57			if result.Response.Response != nil {
58				sc = result.Response.Response.StatusCode
59			}
60			tracing.EndSpan(ctx, sc, err)
61		}()
62	}
63	if err := validation.Validate([]validation.Validation{
64		{TargetValue: client.SubscriptionID,
65			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}}},
66		{TargetValue: resourceGroupName,
67			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
68				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
69				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
70		{TargetValue: workspaceName,
71			Constraints: []validation.Constraint{{Target: "workspaceName", Name: validation.MaxLength, Rule: 90, Chain: nil},
72				{Target: "workspaceName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
73		return result, validation.NewError("securityinsight.AlertRuleTemplatesClient", "Get", err.Error())
74	}
75
76	req, err := client.GetPreparer(ctx, resourceGroupName, workspaceName, alertRuleTemplateID)
77	if err != nil {
78		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "Get", nil, "Failure preparing request")
79		return
80	}
81
82	resp, err := client.GetSender(req)
83	if err != nil {
84		result.Response = autorest.Response{Response: resp}
85		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "Get", resp, "Failure sending request")
86		return
87	}
88
89	result, err = client.GetResponder(resp)
90	if err != nil {
91		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "Get", resp, "Failure responding to request")
92		return
93	}
94
95	return
96}
97
98// GetPreparer prepares the Get request.
99func (client AlertRuleTemplatesClient) GetPreparer(ctx context.Context, resourceGroupName string, workspaceName string, alertRuleTemplateID string) (*http.Request, error) {
100	pathParameters := map[string]interface{}{
101		"alertRuleTemplateId": autorest.Encode("path", alertRuleTemplateID),
102		"resourceGroupName":   autorest.Encode("path", resourceGroupName),
103		"subscriptionId":      autorest.Encode("path", client.SubscriptionID),
104		"workspaceName":       autorest.Encode("path", workspaceName),
105	}
106
107	const APIVersion = "2020-01-01"
108	queryParameters := map[string]interface{}{
109		"api-version": APIVersion,
110	}
111
112	preparer := autorest.CreatePreparer(
113		autorest.AsGet(),
114		autorest.WithBaseURL(client.BaseURI),
115		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRuleTemplates/{alertRuleTemplateId}", pathParameters),
116		autorest.WithQueryParameters(queryParameters))
117	return preparer.Prepare((&http.Request{}).WithContext(ctx))
118}
119
120// GetSender sends the Get request. The method will close the
121// http.Response Body if it receives an error.
122func (client AlertRuleTemplatesClient) GetSender(req *http.Request) (*http.Response, error) {
123	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
124}
125
126// GetResponder handles the response to the Get request. The method always
127// closes the http.Response Body.
128func (client AlertRuleTemplatesClient) GetResponder(resp *http.Response) (result AlertRuleTemplateModel, err error) {
129	err = autorest.Respond(
130		resp,
131		azure.WithErrorUnlessStatusCode(http.StatusOK),
132		autorest.ByUnmarshallingJSON(&result),
133		autorest.ByClosing())
134	result.Response = autorest.Response{Response: resp}
135	return
136}
137
138// List gets all alert rule templates.
139// Parameters:
140// resourceGroupName - the name of the resource group within the user's subscription. The name is case
141// insensitive.
142// workspaceName - the name of the workspace.
143func (client AlertRuleTemplatesClient) List(ctx context.Context, resourceGroupName string, workspaceName string) (result AlertRuleTemplatesListPage, err error) {
144	if tracing.IsEnabled() {
145		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRuleTemplatesClient.List")
146		defer func() {
147			sc := -1
148			if result.artl.Response.Response != nil {
149				sc = result.artl.Response.Response.StatusCode
150			}
151			tracing.EndSpan(ctx, sc, err)
152		}()
153	}
154	if err := validation.Validate([]validation.Validation{
155		{TargetValue: client.SubscriptionID,
156			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}}},
157		{TargetValue: resourceGroupName,
158			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
159				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
160				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
161		{TargetValue: workspaceName,
162			Constraints: []validation.Constraint{{Target: "workspaceName", Name: validation.MaxLength, Rule: 90, Chain: nil},
163				{Target: "workspaceName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
164		return result, validation.NewError("securityinsight.AlertRuleTemplatesClient", "List", err.Error())
165	}
166
167	result.fn = client.listNextResults
168	req, err := client.ListPreparer(ctx, resourceGroupName, workspaceName)
169	if err != nil {
170		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "List", nil, "Failure preparing request")
171		return
172	}
173
174	resp, err := client.ListSender(req)
175	if err != nil {
176		result.artl.Response = autorest.Response{Response: resp}
177		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "List", resp, "Failure sending request")
178		return
179	}
180
181	result.artl, err = client.ListResponder(resp)
182	if err != nil {
183		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "List", resp, "Failure responding to request")
184		return
185	}
186	if result.artl.hasNextLink() && result.artl.IsEmpty() {
187		err = result.NextWithContext(ctx)
188		return
189	}
190
191	return
192}
193
194// ListPreparer prepares the List request.
195func (client AlertRuleTemplatesClient) ListPreparer(ctx context.Context, resourceGroupName string, workspaceName string) (*http.Request, error) {
196	pathParameters := map[string]interface{}{
197		"resourceGroupName": autorest.Encode("path", resourceGroupName),
198		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
199		"workspaceName":     autorest.Encode("path", workspaceName),
200	}
201
202	const APIVersion = "2020-01-01"
203	queryParameters := map[string]interface{}{
204		"api-version": APIVersion,
205	}
206
207	preparer := autorest.CreatePreparer(
208		autorest.AsGet(),
209		autorest.WithBaseURL(client.BaseURI),
210		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRuleTemplates", pathParameters),
211		autorest.WithQueryParameters(queryParameters))
212	return preparer.Prepare((&http.Request{}).WithContext(ctx))
213}
214
215// ListSender sends the List request. The method will close the
216// http.Response Body if it receives an error.
217func (client AlertRuleTemplatesClient) ListSender(req *http.Request) (*http.Response, error) {
218	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
219}
220
221// ListResponder handles the response to the List request. The method always
222// closes the http.Response Body.
223func (client AlertRuleTemplatesClient) ListResponder(resp *http.Response) (result AlertRuleTemplatesList, err error) {
224	err = autorest.Respond(
225		resp,
226		azure.WithErrorUnlessStatusCode(http.StatusOK),
227		autorest.ByUnmarshallingJSON(&result),
228		autorest.ByClosing())
229	result.Response = autorest.Response{Response: resp}
230	return
231}
232
233// listNextResults retrieves the next set of results, if any.
234func (client AlertRuleTemplatesClient) listNextResults(ctx context.Context, lastResults AlertRuleTemplatesList) (result AlertRuleTemplatesList, err error) {
235	req, err := lastResults.alertRuleTemplatesListPreparer(ctx)
236	if err != nil {
237		return result, autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "listNextResults", nil, "Failure preparing next results request")
238	}
239	if req == nil {
240		return
241	}
242	resp, err := client.ListSender(req)
243	if err != nil {
244		result.Response = autorest.Response{Response: resp}
245		return result, autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "listNextResults", resp, "Failure sending next results request")
246	}
247	result, err = client.ListResponder(resp)
248	if err != nil {
249		err = autorest.NewErrorWithError(err, "securityinsight.AlertRuleTemplatesClient", "listNextResults", resp, "Failure responding to next results request")
250	}
251	return
252}
253
254// ListComplete enumerates all values, automatically crossing page boundaries as required.
255func (client AlertRuleTemplatesClient) ListComplete(ctx context.Context, resourceGroupName string, workspaceName string) (result AlertRuleTemplatesListIterator, err error) {
256	if tracing.IsEnabled() {
257		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRuleTemplatesClient.List")
258		defer func() {
259			sc := -1
260			if result.Response().Response.Response != nil {
261				sc = result.page.Response().Response.Response.StatusCode
262			}
263			tracing.EndSpan(ctx, sc, err)
264		}()
265	}
266	result.page, err = client.List(ctx, resourceGroupName, workspaceName)
267	return
268}
269