forked from opsgenie/opsgenie-lamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_policy_cmd.go
136 lines (125 loc) · 3.67 KB
/
integration_policy_cmd.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
package command
import (
"errors"
"fmt"
"github.com/opsgenie/opsgenie-go-sdk-v2/integration"
"github.com/opsgenie/opsgenie-go-sdk-v2/policy"
gcli "github.com/urfave/cli"
"os"
)
func NewIntegrationClient(c *gcli.Context) (*integration.Client, error) {
integrationCli, cliErr := integration.NewClient(getConfigurations(c))
if cliErr != nil {
message := "Can not create the integration client. " + cliErr.Error()
fmt.Printf("%s\n", message)
return nil, errors.New(message)
}
printVerboseMessage("Integration Client created.")
return integrationCli, nil
}
func NewPolicyClient(c *gcli.Context) (*policy.Client, error) {
policyCli, cliErr := policy.NewClient(getConfigurations(c))
if cliErr != nil {
message := "Can not create the policy client. " + cliErr.Error()
fmt.Printf("%s\n", message)
return nil, errors.New(message)
}
printVerboseMessage("Policy Client created.")
return policyCli, nil
}
// EnableAction enables an integration/policy according to the --type parameter at Opsgenie.
func EnableAction(c *gcli.Context) {
val, _ := getVal("type", c)
switch val {
case "policy":
cli, err := NewPolicyClient(c)
if err != nil {
os.Exit(1)
}
req := policy.EnablePolicyRequest{}
if val, success := getVal("id", c); success {
req.Id = val
}
if val, success := getVal("teamId", c); success {
req.TeamId = val
}
if val, success := getVal("policyType", c); success {
req.Type = policy.PolicyType(val)
}
printVerboseMessage("Enable policy request prepared from flags, sending request to Opsgenie..")
_, err = cli.EnablePolicy(nil, &req)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Policy enabled successfuly\n")
case "integration":
cli, err := NewIntegrationClient(c)
if err != nil {
os.Exit(1)
}
req := integration.EnableIntegrationRequest{}
if val, success := getVal("id", c); success {
req.Id = val
}
printVerboseMessage("Enable integration request prepared from flags, sending request to Opsgenie..")
_, err = cli.Enable(nil, &req)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Integration enabled successfuly\n")
default:
fmt.Printf("Invalid type option %s, specify either integration or policy\n", val)
gcli.ShowCommandHelp(c, "enable")
os.Exit(1)
}
}
// DisableAction disables an integration/policy according to the --type parameter at Opsgenie.
func DisableAction(c *gcli.Context) {
val, _ := getVal("type", c)
switch val {
case "policy":
cli, err := NewPolicyClient(c)
if err != nil {
os.Exit(1)
}
req := policy.DisablePolicyRequest{}
if val, success := getVal("id", c); success {
req.Id = val
}
if val, success := getVal("teamId", c); success {
req.TeamId = val
}
if val, success := getVal("policyType", c); success {
req.Type = policy.PolicyType(val)
}
printVerboseMessage("Disable policy request prepared from flags, sending request to Opsgenie..")
_, err = cli.DisablePolicy(nil, &req)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Policy disabled successfuly\n")
case "integration":
cli, err := NewIntegrationClient(c)
if err != nil {
os.Exit(1)
}
req := integration.DisableIntegrationRequest{}
if val, success := getVal("id", c); success {
req.Id = val
}
printVerboseMessage("Disable integration request prepared from flags, sending request to Opsgenie..")
_, err = cli.Disable(nil, &req)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Integration disabled successfuly\n")
default:
fmt.Printf("Invalid type option %s, specify either integration or policy\n", val)
gcli.ShowCommandHelp(c, "disable")
os.Exit(1)
}
}