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.FutureAPI != nil && result.FutureAPI.Response() != nil {
57				sc = result.FutureAPI.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", nil, "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 = "2017-02-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	var azf azure.Future
119	azf, err = azure.NewFutureFromResponse(resp)
120	future.FutureAPI = &azf
121	future.Result = func(client LinkedServerClient) (lswp LinkedServerWithProperties, err error) {
122		var done bool
123		done, err = future.DoneWithContext(context.Background(), client)
124		if err != nil {
125			err = autorest.NewErrorWithError(err, "redis.LinkedServerCreateFuture", "Result", future.Response(), "Polling failure")
126			return
127		}
128		if !done {
129			err = azure.NewAsyncOpIncompleteError("redis.LinkedServerCreateFuture")
130			return
131		}
132		sender := autorest.DecorateSender(client, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
133		lswp.Response.Response, err = future.GetResult(sender)
134		if lswp.Response.Response == nil && err == nil {
135			err = autorest.NewErrorWithError(err, "redis.LinkedServerCreateFuture", "Result", nil, "received nil response and error")
136		}
137		if err == nil && lswp.Response.Response.StatusCode != http.StatusNoContent {
138			lswp, err = client.CreateResponder(lswp.Response.Response)
139			if err != nil {
140				err = autorest.NewErrorWithError(err, "redis.LinkedServerCreateFuture", "Result", lswp.Response.Response, "Failure responding to request")
141			}
142		}
143		return
144	}
145	return
146}
147
148// CreateResponder handles the response to the Create request. The method always
149// closes the http.Response Body.
150func (client LinkedServerClient) CreateResponder(resp *http.Response) (result LinkedServerWithProperties, err error) {
151	err = autorest.Respond(
152		resp,
153		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
154		autorest.ByUnmarshallingJSON(&result),
155		autorest.ByClosing())
156	result.Response = autorest.Response{Response: resp}
157	return
158}
159
160// Delete deletes the linked server from a redis cache (requires Premium SKU).
161// Parameters:
162// resourceGroupName - the name of the resource group.
163// name - the name of the redis cache.
164// linkedServerName - the name of the linked server that is being added to the Redis cache.
165func (client LinkedServerClient) Delete(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (result autorest.Response, err error) {
166	if tracing.IsEnabled() {
167		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.Delete")
168		defer func() {
169			sc := -1
170			if result.Response != nil {
171				sc = result.Response.StatusCode
172			}
173			tracing.EndSpan(ctx, sc, err)
174		}()
175	}
176	req, err := client.DeletePreparer(ctx, resourceGroupName, name, linkedServerName)
177	if err != nil {
178		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Delete", nil, "Failure preparing request")
179		return
180	}
181
182	resp, err := client.DeleteSender(req)
183	if err != nil {
184		result.Response = resp
185		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Delete", resp, "Failure sending request")
186		return
187	}
188
189	result, err = client.DeleteResponder(resp)
190	if err != nil {
191		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Delete", resp, "Failure responding to request")
192		return
193	}
194
195	return
196}
197
198// DeletePreparer prepares the Delete request.
199func (client LinkedServerClient) DeletePreparer(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (*http.Request, error) {
200	pathParameters := map[string]interface{}{
201		"linkedServerName":  autorest.Encode("path", linkedServerName),
202		"name":              autorest.Encode("path", name),
203		"resourceGroupName": autorest.Encode("path", resourceGroupName),
204		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
205	}
206
207	const APIVersion = "2017-02-01"
208	queryParameters := map[string]interface{}{
209		"api-version": APIVersion,
210	}
211
212	preparer := autorest.CreatePreparer(
213		autorest.AsDelete(),
214		autorest.WithBaseURL(client.BaseURI),
215		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers/{linkedServerName}", pathParameters),
216		autorest.WithQueryParameters(queryParameters))
217	return preparer.Prepare((&http.Request{}).WithContext(ctx))
218}
219
220// DeleteSender sends the Delete request. The method will close the
221// http.Response Body if it receives an error.
222func (client LinkedServerClient) DeleteSender(req *http.Request) (*http.Response, error) {
223	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
224}
225
226// DeleteResponder handles the response to the Delete request. The method always
227// closes the http.Response Body.
228func (client LinkedServerClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
229	err = autorest.Respond(
230		resp,
231		azure.WithErrorUnlessStatusCode(http.StatusOK),
232		autorest.ByClosing())
233	result.Response = resp
234	return
235}
236
237// Get gets the detailed information about a linked server of a redis cache (requires Premium SKU).
238// Parameters:
239// resourceGroupName - the name of the resource group.
240// name - the name of the redis cache.
241// linkedServerName - the name of the linked server.
242func (client LinkedServerClient) Get(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (result LinkedServerWithProperties, err error) {
243	if tracing.IsEnabled() {
244		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.Get")
245		defer func() {
246			sc := -1
247			if result.Response.Response != nil {
248				sc = result.Response.Response.StatusCode
249			}
250			tracing.EndSpan(ctx, sc, err)
251		}()
252	}
253	req, err := client.GetPreparer(ctx, resourceGroupName, name, linkedServerName)
254	if err != nil {
255		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Get", nil, "Failure preparing request")
256		return
257	}
258
259	resp, err := client.GetSender(req)
260	if err != nil {
261		result.Response = autorest.Response{Response: resp}
262		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Get", resp, "Failure sending request")
263		return
264	}
265
266	result, err = client.GetResponder(resp)
267	if err != nil {
268		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "Get", resp, "Failure responding to request")
269		return
270	}
271
272	return
273}
274
275// GetPreparer prepares the Get request.
276func (client LinkedServerClient) GetPreparer(ctx context.Context, resourceGroupName string, name string, linkedServerName string) (*http.Request, error) {
277	pathParameters := map[string]interface{}{
278		"linkedServerName":  autorest.Encode("path", linkedServerName),
279		"name":              autorest.Encode("path", name),
280		"resourceGroupName": autorest.Encode("path", resourceGroupName),
281		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
282	}
283
284	const APIVersion = "2017-02-01"
285	queryParameters := map[string]interface{}{
286		"api-version": APIVersion,
287	}
288
289	preparer := autorest.CreatePreparer(
290		autorest.AsGet(),
291		autorest.WithBaseURL(client.BaseURI),
292		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers/{linkedServerName}", pathParameters),
293		autorest.WithQueryParameters(queryParameters))
294	return preparer.Prepare((&http.Request{}).WithContext(ctx))
295}
296
297// GetSender sends the Get request. The method will close the
298// http.Response Body if it receives an error.
299func (client LinkedServerClient) GetSender(req *http.Request) (*http.Response, error) {
300	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
301}
302
303// GetResponder handles the response to the Get request. The method always
304// closes the http.Response Body.
305func (client LinkedServerClient) GetResponder(resp *http.Response) (result LinkedServerWithProperties, err error) {
306	err = autorest.Respond(
307		resp,
308		azure.WithErrorUnlessStatusCode(http.StatusOK),
309		autorest.ByUnmarshallingJSON(&result),
310		autorest.ByClosing())
311	result.Response = autorest.Response{Response: resp}
312	return
313}
314
315// List gets the list of linked servers associated with this redis cache (requires Premium SKU).
316// Parameters:
317// resourceGroupName - the name of the resource group.
318// name - the name of the redis cache.
319func (client LinkedServerClient) List(ctx context.Context, resourceGroupName string, name string) (result LinkedServerWithPropertiesList, err error) {
320	if tracing.IsEnabled() {
321		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServerClient.List")
322		defer func() {
323			sc := -1
324			if result.Response.Response != nil {
325				sc = result.Response.Response.StatusCode
326			}
327			tracing.EndSpan(ctx, sc, err)
328		}()
329	}
330	req, err := client.ListPreparer(ctx, resourceGroupName, name)
331	if err != nil {
332		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "List", nil, "Failure preparing request")
333		return
334	}
335
336	resp, err := client.ListSender(req)
337	if err != nil {
338		result.Response = autorest.Response{Response: resp}
339		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "List", resp, "Failure sending request")
340		return
341	}
342
343	result, err = client.ListResponder(resp)
344	if err != nil {
345		err = autorest.NewErrorWithError(err, "redis.LinkedServerClient", "List", resp, "Failure responding to request")
346		return
347	}
348
349	return
350}
351
352// ListPreparer prepares the List request.
353func (client LinkedServerClient) ListPreparer(ctx context.Context, resourceGroupName string, name string) (*http.Request, error) {
354	pathParameters := map[string]interface{}{
355		"name":              autorest.Encode("path", name),
356		"resourceGroupName": autorest.Encode("path", resourceGroupName),
357		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
358	}
359
360	const APIVersion = "2017-02-01"
361	queryParameters := map[string]interface{}{
362		"api-version": APIVersion,
363	}
364
365	preparer := autorest.CreatePreparer(
366		autorest.AsGet(),
367		autorest.WithBaseURL(client.BaseURI),
368		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}/linkedServers", pathParameters),
369		autorest.WithQueryParameters(queryParameters))
370	return preparer.Prepare((&http.Request{}).WithContext(ctx))
371}
372
373// ListSender sends the List request. The method will close the
374// http.Response Body if it receives an error.
375func (client LinkedServerClient) ListSender(req *http.Request) (*http.Response, error) {
376	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
377}
378
379// ListResponder handles the response to the List request. The method always
380// closes the http.Response Body.
381func (client LinkedServerClient) ListResponder(resp *http.Response) (result LinkedServerWithPropertiesList, err error) {
382	err = autorest.Respond(
383		resp,
384		azure.WithErrorUnlessStatusCode(http.StatusOK),
385		autorest.ByUnmarshallingJSON(&result),
386		autorest.ByClosing())
387	result.Response = autorest.Response{Response: resp}
388	return
389}
390