/
post_process_delete.go
79 lines (64 loc) · 1.7 KB
/
post_process_delete.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
package cmd
import (
"context"
"errors"
"fmt"
"net/http"
"path"
"time"
"github.com/spf13/cobra"
)
// holds value of "post-process-id" flag
var postProcessDeleteID string
var postProcessDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete CA post process",
Long: `Use the "/post_processes/{id}" API to delete specified ID post process with Cloud Automator.`,
RunE: execPostProcessDelete,
}
func init() {
postProcessCmd.AddCommand(postProcessDeleteCmd)
postProcessDeleteCmd.Flags().StringVar(&postProcessDeleteID, "post-process-id", "", "Specify CA post process ID. [required]")
}
func execPostProcessDelete(cmd *cobra.Command, args []string) error {
paramID := postProcessDeleteID
if paramID == "" {
cmd.Help()
return errors.New("\nPlease specify flag [--post-process-id] required CA post process 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("post_processes", paramID)
method := "DELETE"
// 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, 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
}