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