|
| 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