forked from crossplane/crossplane
/
mysql.go
236 lines (195 loc) · 8.79 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyright 2018 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azure
import (
"context"
"fmt"
"strconv"
"github.com/Azure/azure-sdk-for-go/services/mysql/mgmt/2017-12-01/mysql"
databasev1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/azure/database/v1alpha1"
"github.com/crossplaneio/crossplane/pkg/apis/azure/v1alpha1"
corev1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/core/v1alpha1"
"k8s.io/client-go/kubernetes"
)
var (
skuShortTiers = map[mysql.SkuTier]string{
mysql.Basic: "B",
mysql.GeneralPurpose: "GP",
mysql.MemoryOptimized: "MO",
}
)
// MySQLServerAPI represents the API interface for a MySQL Server client
type MySQLServerAPI interface {
GetServer(ctx context.Context, resourceGroupName string, serverName string) (mysql.Server, error)
CreateServerBegin(ctx context.Context, resourceGroupName string, serverName string, parameters mysql.ServerForCreate) ([]byte, error)
CreateServerEnd(createOp []byte) (bool, error)
DeleteServer(ctx context.Context, resourceGroupName string, serverName string) (mysql.ServersDeleteFuture, error)
GetFirewallRule(ctx context.Context, resourceGroupName string, serverName string, firewallRuleName string) (result mysql.FirewallRule, err error)
CreateFirewallRulesBegin(ctx context.Context, resourceGroupName string, serverName string, firewallRuleName string, parameters mysql.FirewallRule) ([]byte, error)
CreateFirewallRulesEnd(createOp []byte) (bool, error)
}
// MySQLServerClient is the concreate implementation of the MySQLServerAPI interface that calls Azure API.
type MySQLServerClient struct {
mysql.ServersClient
mysql.FirewallRulesClient
}
// NewMySQLServerClient creates and initializes a MySQLServerClient instance.
func NewMySQLServerClient(provider *v1alpha1.Provider, clientset kubernetes.Interface) (*MySQLServerClient, error) {
client, err := NewClient(provider, clientset)
if err != nil {
return nil, fmt.Errorf("failed to create Azure client: %+v", err)
}
mysqlServersClient := mysql.NewServersClient(client.SubscriptionID)
mysqlServersClient.Authorizer = client.Authorizer
mysqlServersClient.AddToUserAgent(UserAgent)
firewallRulesClient := mysql.NewFirewallRulesClient(client.SubscriptionID)
firewallRulesClient.Authorizer = client.Authorizer
firewallRulesClient.AddToUserAgent(UserAgent)
return &MySQLServerClient{
ServersClient: mysqlServersClient,
FirewallRulesClient: firewallRulesClient,
}, nil
}
// GetServer returns the given MySQL Server
func (c *MySQLServerClient) GetServer(ctx context.Context, resourceGroupName string, serverName string) (mysql.Server, error) {
return c.ServersClient.Get(ctx, resourceGroupName, serverName)
}
// CreateServerBegin begins the create operation for a MySQL Server with the given properties
func (c *MySQLServerClient) CreateServerBegin(ctx context.Context, resourceGroupName string, serverName string,
parameters mysql.ServerForCreate) ([]byte, error) {
// make the call to the MySQL Server Create API
createFuture, err := c.Create(ctx, resourceGroupName, serverName, parameters)
if err != nil {
return nil, err
}
// serialize the create operation
createFutureJSON, err := createFuture.MarshalJSON()
if err != nil {
return nil, err
}
return createFutureJSON, nil
}
// CreateServerEnd checks to see if the given create operation is completed and if any error has occurred.
func (c *MySQLServerClient) CreateServerEnd(createOp []byte) (done bool, err error) {
// unmarshal the given create complete data into a future object
createFuture := &mysql.ServersCreateFuture{}
if err = createFuture.UnmarshalJSON(createOp); err != nil {
return false, err
}
// check if the operation is done yet
done, err = createFuture.Done(c.ServersClient.Client)
if !done {
return false, err
}
// check the result of the completed operation
if _, err = createFuture.Result(c.ServersClient); err != nil {
return true, err
}
return true, nil
}
// DeleteServer deletes the given MySQL Server
func (c *MySQLServerClient) DeleteServer(ctx context.Context, resourceGroupName string, serverName string) (mysql.ServersDeleteFuture, error) {
return c.ServersClient.Delete(ctx, resourceGroupName, serverName)
}
// GetFirewallRule gets the given firewall rule
func (c *MySQLServerClient) GetFirewallRule(ctx context.Context, resourceGroupName string, serverName string, firewallRuleName string) (result mysql.FirewallRule, err error) {
return c.FirewallRulesClient.Get(ctx, resourceGroupName, serverName, firewallRuleName)
}
// CreateFirewallRulesBegin begins the create operation for a firewall rule
func (c *MySQLServerClient) CreateFirewallRulesBegin(ctx context.Context, resourceGroupName string, serverName string,
firewallRuleName string, parameters mysql.FirewallRule) ([]byte, error) {
// make the call to the MySQL Server Create API
createFuture, err := c.FirewallRulesClient.CreateOrUpdate(ctx, resourceGroupName, serverName, firewallRuleName, parameters)
if err != nil {
return nil, err
}
// serialize the create operation
createFutureJSON, err := createFuture.MarshalJSON()
if err != nil {
return nil, err
}
return createFutureJSON, nil
}
// CreateFirewallRulesEnd checks to see if the given create operation is completed and if any error has occurred.
func (c *MySQLServerClient) CreateFirewallRulesEnd(createOp []byte) (done bool, err error) {
// unmarshal the given create complete data into a future object
createFuture := &mysql.FirewallRulesCreateOrUpdateFuture{}
if err = createFuture.UnmarshalJSON(createOp); err != nil {
return false, err
}
// check if the operation is done yet
done, err = createFuture.Done(c.FirewallRulesClient.Client)
if !done {
return false, err
}
// check the result of the completed operation
if _, err = createFuture.Result(c.FirewallRulesClient); err != nil {
return true, err
}
return true, nil
}
// MySQLServerAPIFactory is an interface that can create instances of the MySQLServerAPI interface
type MySQLServerAPIFactory interface {
CreateAPIInstance(*v1alpha1.Provider, kubernetes.Interface) (MySQLServerAPI, error)
}
// MySQLServerClientFactory implements the MySQLServerAPIFactory by returning the concrete MySQLServerClient implementation
type MySQLServerClientFactory struct {
}
// CreateAPIInstance returns a concrete MySQLServerClient implementation
func (f *MySQLServerClientFactory) CreateAPIInstance(provider *v1alpha1.Provider, clientset kubernetes.Interface) (MySQLServerAPI, error) {
return NewMySQLServerClient(provider, clientset)
}
// MySQLServerConditionType converts the given MySQL Server state string into a corresponding condition type
func MySQLServerConditionType(state mysql.ServerState) corev1alpha1.ConditionType {
switch state {
case mysql.ServerStateReady:
return corev1alpha1.Ready
default:
return corev1alpha1.Failed
}
}
// MySQLServerStatusMessage returns a status message based on the given server state
func MySQLServerStatusMessage(instanceName string, state mysql.ServerState) string {
switch state {
case mysql.ServerStateDisabled:
return fmt.Sprintf("MySQL Server instance %s is disabled", instanceName)
case mysql.ServerStateDropping:
return fmt.Sprintf("MySQL Server instance %s is dropping", instanceName)
case mysql.ServerStateReady:
return fmt.Sprintf("MySQL Server instance %s is ready", instanceName)
default:
return fmt.Sprintf("MySQL Server instance %s is in an unknown state %s", instanceName, string(state))
}
}
// MySQLServerSkuName returns the name of the MySQL Server SKU, which is tier + family + cores, e.g. B_Gen4_1, GP_Gen5_8.
func MySQLServerSkuName(pricingTier databasev1alpha1.PricingTierSpec) (string, error) {
t, ok := skuShortTiers[mysql.SkuTier(pricingTier.Tier)]
if !ok {
return "", fmt.Errorf("tier '%s' is not one of the supported values: %+v", pricingTier.Tier, mysql.PossibleSkuTierValues())
}
return fmt.Sprintf("%s_%s_%s", t, pricingTier.Family, strconv.Itoa(pricingTier.VCores)), nil
}
// ToSslEnforcement converts the given bool its corresponding SslEnforcementEnum value
func ToSslEnforcement(sslEnforced bool) mysql.SslEnforcementEnum {
if sslEnforced {
return mysql.SslEnforcementEnumEnabled
}
return mysql.SslEnforcementEnumDisabled
}
// ToGeoRedundantBackup converts the given bool its corresponding GeoRedundantBackup value
func ToGeoRedundantBackup(geoRedundantBackup bool) mysql.GeoRedundantBackup {
if geoRedundantBackup {
return mysql.Enabled
}
return mysql.Disabled
}