1package operationalinsights
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// SavedSearchesClient is the operational Insights Client
30type SavedSearchesClient struct {
31	BaseClient
32}
33
34// NewSavedSearchesClient creates an instance of the SavedSearchesClient client.
35func NewSavedSearchesClient(subscriptionID string, purgeID string) SavedSearchesClient {
36	return NewSavedSearchesClientWithBaseURI(DefaultBaseURI, subscriptionID, purgeID)
37}
38
39// NewSavedSearchesClientWithBaseURI creates an instance of the SavedSearchesClient client using a custom endpoint.
40// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewSavedSearchesClientWithBaseURI(baseURI string, subscriptionID string, purgeID string) SavedSearchesClient {
42	return SavedSearchesClient{NewWithBaseURI(baseURI, subscriptionID, purgeID)}
43}
44
45// CreateOrUpdate creates or updates a saved search for a given workspace.
46// Parameters:
47// resourceGroupName - the Resource Group name.
48// workspaceName - the Log Analytics Workspace name.
49// savedSearchID - the id of the saved search.
50// parameters - the parameters required to save a search.
51func (client SavedSearchesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, parameters SavedSearch) (result SavedSearch, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/SavedSearchesClient.CreateOrUpdate")
54		defer func() {
55			sc := -1
56			if result.Response.Response != nil {
57				sc = result.Response.Response.StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	if err := validation.Validate([]validation.Validation{
63		{TargetValue: resourceGroupName,
64			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
65				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
66				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
67		{TargetValue: parameters,
68			Constraints: []validation.Constraint{{Target: "parameters.SavedSearchProperties", Name: validation.Null, Rule: true,
69				Chain: []validation.Constraint{{Target: "parameters.SavedSearchProperties.Category", Name: validation.Null, Rule: true, Chain: nil},
70					{Target: "parameters.SavedSearchProperties.DisplayName", Name: validation.Null, Rule: true, Chain: nil},
71					{Target: "parameters.SavedSearchProperties.Query", Name: validation.Null, Rule: true, Chain: nil},
72					{Target: "parameters.SavedSearchProperties.Version", Name: validation.Null, Rule: false,
73						Chain: []validation.Constraint{{Target: "parameters.SavedSearchProperties.Version", Name: validation.InclusiveMaximum, Rule: int64(2), Chain: nil},
74							{Target: "parameters.SavedSearchProperties.Version", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil},
75						}},
76				}}}}}); err != nil {
77		return result, validation.NewError("operationalinsights.SavedSearchesClient", "CreateOrUpdate", err.Error())
78	}
79
80	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, workspaceName, savedSearchID, parameters)
81	if err != nil {
82		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "CreateOrUpdate", nil, "Failure preparing request")
83		return
84	}
85
86	resp, err := client.CreateOrUpdateSender(req)
87	if err != nil {
88		result.Response = autorest.Response{Response: resp}
89		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "CreateOrUpdate", resp, "Failure sending request")
90		return
91	}
92
93	result, err = client.CreateOrUpdateResponder(resp)
94	if err != nil {
95		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "CreateOrUpdate", resp, "Failure responding to request")
96	}
97
98	return
99}
100
101// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
102func (client SavedSearchesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, parameters SavedSearch) (*http.Request, error) {
103	pathParameters := map[string]interface{}{
104		"resourceGroupName": autorest.Encode("path", resourceGroupName),
105		"savedSearchId":     autorest.Encode("path", savedSearchID),
106		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
107		"workspaceName":     autorest.Encode("path", workspaceName),
108	}
109
110	const APIVersion = "2015-03-20"
111	queryParameters := map[string]interface{}{
112		"api-version": APIVersion,
113	}
114
115	parameters.ID = nil
116	parameters.Name = nil
117	parameters.Type = nil
118	preparer := autorest.CreatePreparer(
119		autorest.AsContentType("application/json; charset=utf-8"),
120		autorest.AsPut(),
121		autorest.WithBaseURL(client.BaseURI),
122		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches/{savedSearchId}", pathParameters),
123		autorest.WithJSON(parameters),
124		autorest.WithQueryParameters(queryParameters))
125	return preparer.Prepare((&http.Request{}).WithContext(ctx))
126}
127
128// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
129// http.Response Body if it receives an error.
130func (client SavedSearchesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
131	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
132}
133
134// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
135// closes the http.Response Body.
136func (client SavedSearchesClient) CreateOrUpdateResponder(resp *http.Response) (result SavedSearch, err error) {
137	err = autorest.Respond(
138		resp,
139		client.ByInspecting(),
140		azure.WithErrorUnlessStatusCode(http.StatusOK),
141		autorest.ByUnmarshallingJSON(&result),
142		autorest.ByClosing())
143	result.Response = autorest.Response{Response: resp}
144	return
145}
146
147// Delete deletes the specified saved search in a given workspace.
148// Parameters:
149// resourceGroupName - the Resource Group name.
150// workspaceName - the Log Analytics Workspace name.
151// savedSearchID - the id of the saved search.
152func (client SavedSearchesClient) Delete(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string) (result autorest.Response, err error) {
153	if tracing.IsEnabled() {
154		ctx = tracing.StartSpan(ctx, fqdn+"/SavedSearchesClient.Delete")
155		defer func() {
156			sc := -1
157			if result.Response != nil {
158				sc = result.Response.StatusCode
159			}
160			tracing.EndSpan(ctx, sc, err)
161		}()
162	}
163	if err := validation.Validate([]validation.Validation{
164		{TargetValue: resourceGroupName,
165			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
166				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
167				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
168		return result, validation.NewError("operationalinsights.SavedSearchesClient", "Delete", err.Error())
169	}
170
171	req, err := client.DeletePreparer(ctx, resourceGroupName, workspaceName, savedSearchID)
172	if err != nil {
173		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "Delete", nil, "Failure preparing request")
174		return
175	}
176
177	resp, err := client.DeleteSender(req)
178	if err != nil {
179		result.Response = resp
180		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "Delete", resp, "Failure sending request")
181		return
182	}
183
184	result, err = client.DeleteResponder(resp)
185	if err != nil {
186		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "Delete", resp, "Failure responding to request")
187	}
188
189	return
190}
191
192// DeletePreparer prepares the Delete request.
193func (client SavedSearchesClient) DeletePreparer(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string) (*http.Request, error) {
194	pathParameters := map[string]interface{}{
195		"resourceGroupName": autorest.Encode("path", resourceGroupName),
196		"savedSearchId":     autorest.Encode("path", savedSearchID),
197		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
198		"workspaceName":     autorest.Encode("path", workspaceName),
199	}
200
201	const APIVersion = "2015-03-20"
202	queryParameters := map[string]interface{}{
203		"api-version": APIVersion,
204	}
205
206	preparer := autorest.CreatePreparer(
207		autorest.AsDelete(),
208		autorest.WithBaseURL(client.BaseURI),
209		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches/{savedSearchId}", pathParameters),
210		autorest.WithQueryParameters(queryParameters))
211	return preparer.Prepare((&http.Request{}).WithContext(ctx))
212}
213
214// DeleteSender sends the Delete request. The method will close the
215// http.Response Body if it receives an error.
216func (client SavedSearchesClient) DeleteSender(req *http.Request) (*http.Response, error) {
217	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
218}
219
220// DeleteResponder handles the response to the Delete request. The method always
221// closes the http.Response Body.
222func (client SavedSearchesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
223	err = autorest.Respond(
224		resp,
225		client.ByInspecting(),
226		azure.WithErrorUnlessStatusCode(http.StatusOK),
227		autorest.ByClosing())
228	result.Response = resp
229	return
230}
231
232// Get gets the specified saved search for a given workspace.
233// Parameters:
234// resourceGroupName - the Resource Group name.
235// workspaceName - the Log Analytics Workspace name.
236// savedSearchID - the id of the saved search.
237func (client SavedSearchesClient) Get(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string) (result SavedSearch, err error) {
238	if tracing.IsEnabled() {
239		ctx = tracing.StartSpan(ctx, fqdn+"/SavedSearchesClient.Get")
240		defer func() {
241			sc := -1
242			if result.Response.Response != nil {
243				sc = result.Response.Response.StatusCode
244			}
245			tracing.EndSpan(ctx, sc, err)
246		}()
247	}
248	if err := validation.Validate([]validation.Validation{
249		{TargetValue: resourceGroupName,
250			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
251				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
252				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
253		return result, validation.NewError("operationalinsights.SavedSearchesClient", "Get", err.Error())
254	}
255
256	req, err := client.GetPreparer(ctx, resourceGroupName, workspaceName, savedSearchID)
257	if err != nil {
258		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "Get", nil, "Failure preparing request")
259		return
260	}
261
262	resp, err := client.GetSender(req)
263	if err != nil {
264		result.Response = autorest.Response{Response: resp}
265		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "Get", resp, "Failure sending request")
266		return
267	}
268
269	result, err = client.GetResponder(resp)
270	if err != nil {
271		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "Get", resp, "Failure responding to request")
272	}
273
274	return
275}
276
277// GetPreparer prepares the Get request.
278func (client SavedSearchesClient) GetPreparer(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string) (*http.Request, error) {
279	pathParameters := map[string]interface{}{
280		"resourceGroupName": autorest.Encode("path", resourceGroupName),
281		"savedSearchId":     autorest.Encode("path", savedSearchID),
282		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
283		"workspaceName":     autorest.Encode("path", workspaceName),
284	}
285
286	const APIVersion = "2015-03-20"
287	queryParameters := map[string]interface{}{
288		"api-version": APIVersion,
289	}
290
291	preparer := autorest.CreatePreparer(
292		autorest.AsGet(),
293		autorest.WithBaseURL(client.BaseURI),
294		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches/{savedSearchId}", pathParameters),
295		autorest.WithQueryParameters(queryParameters))
296	return preparer.Prepare((&http.Request{}).WithContext(ctx))
297}
298
299// GetSender sends the Get request. The method will close the
300// http.Response Body if it receives an error.
301func (client SavedSearchesClient) GetSender(req *http.Request) (*http.Response, error) {
302	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
303}
304
305// GetResponder handles the response to the Get request. The method always
306// closes the http.Response Body.
307func (client SavedSearchesClient) GetResponder(resp *http.Response) (result SavedSearch, err error) {
308	err = autorest.Respond(
309		resp,
310		client.ByInspecting(),
311		azure.WithErrorUnlessStatusCode(http.StatusOK),
312		autorest.ByUnmarshallingJSON(&result),
313		autorest.ByClosing())
314	result.Response = autorest.Response{Response: resp}
315	return
316}
317
318// ListByWorkspace gets the saved searches for a given Log Analytics Workspace
319// Parameters:
320// resourceGroupName - the Resource Group name.
321// workspaceName - the Log Analytics Workspace name.
322func (client SavedSearchesClient) ListByWorkspace(ctx context.Context, resourceGroupName string, workspaceName string) (result SavedSearchesListResult, err error) {
323	if tracing.IsEnabled() {
324		ctx = tracing.StartSpan(ctx, fqdn+"/SavedSearchesClient.ListByWorkspace")
325		defer func() {
326			sc := -1
327			if result.Response.Response != nil {
328				sc = result.Response.Response.StatusCode
329			}
330			tracing.EndSpan(ctx, sc, err)
331		}()
332	}
333	if err := validation.Validate([]validation.Validation{
334		{TargetValue: resourceGroupName,
335			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
336				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
337				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
338		return result, validation.NewError("operationalinsights.SavedSearchesClient", "ListByWorkspace", err.Error())
339	}
340
341	req, err := client.ListByWorkspacePreparer(ctx, resourceGroupName, workspaceName)
342	if err != nil {
343		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "ListByWorkspace", nil, "Failure preparing request")
344		return
345	}
346
347	resp, err := client.ListByWorkspaceSender(req)
348	if err != nil {
349		result.Response = autorest.Response{Response: resp}
350		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "ListByWorkspace", resp, "Failure sending request")
351		return
352	}
353
354	result, err = client.ListByWorkspaceResponder(resp)
355	if err != nil {
356		err = autorest.NewErrorWithError(err, "operationalinsights.SavedSearchesClient", "ListByWorkspace", resp, "Failure responding to request")
357	}
358
359	return
360}
361
362// ListByWorkspacePreparer prepares the ListByWorkspace request.
363func (client SavedSearchesClient) ListByWorkspacePreparer(ctx context.Context, resourceGroupName string, workspaceName string) (*http.Request, error) {
364	pathParameters := map[string]interface{}{
365		"resourceGroupName": autorest.Encode("path", resourceGroupName),
366		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
367		"workspaceName":     autorest.Encode("path", workspaceName),
368	}
369
370	const APIVersion = "2015-03-20"
371	queryParameters := map[string]interface{}{
372		"api-version": APIVersion,
373	}
374
375	preparer := autorest.CreatePreparer(
376		autorest.AsGet(),
377		autorest.WithBaseURL(client.BaseURI),
378		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches", pathParameters),
379		autorest.WithQueryParameters(queryParameters))
380	return preparer.Prepare((&http.Request{}).WithContext(ctx))
381}
382
383// ListByWorkspaceSender sends the ListByWorkspace request. The method will close the
384// http.Response Body if it receives an error.
385func (client SavedSearchesClient) ListByWorkspaceSender(req *http.Request) (*http.Response, error) {
386	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
387}
388
389// ListByWorkspaceResponder handles the response to the ListByWorkspace request. The method always
390// closes the http.Response Body.
391func (client SavedSearchesClient) ListByWorkspaceResponder(resp *http.Response) (result SavedSearchesListResult, err error) {
392	err = autorest.Respond(
393		resp,
394		client.ByInspecting(),
395		azure.WithErrorUnlessStatusCode(http.StatusOK),
396		autorest.ByUnmarshallingJSON(&result),
397		autorest.ByClosing())
398	result.Response = autorest.Response{Response: resp}
399	return
400}
401