/
aws_account_edit.go
127 lines (104 loc) · 3.15 KB
/
aws_account_edit.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
package cmd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"path"
"time"
"github.com/spf13/cobra"
)
// holds value of "aws-id" flag
var awsAccountEditID string
// holds value of "name" flag
var awsAccountEditName string
// holds value of "account-number" flag
var awsAccountEditAccountNumber string
// holds value of "access-key-id" flag
var awsAccountEditAccessKeyID string
// holds value of "secret-access-key" flag
var awsAccountEditSecretAccessKey string
var awsAccountEditCmd = &cobra.Command{
Use: "edit",
Short: "Update CA AWS account",
Long: `Use the "/aws_accounts/{id}" API to update specified ID AWS account with Cloud Automator.`,
RunE: execAwsAccountEdit,
}
func init() {
awsAccountCmd.AddCommand(awsAccountEditCmd)
awsAccountEditCmd.Flags().StringVar(&awsAccountEditID, "aws-id", "", "Specify CA AWS account ID. [required]")
awsAccountEditCmd.Flags().StringVar(&awsAccountEditName, "name", "", "Specify CA AWS account name.")
awsAccountEditCmd.Flags().StringVar(&awsAccountEditAccountNumber, "account-number", "", "Specify AWS account number.")
awsAccountEditCmd.Flags().StringVar(&awsAccountEditAccessKeyID, "access-key-id", "", "Specify AWS access key ID.")
awsAccountEditCmd.Flags().StringVar(&awsAccountEditSecretAccessKey, "secret-access-key", "", "Specify AWS secret access key.")
}
func execAwsAccountEdit(cmd *cobra.Command, args []string) error {
paramID := awsAccountEditID
if paramID == "" {
cmd.Help()
return errors.New("\nPlease specify flag [--aws-id] required CA AWS account ID.")
}
// get profile data
paramProfileName := rootPprofileName
currentProfile, err := getProfile(paramProfileName)
if err != nil {
return err
}
// preparing to call API
endURL := currentProfile.Endpoint
apiPath := path.Join("aws_accounts", paramID)
method := "PATCH"
paramData := map[string]interface{}{}
paramName := awsAccountEditName
if paramName != "" {
paramData["name"] = paramName
}
paramNumber := awsAccountEditAccountNumber
if paramNumber != "" {
paramData["account_number"] = paramNumber
}
paramAccessKey := awsAccountEditAccessKeyID
if paramAccessKey != "" {
paramData["access_key_id"] = paramAccessKey
}
paramSecretKey := awsAccountEditSecretAccessKey
if paramSecretKey != "" {
paramData["secret_access_key"] = paramSecretKey
}
if len(paramData) <= 0 {
cmd.Help()
return errors.New("\nNone of the values to be changed has been specified.")
}
paramBytes, err := json.Marshal(paramData)
if err != nil {
return err
}
postBody := bytes.NewBuffer(paramBytes)
// create http client
httpClient := &http.Client{}
client, err := createClient(endURL, httpClient)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// call API [/aws_accounts/{id}]
httpRequest, err := client.createRequest(ctx, method, apiPath, currentProfile.APIKey, postBody)
if err != nil {
return err
}
httpResponse, err := client.HTTPClient.Do(httpRequest)
if err != nil {
return err
}
// get result string
result, err := getBodyString(httpResponse)
if err != nil {
return err
}
// draw result
fmt.Printf("%s\n", result)
return nil
}