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