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
/
credential.go
288 lines (239 loc) · 9.77 KB
/
credential.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package cli
import (
"encoding/json"
"fmt"
"os"
"time"
"errors"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/cloud"
"github.com/hortonworks/cb-cli/cli/types"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v1credentials"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
)
func CreateAwsCredential(c *cli.Context) {
cloud.SetProviderType(cloud.AWS)
createCredential(c)
}
func ModifyAwsCredential(c *cli.Context) {
cloud.SetProviderType(cloud.AWS)
modifyCredential(c)
}
func CreateAzureCredential(c *cli.Context) {
cloud.SetProviderType(cloud.AZURE)
createCredential(c)
}
func ModifyAzureCredential(c *cli.Context) {
cloud.SetProviderType(cloud.AZURE)
modifyCredential(c)
}
func CreateGcpCredential(c *cli.Context) {
cloud.SetProviderType(cloud.GCP)
createCredential(c)
}
func ModifyGcpCredential(c *cli.Context) {
cloud.SetProviderType(cloud.GCP)
modifyCredential(c)
}
func CreateOpenstackCredential(c *cli.Context) {
cloud.SetProviderType(cloud.OPENSTACK)
createCredential(c)
}
func ModifyOpenstackCredential(c *cli.Context) {
cloud.SetProviderType(cloud.OPENSTACK)
modifyCredential(c)
}
func CreateCredentialFromFile(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "create credential")
req := assembleCredentialRequest(c.String(FlInputJson.Name), c.String(FlNameOptional.Name))
cbClient := NewCloudbreakHTTPClientFromContext(c)
postCredential(cbClient.Cloudbreak.V1credentials, c.Bool(FlPublicOptional.Name), req)
}
func ModifyCredentialFromFile(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "modify credential from file")
credReq := assembleCredentialRequest(c.String(FlInputJson.Name), c.String(FlNameOptional.Name))
cbClient := NewCloudbreakHTTPClientFromContext(c)
credential := getCredential(*credReq.Name, cbClient.Cloudbreak.V1credentials)
putCredential(cbClient.Cloudbreak.V1credentials, *credential.Public, credReq)
}
func assembleCredentialRequest(path, credName string) *models_cloudbreak.CredentialRequest {
if _, err := os.Stat(path); os.IsNotExist(err) {
utils.LogErrorAndExit(err)
}
log.Infof("[assembleCredentialRequest] read credential json from file: %s", path)
content := utils.ReadFile(path)
var req models_cloudbreak.CredentialRequest
err := json.Unmarshal(content, &req)
if err != nil {
msg := fmt.Sprintf(`Invalid json format: %s. Please make sure that the json is valid (check for commas and double quotes).`, err.Error())
utils.LogErrorMessageAndExit(msg)
}
if len(credName) != 0 {
req.Name = &credName
}
if req.Name == nil || len(*req.Name) == 0 {
utils.LogErrorMessageAndExit("Name of the credential must be set either in the template or with the --name command line option.")
}
return &req
}
func createCredential(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "create credential")
cbClient := NewCloudbreakHTTPClientFromContext(c)
createCredentialImpl(c.String, c.Bool, cbClient.Cloudbreak.V1credentials)
}
func modifyCredential(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "modify credential")
cbClient := NewCloudbreakHTTPClientFromContext(c)
modifyCredentialImpl(c.String, c.Bool, cbClient.Cloudbreak.V1credentials)
}
type modifyCredentialClient interface {
PutPrivateCredential(params *v1credentials.PutPrivateCredentialParams) (*v1credentials.PutPrivateCredentialOK, error)
PutPublicCredential(params *v1credentials.PutPublicCredentialParams) (*v1credentials.PutPublicCredentialOK, error)
GetPublicCredential(params *v1credentials.GetPublicCredentialParams) (*v1credentials.GetPublicCredentialOK, error)
}
func modifyCredentialImpl(stringFinder func(string) string, boolFinder func(string) bool, client modifyCredentialClient) *models_cloudbreak.CredentialResponse {
name := stringFinder(FlName.Name)
description := stringFinder(FlDescriptionOptional.Name)
provider := cloud.GetProvider()
credential := getCredential(name, client)
if *credential.CloudPlatform != *provider.GetName() {
utils.LogErrorAndExit(errors.New("cloud provider cannot be modified"))
}
log.Infof("[modifyCredentialImpl] original credential found name: %s id: %d", name, credential.ID)
credentialMap, err := provider.GetCredentialParameters(stringFinder, boolFinder)
if err != nil {
utils.LogErrorAndExit(err)
}
if len(description) == 0 {
origDesc := credential.Description
if origDesc != nil && len(*origDesc) > 0 {
description = *origDesc
}
}
credReq := &models_cloudbreak.CredentialRequest{
Name: &name,
Description: &description,
CloudPlatform: provider.GetName(),
Parameters: credentialMap,
}
return putCredential(client, *credential.Public, credReq)
}
func getCredential(name string, client modifyCredentialClient) *models_cloudbreak.CredentialResponse {
defer utils.TimeTrack(time.Now(), "get credential")
log.Infof("[getCredential] get credential by name: %s", name)
response, err := client.GetPublicCredential(v1credentials.NewGetPublicCredentialParams().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
return response.Payload
}
func putCredential(client modifyCredentialClient, public bool, credReq *models_cloudbreak.CredentialRequest) *models_cloudbreak.CredentialResponse {
var credential *models_cloudbreak.CredentialResponse
if public {
log.Infof("[putCredential] modify public credential: %s", *credReq.Name)
resp, err := client.PutPublicCredential(v1credentials.NewPutPublicCredentialParams().WithBody(credReq))
if err != nil {
utils.LogErrorAndExit(err)
}
credential = resp.Payload
} else {
log.Infof("[putCredential] modify private credential: %s", *credReq.Name)
resp, err := client.PutPrivateCredential(v1credentials.NewPutPrivateCredentialParams().WithBody(credReq))
if err != nil {
utils.LogErrorAndExit(err)
}
credential = resp.Payload
}
return credential
}
type createCredentialClient interface {
PostPublicCredential(*v1credentials.PostPublicCredentialParams) (*v1credentials.PostPublicCredentialOK, error)
PostPrivateCredential(*v1credentials.PostPrivateCredentialParams) (*v1credentials.PostPrivateCredentialOK, error)
}
func createCredentialImpl(stringFinder func(string) string, boolFinder func(string) bool, client createCredentialClient) *models_cloudbreak.CredentialResponse {
provider := cloud.GetProvider()
credentialMap, err := provider.GetCredentialParameters(stringFinder, boolFinder)
if err != nil {
utils.LogErrorAndExit(err)
}
name := stringFinder(FlName.Name)
credReq := &models_cloudbreak.CredentialRequest{
Name: &name,
Description: &(&types.S{S: stringFinder(FlDescriptionOptional.Name)}).S,
CloudPlatform: provider.GetName(),
Parameters: credentialMap,
}
public := boolFinder(FlPublicOptional.Name)
return postCredential(client, public, credReq)
}
func postCredential(client createCredentialClient, public bool, credReq *models_cloudbreak.CredentialRequest) *models_cloudbreak.CredentialResponse {
var credential *models_cloudbreak.CredentialResponse
if public {
log.Infof("[postCredential] sending create public credential request")
resp, err := client.PostPublicCredential(v1credentials.NewPostPublicCredentialParams().WithBody(credReq))
if err != nil {
utils.LogErrorAndExit(err)
}
credential = resp.Payload
} else {
log.Infof("[postCredential] sending create private credential request")
resp, err := client.PostPrivateCredential(v1credentials.NewPostPrivateCredentialParams().WithBody(credReq))
if err != nil {
utils.LogErrorAndExit(err)
}
credential = resp.Payload
}
log.Infof("[postCredential] credential created: %s (id: %d)", *credential.Name, credential.ID)
return credential
}
func DescribeCredential(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "describe credential")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
resp, err := cbClient.Cloudbreak.V1credentials.GetPublicCredential(v1credentials.NewGetPublicCredentialParams().WithName(c.String(FlName.Name)))
if err != nil {
utils.LogErrorAndExit(err)
}
cred := resp.Payload
output.Write(cloudResourceHeader, &cloudResourceOut{*cred.Name, *cred.Description, *cred.CloudPlatform})
}
func DeleteCredential(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "delete credential")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[DeleteCredential] sending delete credential request with name: %s", name)
if err := cbClient.Cloudbreak.V1credentials.DeletePublicCredential(v1credentials.NewDeletePublicCredentialParams().WithName(name)); err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[DeleteCredential] credential deleted, name: %s", name)
}
func ListCredentials(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list credentials")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listCredentialsImpl(cbClient.Cloudbreak.V1credentials, output.WriteList)
}
type getPublicsCredentialClient interface {
GetPublicsCredential(*v1credentials.GetPublicsCredentialParams) (*v1credentials.GetPublicsCredentialOK, error)
}
func listCredentialsImpl(client getPublicsCredentialClient, writer func([]string, []utils.Row)) {
log.Infof("[listCredentialsImpl] sending credential list request")
credResp, err := client.GetPublicsCredential(v1credentials.NewGetPublicsCredentialParams())
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, cred := range credResp.Payload {
tableRows = append(tableRows, &cloudResourceOut{*cred.Name, *cred.Description, *cred.CloudPlatform})
}
writer(cloudResourceHeader, tableRows)
}