-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaction.go
More file actions
167 lines (156 loc) · 4.86 KB
/
action.go
File metadata and controls
167 lines (156 loc) · 4.86 KB
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
package cmd
import (
"encoding/json"
"fmt"
"github.com/opslevel/opslevel-go/v2025"
"github.com/opslevel/cli/common"
"github.com/spf13/cobra"
)
var actionType string
var exampleActionCmd = &cobra.Command{
Use: "action",
Short: "Example action",
Long: `Example action`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getExample(opslevel.CustomActionsWebhookActionCreateInput{
Name: "example_name",
Description: opslevel.RefOf("example_description"),
WebhookUrl: "example_webhook_url",
HttpMethod: opslevel.CustomActionsHttpMethodEnumPost,
Headers: &opslevel.JSON{
"example_header": "example_value",
},
LiquidTemplate: opslevel.RefOf("example_liquid_template"),
}))
},
}
var createActionCmd = &cobra.Command{
Use: "action --type=$ACTION_TYPE",
Short: "Create an action",
Long: "Create an action",
Example: `
cat << EOF | opslevel create action --type=webhook -f -
name: "Page The On Call"
description: "Pages the On Call"
webhookUrl: "https://api.pagerduty.com/incidents"
httpMethod: "POST"
headers:
accept: "application/vnd.pagerduty+json;version=2"
authorization: "Token token=XXXXXXXXXXXXX"
from: "someone@example.com"
liquidTemplate: |
{
"incident": {
"type": "incident",
"title": "{{manualInputs.IncidentTitle}}",
"service": {
"id": "{{ service | tag_value: 'pd_id' }}",
"type": "service_reference"
},
"body": {
"type": "incident_body",
"details": "Incident triggered from OpsLevel by {{user.name}} with the email {{user.email}}. {{manualInputs.IncidentDescription}}"
}
}
}
EOF`,
Run: func(cmd *cobra.Command, args []string) {
switch actionType {
case "webhook":
input, err := readResourceInput[opslevel.CustomActionsWebhookActionCreateInput]()
cobra.CheckErr(err)
result, err := getClientGQL().CreateWebhookAction(*input)
cobra.CheckErr(err)
fmt.Printf("created webhook action: %s\n", string(result.CustomActionsId.Id))
default:
err := fmt.Errorf("unknown action type: '%s'", actionType)
cobra.CheckErr(err)
}
},
}
var getActionCmd = &cobra.Command{
Use: "action ID|ALIAS",
Short: "Get details about an action",
Long: "Get details about an action",
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID", "ALIAS"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
action, err := getClientGQL().GetCustomAction(key)
cobra.CheckErr(err)
common.PrettyPrint(action)
},
}
var listActionCmd = &cobra.Command{
Use: "action",
Aliases: []string{"actions"},
Short: "List actions",
Long: "List actions",
Run: func(cmd *cobra.Command, args []string) {
resp, err := getClientGQL().ListCustomActions(nil)
cobra.CheckErr(err)
list := resp.Nodes
if isJsonOutput() {
common.JsonPrint(json.MarshalIndent(list, "", " "))
} else {
w := common.NewTabWriter("ID", "NAME", "HTTP_METHOD", "WEBHOOK_URL")
for _, item := range list {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", string(item.CustomActionsId.Id), item.Name, item.HttpMethod, item.WebhookUrl)
}
w.Flush()
}
},
}
var updateActionCmd = &cobra.Command{
Use: "action ID",
Short: "Update an action",
Long: "Update an action",
Example: `
cat << EOF | opslevel update action --type=webhook $ACTION_ID -f -
description: "Pages the oncall and creates an incident"
headers:
accept: "application/vnd.pagerduty+json;version=2"
authorization: "Token token=XXXXXXXXXXXXX"
from: "someone-else@example.com"
EOF`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
switch actionType {
case "webhook":
input, err := readResourceInput[opslevel.CustomActionsWebhookActionUpdateInput]()
input.Id = *opslevel.NewID(key)
cobra.CheckErr(err)
action, err := getClientGQL().UpdateWebhookAction(*input)
cobra.CheckErr(err)
common.JsonPrint(json.MarshalIndent(action, "", " "))
default:
err := fmt.Errorf("unknown action type: '%s'", actionType)
cobra.CheckErr(err)
}
},
}
var deleteActionCmd = &cobra.Command{
Use: "action ID|ALIAS",
Short: "Delete an action",
Long: "Delete an action",
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID", "ALIAS"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
err := getClientGQL().DeleteWebhookAction(key)
cobra.CheckErr(err)
fmt.Printf("deleted webhook action: %s\n", key)
},
}
func init() {
exampleCmd.AddCommand(exampleActionCmd)
createCmd.AddCommand(createActionCmd)
createActionCmd.Flags().StringVar(&actionType, "type", "webhook", "action type, default=webhook")
updateCmd.AddCommand(updateActionCmd)
updateActionCmd.Flags().StringVar(&actionType, "type", "webhook", "action type, default=webhook")
getCmd.AddCommand(getActionCmd)
listCmd.AddCommand(listActionCmd)
deleteCmd.AddCommand(deleteActionCmd)
}