Skip to content

Commit

Permalink
Adding Integration V3 to go-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar-mudaiya committed Oct 6, 2021
1 parent f67f1e1 commit 848dbe5
Show file tree
Hide file tree
Showing 6 changed files with 1,180 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ func (cli *OpsGenieClient) Exec(ctx context.Context, request ApiRequest, result
return err
}

if response.StatusCode == 204 {
return nil
}

err = result.Parse(response, result)
if err != nil {
cli.Config.Logger.Errorf(err.Error())
Expand Down
183 changes: 183 additions & 0 deletions integration_v3/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package integration_v3

import (
"context"
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
)

type Client struct {
client *client.OpsGenieClient
}

func NewClient(config *client.Config) (*Client, error) {
opsgenieClient, err := client.NewOpsGenieClient(config)
if err != nil {
return nil, err
}
return &Client{opsgenieClient}, nil
}

func (c *Client) Get(context context.Context, request *GetRequest) (*GetResult, error) {
result := &GetResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) List(context context.Context, request *ListRequest) (*ListResult, error) {
result := &ListResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) Create(context context.Context, request *CreateIntegrationRequest) (*CreateIntegrationResult, error) {
result := &CreateIntegrationResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) Update(context context.Context, request *UpdateIntegrationRequest) (*UpdateResult, error) {
result := &UpdateResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) Delete(context context.Context, request *DeleteIntegrationRequest) (*BasicResult, error) {
result := &BasicResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
result.ResultString = "Integration Deleted Succesfully with status code 204"
return result, nil
}

func (c *Client) Authenticate(context context.Context, request *AuthenticateIntegrationRequest) (*AuthenticateResult, error) {
result := &AuthenticateResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) GetAction(context context.Context, request *GetIntegrationActionsRequest) (*Action, error) {
result := &Action{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) CreateAction(context context.Context, request *CreateIntegrationActionsRequest) (*CreateActionResult, error) {
result := &CreateActionResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) UpdateAction(context context.Context, request *UpdateIntegrationActionsRequest) (*UpdateActionResult, error) {
result := &UpdateActionResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) DeleteAction(context context.Context, request *DeleteIntegrationActionsRequest) (*BasicResult, error) {
result := &BasicResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
result.ResultString = "Integration Action Deleted Succesfully with status code 204"
return result, nil
}

func (c *Client) ReorderAction(context context.Context, request *ReOrderIntegrationActionsRequest) (*BasicResult, error) {
result := &BasicResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
result.ResultString = "Integration Action Reorder Succesfully with status code 204"
return result, nil
}

func (c *Client) ListIntegrationAction(context context.Context, request *ListIntegrationActionsRequest) (*ListIntegrationActionsResult, error) {
result := &ListIntegrationActionsResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) GetActionGroup(context context.Context, request *GetIntegrationActionsGroupRequest) (*GetIntegrationActionGroupResult, error) {
result := &GetIntegrationActionGroupResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) CreateActionGroup(context context.Context, request *CreateIntegrationActionGroupRequest) (*CreateIntegrationActionGroupsResult, error) {
result := &CreateIntegrationActionGroupsResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) UpdateActionGroup(context context.Context, request *UpdateIntegrationActionsRequest) (*UpdateIntegrationActionGroupsResult, error) {
result := &UpdateIntegrationActionGroupsResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) DeleteActionGroup(context context.Context, request *DeleteIntegrationActionGroupRequest) (*BasicResult, error) {
result := &BasicResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) ReorderActionGroup(context context.Context, request *ReOrderIntegrationActionsRequest) (*BasicResult, error) {
result := &BasicResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}

func (c *Client) ListIntegrationActionGroup(context context.Context, request *ListIntegrationActionsGroupRequest) (*ListIntegrationActionGroupsResult, error) {
result := &ListIntegrationActionGroupsResult{}
err := c.client.Exec(context, request, result)
if err != nil {
return nil, err
}
return result, nil
}
152 changes: 152 additions & 0 deletions integration_v3/integration_consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package integration_v3

import (
"github.com/opsgenie/opsgenie-go-sdk-v2/og"
"github.com/pkg/errors"
)

const (
User ResponderType = "user"
Team ResponderType = "team"
Escalation ResponderType = "escalation"
Schedule ResponderType = "schedule"

Create ActionType = "create"
Close ActionType = "close"
Acknowledge ActionType = "acknowledge"
AddNote ActionType = "AddNote"
Ignore ActionType = "ignore"
)

// Common Struct //

type ResponderType string
type ActionType string

type ParentIntegration struct {
Id string `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Order float32 `json:"order"`
Direction string `json:"direction"`
Domain string `json:"domain"`
}

type ActionMapping struct {
Type ActionType `json:"type,omitempty"`
Parameter string `json:"parameter,omitempty"`
}

type Filter struct {
ConditionMatchType og.ConditionMatchType `json:"conditionMatchType,omitempty"`
Conditions []og.Condition `json:"conditions,omitempty"`
}

type FieldMapping struct {
User string `json:"user,omitempty"`
Note string `json:"note,omitempty"`
Alias string `json:"alias,omitempty"`
Source string `json:"source,omitempty"`
Message string `json:"message,omitempty"`
Description string `json:"description,omitempty"`
Entity string `json:"entity,omitempty"`
AlertActions []string `json:"alertActions,omitempty"`
Responders []Responder `json:"responders,omitempty"`
Tags []string `json:"tags,omitempty"`
ExtraProperties map[string]string `json:"extraProperties,omitempty"`
}

type Responder struct {
Type ResponderType `json:"type, omitempty"`
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
Username string `json:"username, omitempty"`
}

type FilterV3 struct {
ConditionMatchType og.ConditionMatchType `json:"conditionMatchType,omitempty"`
Conditions []og.Condition `json:"conditions,omitempty"`
}

type ActionGroup struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Order float32 `json:"order,omitempty"`
Direction string `json:"direction,omitempty"`
Domain string `json:"domain,omitempty"`
}

type UpdateActionGroup struct {
AddedActions []Action `json:"added,omitempty"`
UpdatedActions []Action `json:"updated,omitempty"`
DeletedActions []string `json:"deleted,omitempty"`
}

func validateActionType(actionType ActionType) error {
if actionType == "" {
return nil
}

switch actionType {
case Create, Close, Acknowledge, AddNote, Ignore:
return nil
}
return errors.New("Action type should be one of these: " +
"'Create','Close','Acknowledge','AddNote','Ignore'")
}

func validateConditionMatchType(matchType og.ConditionMatchType) error {
switch matchType {
case og.MatchAll, og.MatchAllConditions, og.MatchAnyCondition, "":
return nil
}
return errors.New("Action type should be one of these: " +
"'MatchAll','MatchAllConditions','MatchAnyCondition'")
}

func validateResponders(responders []Responder) error {
for _, responder := range responders {
if responder.Type == "" {
return errors.New("Responder type cannot be empty.")
}
if !(responder.Type == User || responder.Type == Team || responder.Type == Schedule || responder.Type == Escalation) {
return errors.New("Responder type should be one of these: 'User', 'Team', 'Schedule', 'Escalation'")
}
if responder.Type == User && responder.Username == "" && responder.Id == "" {
return errors.New("For responder type user either username or id must be provided.")
}
if responder.Type == Team && responder.Name == "" && responder.Id == "" {
return errors.New("For responder type team either team name or id must be provided.")
}
if responder.Type == Schedule && responder.Name == "" && responder.Id == "" {
return errors.New("For responder type schedule either schedule name or id must be provided.")
}
if responder.Type == Escalation && responder.Name == "" && responder.Id == "" {
return errors.New("For responder type escalation either escalation name or id must be provided.")
}
}
return nil
}

func validateActions(actions []Action) error {
if actions == nil {
return nil
}

for _, r := range actions {
err := validateActionType(r.Type)

if r.Filter != nil {
err = validateConditionMatchType(r.Filter.ConditionMatchType)
if err != nil {
return err
}
err = og.ValidateFilter(*r.Filter)
if err != nil {
return err
}
}
}
return nil
}
Loading

0 comments on commit 848dbe5

Please sign in to comment.