1package search
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/tracing"
14	"github.com/gofrs/uuid"
15	"net/http"
16)
17
18// QueryKeysClient is the client that can be used to manage Azure Cognitive Search services and API keys.
19type QueryKeysClient struct {
20	BaseClient
21}
22
23// NewQueryKeysClient creates an instance of the QueryKeysClient client.
24func NewQueryKeysClient(subscriptionID string) QueryKeysClient {
25	return NewQueryKeysClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewQueryKeysClientWithBaseURI creates an instance of the QueryKeysClient client using a custom endpoint.  Use this
29// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewQueryKeysClientWithBaseURI(baseURI string, subscriptionID string) QueryKeysClient {
31	return QueryKeysClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// Create generates a new query key for the specified Search service. You can create up to 50 query keys per service.
35// Parameters:
36// resourceGroupName - the name of the resource group within the current subscription. You can obtain this
37// value from the Azure Resource Manager API or the portal.
38// searchServiceName - the name of the Azure Cognitive Search service associated with the specified resource
39// group.
40// name - the name of the new query API key.
41// clientRequestID - a client-generated GUID value that identifies this request. If specified, this will be
42// included in response information as a way to track the request.
43func (client QueryKeysClient) Create(ctx context.Context, resourceGroupName string, searchServiceName string, name string, clientRequestID *uuid.UUID) (result QueryKey, err error) {
44	if tracing.IsEnabled() {
45		ctx = tracing.StartSpan(ctx, fqdn+"/QueryKeysClient.Create")
46		defer func() {
47			sc := -1
48			if result.Response.Response != nil {
49				sc = result.Response.Response.StatusCode
50			}
51			tracing.EndSpan(ctx, sc, err)
52		}()
53	}
54	req, err := client.CreatePreparer(ctx, resourceGroupName, searchServiceName, name, clientRequestID)
55	if err != nil {
56		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "Create", nil, "Failure preparing request")
57		return
58	}
59
60	resp, err := client.CreateSender(req)
61	if err != nil {
62		result.Response = autorest.Response{Response: resp}
63		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "Create", resp, "Failure sending request")
64		return
65	}
66
67	result, err = client.CreateResponder(resp)
68	if err != nil {
69		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "Create", resp, "Failure responding to request")
70		return
71	}
72
73	return
74}
75
76// CreatePreparer prepares the Create request.
77func (client QueryKeysClient) CreatePreparer(ctx context.Context, resourceGroupName string, searchServiceName string, name string, clientRequestID *uuid.UUID) (*http.Request, error) {
78	pathParameters := map[string]interface{}{
79		"name":              autorest.Encode("path", name),
80		"resourceGroupName": autorest.Encode("path", resourceGroupName),
81		"searchServiceName": autorest.Encode("path", searchServiceName),
82		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
83	}
84
85	const APIVersion = "2020-03-13"
86	queryParameters := map[string]interface{}{
87		"api-version": APIVersion,
88	}
89
90	preparer := autorest.CreatePreparer(
91		autorest.AsPost(),
92		autorest.WithBaseURL(client.BaseURI),
93		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}/createQueryKey/{name}", pathParameters),
94		autorest.WithQueryParameters(queryParameters))
95	if clientRequestID != nil {
96		preparer = autorest.DecoratePreparer(preparer,
97			autorest.WithHeader("x-ms-client-request-id", autorest.String(clientRequestID)))
98	}
99	return preparer.Prepare((&http.Request{}).WithContext(ctx))
100}
101
102// CreateSender sends the Create request. The method will close the
103// http.Response Body if it receives an error.
104func (client QueryKeysClient) CreateSender(req *http.Request) (*http.Response, error) {
105	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
106}
107
108// CreateResponder handles the response to the Create request. The method always
109// closes the http.Response Body.
110func (client QueryKeysClient) CreateResponder(resp *http.Response) (result QueryKey, err error) {
111	err = autorest.Respond(
112		resp,
113		azure.WithErrorUnlessStatusCode(http.StatusOK),
114		autorest.ByUnmarshallingJSON(&result),
115		autorest.ByClosing())
116	result.Response = autorest.Response{Response: resp}
117	return
118}
119
120// Delete deletes the specified query key. Unlike admin keys, query keys are not regenerated. The process for
121// regenerating a query key is to delete and then recreate it.
122// Parameters:
123// resourceGroupName - the name of the resource group within the current subscription. You can obtain this
124// value from the Azure Resource Manager API or the portal.
125// searchServiceName - the name of the Azure Cognitive Search service associated with the specified resource
126// group.
127// key - the query key to be deleted. Query keys are identified by value, not by name.
128// clientRequestID - a client-generated GUID value that identifies this request. If specified, this will be
129// included in response information as a way to track the request.
130func (client QueryKeysClient) Delete(ctx context.Context, resourceGroupName string, searchServiceName string, key string, clientRequestID *uuid.UUID) (result autorest.Response, err error) {
131	if tracing.IsEnabled() {
132		ctx = tracing.StartSpan(ctx, fqdn+"/QueryKeysClient.Delete")
133		defer func() {
134			sc := -1
135			if result.Response != nil {
136				sc = result.Response.StatusCode
137			}
138			tracing.EndSpan(ctx, sc, err)
139		}()
140	}
141	req, err := client.DeletePreparer(ctx, resourceGroupName, searchServiceName, key, clientRequestID)
142	if err != nil {
143		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "Delete", nil, "Failure preparing request")
144		return
145	}
146
147	resp, err := client.DeleteSender(req)
148	if err != nil {
149		result.Response = resp
150		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "Delete", resp, "Failure sending request")
151		return
152	}
153
154	result, err = client.DeleteResponder(resp)
155	if err != nil {
156		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "Delete", resp, "Failure responding to request")
157		return
158	}
159
160	return
161}
162
163// DeletePreparer prepares the Delete request.
164func (client QueryKeysClient) DeletePreparer(ctx context.Context, resourceGroupName string, searchServiceName string, key string, clientRequestID *uuid.UUID) (*http.Request, error) {
165	pathParameters := map[string]interface{}{
166		"key":               autorest.Encode("path", key),
167		"resourceGroupName": autorest.Encode("path", resourceGroupName),
168		"searchServiceName": autorest.Encode("path", searchServiceName),
169		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
170	}
171
172	const APIVersion = "2020-03-13"
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.Search/searchServices/{searchServiceName}/deleteQueryKey/{key}", pathParameters),
181		autorest.WithQueryParameters(queryParameters))
182	if clientRequestID != nil {
183		preparer = autorest.DecoratePreparer(preparer,
184			autorest.WithHeader("x-ms-client-request-id", autorest.String(clientRequestID)))
185	}
186	return preparer.Prepare((&http.Request{}).WithContext(ctx))
187}
188
189// DeleteSender sends the Delete request. The method will close the
190// http.Response Body if it receives an error.
191func (client QueryKeysClient) DeleteSender(req *http.Request) (*http.Response, error) {
192	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
193}
194
195// DeleteResponder handles the response to the Delete request. The method always
196// closes the http.Response Body.
197func (client QueryKeysClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
198	err = autorest.Respond(
199		resp,
200		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent, http.StatusNotFound),
201		autorest.ByClosing())
202	result.Response = resp
203	return
204}
205
206// ListBySearchService returns the list of query API keys for the given Azure Cognitive Search service.
207// Parameters:
208// resourceGroupName - the name of the resource group within the current subscription. You can obtain this
209// value from the Azure Resource Manager API or the portal.
210// searchServiceName - the name of the Azure Cognitive Search service associated with the specified resource
211// group.
212// clientRequestID - a client-generated GUID value that identifies this request. If specified, this will be
213// included in response information as a way to track the request.
214func (client QueryKeysClient) ListBySearchService(ctx context.Context, resourceGroupName string, searchServiceName string, clientRequestID *uuid.UUID) (result ListQueryKeysResultPage, err error) {
215	if tracing.IsEnabled() {
216		ctx = tracing.StartSpan(ctx, fqdn+"/QueryKeysClient.ListBySearchService")
217		defer func() {
218			sc := -1
219			if result.lqkr.Response.Response != nil {
220				sc = result.lqkr.Response.Response.StatusCode
221			}
222			tracing.EndSpan(ctx, sc, err)
223		}()
224	}
225	result.fn = client.listBySearchServiceNextResults
226	req, err := client.ListBySearchServicePreparer(ctx, resourceGroupName, searchServiceName, clientRequestID)
227	if err != nil {
228		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "ListBySearchService", nil, "Failure preparing request")
229		return
230	}
231
232	resp, err := client.ListBySearchServiceSender(req)
233	if err != nil {
234		result.lqkr.Response = autorest.Response{Response: resp}
235		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "ListBySearchService", resp, "Failure sending request")
236		return
237	}
238
239	result.lqkr, err = client.ListBySearchServiceResponder(resp)
240	if err != nil {
241		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "ListBySearchService", resp, "Failure responding to request")
242		return
243	}
244	if result.lqkr.hasNextLink() && result.lqkr.IsEmpty() {
245		err = result.NextWithContext(ctx)
246		return
247	}
248
249	return
250}
251
252// ListBySearchServicePreparer prepares the ListBySearchService request.
253func (client QueryKeysClient) ListBySearchServicePreparer(ctx context.Context, resourceGroupName string, searchServiceName string, clientRequestID *uuid.UUID) (*http.Request, error) {
254	pathParameters := map[string]interface{}{
255		"resourceGroupName": autorest.Encode("path", resourceGroupName),
256		"searchServiceName": autorest.Encode("path", searchServiceName),
257		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
258	}
259
260	const APIVersion = "2020-03-13"
261	queryParameters := map[string]interface{}{
262		"api-version": APIVersion,
263	}
264
265	preparer := autorest.CreatePreparer(
266		autorest.AsPost(),
267		autorest.WithBaseURL(client.BaseURI),
268		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}/listQueryKeys", pathParameters),
269		autorest.WithQueryParameters(queryParameters))
270	if clientRequestID != nil {
271		preparer = autorest.DecoratePreparer(preparer,
272			autorest.WithHeader("x-ms-client-request-id", autorest.String(clientRequestID)))
273	}
274	return preparer.Prepare((&http.Request{}).WithContext(ctx))
275}
276
277// ListBySearchServiceSender sends the ListBySearchService request. The method will close the
278// http.Response Body if it receives an error.
279func (client QueryKeysClient) ListBySearchServiceSender(req *http.Request) (*http.Response, error) {
280	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
281}
282
283// ListBySearchServiceResponder handles the response to the ListBySearchService request. The method always
284// closes the http.Response Body.
285func (client QueryKeysClient) ListBySearchServiceResponder(resp *http.Response) (result ListQueryKeysResult, err error) {
286	err = autorest.Respond(
287		resp,
288		azure.WithErrorUnlessStatusCode(http.StatusOK),
289		autorest.ByUnmarshallingJSON(&result),
290		autorest.ByClosing())
291	result.Response = autorest.Response{Response: resp}
292	return
293}
294
295// listBySearchServiceNextResults retrieves the next set of results, if any.
296func (client QueryKeysClient) listBySearchServiceNextResults(ctx context.Context, lastResults ListQueryKeysResult) (result ListQueryKeysResult, err error) {
297	req, err := lastResults.listQueryKeysResultPreparer(ctx)
298	if err != nil {
299		return result, autorest.NewErrorWithError(err, "search.QueryKeysClient", "listBySearchServiceNextResults", nil, "Failure preparing next results request")
300	}
301	if req == nil {
302		return
303	}
304	resp, err := client.ListBySearchServiceSender(req)
305	if err != nil {
306		result.Response = autorest.Response{Response: resp}
307		return result, autorest.NewErrorWithError(err, "search.QueryKeysClient", "listBySearchServiceNextResults", resp, "Failure sending next results request")
308	}
309	result, err = client.ListBySearchServiceResponder(resp)
310	if err != nil {
311		err = autorest.NewErrorWithError(err, "search.QueryKeysClient", "listBySearchServiceNextResults", resp, "Failure responding to next results request")
312	}
313	return
314}
315
316// ListBySearchServiceComplete enumerates all values, automatically crossing page boundaries as required.
317func (client QueryKeysClient) ListBySearchServiceComplete(ctx context.Context, resourceGroupName string, searchServiceName string, clientRequestID *uuid.UUID) (result ListQueryKeysResultIterator, err error) {
318	if tracing.IsEnabled() {
319		ctx = tracing.StartSpan(ctx, fqdn+"/QueryKeysClient.ListBySearchService")
320		defer func() {
321			sc := -1
322			if result.Response().Response.Response != nil {
323				sc = result.page.Response().Response.Response.StatusCode
324			}
325			tracing.EndSpan(ctx, sc, err)
326		}()
327	}
328	result.page, err = client.ListBySearchService(ctx, resourceGroupName, searchServiceName, clientRequestID)
329	return
330}
331