1package sql
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/tracing"
25	"net/http"
26)
27
28// ReplicationLinksClient is the the Azure SQL Database management API provides a RESTful set of web services that
29// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
30// and delete databases.
31type ReplicationLinksClient struct {
32	BaseClient
33}
34
35// NewReplicationLinksClient creates an instance of the ReplicationLinksClient client.
36func NewReplicationLinksClient(subscriptionID string) ReplicationLinksClient {
37	return NewReplicationLinksClientWithBaseURI(DefaultBaseURI, subscriptionID)
38}
39
40// NewReplicationLinksClientWithBaseURI creates an instance of the ReplicationLinksClient client using a custom
41// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
42// stack).
43func NewReplicationLinksClientWithBaseURI(baseURI string, subscriptionID string) ReplicationLinksClient {
44	return ReplicationLinksClient{NewWithBaseURI(baseURI, subscriptionID)}
45}
46
47// Delete deletes a database replication link. Cannot be done during failover.
48// Parameters:
49// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
50// from the Azure Resource Manager API or the portal.
51// serverName - the name of the server.
52// databaseName - the name of the database that has the replication link to be dropped.
53// linkID - the ID of the replication link to be deleted.
54func (client ReplicationLinksClient) Delete(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (result autorest.Response, err error) {
55	if tracing.IsEnabled() {
56		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicationLinksClient.Delete")
57		defer func() {
58			sc := -1
59			if result.Response != nil {
60				sc = result.Response.StatusCode
61			}
62			tracing.EndSpan(ctx, sc, err)
63		}()
64	}
65	req, err := client.DeletePreparer(ctx, resourceGroupName, serverName, databaseName, linkID)
66	if err != nil {
67		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Delete", nil, "Failure preparing request")
68		return
69	}
70
71	resp, err := client.DeleteSender(req)
72	if err != nil {
73		result.Response = resp
74		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Delete", resp, "Failure sending request")
75		return
76	}
77
78	result, err = client.DeleteResponder(resp)
79	if err != nil {
80		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Delete", resp, "Failure responding to request")
81	}
82
83	return
84}
85
86// DeletePreparer prepares the Delete request.
87func (client ReplicationLinksClient) DeletePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (*http.Request, error) {
88	pathParameters := map[string]interface{}{
89		"databaseName":      autorest.Encode("path", databaseName),
90		"linkId":            autorest.Encode("path", linkID),
91		"resourceGroupName": autorest.Encode("path", resourceGroupName),
92		"serverName":        autorest.Encode("path", serverName),
93		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
94	}
95
96	const APIVersion = "2014-04-01"
97	queryParameters := map[string]interface{}{
98		"api-version": APIVersion,
99	}
100
101	preparer := autorest.CreatePreparer(
102		autorest.AsDelete(),
103		autorest.WithBaseURL(client.BaseURI),
104		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/replicationLinks/{linkId}", pathParameters),
105		autorest.WithQueryParameters(queryParameters))
106	return preparer.Prepare((&http.Request{}).WithContext(ctx))
107}
108
109// DeleteSender sends the Delete request. The method will close the
110// http.Response Body if it receives an error.
111func (client ReplicationLinksClient) DeleteSender(req *http.Request) (*http.Response, error) {
112	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
113}
114
115// DeleteResponder handles the response to the Delete request. The method always
116// closes the http.Response Body.
117func (client ReplicationLinksClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
118	err = autorest.Respond(
119		resp,
120		client.ByInspecting(),
121		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
122		autorest.ByClosing())
123	result.Response = resp
124	return
125}
126
127// Failover sets which replica database is primary by failing over from the current primary replica database.
128// Parameters:
129// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
130// from the Azure Resource Manager API or the portal.
131// serverName - the name of the server.
132// databaseName - the name of the database that has the replication link to be failed over.
133// linkID - the ID of the replication link to be failed over.
134func (client ReplicationLinksClient) Failover(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (result ReplicationLinksFailoverFuture, err error) {
135	if tracing.IsEnabled() {
136		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicationLinksClient.Failover")
137		defer func() {
138			sc := -1
139			if result.Response() != nil {
140				sc = result.Response().StatusCode
141			}
142			tracing.EndSpan(ctx, sc, err)
143		}()
144	}
145	req, err := client.FailoverPreparer(ctx, resourceGroupName, serverName, databaseName, linkID)
146	if err != nil {
147		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Failover", nil, "Failure preparing request")
148		return
149	}
150
151	result, err = client.FailoverSender(req)
152	if err != nil {
153		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Failover", result.Response(), "Failure sending request")
154		return
155	}
156
157	return
158}
159
160// FailoverPreparer prepares the Failover request.
161func (client ReplicationLinksClient) FailoverPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (*http.Request, error) {
162	pathParameters := map[string]interface{}{
163		"databaseName":      autorest.Encode("path", databaseName),
164		"linkId":            autorest.Encode("path", linkID),
165		"resourceGroupName": autorest.Encode("path", resourceGroupName),
166		"serverName":        autorest.Encode("path", serverName),
167		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
168	}
169
170	const APIVersion = "2014-04-01"
171	queryParameters := map[string]interface{}{
172		"api-version": APIVersion,
173	}
174
175	preparer := autorest.CreatePreparer(
176		autorest.AsPost(),
177		autorest.WithBaseURL(client.BaseURI),
178		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/replicationLinks/{linkId}/failover", pathParameters),
179		autorest.WithQueryParameters(queryParameters))
180	return preparer.Prepare((&http.Request{}).WithContext(ctx))
181}
182
183// FailoverSender sends the Failover request. The method will close the
184// http.Response Body if it receives an error.
185func (client ReplicationLinksClient) FailoverSender(req *http.Request) (future ReplicationLinksFailoverFuture, err error) {
186	var resp *http.Response
187	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
188	if err != nil {
189		return
190	}
191	future.Future, err = azure.NewFutureFromResponse(resp)
192	return
193}
194
195// FailoverResponder handles the response to the Failover request. The method always
196// closes the http.Response Body.
197func (client ReplicationLinksClient) FailoverResponder(resp *http.Response) (result autorest.Response, err error) {
198	err = autorest.Respond(
199		resp,
200		client.ByInspecting(),
201		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
202		autorest.ByClosing())
203	result.Response = resp
204	return
205}
206
207// FailoverAllowDataLoss sets which replica database is primary by failing over from the current primary replica
208// database. This operation might result in data loss.
209// Parameters:
210// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
211// from the Azure Resource Manager API or the portal.
212// serverName - the name of the server.
213// databaseName - the name of the database that has the replication link to be failed over.
214// linkID - the ID of the replication link to be failed over.
215func (client ReplicationLinksClient) FailoverAllowDataLoss(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (result ReplicationLinksFailoverAllowDataLossFuture, err error) {
216	if tracing.IsEnabled() {
217		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicationLinksClient.FailoverAllowDataLoss")
218		defer func() {
219			sc := -1
220			if result.Response() != nil {
221				sc = result.Response().StatusCode
222			}
223			tracing.EndSpan(ctx, sc, err)
224		}()
225	}
226	req, err := client.FailoverAllowDataLossPreparer(ctx, resourceGroupName, serverName, databaseName, linkID)
227	if err != nil {
228		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "FailoverAllowDataLoss", nil, "Failure preparing request")
229		return
230	}
231
232	result, err = client.FailoverAllowDataLossSender(req)
233	if err != nil {
234		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "FailoverAllowDataLoss", result.Response(), "Failure sending request")
235		return
236	}
237
238	return
239}
240
241// FailoverAllowDataLossPreparer prepares the FailoverAllowDataLoss request.
242func (client ReplicationLinksClient) FailoverAllowDataLossPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (*http.Request, error) {
243	pathParameters := map[string]interface{}{
244		"databaseName":      autorest.Encode("path", databaseName),
245		"linkId":            autorest.Encode("path", linkID),
246		"resourceGroupName": autorest.Encode("path", resourceGroupName),
247		"serverName":        autorest.Encode("path", serverName),
248		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
249	}
250
251	const APIVersion = "2014-04-01"
252	queryParameters := map[string]interface{}{
253		"api-version": APIVersion,
254	}
255
256	preparer := autorest.CreatePreparer(
257		autorest.AsPost(),
258		autorest.WithBaseURL(client.BaseURI),
259		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/replicationLinks/{linkId}/forceFailoverAllowDataLoss", pathParameters),
260		autorest.WithQueryParameters(queryParameters))
261	return preparer.Prepare((&http.Request{}).WithContext(ctx))
262}
263
264// FailoverAllowDataLossSender sends the FailoverAllowDataLoss request. The method will close the
265// http.Response Body if it receives an error.
266func (client ReplicationLinksClient) FailoverAllowDataLossSender(req *http.Request) (future ReplicationLinksFailoverAllowDataLossFuture, err error) {
267	var resp *http.Response
268	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
269	if err != nil {
270		return
271	}
272	future.Future, err = azure.NewFutureFromResponse(resp)
273	return
274}
275
276// FailoverAllowDataLossResponder handles the response to the FailoverAllowDataLoss request. The method always
277// closes the http.Response Body.
278func (client ReplicationLinksClient) FailoverAllowDataLossResponder(resp *http.Response) (result autorest.Response, err error) {
279	err = autorest.Respond(
280		resp,
281		client.ByInspecting(),
282		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
283		autorest.ByClosing())
284	result.Response = resp
285	return
286}
287
288// Get gets a database replication link.
289// Parameters:
290// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
291// from the Azure Resource Manager API or the portal.
292// serverName - the name of the server.
293// databaseName - the name of the database to get the link for.
294// linkID - the replication link ID to be retrieved.
295func (client ReplicationLinksClient) Get(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (result ReplicationLink, err error) {
296	if tracing.IsEnabled() {
297		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicationLinksClient.Get")
298		defer func() {
299			sc := -1
300			if result.Response.Response != nil {
301				sc = result.Response.Response.StatusCode
302			}
303			tracing.EndSpan(ctx, sc, err)
304		}()
305	}
306	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, databaseName, linkID)
307	if err != nil {
308		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Get", nil, "Failure preparing request")
309		return
310	}
311
312	resp, err := client.GetSender(req)
313	if err != nil {
314		result.Response = autorest.Response{Response: resp}
315		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Get", resp, "Failure sending request")
316		return
317	}
318
319	result, err = client.GetResponder(resp)
320	if err != nil {
321		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Get", resp, "Failure responding to request")
322	}
323
324	return
325}
326
327// GetPreparer prepares the Get request.
328func (client ReplicationLinksClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string) (*http.Request, error) {
329	pathParameters := map[string]interface{}{
330		"databaseName":      autorest.Encode("path", databaseName),
331		"linkId":            autorest.Encode("path", linkID),
332		"resourceGroupName": autorest.Encode("path", resourceGroupName),
333		"serverName":        autorest.Encode("path", serverName),
334		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
335	}
336
337	const APIVersion = "2014-04-01"
338	queryParameters := map[string]interface{}{
339		"api-version": APIVersion,
340	}
341
342	preparer := autorest.CreatePreparer(
343		autorest.AsGet(),
344		autorest.WithBaseURL(client.BaseURI),
345		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/replicationLinks/{linkId}", pathParameters),
346		autorest.WithQueryParameters(queryParameters))
347	return preparer.Prepare((&http.Request{}).WithContext(ctx))
348}
349
350// GetSender sends the Get request. The method will close the
351// http.Response Body if it receives an error.
352func (client ReplicationLinksClient) GetSender(req *http.Request) (*http.Response, error) {
353	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
354}
355
356// GetResponder handles the response to the Get request. The method always
357// closes the http.Response Body.
358func (client ReplicationLinksClient) GetResponder(resp *http.Response) (result ReplicationLink, err error) {
359	err = autorest.Respond(
360		resp,
361		client.ByInspecting(),
362		azure.WithErrorUnlessStatusCode(http.StatusOK),
363		autorest.ByUnmarshallingJSON(&result),
364		autorest.ByClosing())
365	result.Response = autorest.Response{Response: resp}
366	return
367}
368
369// ListByDatabase lists a database's replication links.
370// Parameters:
371// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
372// from the Azure Resource Manager API or the portal.
373// serverName - the name of the server.
374// databaseName - the name of the database to retrieve links for.
375func (client ReplicationLinksClient) ListByDatabase(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (result ReplicationLinkListResult, err error) {
376	if tracing.IsEnabled() {
377		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicationLinksClient.ListByDatabase")
378		defer func() {
379			sc := -1
380			if result.Response.Response != nil {
381				sc = result.Response.Response.StatusCode
382			}
383			tracing.EndSpan(ctx, sc, err)
384		}()
385	}
386	req, err := client.ListByDatabasePreparer(ctx, resourceGroupName, serverName, databaseName)
387	if err != nil {
388		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "ListByDatabase", nil, "Failure preparing request")
389		return
390	}
391
392	resp, err := client.ListByDatabaseSender(req)
393	if err != nil {
394		result.Response = autorest.Response{Response: resp}
395		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "ListByDatabase", resp, "Failure sending request")
396		return
397	}
398
399	result, err = client.ListByDatabaseResponder(resp)
400	if err != nil {
401		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "ListByDatabase", resp, "Failure responding to request")
402	}
403
404	return
405}
406
407// ListByDatabasePreparer prepares the ListByDatabase request.
408func (client ReplicationLinksClient) ListByDatabasePreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string) (*http.Request, error) {
409	pathParameters := map[string]interface{}{
410		"databaseName":      autorest.Encode("path", databaseName),
411		"resourceGroupName": autorest.Encode("path", resourceGroupName),
412		"serverName":        autorest.Encode("path", serverName),
413		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
414	}
415
416	const APIVersion = "2014-04-01"
417	queryParameters := map[string]interface{}{
418		"api-version": APIVersion,
419	}
420
421	preparer := autorest.CreatePreparer(
422		autorest.AsGet(),
423		autorest.WithBaseURL(client.BaseURI),
424		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/replicationLinks", pathParameters),
425		autorest.WithQueryParameters(queryParameters))
426	return preparer.Prepare((&http.Request{}).WithContext(ctx))
427}
428
429// ListByDatabaseSender sends the ListByDatabase request. The method will close the
430// http.Response Body if it receives an error.
431func (client ReplicationLinksClient) ListByDatabaseSender(req *http.Request) (*http.Response, error) {
432	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
433}
434
435// ListByDatabaseResponder handles the response to the ListByDatabase request. The method always
436// closes the http.Response Body.
437func (client ReplicationLinksClient) ListByDatabaseResponder(resp *http.Response) (result ReplicationLinkListResult, err error) {
438	err = autorest.Respond(
439		resp,
440		client.ByInspecting(),
441		azure.WithErrorUnlessStatusCode(http.StatusOK),
442		autorest.ByUnmarshallingJSON(&result),
443		autorest.ByClosing())
444	result.Response = autorest.Response{Response: resp}
445	return
446}
447
448// Unlink deletes a database replication link in forced or friendly way.
449// Parameters:
450// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
451// from the Azure Resource Manager API or the portal.
452// serverName - the name of the server.
453// databaseName - the name of the database that has the replication link to be failed over.
454// linkID - the ID of the replication link to be failed over.
455// parameters - the required parameters for unlinking replication link.
456func (client ReplicationLinksClient) Unlink(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string, parameters UnlinkParameters) (result ReplicationLinksUnlinkFuture, err error) {
457	if tracing.IsEnabled() {
458		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicationLinksClient.Unlink")
459		defer func() {
460			sc := -1
461			if result.Response() != nil {
462				sc = result.Response().StatusCode
463			}
464			tracing.EndSpan(ctx, sc, err)
465		}()
466	}
467	req, err := client.UnlinkPreparer(ctx, resourceGroupName, serverName, databaseName, linkID, parameters)
468	if err != nil {
469		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Unlink", nil, "Failure preparing request")
470		return
471	}
472
473	result, err = client.UnlinkSender(req)
474	if err != nil {
475		err = autorest.NewErrorWithError(err, "sql.ReplicationLinksClient", "Unlink", result.Response(), "Failure sending request")
476		return
477	}
478
479	return
480}
481
482// UnlinkPreparer prepares the Unlink request.
483func (client ReplicationLinksClient) UnlinkPreparer(ctx context.Context, resourceGroupName string, serverName string, databaseName string, linkID string, parameters UnlinkParameters) (*http.Request, error) {
484	pathParameters := map[string]interface{}{
485		"databaseName":      autorest.Encode("path", databaseName),
486		"linkId":            autorest.Encode("path", linkID),
487		"resourceGroupName": autorest.Encode("path", resourceGroupName),
488		"serverName":        autorest.Encode("path", serverName),
489		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
490	}
491
492	const APIVersion = "2014-04-01"
493	queryParameters := map[string]interface{}{
494		"api-version": APIVersion,
495	}
496
497	preparer := autorest.CreatePreparer(
498		autorest.AsContentType("application/json; charset=utf-8"),
499		autorest.AsPost(),
500		autorest.WithBaseURL(client.BaseURI),
501		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/replicationLinks/{linkId}/unlink", pathParameters),
502		autorest.WithJSON(parameters),
503		autorest.WithQueryParameters(queryParameters))
504	return preparer.Prepare((&http.Request{}).WithContext(ctx))
505}
506
507// UnlinkSender sends the Unlink request. The method will close the
508// http.Response Body if it receives an error.
509func (client ReplicationLinksClient) UnlinkSender(req *http.Request) (future ReplicationLinksUnlinkFuture, err error) {
510	var resp *http.Response
511	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
512	if err != nil {
513		return
514	}
515	future.Future, err = azure.NewFutureFromResponse(resp)
516	return
517}
518
519// UnlinkResponder handles the response to the Unlink request. The method always
520// closes the http.Response Body.
521func (client ReplicationLinksClient) UnlinkResponder(resp *http.Response) (result autorest.Response, err error) {
522	err = autorest.Respond(
523		resp,
524		client.ByInspecting(),
525		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
526		autorest.ByClosing())
527	result.Response = resp
528	return
529}
530