1package insights
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// AlertRulesClient is the monitor Management Client
30type AlertRulesClient struct {
31	BaseClient
32}
33
34// NewAlertRulesClient creates an instance of the AlertRulesClient client.
35func NewAlertRulesClient(subscriptionID string) AlertRulesClient {
36	return NewAlertRulesClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewAlertRulesClientWithBaseURI creates an instance of the AlertRulesClient client.
40func NewAlertRulesClientWithBaseURI(baseURI string, subscriptionID string) AlertRulesClient {
41	return AlertRulesClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// CreateOrUpdate creates or updates an alert rule.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// ruleName - the name of the rule.
48// parameters - the parameters of the rule to create or update.
49func (client AlertRulesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, ruleName string, parameters AlertRuleResource) (result AlertRuleResource, err error) {
50	if tracing.IsEnabled() {
51		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.CreateOrUpdate")
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	if err := validation.Validate([]validation.Validation{
61		{TargetValue: parameters,
62			Constraints: []validation.Constraint{{Target: "parameters.AlertRule", Name: validation.Null, Rule: true,
63				Chain: []validation.Constraint{{Target: "parameters.AlertRule.Name", Name: validation.Null, Rule: true, Chain: nil},
64					{Target: "parameters.AlertRule.IsEnabled", Name: validation.Null, Rule: true, Chain: nil},
65					{Target: "parameters.AlertRule.Condition", Name: validation.Null, Rule: true, Chain: nil},
66				}}}}}); err != nil {
67		return result, validation.NewError("insights.AlertRulesClient", "CreateOrUpdate", err.Error())
68	}
69
70	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, ruleName, parameters)
71	if err != nil {
72		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "CreateOrUpdate", nil, "Failure preparing request")
73		return
74	}
75
76	resp, err := client.CreateOrUpdateSender(req)
77	if err != nil {
78		result.Response = autorest.Response{Response: resp}
79		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "CreateOrUpdate", resp, "Failure sending request")
80		return
81	}
82
83	result, err = client.CreateOrUpdateResponder(resp)
84	if err != nil {
85		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "CreateOrUpdate", resp, "Failure responding to request")
86	}
87
88	return
89}
90
91// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
92func (client AlertRulesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, ruleName string, parameters AlertRuleResource) (*http.Request, error) {
93	pathParameters := map[string]interface{}{
94		"resourceGroupName": autorest.Encode("path", resourceGroupName),
95		"ruleName":          autorest.Encode("path", ruleName),
96		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
97	}
98
99	const APIVersion = "2016-03-01"
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/microsoft.insights/alertrules/{ruleName}", pathParameters),
109		autorest.WithJSON(parameters),
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	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
118	return autorest.SendWithSender(client, req, sd...)
119}
120
121// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
122// closes the http.Response Body.
123func (client AlertRulesClient) CreateOrUpdateResponder(resp *http.Response) (result AlertRuleResource, err error) {
124	err = autorest.Respond(
125		resp,
126		client.ByInspecting(),
127		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
128		autorest.ByUnmarshallingJSON(&result),
129		autorest.ByClosing())
130	result.Response = autorest.Response{Response: resp}
131	return
132}
133
134// Delete deletes an alert rule
135// Parameters:
136// resourceGroupName - the name of the resource group.
137// ruleName - the name of the rule.
138func (client AlertRulesClient) Delete(ctx context.Context, resourceGroupName string, ruleName string) (result autorest.Response, err error) {
139	if tracing.IsEnabled() {
140		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Delete")
141		defer func() {
142			sc := -1
143			if result.Response != nil {
144				sc = result.Response.StatusCode
145			}
146			tracing.EndSpan(ctx, sc, err)
147		}()
148	}
149	req, err := client.DeletePreparer(ctx, resourceGroupName, ruleName)
150	if err != nil {
151		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Delete", nil, "Failure preparing request")
152		return
153	}
154
155	resp, err := client.DeleteSender(req)
156	if err != nil {
157		result.Response = resp
158		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Delete", resp, "Failure sending request")
159		return
160	}
161
162	result, err = client.DeleteResponder(resp)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Delete", resp, "Failure responding to request")
165	}
166
167	return
168}
169
170// DeletePreparer prepares the Delete request.
171func (client AlertRulesClient) DeletePreparer(ctx context.Context, resourceGroupName string, ruleName string) (*http.Request, error) {
172	pathParameters := map[string]interface{}{
173		"resourceGroupName": autorest.Encode("path", resourceGroupName),
174		"ruleName":          autorest.Encode("path", ruleName),
175		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
176	}
177
178	const APIVersion = "2016-03-01"
179	queryParameters := map[string]interface{}{
180		"api-version": APIVersion,
181	}
182
183	preparer := autorest.CreatePreparer(
184		autorest.AsDelete(),
185		autorest.WithBaseURL(client.BaseURI),
186		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
187		autorest.WithQueryParameters(queryParameters))
188	return preparer.Prepare((&http.Request{}).WithContext(ctx))
189}
190
191// DeleteSender sends the Delete request. The method will close the
192// http.Response Body if it receives an error.
193func (client AlertRulesClient) DeleteSender(req *http.Request) (*http.Response, error) {
194	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
195	return autorest.SendWithSender(client, req, sd...)
196}
197
198// DeleteResponder handles the response to the Delete request. The method always
199// closes the http.Response Body.
200func (client AlertRulesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
201	err = autorest.Respond(
202		resp,
203		client.ByInspecting(),
204		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
205		autorest.ByClosing())
206	result.Response = resp
207	return
208}
209
210// Get gets an alert rule
211// Parameters:
212// resourceGroupName - the name of the resource group.
213// ruleName - the name of the rule.
214func (client AlertRulesClient) Get(ctx context.Context, resourceGroupName string, ruleName string) (result AlertRuleResource, err error) {
215	if tracing.IsEnabled() {
216		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Get")
217		defer func() {
218			sc := -1
219			if result.Response.Response != nil {
220				sc = result.Response.Response.StatusCode
221			}
222			tracing.EndSpan(ctx, sc, err)
223		}()
224	}
225	req, err := client.GetPreparer(ctx, resourceGroupName, ruleName)
226	if err != nil {
227		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Get", nil, "Failure preparing request")
228		return
229	}
230
231	resp, err := client.GetSender(req)
232	if err != nil {
233		result.Response = autorest.Response{Response: resp}
234		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Get", resp, "Failure sending request")
235		return
236	}
237
238	result, err = client.GetResponder(resp)
239	if err != nil {
240		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Get", resp, "Failure responding to request")
241	}
242
243	return
244}
245
246// GetPreparer prepares the Get request.
247func (client AlertRulesClient) GetPreparer(ctx context.Context, resourceGroupName string, ruleName string) (*http.Request, error) {
248	pathParameters := map[string]interface{}{
249		"resourceGroupName": autorest.Encode("path", resourceGroupName),
250		"ruleName":          autorest.Encode("path", ruleName),
251		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
252	}
253
254	const APIVersion = "2016-03-01"
255	queryParameters := map[string]interface{}{
256		"api-version": APIVersion,
257	}
258
259	preparer := autorest.CreatePreparer(
260		autorest.AsGet(),
261		autorest.WithBaseURL(client.BaseURI),
262		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
263		autorest.WithQueryParameters(queryParameters))
264	return preparer.Prepare((&http.Request{}).WithContext(ctx))
265}
266
267// GetSender sends the Get request. The method will close the
268// http.Response Body if it receives an error.
269func (client AlertRulesClient) GetSender(req *http.Request) (*http.Response, error) {
270	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
271	return autorest.SendWithSender(client, req, sd...)
272}
273
274// GetResponder handles the response to the Get request. The method always
275// closes the http.Response Body.
276func (client AlertRulesClient) GetResponder(resp *http.Response) (result AlertRuleResource, err error) {
277	err = autorest.Respond(
278		resp,
279		client.ByInspecting(),
280		azure.WithErrorUnlessStatusCode(http.StatusOK),
281		autorest.ByUnmarshallingJSON(&result),
282		autorest.ByClosing())
283	result.Response = autorest.Response{Response: resp}
284	return
285}
286
287// ListByResourceGroup list the alert rules within a resource group.
288// Parameters:
289// resourceGroupName - the name of the resource group.
290func (client AlertRulesClient) ListByResourceGroup(ctx context.Context, resourceGroupName string) (result AlertRuleResourceCollection, err error) {
291	if tracing.IsEnabled() {
292		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.ListByResourceGroup")
293		defer func() {
294			sc := -1
295			if result.Response.Response != nil {
296				sc = result.Response.Response.StatusCode
297			}
298			tracing.EndSpan(ctx, sc, err)
299		}()
300	}
301	req, err := client.ListByResourceGroupPreparer(ctx, resourceGroupName)
302	if err != nil {
303		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListByResourceGroup", nil, "Failure preparing request")
304		return
305	}
306
307	resp, err := client.ListByResourceGroupSender(req)
308	if err != nil {
309		result.Response = autorest.Response{Response: resp}
310		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListByResourceGroup", resp, "Failure sending request")
311		return
312	}
313
314	result, err = client.ListByResourceGroupResponder(resp)
315	if err != nil {
316		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListByResourceGroup", resp, "Failure responding to request")
317	}
318
319	return
320}
321
322// ListByResourceGroupPreparer prepares the ListByResourceGroup request.
323func (client AlertRulesClient) ListByResourceGroupPreparer(ctx context.Context, resourceGroupName string) (*http.Request, error) {
324	pathParameters := map[string]interface{}{
325		"resourceGroupName": autorest.Encode("path", resourceGroupName),
326		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
327	}
328
329	const APIVersion = "2016-03-01"
330	queryParameters := map[string]interface{}{
331		"api-version": APIVersion,
332	}
333
334	preparer := autorest.CreatePreparer(
335		autorest.AsGet(),
336		autorest.WithBaseURL(client.BaseURI),
337		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules", pathParameters),
338		autorest.WithQueryParameters(queryParameters))
339	return preparer.Prepare((&http.Request{}).WithContext(ctx))
340}
341
342// ListByResourceGroupSender sends the ListByResourceGroup request. The method will close the
343// http.Response Body if it receives an error.
344func (client AlertRulesClient) ListByResourceGroupSender(req *http.Request) (*http.Response, error) {
345	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
346	return autorest.SendWithSender(client, req, sd...)
347}
348
349// ListByResourceGroupResponder handles the response to the ListByResourceGroup request. The method always
350// closes the http.Response Body.
351func (client AlertRulesClient) ListByResourceGroupResponder(resp *http.Response) (result AlertRuleResourceCollection, err error) {
352	err = autorest.Respond(
353		resp,
354		client.ByInspecting(),
355		azure.WithErrorUnlessStatusCode(http.StatusOK),
356		autorest.ByUnmarshallingJSON(&result),
357		autorest.ByClosing())
358	result.Response = autorest.Response{Response: resp}
359	return
360}
361
362// ListBySubscription list the alert rules within a subscription.
363func (client AlertRulesClient) ListBySubscription(ctx context.Context) (result AlertRuleResourceCollection, err error) {
364	if tracing.IsEnabled() {
365		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.ListBySubscription")
366		defer func() {
367			sc := -1
368			if result.Response.Response != nil {
369				sc = result.Response.Response.StatusCode
370			}
371			tracing.EndSpan(ctx, sc, err)
372		}()
373	}
374	req, err := client.ListBySubscriptionPreparer(ctx)
375	if err != nil {
376		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListBySubscription", nil, "Failure preparing request")
377		return
378	}
379
380	resp, err := client.ListBySubscriptionSender(req)
381	if err != nil {
382		result.Response = autorest.Response{Response: resp}
383		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListBySubscription", resp, "Failure sending request")
384		return
385	}
386
387	result, err = client.ListBySubscriptionResponder(resp)
388	if err != nil {
389		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "ListBySubscription", resp, "Failure responding to request")
390	}
391
392	return
393}
394
395// ListBySubscriptionPreparer prepares the ListBySubscription request.
396func (client AlertRulesClient) ListBySubscriptionPreparer(ctx context.Context) (*http.Request, error) {
397	pathParameters := map[string]interface{}{
398		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
399	}
400
401	const APIVersion = "2016-03-01"
402	queryParameters := map[string]interface{}{
403		"api-version": APIVersion,
404	}
405
406	preparer := autorest.CreatePreparer(
407		autorest.AsGet(),
408		autorest.WithBaseURL(client.BaseURI),
409		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/microsoft.insights/alertrules", pathParameters),
410		autorest.WithQueryParameters(queryParameters))
411	return preparer.Prepare((&http.Request{}).WithContext(ctx))
412}
413
414// ListBySubscriptionSender sends the ListBySubscription request. The method will close the
415// http.Response Body if it receives an error.
416func (client AlertRulesClient) ListBySubscriptionSender(req *http.Request) (*http.Response, error) {
417	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
418	return autorest.SendWithSender(client, req, sd...)
419}
420
421// ListBySubscriptionResponder handles the response to the ListBySubscription request. The method always
422// closes the http.Response Body.
423func (client AlertRulesClient) ListBySubscriptionResponder(resp *http.Response) (result AlertRuleResourceCollection, err error) {
424	err = autorest.Respond(
425		resp,
426		client.ByInspecting(),
427		azure.WithErrorUnlessStatusCode(http.StatusOK),
428		autorest.ByUnmarshallingJSON(&result),
429		autorest.ByClosing())
430	result.Response = autorest.Response{Response: resp}
431	return
432}
433
434// Update updates an existing AlertRuleResource. To update other fields use the CreateOrUpdate method.
435// Parameters:
436// resourceGroupName - the name of the resource group.
437// ruleName - the name of the rule.
438// alertRulesResource - parameters supplied to the operation.
439func (client AlertRulesClient) Update(ctx context.Context, resourceGroupName string, ruleName string, alertRulesResource AlertRuleResourcePatch) (result AlertRuleResource, err error) {
440	if tracing.IsEnabled() {
441		ctx = tracing.StartSpan(ctx, fqdn+"/AlertRulesClient.Update")
442		defer func() {
443			sc := -1
444			if result.Response.Response != nil {
445				sc = result.Response.Response.StatusCode
446			}
447			tracing.EndSpan(ctx, sc, err)
448		}()
449	}
450	req, err := client.UpdatePreparer(ctx, resourceGroupName, ruleName, alertRulesResource)
451	if err != nil {
452		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Update", nil, "Failure preparing request")
453		return
454	}
455
456	resp, err := client.UpdateSender(req)
457	if err != nil {
458		result.Response = autorest.Response{Response: resp}
459		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Update", resp, "Failure sending request")
460		return
461	}
462
463	result, err = client.UpdateResponder(resp)
464	if err != nil {
465		err = autorest.NewErrorWithError(err, "insights.AlertRulesClient", "Update", resp, "Failure responding to request")
466	}
467
468	return
469}
470
471// UpdatePreparer prepares the Update request.
472func (client AlertRulesClient) UpdatePreparer(ctx context.Context, resourceGroupName string, ruleName string, alertRulesResource AlertRuleResourcePatch) (*http.Request, error) {
473	pathParameters := map[string]interface{}{
474		"resourceGroupName": autorest.Encode("path", resourceGroupName),
475		"ruleName":          autorest.Encode("path", ruleName),
476		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
477	}
478
479	const APIVersion = "2016-03-01"
480	queryParameters := map[string]interface{}{
481		"api-version": APIVersion,
482	}
483
484	preparer := autorest.CreatePreparer(
485		autorest.AsContentType("application/json; charset=utf-8"),
486		autorest.AsPatch(),
487		autorest.WithBaseURL(client.BaseURI),
488		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules/{ruleName}", pathParameters),
489		autorest.WithJSON(alertRulesResource),
490		autorest.WithQueryParameters(queryParameters))
491	return preparer.Prepare((&http.Request{}).WithContext(ctx))
492}
493
494// UpdateSender sends the Update request. The method will close the
495// http.Response Body if it receives an error.
496func (client AlertRulesClient) UpdateSender(req *http.Request) (*http.Response, error) {
497	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
498	return autorest.SendWithSender(client, req, sd...)
499}
500
501// UpdateResponder handles the response to the Update request. The method always
502// closes the http.Response Body.
503func (client AlertRulesClient) UpdateResponder(resp *http.Response) (result AlertRuleResource, err error) {
504	err = autorest.Respond(
505		resp,
506		client.ByInspecting(),
507		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
508		autorest.ByUnmarshallingJSON(&result),
509		autorest.ByClosing())
510	result.Response = autorest.Response{Response: resp}
511	return
512}
513