/
job_edit.go
165 lines (132 loc) · 4.13 KB
/
job_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
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
package cmd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"path"
"strings"
"time"
"github.com/spf13/cobra"
)
// holds value of "job-id" flag
var jobEditID string
// holds value of "name" flag
var jobEditName string
// holds value of "aws-account-id" flag
var jobEditAwsAccountID string
// holds value of "rule-value" flag
var jobEditRuleValue string
// holds value of "action-value" flag
var jobEditActionValue string
// holds value of "completed-post-process-id" flag
var jobEditCompletedPostProcessID string
// holds value of "failed-post-process-id" flag
var jobEditFailedPostProcessID string
var jobEditCmd = &cobra.Command{
Use: "edit",
Short: "Update CA job",
Long: `Use the "/jobs{id}" API to update specified ID job with Cloud Automator.`,
RunE: execJobEdit,
}
func init() {
jobCmd.AddCommand(jobEditCmd)
jobEditCmd.Flags().StringVar(&jobEditID, "job-id", "", "Specify CA job ID. [required]")
jobEditCmd.Flags().StringVar(&jobEditName, "name", "", "Specify CA job name.")
jobEditCmd.Flags().StringVar(&jobEditAwsAccountID, "aws-account-id", "", "Specify CA AWS account ID.")
jobEditCmd.Flags().StringVar(&jobEditRuleValue, "rule-value", "", "Specify CA job trigger setting values.")
jobEditCmd.Flags().StringVar(&jobEditActionValue, "action-value", "", "Specify CA job action setting values.")
jobEditCmd.Flags().StringVar(&jobEditCompletedPostProcessID, "completed-post-process-id", "", "Specify array that contains post-processing IDs to be executed if the CA job succeeds.")
jobEditCmd.Flags().StringVar(&jobEditFailedPostProcessID, "failed-post-process-id", "", "Specify array that contains post-processing IDs to be executed if the CA job faileds.")
}
func execJobEdit(cmd *cobra.Command, args []string) error {
paramID := jobEditID
if paramID == "" {
cmd.Help()
return errors.New("\nPlease specify flag [--job-id] required CA job ID.")
}
postBody, err := createJobEditPostBody()
if err != nil {
cmd.Help()
return err
}
// get profile data
paramProfileName := rootPprofileName
currentProfile, err := getProfile(paramProfileName)
if err != nil {
return err
}
// preparing to call API
endURL := currentProfile.Endpoint
apiPath := path.Join("jobs", paramID)
method := "PATCH"
// 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 [/jobs/{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
}
func createJobEditPostBody() (io.Reader, error) {
params := map[string]interface{}{}
paramName := jobEditName
if paramName != "" {
params["name"] = paramName
}
paramAwsAccountID := jobEditAwsAccountID
if paramAwsAccountID != "" {
params["aws_account_id"] = paramAwsAccountID
}
paramRuleValue := jobEditRuleValue
if paramRuleValue != "" {
parsedParam, err := createJobParseObjectParameter(paramRuleValue, "")
if err == nil {
params["rule_value"] = *parsedParam
}
}
paramActionValue := jobEditActionValue
if paramActionValue != "" {
parsedParam, err := createJobParseObjectParameter(paramActionValue, "")
if err == nil {
params["action_value"] = *parsedParam
}
}
paramCompletedPostProcessID := jobEditCompletedPostProcessID
if paramCompletedPostProcessID != "" {
params["completed_post_process_id"] = strings.Split(paramCompletedPostProcessID, ",")
}
paramFailedPostProcessID := jobEditFailedPostProcessID
if paramFailedPostProcessID != "" {
params["failed_post_process_id"] = strings.Split(paramFailedPostProcessID, ",")
}
if len(params) <= 0 {
return nil, errors.New("\nNone of the values to be changed has been specified.")
}
paramBytes, err := json.Marshal(params)
if err != nil {
return nil, err
}
return bytes.NewBuffer(paramBytes), nil
}