/
post_process_list.go
77 lines (62 loc) · 1.55 KB
/
post_process_list.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
package cmd
import (
"context"
"fmt"
"net/http"
"time"
"github.com/spf13/cobra"
)
// holds value of "link" flag
var postProcessListLink string
var postProcessListCmd = &cobra.Command{
Use: "list",
Short: "Return CA post process list",
Long: `Use the "/post_processes" API to get post process list with Cloud Automator.`,
RunE: execPostProcessList,
}
func init() {
postProcessCmd.AddCommand(postProcessListCmd)
postProcessListCmd.Flags().StringVar(&postProcessListLink, "link", "", "Specify CA paging URL of API response.")
}
func execPostProcessList(cmd *cobra.Command, args []string) error {
paramlink := postProcessListLink
// get profile data
paramProfileName := rootPprofileName
currentProfile, err := getProfile(paramProfileName)
if err != nil {
return err
}
// preparing to call API
endURL := currentProfile.Endpoint
apiPath := "post_processes"
method := "GET"
if paramlink != "" {
endURL = paramlink
apiPath = ""
}
// 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]
httpRequest, err := client.createRequest(ctx, method, apiPath, currentProfile.APIKey, nil)
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
}