1package redis
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// LinkedServerClient is the REST API for Azure Redis Cache Service.
30type LinkedServerClient struct {
31	BaseClient
32}
33
34// NewLinkedServerClient creates an instance of the LinkedServerClient client.
35func NewLinkedServerClient(subscriptionID string) LinkedServerClient {
36	return NewLinkedServerClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewLinkedServerClientWithBaseURI creates an instance of the LinkedServerClient client using a custom endpoint.  Use
40// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewLinkedServerClientWithBaseURI(baseURI string, subscriptionID string) LinkedServerClient {
42	return LinkedServerClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// Create adds a linked server to the Redis cache (requires Premium SKU).
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// name - the name of the Redis cache.
49// linkedServerName - the name of the linked server that is being added to the Redis cache.
50// parameters - parameters supplied to the Create Linked server operation.
51func (client LinkedServerClient) Create(ctx context.Context, resourceGroupName string, name string, linkedServerName string, parameters LinkedServerCreateParameters) (result LinkedServerCreateFuture, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.Create")
54		defer func() {
55			sc := -1
56			if result.Response() != nil {
57				sc = result.Response().StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	if err := validation.Validate([]validation.Validation{
63		{TargetValue: parameters,
64			Constraints: []validation.Constraint{{Target: "parameters.LinkedServerCreateProperties", Name: validation.Null, Rule: true,
65				Chain: []validation.Constraint{{Target: "parameters.LinkedServerCreateProperties.LinkedRedisCacheID", Name: validation.Null, Rule: true, Chain: nil},
66					{Target: "parameters.LinkedServerCreateProperties.LinkedRedisCacheLocation", Name: validation.Null, Rule: true, Chain: nil},
67				}}}}}); err != nil {
68		return result, validation.NewError("redis.LinkedServerClient", "Create", err.Error())
69	}
70
71	req, err := client.CreatePreparer(ctx, resourceGroupName, name, linkedServerName, parameters)
72	if err != nil {
73		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Create", nil, "Failure preparing request")
74		return
75	}
76
77	result, err = client.CreateSender(req)
78	if err != nil {
79		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Create", result.Response(), "Failure sending request")
80		return
81	}
82
83	return
84}
85
86// CreatePreparer prepares the Create request.
87func (client LinkedServerClient) CreatePreparer(ctx context.Context, resourceGroupName string, name string, linkedServerName string, parameters LinkedServerCreateParameters) (*http.Request, error) {
88	pathParameters := map[string]interface{}{
89		"linkedServerName":  autorest.Encode("path", linkedServerName),
90		"name":              autorest.Encode("path", name),
91		"resourceGroupName": autorest.Encode("path", resourceGroupName),
92		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
93	}
94
95	const APIVersion = "2018-03-01"
96	queryParameters := map[string]interface{}{
97		"api-version": APIVersion,
98	}
99
100	preparer := autorest.CreatePreparer(
101		autorest.AsContentType("application/json; charset=utf-8"),
102		autorest.AsPut(),
103		autorest.WithBaseURL(client.BaseURI),
104		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers/{linkedServerName}", pathParameters),
105		autorest.WithJSON(parameters),
106		autorest.WithQueryParameters(queryParameters))
107	return preparer.Prepare((&http.Request{}).WithContext(ctx))
108}
109
110// CreateSender sends the Create request. The method will close the
111// http.Response Body if it receives an error.
112func (client LinkedServerClient) CreateSender(req *http.Request) (future LinkedServerCreateFuture, err error) {
113	var resp *http.Response
114	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
115	if err != nil {
116		return
117	}
118	future.Future, err = azure.NewFutureFromResponse(resp)
119	return
120}
121
122// CreateResponder handles the response to the Create request. The method always
123// closes the http.Response Body.
124func (client LinkedServerClient) CreateResponder(resp *http.Response) (result LinkedServerWithProperties, err error) {
125	err = autorest.Respond(
126		resp,
127		client.ByInspecting(),
128		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
129		autorest.ByUnmarshallingJSON(&result),
130		autorest.ByClosing())
131	result.Response = autorest.Response{Response: resp}
132	return
133}
134
135// Delete deletes the linked server from a redis cache (requires Premium SKU).
136// Parameters:
137// resourceGroupName - the name of the resource group.
138// name - the name of the redis cache.
139// linkedServerName - the name of the linked server that is being added to the Redis cache.
140func (client LinkedServerClient) Delete(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (result autorest.Response, err error) {
141	if tracing.IsEnabled() {
142		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.Delete")
143		defer func() {
144			sc := -1
145			if result.Response != nil {
146				sc = result.Response.StatusCode
147			}
148			tracing.EndSpan(ctx, sc, err)
149		}()
150	}
151	req, err := client.DeletePreparer(ctx, resourceGroupName, name, linkedServerName)
152	if err != nil {
153		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Delete", nil, "Failure preparing request")
154		return
155	}
156
157	resp, err := client.DeleteSender(req)
158	if err != nil {
159		result.Response = resp
160		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Delete", resp, "Failure sending request")
161		return
162	}
163
164	result, err = client.DeleteResponder(resp)
165	if err != nil {
166		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Delete", resp, "Failure responding to request")
167	}
168
169	return
170}
171
172// DeletePreparer prepares the Delete request.
173func (client LinkedServerClient) DeletePreparer(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (*http.Request, error) {
174	pathParameters := map[string]interface{}{
175		"linkedServerName":  autorest.Encode("path", linkedServerName),
176		"name":              autorest.Encode("path", name),
177		"resourceGroupName": autorest.Encode("path", resourceGroupName),
178		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
179	}
180
181	const APIVersion = "2018-03-01"
182	queryParameters := map[string]interface{}{
183		"api-version": APIVersion,
184	}
185
186	preparer := autorest.CreatePreparer(
187		autorest.AsDelete(),
188		autorest.WithBaseURL(client.BaseURI),
189		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers/{linkedServerName}", pathParameters),
190		autorest.WithQueryParameters(queryParameters))
191	return preparer.Prepare((&http.Request{}).WithContext(ctx))
192}
193
194// DeleteSender sends the Delete request. The method will close the
195// http.Response Body if it receives an error.
196func (client LinkedServerClient) DeleteSender(req *http.Request) (*http.Response, error) {
197	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
198}
199
200// DeleteResponder handles the response to the Delete request. The method always
201// closes the http.Response Body.
202func (client LinkedServerClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
203	err = autorest.Respond(
204		resp,
205		client.ByInspecting(),
206		azure.WithErrorUnlessStatusCode(http.StatusOK),
207		autorest.ByClosing())
208	result.Response = resp
209	return
210}
211
212// Get gets the detailed information about a linked server of a redis cache (requires Premium SKU).
213// Parameters:
214// resourceGroupName - the name of the resource group.
215// name - the name of the redis cache.
216// linkedServerName - the name of the linked server.
217func (client LinkedServerClient) Get(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (result LinkedServerWithProperties, err error) {
218	if tracing.IsEnabled() {
219		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.Get")
220		defer func() {
221			sc := -1
222			if result.Response.Response != nil {
223				sc = result.Response.Response.StatusCode
224			}
225			tracing.EndSpan(ctx, sc, err)
226		}()
227	}
228	req, err := client.GetPreparer(ctx, resourceGroupName, name, linkedServerName)
229	if err != nil {
230		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Get", nil, "Failure preparing request")
231		return
232	}
233
234	resp, err := client.GetSender(req)
235	if err != nil {
236		result.Response = autorest.Response{Response: resp}
237		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Get", resp, "Failure sending request")
238		return
239	}
240
241	result, err = client.GetResponder(resp)
242	if err != nil {
243		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Get", resp, "Failure responding to request")
244	}
245
246	return
247}
248
249// GetPreparer prepares the Get request.
250func (client LinkedServerClient) GetPreparer(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (*http.Request, error) {
251	pathParameters := map[string]interface{}{
252		"linkedServerName":  autorest.Encode("path", linkedServerName),
253		"name":              autorest.Encode("path", name),
254		"resourceGroupName": autorest.Encode("path", resourceGroupName),
255		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
256	}
257
258	const APIVersion = "2018-03-01"
259	queryParameters := map[string]interface{}{
260		"api-version": APIVersion,
261	}
262
263	preparer := autorest.CreatePreparer(
264		autorest.AsGet(),
265		autorest.WithBaseURL(client.BaseURI),
266		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers/{linkedServerName}", pathParameters),
267		autorest.WithQueryParameters(queryParameters))
268	return preparer.Prepare((&http.Request{}).WithContext(ctx))
269}
270
271// GetSender sends the Get request. The method will close the
272// http.Response Body if it receives an error.
273func (client LinkedServerClient) GetSender(req *http.Request) (*http.Response, error) {
274	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
275}
276
277// GetResponder handles the response to the Get request. The method always
278// closes the http.Response Body.
279func (client LinkedServerClient) GetResponder(resp *http.Response) (result LinkedServerWithProperties, err error) {
280	err = autorest.Respond(
281		resp,
282		client.ByInspecting(),
283		azure.WithErrorUnlessStatusCode(http.StatusOK),
284		autorest.ByUnmarshallingJSON(&result),
285		autorest.ByClosing())
286	result.Response = autorest.Response{Response: resp}
287	return
288}
289
290// List gets the list of linked servers associated with this redis cache (requires Premium SKU).
291// Parameters:
292// resourceGroupName - the name of the resource group.
293// name - the name of the redis cache.
294func (client LinkedServerClient) List(ctx context.Context, resourceGroupName string, name string) (result LinkedServerWithPropertiesListPage, err error) {
295	if tracing.IsEnabled() {
296		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.List")
297		defer func() {
298			sc := -1
299			if result.lswpl.Response.Response != nil {
300				sc = result.lswpl.Response.Response.StatusCode
301			}
302			tracing.EndSpan(ctx, sc, err)
303		}()
304	}
305	result.fn = client.listNextResults
306	req, err := client.ListPreparer(ctx, resourceGroupName, name)
307	if err != nil {
308		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "List", nil, "Failure preparing request")
309		return
310	}
311
312	resp, err := client.ListSender(req)
313	if err != nil {
314		result.lswpl.Response = autorest.Response{Response: resp}
315		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "List", resp, "Failure sending request")
316		return
317	}
318
319	result.lswpl, err = client.ListResponder(resp)
320	if err != nil {
321		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "List", resp, "Failure responding to request")
322	}
323
324	return
325}
326
327// ListPreparer prepares the List request.
328func (client LinkedServerClient) ListPreparer(ctx context.Context, resourceGroupName string, name string) (*http.Request, error) {
329	pathParameters := map[string]interface{}{
330		"name":              autorest.Encode("path", name),
331		"resourceGroupName": autorest.Encode("path", resourceGroupName),
332		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
333	}
334
335	const APIVersion = "2018-03-01"
336	queryParameters := map[string]interface{}{
337		"api-version": APIVersion,
338	}
339
340	preparer := autorest.CreatePreparer(
341		autorest.AsGet(),
342		autorest.WithBaseURL(client.BaseURI),
343		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers", pathParameters),
344		autorest.WithQueryParameters(queryParameters))
345	return preparer.Prepare((&http.Request{}).WithContext(ctx))
346}
347
348// ListSender sends the List request. The method will close the
349// http.Response Body if it receives an error.
350func (client LinkedServerClient) ListSender(req *http.Request) (*http.Response, error) {
351	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
352}
353
354// ListResponder handles the response to the List request. The method always
355// closes the http.Response Body.
356func (client LinkedServerClient) ListResponder(resp *http.Response) (result LinkedServerWithPropertiesList, err error) {
357	err = autorest.Respond(
358		resp,
359		client.ByInspecting(),
360		azure.WithErrorUnlessStatusCode(http.StatusOK),
361		autorest.ByUnmarshallingJSON(&result),
362		autorest.ByClosing())
363	result.Response = autorest.Response{Response: resp}
364	return
365}
366
367// listNextResults retrieves the next set of results, if any.
368func (client LinkedServerClient) listNextResults(ctx context.Context, lastResults LinkedServerWithPropertiesList) (result LinkedServerWithPropertiesList, err error) {
369	req, err := lastResults.linkedServerWithPropertiesListPreparer(ctx)
370	if err != nil {
371		return result, autorest.NewErrorWithError(err, "redis.LinkedServerClient", "listNextResults", nil, "Failure preparing next results request")
372	}
373	if req == nil {
374		return
375	}
376	resp, err := client.ListSender(req)
377	if err != nil {
378		result.Response = autorest.Response{Response: resp}
379		return result, autorest.NewErrorWithError(err, "redis.LinkedServerClient", "listNextResults", resp, "Failure sending next results request")
380	}
381	result, err = client.ListResponder(resp)
382	if err != nil {
383		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "listNextResults", resp, "Failure responding to next results request")
384	}
385	return
386}
387
388// ListComplete enumerates all values, automatically crossing page boundaries as required.
389func (client LinkedServerClient) ListComplete(ctx context.Context, resourceGroupName string, name string) (result LinkedServerWithPropertiesListIterator, err error) {
390	if tracing.IsEnabled() {
391		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.List")
392		defer func() {
393			sc := -1
394			if result.Response().Response.Response != nil {
395				sc = result.page.Response().Response.Response.StatusCode
396			}
397			tracing.EndSpan(ctx, sc, err)
398		}()
399	}
400	result.page, err = client.List(ctx, resourceGroupName, name)
401	return
402}
403