This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
rds.go
261 lines (229 loc) · 9.26 KB
/
rds.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
package cli
import (
"fmt"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/types"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v3_organization_id_rdsconfigs"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
)
var rdsHeader = []string{"Name", "ConnectionURL", "DatabaseEngine", "Type", "Driver"}
type rds struct {
Name string `json:"Name" yaml:"Name"`
URL string `json:"ConnectionURL" yaml:"ConnectionURL"`
DatabaseEngine string `json:"DatabaseEngine" yaml:"DatabaseEngine"`
Type string `json:"Type" yaml:"Type"`
Driver string `json:"Driver" yaml:"Driver"`
}
type rdsOutDescribe struct {
*rds
ID string `json:"ID" yaml:"ID"`
}
func (r *rds) DataAsStringArray() []string {
return []string{r.Name, r.URL, r.DatabaseEngine, r.Type, r.Driver}
}
func (r *rdsOutDescribe) DataAsStringArray() []string {
return append(r.rds.DataAsStringArray(), r.ID)
}
type rdsClient interface {
ListRdsConfigsByOrganization(params *v3_organization_id_rdsconfigs.ListRdsConfigsByOrganizationParams) (*v3_organization_id_rdsconfigs.ListRdsConfigsByOrganizationOK, error)
CreateRdsConfigInOrganization(params *v3_organization_id_rdsconfigs.CreateRdsConfigInOrganizationParams) (*v3_organization_id_rdsconfigs.CreateRdsConfigInOrganizationOK, error)
TestRdsConnectionInOrganization(params *v3_organization_id_rdsconfigs.TestRdsConnectionInOrganizationParams) (*v3_organization_id_rdsconfigs.TestRdsConnectionInOrganizationOK, error)
}
func TestRdsByName(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[TestRdsByParams] test a database configuration")
cbClient := NewCloudbreakHTTPClientFromContext(c)
testRdsByNameImpl(
cbClient.Cloudbreak.V3OrganizationIDRdsconfigs,
c.Int64(FlOrganizationOptional.Name),
c.String(FlName.Name))
}
func TestRdsByParams(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[TestRdsByParams] test a database configuration")
cbClient := NewCloudbreakHTTPClientFromContext(c)
testRdsByParamsImpl(
cbClient.Cloudbreak.V3OrganizationIDRdsconfigs,
c.Int64(FlOrganizationOptional.Name),
c.String(FlRdsUserName.Name),
c.String(FlRdsPassword.Name),
c.String(FlRdsURL.Name),
c.String(FlRdsType.Name))
}
func testRdsByNameImpl(client rdsClient, orgID int64, name string) {
defer utils.TimeTrack(time.Now(), "test database configuration by name")
rdsRequest := &models_cloudbreak.RdsTestRequest{
Name: name,
}
log.Infof("[testRdsByParamsImpl] sending test database configuration by parameters request")
resp, err := client.TestRdsConnectionInOrganization(v3_organization_id_rdsconfigs.NewTestRdsConnectionInOrganizationParams().WithOrganizationID(orgID).WithBody(rdsRequest))
if err != nil {
utils.LogErrorAndExit(err)
}
if responseText := getEmptyIfNil(resp.Payload.ConnectionResult); responseText != "connected" {
utils.LogErrorMessageAndExit(fmt.Sprintf("database configuration test result: %s", responseText))
}
}
func testRdsByParamsImpl(client rdsClient, orgID int64, username string, password string, URL string, rdsType string) {
defer utils.TimeTrack(time.Now(), "test database configuration by parameters")
rdsRequest := &models_cloudbreak.RdsTestRequest{
RdsConfig: &models_cloudbreak.RdsConfig{
Name: &(&types.S{S: "testconnection"}).S,
ConnectionUserName: &username,
ConnectionPassword: &password,
ConnectionURL: &URL,
Type: &rdsType,
},
}
log.Infof("[testRdsByParamsImpl] sending test database configuration by parameters request")
resp, err := client.TestRdsConnectionInOrganization(v3_organization_id_rdsconfigs.NewTestRdsConnectionInOrganizationParams().WithOrganizationID(orgID).WithBody(rdsRequest))
if err != nil {
utils.LogErrorAndExit(err)
}
if responseText := getEmptyIfNil(resp.Payload.ConnectionResult); responseText != "connected" {
utils.LogErrorMessageAndExit(fmt.Sprintf("database configuration test result: %s", responseText))
}
}
func CreateRdsOracle11(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[CreateRdsOracle11] creating a database configuration")
cbClient := NewCloudbreakHTTPClientFromContext(c)
createRdsImpl(
cbClient.Cloudbreak.V3OrganizationIDRdsconfigs,
c.Int64(FlOrganizationOptional.Name),
c.String(FlName.Name),
c.String(FlRdsUserName.Name),
c.String(FlRdsPassword.Name),
c.String(FlRdsURL.Name),
c.String(FlRdsType.Name),
c.String(FlRdsConnectorJarURLOptional.Name),
&models_cloudbreak.Oracle{
Version: &(&types.S{S: "11"}).S,
})
}
func CreateRdsOracle12(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[CreateRdsOracle12] creating a database configuration")
cbClient := NewCloudbreakHTTPClientFromContext(c)
createRdsImpl(
cbClient.Cloudbreak.V3OrganizationIDRdsconfigs,
c.Int64(FlOrganizationOptional.Name),
c.String(FlName.Name),
c.String(FlRdsUserName.Name),
c.String(FlRdsPassword.Name),
c.String(FlRdsURL.Name),
c.String(FlRdsType.Name),
c.String(FlRdsConnectorJarURLOptional.Name),
&models_cloudbreak.Oracle{
Version: &(&types.S{S: "12"}).S,
})
}
func CreateRds(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[CreateRds] creating a database configuration")
cbClient := NewCloudbreakHTTPClientFromContext(c)
createRdsImpl(
cbClient.Cloudbreak.V3OrganizationIDRdsconfigs,
c.Int64(FlOrganizationOptional.Name),
c.String(FlName.Name),
c.String(FlRdsUserName.Name),
c.String(FlRdsPassword.Name),
c.String(FlRdsURL.Name),
c.String(FlRdsType.Name),
c.String(FlRdsConnectorJarURLOptional.Name),
nil)
}
func createRdsImpl(client rdsClient, orgID int64, name string, username string, password string, URL string, rdsType string, jarURL string, oracle *models_cloudbreak.Oracle) {
defer utils.TimeTrack(time.Now(), "create database")
rdsRequest := &models_cloudbreak.RdsConfig{
Name: &name,
ConnectionUserName: &username,
ConnectionPassword: &password,
ConnectionURL: &URL,
Type: &rdsType,
ConnectorJarURL: jarURL,
}
if oracle != nil {
rdsRequest.Oracle = oracle
}
var rdsResponse *models_cloudbreak.RDSConfigResponse
log.Infof("[createRdsImpl] sending create database request")
resp, err := client.CreateRdsConfigInOrganization(v3_organization_id_rdsconfigs.NewCreateRdsConfigInOrganizationParams().WithOrganizationID(orgID).WithBody(rdsRequest))
if err != nil {
utils.LogErrorAndExit(err)
}
rdsResponse = resp.Payload
log.Infof("[createRdsImpl] rds created: %s (id: %d)", *rdsResponse.Name, rdsResponse.ID)
}
func DeleteRds(c *cli.Context) error {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "delete a database configuration")
orgID := c.Int64(FlOrganizationOptional.Name)
rdsName := c.String(FlName.Name)
log.Infof("[DeleteRds] delete database configuration by name: %s", rdsName)
cbClient := NewCloudbreakHTTPClientFromContext(c)
if _, err := cbClient.Cloudbreak.V3OrganizationIDRdsconfigs.DeleteRdsConfigInOrganization(v3_organization_id_rdsconfigs.NewDeleteRdsConfigInOrganizationParams().WithOrganizationID(orgID).WithName(rdsName)); err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[DeleteRds] database configuration deleted: %s", rdsName)
return nil
}
func DescribeRds(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "describe a database configuration")
log.Infof("[DescribeRds] Describes a database configuration")
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
orgID := c.Int64(FlOrganizationOptional.Name)
rdsName := c.String(FlName.Name)
cbClient := NewCloudbreakHTTPClientFromContext(c)
resp, err := cbClient.Cloudbreak.V3OrganizationIDRdsconfigs.GetRdsConfigInOrganization(v3_organization_id_rdsconfigs.NewGetRdsConfigInOrganizationParams().WithOrganizationID(orgID).WithName(rdsName))
if err != nil {
utils.LogErrorAndExit(err)
}
r := resp.Payload
output.Write(append(rdsHeader, "ID"), &rdsOutDescribe{
&rds{
Name: *r.Name,
URL: *r.ConnectionURL,
DatabaseEngine: *r.DatabaseEngine,
Type: getEmptyIfNil(r.Type),
Driver: getEmptyIfNil(r.ConnectionDriver),
}, strconv.FormatInt(r.ID, 10)})
}
func ListAllRds(c *cli.Context) error {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list database configurations")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
orgID := c.Int64(FlOrganizationOptional.Name)
return listAllRdsImpl(cbClient.Cloudbreak.V3OrganizationIDRdsconfigs, output.WriteList, orgID)
}
func listAllRdsImpl(rdsClient rdsClient, writer func([]string, []utils.Row), orgID int64) error {
resp, err := rdsClient.ListRdsConfigsByOrganization(v3_organization_id_rdsconfigs.NewListRdsConfigsByOrganizationParams().WithOrganizationID(orgID))
if err != nil {
utils.LogErrorAndExit(err)
}
var tableRows []utils.Row
for _, r := range resp.Payload {
row := &rds{
Name: *r.Name,
URL: *r.ConnectionURL,
DatabaseEngine: *r.DatabaseEngine,
Type: getEmptyIfNil(r.Type),
Driver: getEmptyIfNil(r.ConnectionDriver),
}
tableRows = append(tableRows, row)
}
writer(rdsHeader, tableRows)
return nil
}
func getEmptyIfNil(value *string) string {
if value == nil {
return ""
}
return *value
}