1package mysqlflexibleservers
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/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// ReplicasClient is the the Microsoft Azure management API provides create, read, update, and delete functionality for
19// Azure MySQL resources including servers, databases, firewall rules, VNET rules, log files and configurations with
20// new business model.
21type ReplicasClient struct {
22	BaseClient
23}
24
25// NewReplicasClient creates an instance of the ReplicasClient client.
26func NewReplicasClient(subscriptionID string) ReplicasClient {
27	return NewReplicasClientWithBaseURI(DefaultBaseURI, subscriptionID)
28}
29
30// NewReplicasClientWithBaseURI creates an instance of the ReplicasClient client using a custom endpoint.  Use this
31// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
32func NewReplicasClientWithBaseURI(baseURI string, subscriptionID string) ReplicasClient {
33	return ReplicasClient{NewWithBaseURI(baseURI, subscriptionID)}
34}
35
36// ListByServer list all the replicas for a given server.
37// Parameters:
38// resourceGroupName - the name of the resource group. The name is case insensitive.
39// serverName - the name of the server.
40func (client ReplicasClient) ListByServer(ctx context.Context, resourceGroupName string, serverName string) (result ServerListResultPage, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicasClient.ListByServer")
43		defer func() {
44			sc := -1
45			if result.slr.Response.Response != nil {
46				sc = result.slr.Response.Response.StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: client.SubscriptionID,
53			Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.MinLength, Rule: 1, Chain: nil}}},
54		{TargetValue: resourceGroupName,
55			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
56				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
57		return result, validation.NewError("mysqlflexibleservers.ReplicasClient", "ListByServer", err.Error())
58	}
59
60	result.fn = client.listByServerNextResults
61	req, err := client.ListByServerPreparer(ctx, resourceGroupName, serverName)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "mysqlflexibleservers.ReplicasClient", "ListByServer", nil, "Failure preparing request")
64		return
65	}
66
67	resp, err := client.ListByServerSender(req)
68	if err != nil {
69		result.slr.Response = autorest.Response{Response: resp}
70		err = autorest.NewErrorWithError(err, "mysqlflexibleservers.ReplicasClient", "ListByServer", resp, "Failure sending request")
71		return
72	}
73
74	result.slr, err = client.ListByServerResponder(resp)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "mysqlflexibleservers.ReplicasClient", "ListByServer", resp, "Failure responding to request")
77		return
78	}
79	if result.slr.hasNextLink() && result.slr.IsEmpty() {
80		err = result.NextWithContext(ctx)
81		return
82	}
83
84	return
85}
86
87// ListByServerPreparer prepares the ListByServer request.
88func (client ReplicasClient) ListByServerPreparer(ctx context.Context, resourceGroupName string, serverName string) (*http.Request, error) {
89	pathParameters := map[string]interface{}{
90		"resourceGroupName": autorest.Encode("path", resourceGroupName),
91		"serverName":        autorest.Encode("path", serverName),
92		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
93	}
94
95	const APIVersion = "2021-05-01-preview"
96	queryParameters := map[string]interface{}{
97		"api-version": APIVersion,
98	}
99
100	preparer := autorest.CreatePreparer(
101		autorest.AsGet(),
102		autorest.WithBaseURL(client.BaseURI),
103		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMySQL/flexibleServers/{serverName}/replicas", pathParameters),
104		autorest.WithQueryParameters(queryParameters))
105	return preparer.Prepare((&http.Request{}).WithContext(ctx))
106}
107
108// ListByServerSender sends the ListByServer request. The method will close the
109// http.Response Body if it receives an error.
110func (client ReplicasClient) ListByServerSender(req *http.Request) (*http.Response, error) {
111	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
112}
113
114// ListByServerResponder handles the response to the ListByServer request. The method always
115// closes the http.Response Body.
116func (client ReplicasClient) ListByServerResponder(resp *http.Response) (result ServerListResult, err error) {
117	err = autorest.Respond(
118		resp,
119		azure.WithErrorUnlessStatusCode(http.StatusOK),
120		autorest.ByUnmarshallingJSON(&result),
121		autorest.ByClosing())
122	result.Response = autorest.Response{Response: resp}
123	return
124}
125
126// listByServerNextResults retrieves the next set of results, if any.
127func (client ReplicasClient) listByServerNextResults(ctx context.Context, lastResults ServerListResult) (result ServerListResult, err error) {
128	req, err := lastResults.serverListResultPreparer(ctx)
129	if err != nil {
130		return result, autorest.NewErrorWithError(err, "mysqlflexibleservers.ReplicasClient", "listByServerNextResults", nil, "Failure preparing next results request")
131	}
132	if req == nil {
133		return
134	}
135	resp, err := client.ListByServerSender(req)
136	if err != nil {
137		result.Response = autorest.Response{Response: resp}
138		return result, autorest.NewErrorWithError(err, "mysqlflexibleservers.ReplicasClient", "listByServerNextResults", resp, "Failure sending next results request")
139	}
140	result, err = client.ListByServerResponder(resp)
141	if err != nil {
142		err = autorest.NewErrorWithError(err, "mysqlflexibleservers.ReplicasClient", "listByServerNextResults", resp, "Failure responding to next results request")
143	}
144	return
145}
146
147// ListByServerComplete enumerates all values, automatically crossing page boundaries as required.
148func (client ReplicasClient) ListByServerComplete(ctx context.Context, resourceGroupName string, serverName string) (result ServerListResultIterator, err error) {
149	if tracing.IsEnabled() {
150		ctx = tracing.StartSpan(ctx, fqdn+"/ReplicasClient.ListByServer")
151		defer func() {
152			sc := -1
153			if result.Response().Response.Response != nil {
154				sc = result.page.Response().Response.Response.StatusCode
155			}
156			tracing.EndSpan(ctx, sc, err)
157		}()
158	}
159	result.page, err = client.ListByServer(ctx, resourceGroupName, serverName)
160	return
161}
162