@@ -3,8 +3,11 @@ package command
3
3
import (
4
4
"errors"
5
5
"github.com/opsgenie/opsgenie-go-sdk-v2/heartbeat"
6
+ "github.com/opsgenie/opsgenie-go-sdk-v2/og"
6
7
gcli "github.com/urfave/cli"
7
8
"os"
9
+ "strconv"
10
+ "strings"
8
11
)
9
12
10
13
func NewHeartbeatClient (c * gcli.Context ) (* heartbeat.Client , error ) {
@@ -19,7 +22,7 @@ func NewHeartbeatClient(c *gcli.Context) (*heartbeat.Client, error) {
19
22
}
20
23
21
24
// HeartbeatAction sends an Heartbeat signal to Opsgenie.
22
- func HeartbeatAction (c * gcli.Context ) {
25
+ func PingHeartbeatAction (c * gcli.Context ) {
23
26
cli , err := NewHeartbeatClient (c )
24
27
if err != nil {
25
28
os .Exit (1 )
@@ -39,3 +42,169 @@ func HeartbeatAction(c *gcli.Context) {
39
42
}
40
43
printMessage (DEBUG ,"Ping request has received. RequestID: " + response .RequestId )
41
44
}
45
+
46
+ func CreateHeartbeatAction (c * gcli.Context ) {
47
+ cli , err := NewHeartbeatClient (c )
48
+ if err != nil {
49
+ os .Exit (1 )
50
+ }
51
+
52
+ addRequest := heartbeat.AddRequest {}
53
+
54
+ if val , success := getVal ("name" , c ); success {
55
+ addRequest .Name = val
56
+ }
57
+
58
+ if val , success := getVal ("description" , c ); success {
59
+ addRequest .Description = val
60
+ }
61
+
62
+ if val , success := getVal ("alertMessage" , c ); success {
63
+ addRequest .AlertMessage = val
64
+ }
65
+
66
+ if val , success := getVal ("alertTag" , c ); success {
67
+ addRequest .AlertTag = strings .Split (val , "," )
68
+ }
69
+
70
+ if val , success := getVal ("alertPriority" , c ); success {
71
+ addRequest .AlertPriority = val
72
+ }
73
+
74
+ if val , success := getVal ("ownerTeam" , c ); success {
75
+ addRequest .OwnerTeam = og.OwnerTeam {
76
+ Name : val ,
77
+ }
78
+ }
79
+
80
+ if val , success := getVal ("interval" , c ); success {
81
+ addRequest .Interval , err = strconv .Atoi (val )
82
+
83
+ if err != nil {
84
+ printMessage (ERROR , "Please provide a valid integer for interval." )
85
+ os .Exit (1 )
86
+ }
87
+ }
88
+
89
+ if val , success := getVal ("intervalType" , c ); success {
90
+ if val == "m" {
91
+ addRequest .IntervalUnit = heartbeat .Minutes
92
+ } else if val == "h" {
93
+ addRequest .IntervalUnit = heartbeat .Hours
94
+ } else if val == "d" {
95
+ addRequest .IntervalUnit = heartbeat .Days
96
+ } else {
97
+ printMessage (ERROR , "Please provide a valid interval unit." )
98
+ os .Exit (1 )
99
+ }
100
+ }
101
+
102
+ enabled := c .IsSet ("enabled" )
103
+ addRequest .Enabled = & enabled
104
+
105
+ printMessage (DEBUG , "Heartbeat create request created from flags. Sedning to Opsgenie..." )
106
+
107
+ response , err := cli .Add (nil , & addRequest )
108
+ if err != nil {
109
+ printMessage (ERROR ,err .Error ())
110
+ os .Exit (1 )
111
+ }
112
+ printMessage (DEBUG ,"Heartbeat will be created " + response .RequestId )
113
+ }
114
+
115
+ func DeleteHeartbeatAction (c * gcli.Context ) {
116
+ cli , err := NewHeartbeatClient (c )
117
+ if err != nil {
118
+ os .Exit (1 )
119
+ }
120
+
121
+ var name string
122
+ if val , success := getVal ("name" , c ); success {
123
+ name = val
124
+ }
125
+
126
+ printMessage (DEBUG , "Heartbeat delete request created from flags. Sending to Opsgenie..." )
127
+
128
+ response , err := cli .Delete (nil , name )
129
+ if err != nil {
130
+ printMessage (ERROR ,err .Error ())
131
+ os .Exit (1 )
132
+ }
133
+ printMessage (DEBUG ,"Heartbeat will be deleted." )
134
+ printMessage (INFO , response .RequestId )
135
+ }
136
+
137
+ func DisableHeartbeatAction (c * gcli.Context ) {
138
+ cli , err := NewHeartbeatClient (c )
139
+ if err != nil {
140
+ os .Exit (1 )
141
+ }
142
+
143
+ var name string
144
+ if val , success := getVal ("name" , c ); success {
145
+ name = val
146
+ }
147
+
148
+ printMessage (DEBUG , "Heartbeat disable request created from flags. Sending to Opsgenie..." )
149
+
150
+ response , err := cli .Disable (nil , name )
151
+ if err != nil {
152
+ printMessage (ERROR ,err .Error ())
153
+ os .Exit (1 )
154
+ }
155
+ printMessage (DEBUG ,"Heartbeat will be disabled." )
156
+ printMessage (INFO , response .RequestId )
157
+ }
158
+
159
+ func EnableHeartbeatAction (c * gcli.Context ) {
160
+ cli , err := NewHeartbeatClient (c )
161
+ if err != nil {
162
+ os .Exit (1 )
163
+ }
164
+
165
+ var name string
166
+ if val , success := getVal ("name" , c ); success {
167
+ name = val
168
+ }
169
+
170
+ printMessage (DEBUG , "Heartbeat enable request created from flags. Sending to Opsgenie..." )
171
+
172
+ response , err := cli .Enable (nil , name )
173
+ if err != nil {
174
+ printMessage (ERROR ,err .Error ())
175
+ }
176
+ printMessage (DEBUG , "Heartbeat will be enabled" )
177
+ printMessage (INFO , response .RequestId )
178
+ }
179
+
180
+ func ListHeartbeatAction (c * gcli.Context ) {
181
+ cli , err := NewHeartbeatClient (c )
182
+ if err != nil {
183
+ os .Exit (1 )
184
+ }
185
+
186
+ response , err := cli .List (nil )
187
+ if err != nil {
188
+ printMessage (ERROR ,err .Error ())
189
+ }
190
+
191
+ outputFormat := strings .ToLower (c .String ("output-format" ))
192
+ printMessage (DEBUG ,"Heartbeats listed successfully, and will print as " + outputFormat )
193
+ switch outputFormat {
194
+ case "yaml" :
195
+ output , err := resultToYAML (response .Heartbeats )
196
+ if err != nil {
197
+ printMessage (ERROR ,err .Error ())
198
+ os .Exit (1 )
199
+ }
200
+ printMessage (INFO , output )
201
+ default :
202
+ isPretty := c .IsSet ("pretty" )
203
+ output , err := resultToJSON (response .Heartbeats , isPretty )
204
+ if err != nil {
205
+ printMessage (ERROR ,err .Error ())
206
+ os .Exit (1 )
207
+ }
208
+ printMessage (INFO , output )
209
+ }
210
+ }
0 commit comments