1// +build go1.13
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// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armoperationalinsights
9
10import (
11	"context"
12	"errors"
13	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
14	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
15	"net/http"
16	"net/url"
17	"strings"
18)
19
20// SavedSearchesClient contains the methods for the SavedSearches group.
21// Don't use this type directly, use NewSavedSearchesClient() instead.
22type SavedSearchesClient struct {
23	con            *armcore.Connection
24	subscriptionID string
25}
26
27// NewSavedSearchesClient creates a new instance of SavedSearchesClient with the specified values.
28func NewSavedSearchesClient(con *armcore.Connection, subscriptionID string) *SavedSearchesClient {
29	return &SavedSearchesClient{con: con, subscriptionID: subscriptionID}
30}
31
32// CreateOrUpdate - Creates or updates a saved search for a given workspace.
33// If the operation fails it returns a generic error.
34func (client *SavedSearchesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, parameters SavedSearch, options *SavedSearchesCreateOrUpdateOptions) (SavedSearchesCreateOrUpdateResponse, error) {
35	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, workspaceName, savedSearchID, parameters, options)
36	if err != nil {
37		return SavedSearchesCreateOrUpdateResponse{}, err
38	}
39	resp, err := client.con.Pipeline().Do(req)
40	if err != nil {
41		return SavedSearchesCreateOrUpdateResponse{}, err
42	}
43	if !resp.HasStatusCode(http.StatusOK) {
44		return SavedSearchesCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
45	}
46	return client.createOrUpdateHandleResponse(resp)
47}
48
49// createOrUpdateCreateRequest creates the CreateOrUpdate request.
50func (client *SavedSearchesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, parameters SavedSearch, options *SavedSearchesCreateOrUpdateOptions) (*azcore.Request, error) {
51	urlPath := "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches/{savedSearchId}"
52	if client.subscriptionID == "" {
53		return nil, errors.New("parameter client.subscriptionID cannot be empty")
54	}
55	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
56	if resourceGroupName == "" {
57		return nil, errors.New("parameter resourceGroupName cannot be empty")
58	}
59	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
60	if workspaceName == "" {
61		return nil, errors.New("parameter workspaceName cannot be empty")
62	}
63	urlPath = strings.ReplaceAll(urlPath, "{workspaceName}", url.PathEscape(workspaceName))
64	if savedSearchID == "" {
65		return nil, errors.New("parameter savedSearchID cannot be empty")
66	}
67	urlPath = strings.ReplaceAll(urlPath, "{savedSearchId}", url.PathEscape(savedSearchID))
68	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
69	if err != nil {
70		return nil, err
71	}
72	req.Telemetry(telemetryInfo)
73	reqQP := req.URL.Query()
74	reqQP.Set("api-version", "2020-08-01")
75	req.URL.RawQuery = reqQP.Encode()
76	req.Header.Set("Accept", "application/json")
77	return req, req.MarshalAsJSON(parameters)
78}
79
80// createOrUpdateHandleResponse handles the CreateOrUpdate response.
81func (client *SavedSearchesClient) createOrUpdateHandleResponse(resp *azcore.Response) (SavedSearchesCreateOrUpdateResponse, error) {
82	result := SavedSearchesCreateOrUpdateResponse{RawResponse: resp.Response}
83	if err := resp.UnmarshalAsJSON(&result.SavedSearch); err != nil {
84		return SavedSearchesCreateOrUpdateResponse{}, err
85	}
86	return result, nil
87}
88
89// createOrUpdateHandleError handles the CreateOrUpdate error response.
90func (client *SavedSearchesClient) createOrUpdateHandleError(resp *azcore.Response) error {
91	body, err := resp.Payload()
92	if err != nil {
93		return azcore.NewResponseError(err, resp.Response)
94	}
95	if len(body) == 0 {
96		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
97	}
98	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
99}
100
101// Delete - Deletes the specified saved search in a given workspace.
102// If the operation fails it returns a generic error.
103func (client *SavedSearchesClient) Delete(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, options *SavedSearchesDeleteOptions) (SavedSearchesDeleteResponse, error) {
104	req, err := client.deleteCreateRequest(ctx, resourceGroupName, workspaceName, savedSearchID, options)
105	if err != nil {
106		return SavedSearchesDeleteResponse{}, err
107	}
108	resp, err := client.con.Pipeline().Do(req)
109	if err != nil {
110		return SavedSearchesDeleteResponse{}, err
111	}
112	if !resp.HasStatusCode(http.StatusOK) {
113		return SavedSearchesDeleteResponse{}, client.deleteHandleError(resp)
114	}
115	return SavedSearchesDeleteResponse{RawResponse: resp.Response}, nil
116}
117
118// deleteCreateRequest creates the Delete request.
119func (client *SavedSearchesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, options *SavedSearchesDeleteOptions) (*azcore.Request, error) {
120	urlPath := "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches/{savedSearchId}"
121	if client.subscriptionID == "" {
122		return nil, errors.New("parameter client.subscriptionID cannot be empty")
123	}
124	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
125	if resourceGroupName == "" {
126		return nil, errors.New("parameter resourceGroupName cannot be empty")
127	}
128	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
129	if workspaceName == "" {
130		return nil, errors.New("parameter workspaceName cannot be empty")
131	}
132	urlPath = strings.ReplaceAll(urlPath, "{workspaceName}", url.PathEscape(workspaceName))
133	if savedSearchID == "" {
134		return nil, errors.New("parameter savedSearchID cannot be empty")
135	}
136	urlPath = strings.ReplaceAll(urlPath, "{savedSearchId}", url.PathEscape(savedSearchID))
137	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
138	if err != nil {
139		return nil, err
140	}
141	req.Telemetry(telemetryInfo)
142	reqQP := req.URL.Query()
143	reqQP.Set("api-version", "2020-08-01")
144	req.URL.RawQuery = reqQP.Encode()
145	return req, nil
146}
147
148// deleteHandleError handles the Delete error response.
149func (client *SavedSearchesClient) deleteHandleError(resp *azcore.Response) error {
150	body, err := resp.Payload()
151	if err != nil {
152		return azcore.NewResponseError(err, resp.Response)
153	}
154	if len(body) == 0 {
155		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
156	}
157	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
158}
159
160// Get - Gets the specified saved search for a given workspace.
161// If the operation fails it returns a generic error.
162func (client *SavedSearchesClient) Get(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, options *SavedSearchesGetOptions) (SavedSearchesGetResponse, error) {
163	req, err := client.getCreateRequest(ctx, resourceGroupName, workspaceName, savedSearchID, options)
164	if err != nil {
165		return SavedSearchesGetResponse{}, err
166	}
167	resp, err := client.con.Pipeline().Do(req)
168	if err != nil {
169		return SavedSearchesGetResponse{}, err
170	}
171	if !resp.HasStatusCode(http.StatusOK) {
172		return SavedSearchesGetResponse{}, client.getHandleError(resp)
173	}
174	return client.getHandleResponse(resp)
175}
176
177// getCreateRequest creates the Get request.
178func (client *SavedSearchesClient) getCreateRequest(ctx context.Context, resourceGroupName string, workspaceName string, savedSearchID string, options *SavedSearchesGetOptions) (*azcore.Request, error) {
179	urlPath := "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches/{savedSearchId}"
180	if client.subscriptionID == "" {
181		return nil, errors.New("parameter client.subscriptionID cannot be empty")
182	}
183	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
184	if resourceGroupName == "" {
185		return nil, errors.New("parameter resourceGroupName cannot be empty")
186	}
187	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
188	if workspaceName == "" {
189		return nil, errors.New("parameter workspaceName cannot be empty")
190	}
191	urlPath = strings.ReplaceAll(urlPath, "{workspaceName}", url.PathEscape(workspaceName))
192	if savedSearchID == "" {
193		return nil, errors.New("parameter savedSearchID cannot be empty")
194	}
195	urlPath = strings.ReplaceAll(urlPath, "{savedSearchId}", url.PathEscape(savedSearchID))
196	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
197	if err != nil {
198		return nil, err
199	}
200	req.Telemetry(telemetryInfo)
201	reqQP := req.URL.Query()
202	reqQP.Set("api-version", "2020-08-01")
203	req.URL.RawQuery = reqQP.Encode()
204	req.Header.Set("Accept", "application/json")
205	return req, nil
206}
207
208// getHandleResponse handles the Get response.
209func (client *SavedSearchesClient) getHandleResponse(resp *azcore.Response) (SavedSearchesGetResponse, error) {
210	result := SavedSearchesGetResponse{RawResponse: resp.Response}
211	if err := resp.UnmarshalAsJSON(&result.SavedSearch); err != nil {
212		return SavedSearchesGetResponse{}, err
213	}
214	return result, nil
215}
216
217// getHandleError handles the Get error response.
218func (client *SavedSearchesClient) getHandleError(resp *azcore.Response) error {
219	body, err := resp.Payload()
220	if err != nil {
221		return azcore.NewResponseError(err, resp.Response)
222	}
223	if len(body) == 0 {
224		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
225	}
226	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
227}
228
229// ListByWorkspace - Gets the saved searches for a given Log Analytics Workspace
230// If the operation fails it returns a generic error.
231func (client *SavedSearchesClient) ListByWorkspace(ctx context.Context, resourceGroupName string, workspaceName string, options *SavedSearchesListByWorkspaceOptions) (SavedSearchesListByWorkspaceResponse, error) {
232	req, err := client.listByWorkspaceCreateRequest(ctx, resourceGroupName, workspaceName, options)
233	if err != nil {
234		return SavedSearchesListByWorkspaceResponse{}, err
235	}
236	resp, err := client.con.Pipeline().Do(req)
237	if err != nil {
238		return SavedSearchesListByWorkspaceResponse{}, err
239	}
240	if !resp.HasStatusCode(http.StatusOK) {
241		return SavedSearchesListByWorkspaceResponse{}, client.listByWorkspaceHandleError(resp)
242	}
243	return client.listByWorkspaceHandleResponse(resp)
244}
245
246// listByWorkspaceCreateRequest creates the ListByWorkspace request.
247func (client *SavedSearchesClient) listByWorkspaceCreateRequest(ctx context.Context, resourceGroupName string, workspaceName string, options *SavedSearchesListByWorkspaceOptions) (*azcore.Request, error) {
248	urlPath := "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/savedSearches"
249	if resourceGroupName == "" {
250		return nil, errors.New("parameter resourceGroupName cannot be empty")
251	}
252	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
253	if workspaceName == "" {
254		return nil, errors.New("parameter workspaceName cannot be empty")
255	}
256	urlPath = strings.ReplaceAll(urlPath, "{workspaceName}", url.PathEscape(workspaceName))
257	if client.subscriptionID == "" {
258		return nil, errors.New("parameter client.subscriptionID cannot be empty")
259	}
260	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
261	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
262	if err != nil {
263		return nil, err
264	}
265	req.Telemetry(telemetryInfo)
266	reqQP := req.URL.Query()
267	reqQP.Set("api-version", "2020-08-01")
268	req.URL.RawQuery = reqQP.Encode()
269	req.Header.Set("Accept", "application/json")
270	return req, nil
271}
272
273// listByWorkspaceHandleResponse handles the ListByWorkspace response.
274func (client *SavedSearchesClient) listByWorkspaceHandleResponse(resp *azcore.Response) (SavedSearchesListByWorkspaceResponse, error) {
275	result := SavedSearchesListByWorkspaceResponse{RawResponse: resp.Response}
276	if err := resp.UnmarshalAsJSON(&result.SavedSearchesListResult); err != nil {
277		return SavedSearchesListByWorkspaceResponse{}, err
278	}
279	return result, nil
280}
281
282// listByWorkspaceHandleError handles the ListByWorkspace error response.
283func (client *SavedSearchesClient) listByWorkspaceHandleError(resp *azcore.Response) error {
284	body, err := resp.Payload()
285	if err != nil {
286		return azcore.NewResponseError(err, resp.Response)
287	}
288	if len(body) == 0 {
289		return azcore.NewResponseError(errors.New(resp.Status), resp.Response)
290	}
291	return azcore.NewResponseError(errors.New(string(body)), resp.Response)
292}
293