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