Skip to content

Commit 848dbe5

Browse files
Adding Integration V3 to go-sdk
1 parent f67f1e1 commit 848dbe5

File tree

6 files changed

+1180
-0
lines changed

6 files changed

+1180
-0
lines changed

client/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ func (cli *OpsGenieClient) Exec(ctx context.Context, request ApiRequest, result
478478
return err
479479
}
480480

481+
if response.StatusCode == 204 {
482+
return nil
483+
}
484+
481485
err = result.Parse(response, result)
482486
if err != nil {
483487
cli.Config.Logger.Errorf(err.Error())

integration_v3/integration.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package integration_v3
2+
3+
import (
4+
"context"
5+
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
6+
)
7+
8+
type Client struct {
9+
client *client.OpsGenieClient
10+
}
11+
12+
func NewClient(config *client.Config) (*Client, error) {
13+
opsgenieClient, err := client.NewOpsGenieClient(config)
14+
if err != nil {
15+
return nil, err
16+
}
17+
return &Client{opsgenieClient}, nil
18+
}
19+
20+
func (c *Client) Get(context context.Context, request *GetRequest) (*GetResult, error) {
21+
result := &GetResult{}
22+
err := c.client.Exec(context, request, result)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return result, nil
27+
}
28+
29+
func (c *Client) List(context context.Context, request *ListRequest) (*ListResult, error) {
30+
result := &ListResult{}
31+
err := c.client.Exec(context, request, result)
32+
if err != nil {
33+
return nil, err
34+
}
35+
return result, nil
36+
}
37+
38+
func (c *Client) Create(context context.Context, request *CreateIntegrationRequest) (*CreateIntegrationResult, error) {
39+
result := &CreateIntegrationResult{}
40+
err := c.client.Exec(context, request, result)
41+
if err != nil {
42+
return nil, err
43+
}
44+
return result, nil
45+
}
46+
47+
func (c *Client) Update(context context.Context, request *UpdateIntegrationRequest) (*UpdateResult, error) {
48+
result := &UpdateResult{}
49+
err := c.client.Exec(context, request, result)
50+
if err != nil {
51+
return nil, err
52+
}
53+
return result, nil
54+
}
55+
56+
func (c *Client) Delete(context context.Context, request *DeleteIntegrationRequest) (*BasicResult, error) {
57+
result := &BasicResult{}
58+
err := c.client.Exec(context, request, result)
59+
if err != nil {
60+
return nil, err
61+
}
62+
result.ResultString = "Integration Deleted Succesfully with status code 204"
63+
return result, nil
64+
}
65+
66+
func (c *Client) Authenticate(context context.Context, request *AuthenticateIntegrationRequest) (*AuthenticateResult, error) {
67+
result := &AuthenticateResult{}
68+
err := c.client.Exec(context, request, result)
69+
if err != nil {
70+
return nil, err
71+
}
72+
return result, nil
73+
}
74+
75+
func (c *Client) GetAction(context context.Context, request *GetIntegrationActionsRequest) (*Action, error) {
76+
result := &Action{}
77+
err := c.client.Exec(context, request, result)
78+
if err != nil {
79+
return nil, err
80+
}
81+
return result, nil
82+
}
83+
84+
func (c *Client) CreateAction(context context.Context, request *CreateIntegrationActionsRequest) (*CreateActionResult, error) {
85+
result := &CreateActionResult{}
86+
err := c.client.Exec(context, request, result)
87+
if err != nil {
88+
return nil, err
89+
}
90+
return result, nil
91+
}
92+
93+
func (c *Client) UpdateAction(context context.Context, request *UpdateIntegrationActionsRequest) (*UpdateActionResult, error) {
94+
result := &UpdateActionResult{}
95+
err := c.client.Exec(context, request, result)
96+
if err != nil {
97+
return nil, err
98+
}
99+
return result, nil
100+
}
101+
102+
func (c *Client) DeleteAction(context context.Context, request *DeleteIntegrationActionsRequest) (*BasicResult, error) {
103+
result := &BasicResult{}
104+
err := c.client.Exec(context, request, result)
105+
if err != nil {
106+
return nil, err
107+
}
108+
result.ResultString = "Integration Action Deleted Succesfully with status code 204"
109+
return result, nil
110+
}
111+
112+
func (c *Client) ReorderAction(context context.Context, request *ReOrderIntegrationActionsRequest) (*BasicResult, error) {
113+
result := &BasicResult{}
114+
err := c.client.Exec(context, request, result)
115+
if err != nil {
116+
return nil, err
117+
}
118+
result.ResultString = "Integration Action Reorder Succesfully with status code 204"
119+
return result, nil
120+
}
121+
122+
func (c *Client) ListIntegrationAction(context context.Context, request *ListIntegrationActionsRequest) (*ListIntegrationActionsResult, error) {
123+
result := &ListIntegrationActionsResult{}
124+
err := c.client.Exec(context, request, result)
125+
if err != nil {
126+
return nil, err
127+
}
128+
return result, nil
129+
}
130+
131+
func (c *Client) GetActionGroup(context context.Context, request *GetIntegrationActionsGroupRequest) (*GetIntegrationActionGroupResult, error) {
132+
result := &GetIntegrationActionGroupResult{}
133+
err := c.client.Exec(context, request, result)
134+
if err != nil {
135+
return nil, err
136+
}
137+
return result, nil
138+
}
139+
140+
func (c *Client) CreateActionGroup(context context.Context, request *CreateIntegrationActionGroupRequest) (*CreateIntegrationActionGroupsResult, error) {
141+
result := &CreateIntegrationActionGroupsResult{}
142+
err := c.client.Exec(context, request, result)
143+
if err != nil {
144+
return nil, err
145+
}
146+
return result, nil
147+
}
148+
149+
func (c *Client) UpdateActionGroup(context context.Context, request *UpdateIntegrationActionsRequest) (*UpdateIntegrationActionGroupsResult, error) {
150+
result := &UpdateIntegrationActionGroupsResult{}
151+
err := c.client.Exec(context, request, result)
152+
if err != nil {
153+
return nil, err
154+
}
155+
return result, nil
156+
}
157+
158+
func (c *Client) DeleteActionGroup(context context.Context, request *DeleteIntegrationActionGroupRequest) (*BasicResult, error) {
159+
result := &BasicResult{}
160+
err := c.client.Exec(context, request, result)
161+
if err != nil {
162+
return nil, err
163+
}
164+
return result, nil
165+
}
166+
167+
func (c *Client) ReorderActionGroup(context context.Context, request *ReOrderIntegrationActionsRequest) (*BasicResult, error) {
168+
result := &BasicResult{}
169+
err := c.client.Exec(context, request, result)
170+
if err != nil {
171+
return nil, err
172+
}
173+
return result, nil
174+
}
175+
176+
func (c *Client) ListIntegrationActionGroup(context context.Context, request *ListIntegrationActionsGroupRequest) (*ListIntegrationActionGroupsResult, error) {
177+
result := &ListIntegrationActionGroupsResult{}
178+
err := c.client.Exec(context, request, result)
179+
if err != nil {
180+
return nil, err
181+
}
182+
return result, nil
183+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package integration_v3
2+
3+
import (
4+
"github.com/opsgenie/opsgenie-go-sdk-v2/og"
5+
"github.com/pkg/errors"
6+
)
7+
8+
const (
9+
User ResponderType = "user"
10+
Team ResponderType = "team"
11+
Escalation ResponderType = "escalation"
12+
Schedule ResponderType = "schedule"
13+
14+
Create ActionType = "create"
15+
Close ActionType = "close"
16+
Acknowledge ActionType = "acknowledge"
17+
AddNote ActionType = "AddNote"
18+
Ignore ActionType = "ignore"
19+
)
20+
21+
// Common Struct //
22+
23+
type ResponderType string
24+
type ActionType string
25+
26+
type ParentIntegration struct {
27+
Id string `json:"id"`
28+
Name string `json:"name"`
29+
Enabled bool `json:"enabled"`
30+
Order float32 `json:"order"`
31+
Direction string `json:"direction"`
32+
Domain string `json:"domain"`
33+
}
34+
35+
type ActionMapping struct {
36+
Type ActionType `json:"type,omitempty"`
37+
Parameter string `json:"parameter,omitempty"`
38+
}
39+
40+
type Filter struct {
41+
ConditionMatchType og.ConditionMatchType `json:"conditionMatchType,omitempty"`
42+
Conditions []og.Condition `json:"conditions,omitempty"`
43+
}
44+
45+
type FieldMapping struct {
46+
User string `json:"user,omitempty"`
47+
Note string `json:"note,omitempty"`
48+
Alias string `json:"alias,omitempty"`
49+
Source string `json:"source,omitempty"`
50+
Message string `json:"message,omitempty"`
51+
Description string `json:"description,omitempty"`
52+
Entity string `json:"entity,omitempty"`
53+
AlertActions []string `json:"alertActions,omitempty"`
54+
Responders []Responder `json:"responders,omitempty"`
55+
Tags []string `json:"tags,omitempty"`
56+
ExtraProperties map[string]string `json:"extraProperties,omitempty"`
57+
}
58+
59+
type Responder struct {
60+
Type ResponderType `json:"type, omitempty"`
61+
Name string `json:"name,omitempty"`
62+
Id string `json:"id,omitempty"`
63+
Username string `json:"username, omitempty"`
64+
}
65+
66+
type FilterV3 struct {
67+
ConditionMatchType og.ConditionMatchType `json:"conditionMatchType,omitempty"`
68+
Conditions []og.Condition `json:"conditions,omitempty"`
69+
}
70+
71+
type ActionGroup struct {
72+
Id string `json:"id,omitempty"`
73+
Type string `json:"type,omitempty"`
74+
Enabled bool `json:"enabled,omitempty"`
75+
Order float32 `json:"order,omitempty"`
76+
Direction string `json:"direction,omitempty"`
77+
Domain string `json:"domain,omitempty"`
78+
}
79+
80+
type UpdateActionGroup struct {
81+
AddedActions []Action `json:"added,omitempty"`
82+
UpdatedActions []Action `json:"updated,omitempty"`
83+
DeletedActions []string `json:"deleted,omitempty"`
84+
}
85+
86+
func validateActionType(actionType ActionType) error {
87+
if actionType == "" {
88+
return nil
89+
}
90+
91+
switch actionType {
92+
case Create, Close, Acknowledge, AddNote, Ignore:
93+
return nil
94+
}
95+
return errors.New("Action type should be one of these: " +
96+
"'Create','Close','Acknowledge','AddNote','Ignore'")
97+
}
98+
99+
func validateConditionMatchType(matchType og.ConditionMatchType) error {
100+
switch matchType {
101+
case og.MatchAll, og.MatchAllConditions, og.MatchAnyCondition, "":
102+
return nil
103+
}
104+
return errors.New("Action type should be one of these: " +
105+
"'MatchAll','MatchAllConditions','MatchAnyCondition'")
106+
}
107+
108+
func validateResponders(responders []Responder) error {
109+
for _, responder := range responders {
110+
if responder.Type == "" {
111+
return errors.New("Responder type cannot be empty.")
112+
}
113+
if !(responder.Type == User || responder.Type == Team || responder.Type == Schedule || responder.Type == Escalation) {
114+
return errors.New("Responder type should be one of these: 'User', 'Team', 'Schedule', 'Escalation'")
115+
}
116+
if responder.Type == User && responder.Username == "" && responder.Id == "" {
117+
return errors.New("For responder type user either username or id must be provided.")
118+
}
119+
if responder.Type == Team && responder.Name == "" && responder.Id == "" {
120+
return errors.New("For responder type team either team name or id must be provided.")
121+
}
122+
if responder.Type == Schedule && responder.Name == "" && responder.Id == "" {
123+
return errors.New("For responder type schedule either schedule name or id must be provided.")
124+
}
125+
if responder.Type == Escalation && responder.Name == "" && responder.Id == "" {
126+
return errors.New("For responder type escalation either escalation name or id must be provided.")
127+
}
128+
}
129+
return nil
130+
}
131+
132+
func validateActions(actions []Action) error {
133+
if actions == nil {
134+
return nil
135+
}
136+
137+
for _, r := range actions {
138+
err := validateActionType(r.Type)
139+
140+
if r.Filter != nil {
141+
err = validateConditionMatchType(r.Filter.ConditionMatchType)
142+
if err != nil {
143+
return err
144+
}
145+
err = og.ValidateFilter(*r.Filter)
146+
if err != nil {
147+
return err
148+
}
149+
}
150+
}
151+
return nil
152+
}

0 commit comments

Comments
 (0)