-
Notifications
You must be signed in to change notification settings - Fork 151
/
custom_endpoint.go
274 lines (239 loc) · 7.87 KB
/
custom_endpoint.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package rdb
import (
"context"
"fmt"
"reflect"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/human"
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
type rdbEndpointCustomResult struct {
Endpoints []*rdb.Endpoint
Success core.SuccessResult
}
func rdbEndpointCustomResultMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
result := i.(rdbEndpointCustomResult)
messageStr, err := result.Success.MarshalHuman()
if err != nil {
return "", err
}
if len(result.Endpoints) == 0 {
return messageStr, nil
}
endpointsStr, err := human.Marshal(result.Endpoints, opt)
if err != nil {
return "", err
}
return messageStr + "\n" + endpointsStr, nil
}
type rdbEndpointSpecCustom struct {
PrivateNetwork *rdbEndpointSpecPrivateNetworkCustom `json:"private-network"`
LoadBalancer bool `json:"load-balancer"`
}
type rdbEndpointSpecPrivateNetworkCustom struct {
*rdb.EndpointSpecPrivateNetwork
EnableIpam bool `json:"enable-ipam"`
}
func endpointCreateBuilder(c *core.Command) *core.Command {
type rdbCreateEndpointRequestCustom struct {
*rdb.CreateEndpointRequest
LoadBalancer bool `json:"load-balancer"`
PrivateNetwork *rdbEndpointSpecPrivateNetworkCustom `json:"private-network"`
}
c.ArgsType = reflect.TypeOf(rdbCreateEndpointRequestCustom{})
c.ArgSpecs = core.ArgSpecs{
{
Name: "instance-id",
Short: `UUID of the Database Instance to which you want to add an endpoint`,
Required: true,
Positional: true,
},
{
Name: "private-network.private-network-id",
Short: `UUID of the Private Network to be connected to the Database Instance`,
Required: false,
},
{
Name: "private-network.service-ip",
Short: `Endpoint IPv4 address with a CIDR notation. Refer to the official Scaleway documentation to learn more about IP and subnet limitations.`,
Required: false,
},
{
Name: "private-network.enable-ipam",
Short: "Will configure your Private Network endpoint with Scaleway IPAM service if true",
Required: false,
Default: core.DefaultValueSetter("true"),
},
{
Name: "load-balancer",
Short: "Will configure a public Load-Balancer endpoint",
Required: false,
Default: core.DefaultValueSetter("true"),
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw),
}
c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
args := argsI.(*rdbCreateEndpointRequestCustom)
customEndpointSpec := &rdbEndpointSpecCustom{
PrivateNetwork: args.PrivateNetwork,
LoadBalancer: args.LoadBalancer,
}
endpointRequest, err := endpointRequestFromCustom([]*rdbEndpointSpecCustom{customEndpointSpec})
if err != nil {
return nil, err
}
endpoint, err := api.CreateEndpoint(&rdb.CreateEndpointRequest{
Region: args.Region,
InstanceID: args.InstanceID,
EndpointSpec: endpointRequest[0],
}, scw.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("could not create endpoint: %w", err)
}
return &rdbEndpointCustomResult{
Endpoints: []*rdb.Endpoint{endpoint},
Success: core.SuccessResult{
Message: fmt.Sprintf("Endpoint %s successfully added", endpoint.ID),
},
}, nil
}
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
args := argsI.(*rdbCreateEndpointRequestCustom)
instance, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: args.InstanceID,
Region: args.Region,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}
newEndpointID := respI.(*rdbEndpointCustomResult).Endpoints[0].ID
// The ID in the CreateEndpoint response is empty for public endpoints, so we have to fetch it among the WaitForInstance response's endpoints
if newEndpointID == "" {
for _, endpoint := range instance.Endpoints {
if endpoint.LoadBalancer != nil {
newEndpointID = endpoint.ID
}
}
}
return &rdbEndpointCustomResult{
Endpoints: instance.Endpoints,
Success: core.SuccessResult{
Message: fmt.Sprintf("Endpoint %s successfully added", newEndpointID),
},
}, nil
}
return c
}
func endpointDeleteBuilder(c *core.Command) *core.Command {
type rdbDeleteEndpointRequestCustom struct {
*rdb.DeleteEndpointRequest
InstanceID string `json:"instance-id"`
}
c.ArgsType = reflect.TypeOf(rdbDeleteEndpointRequestCustom{})
c.ArgSpecs.GetByName("endpoint-id").Positional = true
c.ArgSpecs.AddBefore("endpoint-id", &core.ArgSpec{
Name: "instance-id",
Short: `UUID of the Database Instance from which you want to delete an endpoint`,
Required: true,
Positional: false,
})
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
args := argsI.(*rdbDeleteEndpointRequestCustom)
err := api.DeleteEndpoint(&rdb.DeleteEndpointRequest{
Region: args.Region,
EndpointID: args.EndpointID,
})
if err != nil {
return nil, err
}
return &core.SuccessResult{
Resource: "endpoint",
Verb: "delete",
}, nil
}
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
args := argsI.(*rdbDeleteEndpointRequestCustom)
_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: args.InstanceID,
Region: args.Region,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}
return respI, nil
}
return c
}
func endpointGetBuilder(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("endpoint-id").Positional = true
return c
}
func endpointListCommand() *core.Command {
type rdbEndpointListCustomArgs struct {
InstanceID string
Region scw.Region
}
return &core.Command{
Namespace: "rdb",
Resource: "endpoint",
Verb: "list",
Short: "Lists a Database Instance's endpoints",
Long: "Lists all public and private endpoints of a Database Instance",
ArgsType: reflect.TypeOf(rdbEndpointListCustomArgs{}),
ArgSpecs: core.ArgSpecs{
{
Name: "instance-id",
Short: `ID of the Database Instance`,
Required: true,
Positional: true,
},
},
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
args := argsI.(*rdbEndpointListCustomArgs)
instance, err := api.GetInstance(&rdb.GetInstanceRequest{
Region: args.Region,
InstanceID: args.InstanceID,
}, scw.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("failed to get instance: %w", err)
}
return instance.Endpoints, nil
},
}
}
func endpointRequestFromCustom(customEndpoints []*rdbEndpointSpecCustom) ([]*rdb.EndpointSpec, error) {
endpoints := []*rdb.EndpointSpec(nil)
for _, customEndpoint := range customEndpoints {
if customEndpoint.PrivateNetwork != nil && customEndpoint.PrivateNetwork.EndpointSpecPrivateNetwork != nil {
ipamConfig := &rdb.EndpointSpecPrivateNetworkIpamConfig{}
if !customEndpoint.PrivateNetwork.EnableIpam || customEndpoint.PrivateNetwork.ServiceIP != nil {
ipamConfig = nil
}
endpoints = append(endpoints, &rdb.EndpointSpec{
PrivateNetwork: &rdb.EndpointSpecPrivateNetwork{
PrivateNetworkID: customEndpoint.PrivateNetwork.PrivateNetworkID,
ServiceIP: customEndpoint.PrivateNetwork.ServiceIP,
IpamConfig: ipamConfig,
},
})
} else if customEndpoint.LoadBalancer {
endpoints = append(endpoints, &rdb.EndpointSpec{
LoadBalancer: &rdb.EndpointSpecLoadBalancer{},
})
} else {
return nil, fmt.Errorf("endpoint must be either a load-balancer or a private network")
}
}
return endpoints, nil
}