From 848dbe545b79a36b8f004244bbb9e90a0fe176ad Mon Sep 17 00:00:00 2001 From: pmudaiya Date: Wed, 29 Sep 2021 09:44:25 +0530 Subject: [PATCH] Adding Integration V3 to go-sdk --- client/client.go | 4 + integration_v3/integration.go | 183 ++++++++++ integration_v3/integration_consts.go | 152 ++++++++ integration_v3/integration_test.go | 231 ++++++++++++ integration_v3/request.go | 502 +++++++++++++++++++++++++++ integration_v3/result.go | 108 ++++++ 6 files changed, 1180 insertions(+) create mode 100644 integration_v3/integration.go create mode 100644 integration_v3/integration_consts.go create mode 100644 integration_v3/integration_test.go create mode 100644 integration_v3/request.go create mode 100644 integration_v3/result.go diff --git a/client/client.go b/client/client.go index 4f61d1e..c79982b 100644 --- a/client/client.go +++ b/client/client.go @@ -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()) diff --git a/integration_v3/integration.go b/integration_v3/integration.go new file mode 100644 index 0000000..bf67733 --- /dev/null +++ b/integration_v3/integration.go @@ -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 +} diff --git a/integration_v3/integration_consts.go b/integration_v3/integration_consts.go new file mode 100644 index 0000000..f047510 --- /dev/null +++ b/integration_v3/integration_consts.go @@ -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 +} diff --git a/integration_v3/integration_test.go b/integration_v3/integration_test.go new file mode 100644 index 0000000..48165c8 --- /dev/null +++ b/integration_v3/integration_test.go @@ -0,0 +1,231 @@ +package integration_v3 + +import ( + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetRequest_Validate(t *testing.T) { + request := &GetRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID cannot be blank.").Error()) + + request.Id = "6b0f1d04-7911-4369-b61f-694492034558" + err = request.Validate() + assert.Nil(t, err) +} + +func TestAPIBasedIntegrationRequest_Validate(t *testing.T) { + request := &CreateIntegrationRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Name and Type fields cannot be empty.").Error()) + + request.Type = "Type" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name and Type fields cannot be empty.").Error()) + + request.Name = "Alerting Tool" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionCreateRequest_Validate(t *testing.T) { + request := &CreateIntegrationActionsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name, Type, Direction and Domain fields cannot be empty.").Error()) + + request.Name = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name, Type, Direction and Domain fields cannot be empty.").Error()) + + request.Type = Create + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name, Type, Direction and Domain fields cannot be empty.").Error()) + + request.Direction = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name, Type, Direction and Domain fields cannot be empty.").Error()) + + request.Domain = "Alerting Tool" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionUpdateRequest_Validate(t *testing.T) { + request := &UpdateIntegrationActionsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Action ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Action ID cannot be blank.").Error()) + + request.ActionId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name and Type fields cannot be empty.").Error()) + + request.Type = Create + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Name and Type fields cannot be empty.").Error()) + + request.Name = "Alerting Tool" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionDeleteRequest_Validate(t *testing.T) { + request := &DeleteIntegrationActionsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Action ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Action ID cannot be blank.").Error()) + + request.ActionId = "Alerting Tool" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionReorderRequest_Validate(t *testing.T) { + request := &ReOrderIntegrationActionsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Action ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Action ID cannot be blank.").Error()) + + request.ActionId = "Alerting Tool" + err = request.Validate() + assert.Nil(t, err) +} + +func TestDeleteIntegrationRequest_Validate(t *testing.T) { + request := &DeleteIntegrationRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID cannot be blank.").Error()) + + request.Id = "6b0f1d04" + err = request.Validate() + assert.Nil(t, err) +} + +func TestGetIntegrationActionRequest_Validate(t *testing.T) { + request := &GetIntegrationActionsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration Id cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Action Id cannot be blank.").Error()) + + request.ActionId = "6b0f1d04" + err = request.Validate() + assert.Nil(t, err) +} + +func TestListIntegrationActionRequest_Validate(t *testing.T) { + request := &ListIntegrationActionsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID cannot be blank.").Error()) + + request.IntegrationId = "6b0f1d04" + err = request.Validate() + assert.Nil(t, err) +} + +func TestAuthenticateIntegrationRequest_Validate(t *testing.T) { + request := &AuthenticateIntegrationRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Type cannot be blank.").Error()) + + request.Type = "CemType" + err = request.Validate() + assert.Nil(t, err) +} + +func TestGetIntegrationActionGroupRequest_Validate(t *testing.T) { + request := &GetIntegrationActionsGroupRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.GroupId = "6b0f1d04" + err = request.Validate() + assert.Nil(t, err) +} + +func TestListIntegrationActionGroupRequest_Validate(t *testing.T) { + request := &ListIntegrationActionsGroupRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID cannot be blank.").Error()) + + request.IntegrationId = "6b0f1d04" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionCreateGroupRequest_Validate(t *testing.T) { + request := &CreateIntegrationActionGroupRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Group Name cannot be blank.").Error()) + + request.Name = "Alerting Tool" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionGroupUpdateRequest_Validate(t *testing.T) { + request := &UpdateIntegrationActionGroupRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.GroupId = "123" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionGroupDeleteRequest_Validate(t *testing.T) { + request := &DeleteIntegrationActionGroupRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.GroupId = "123" + err = request.Validate() + assert.Nil(t, err) +} + +func TestIntegrationActionGroupReorderRequest_Validate(t *testing.T) { + request := &ReOrderIntegrationActionGroupRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.IntegrationId = "123" + err = request.Validate() + assert.Equal(t, err.Error(), errors.New("Integration ID and Group ID cannot be blank.").Error()) + + request.GroupId = "123" + err = request.Validate() + assert.Nil(t, err) +} \ No newline at end of file diff --git a/integration_v3/request.go b/integration_v3/request.go new file mode 100644 index 0000000..26093e7 --- /dev/null +++ b/integration_v3/request.go @@ -0,0 +1,502 @@ +package integration_v3 + +import ( + "github.com/opsgenie/opsgenie-go-sdk-v2/client" + "github.com/opsgenie/opsgenie-go-sdk-v2/og" + "github.com/pkg/errors" + "net/http" +) + +type GetRequest struct { + client.BaseRequest + Id string +} + +func (r *GetRequest) Validate() error { + if r.Id == "" { + return errors.New("Integration ID cannot be blank.") + } + return nil +} + +func (r *GetRequest) ResourcePath() string { + return "/v3/integrations/" + r.Id +} + +func (r *GetRequest) Method() string { + return http.MethodGet +} + +type ListRequest struct { + client.BaseRequest + TeamId string + IntegrationType string +} + +func (r *ListRequest) Validate() error { + return nil +} + +func (r *ListRequest) ResourcePath() string { + return "/v3/integrations" +} + +func (r *ListRequest) RequestParams() map[string]string { + + params := make(map[string]string) + + if r.TeamId != "" { + params["teamId"] = r.TeamId + } + + if r.IntegrationType != "" { + params["type"] = r.IntegrationType + } + + return params +} + +func (r *ListRequest) Method() string { + return http.MethodGet +} + +type CreateIntegrationRequest struct { + client.BaseRequest + Name string `json:"name"` + Type string `json:"type"` + TeamId string `json:"teamId"` + Description string `json:"description"` + Enabled bool `json:"enabled"` + TypeSpecificProperties map[string]string `json:"typeSpecificProperties,omitempty"` +} + +func (r *CreateIntegrationRequest) Validate() error { + if r.Name == "" || r.Type == "" { + return errors.New("Name and Type fields cannot be empty.") + } + return nil +} + +func (r *CreateIntegrationRequest) ResourcePath() string { + return "/v3/integrations" +} + +func (r *CreateIntegrationRequest) Method() string { + return http.MethodPost +} + +type UpdateIntegrationRequest struct { + client.BaseRequest + Id string + Name string `json:"name"` + TeamId string `json:"teamId"` + Description string `json:"description"` + Enabled bool `json:"enabled"` + TypeSpecificProperties map[string]string `json:"typeSpecificProperties,omitempty"` +} + +func (r *UpdateIntegrationRequest) Validate() error { + + if r.Id == "" { + return errors.New("Integration ID cannot be blank.") + } + return nil +} + +func (r *UpdateIntegrationRequest) ResourcePath() string { + return "/v3/integrations/" + r.Id +} + +func (r *UpdateIntegrationRequest) Method() string { + return http.MethodPatch +} + +type DeleteIntegrationRequest struct { + client.BaseRequest + Id string +} + +func (r *DeleteIntegrationRequest) Validate() error { + if r.Id == "" { + return errors.New("Integration ID cannot be blank.") + } + return nil +} + +func (r *DeleteIntegrationRequest) ResourcePath() string { + return "/v3/integrations/" + r.Id +} + +func (r *DeleteIntegrationRequest) Method() string { + return http.MethodDelete +} + +type AuthenticateIntegrationRequest struct { + client.BaseRequest + Type string `json:"type"` +} + +func (r *AuthenticateIntegrationRequest) Validate() error { + if r.Type == "" { + return errors.New("Type cannot be blank.") + } + return nil +} + +func (r *AuthenticateIntegrationRequest) ResourcePath() string { + return "/v3/integrations/authenticate" +} + +func (r *AuthenticateIntegrationRequest) Method() string { + return http.MethodPost +} + +type GetIntegrationActionsRequest struct { + client.BaseRequest + IntegrationId string + ActionId string +} + +func (r *GetIntegrationActionsRequest) Validate() error { + if r.IntegrationId == "" { + return errors.New("Integration Id cannot be blank.") + } + if r.ActionId == "" { + return errors.New("Action Id cannot be blank.") + } + return nil +} + +func (r *GetIntegrationActionsRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/actions/" + r.ActionId +} + +func (r *GetIntegrationActionsRequest) Method() string { + return http.MethodGet +} + +type CreateIntegrationActionsRequest struct { + client.BaseRequest + IntegrationId string + Type ActionType `json:"type"` + Name string `json:"name"` + Direction string `json:"direction"` + Domain string `json:"domain"` + ActionGroupId string `json:"actionGroupId,omitempty"` + ActionMapping ActionMapping `json:"actionMapping,omitempty"` + Filter *og.Filter `json:"filter,omitempty"` + Mapping FieldMapping `json:"fieldMappings,omitempty"` + TypeSpecificProperties map[string]string `json:"typeSpecificProperties,omitempty"` + Enabled *bool `json:"enabled,omitempty"` +} + +func (r *CreateIntegrationActionsRequest) Validate() error { + if r.IntegrationId == "" { + return errors.New("Integration ID cannot be blank.") + } + if r.Name == "" || r.Type == "" || r.Direction == "" || r.Domain == "" { + return errors.New("Name, Type, Direction and Domain fields cannot be empty.") + } + + err := validateActionType(r.Type) + if err != nil { + return err + } + if r.Filter != nil { + err = validateConditionMatchType(r.Filter.ConditionMatchType) + if err != nil { + return err + } + err = og.ValidateFilter(og.Filter(*r.Filter)) + if err != nil { + return err + } + } + + err2 := validateResponders(r.Mapping.Responders) + if err2 != nil { + return err2 + } + return nil +} + +func (r *CreateIntegrationActionsRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/actions" +} + +func (r *CreateIntegrationActionsRequest) Method() string { + return http.MethodPost +} + +type UpdateIntegrationActionsRequest struct { + client.BaseRequest + IntegrationId string + ActionId string + Type ActionType `json:"type"` + Name string `json:"name"` + ActionMapping ActionMapping `json:"actionMapping,omitempty"` + Filter *og.Filter `json:"filter,omitempty"` + Mapping FieldMapping `json:"fieldMappings,omitempty"` + TypeSpecificProperties map[string]string `json:"typeSpecificProperties,omitempty"` + Enabled *bool `json:"enabled,omitempty"` +} + +func (r *UpdateIntegrationActionsRequest) Validate() error { + if r.IntegrationId == "" || r.ActionId == "" { + return errors.New("Integration ID and Action ID cannot be blank.") + } + if r.Name == "" || r.Type == "" { + return errors.New("Name and Type fields cannot be empty.") + } + + err2 := validateResponders(r.Mapping.Responders) + if err2 != nil { + return err2 + } + return nil +} + +func (r *UpdateIntegrationActionsRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/actions/" + r.ActionId +} + +func (r *UpdateIntegrationActionsRequest) Method() string { + return http.MethodPatch +} + +type DeleteIntegrationActionsRequest struct { + client.BaseRequest + IntegrationId string + ActionId string +} + +func (r *DeleteIntegrationActionsRequest) Validate() error { + if r.IntegrationId == "" || r.ActionId == "" { + return errors.New("Integration ID and Action ID cannot be blank.") + } + return nil +} + +func (r *DeleteIntegrationActionsRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/actions/" + r.ActionId +} + +func (r *DeleteIntegrationActionsRequest) Method() string { + return http.MethodDelete +} + +type ReOrderIntegrationActionsRequest struct { + client.BaseRequest + IntegrationId string + ActionId string + SuccessorId string `json:"successorId,omitempty"` +} + +func (r *ReOrderIntegrationActionsRequest) Validate() error { + if r.IntegrationId == "" || r.ActionId == "" { + return errors.New("Integration ID and Action ID cannot be blank.") + } + return nil +} + +func (r *ReOrderIntegrationActionsRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/actions/" + r.ActionId + "/order" +} + +func (r *ReOrderIntegrationActionsRequest) Method() string { + return http.MethodPatch +} + +type ListIntegrationActionsRequest struct { + client.BaseRequest + IntegrationId string + Direction string + IntegrationType string + Domain string +} + +func (r *ListIntegrationActionsRequest) Validate() error { + if r.IntegrationId == "" { + return errors.New("Integration ID cannot be blank.") + } + return nil +} + +func (r *ListIntegrationActionsRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/actions" +} + +func (r *ListIntegrationActionsRequest) RequestParams() map[string]string { + + params := make(map[string]string) + + if r.Direction != "" { + params["direction"] = r.Direction + } + + if r.IntegrationType != "" { + params["integrationType"] = r.IntegrationType + } + + if r.Domain != "" { + params["domain"] = r.Domain + } + return params +} + +func (r *ListIntegrationActionsRequest) Method() string { + return http.MethodGet +} + +type ListIntegrationActionsGroupRequest struct { + client.BaseRequest + IntegrationId string + Type string +} + +func (r *ListIntegrationActionsGroupRequest) Validate() error { + if r.IntegrationId == "" { + return errors.New("Integration ID cannot be blank.") + } + return nil +} + +func (r *ListIntegrationActionsGroupRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/action-groups" +} + +func (r *ListIntegrationActionsGroupRequest) RequestParams() map[string]string { + + params := make(map[string]string) + + if r.Type != "" { + params["type"] = r.Type + } + + return params +} + +func (r *ListIntegrationActionsGroupRequest) Method() string { + return http.MethodGet +} + +type GetIntegrationActionsGroupRequest struct { + client.BaseRequest + IntegrationId string + GroupId string +} + +func (r *GetIntegrationActionsGroupRequest) Validate() error { + if r.IntegrationId == "" || r.GroupId == "" { + return errors.New("Integration ID and Group ID cannot be blank.") + } + return nil +} + +func (r *GetIntegrationActionsGroupRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/action-groups/" + r.GroupId +} + +func (r *GetIntegrationActionsGroupRequest) Method() string { + return http.MethodGet +} + +type CreateIntegrationActionGroupRequest struct { + client.BaseRequest + IntegrationId string + Name string `json:"name"` + Type string `json:"type"` + Enabled bool `json:"enabled"` + Order float32 `json:"order"` + Domain string `json:"domain"` + ActionData []Action `json:"actions"` +} + +func (r *CreateIntegrationActionGroupRequest) Validate() error { + if r.IntegrationId == "" { + return errors.New("Integration ID cannot be blank.") + } + + if r.Name == "" { + return errors.New("Group Name cannot be blank.") + } + return nil +} + +func (r *CreateIntegrationActionGroupRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/action-groups" +} + +func (r *CreateIntegrationActionGroupRequest) Method() string { + return http.MethodPost +} + +type UpdateIntegrationActionGroupRequest struct { + client.BaseRequest + IntegrationId string + GroupId string + Name string `json:"id"` + Type string `json:"type"` + Enabled bool `json:"enabled"` + Order float32 `json:"order"` + Domain string `json:"domain"` + ActionResultData UpdateActionGroup `json:"actions"` +} + +func (r *UpdateIntegrationActionGroupRequest) Validate() error { + if r.IntegrationId == "" || r.GroupId == "" { + return errors.New("Integration ID and Group ID cannot be blank.") + } + return nil +} + +func (r *UpdateIntegrationActionGroupRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/action-groups/" + r.GroupId +} + +func (r *UpdateIntegrationActionGroupRequest) Method() string { + return http.MethodPatch +} + +type DeleteIntegrationActionGroupRequest struct { + client.BaseRequest + IntegrationId string + GroupId string +} + +func (r *DeleteIntegrationActionGroupRequest) Validate() error { + if r.IntegrationId == "" || r.GroupId == "" { + return errors.New("Integration ID and Group ID cannot be blank.") + } + return nil +} + +func (r *DeleteIntegrationActionGroupRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/action-groups/" + r.GroupId +} + +func (r *DeleteIntegrationActionGroupRequest) Method() string { + return http.MethodDelete +} + +type ReOrderIntegrationActionGroupRequest struct { + client.BaseRequest + IntegrationId string + GroupId string + SuccessorId string `json:"successorId,omitempty"` +} + +func (r *ReOrderIntegrationActionGroupRequest) Validate() error { + if r.IntegrationId == "" || r.GroupId == "" { + return errors.New("Integration ID and Group ID cannot be blank.") + } + return nil +} + +func (r *ReOrderIntegrationActionGroupRequest) ResourcePath() string { + return "/v3/integrations/" + r.IntegrationId + "/action-groups/" + r.GroupId + "/reorder" +} + +func (r *ReOrderIntegrationActionGroupRequest) Method() string { + return http.MethodPatch +} diff --git a/integration_v3/result.go b/integration_v3/result.go new file mode 100644 index 0000000..f7e6b47 --- /dev/null +++ b/integration_v3/result.go @@ -0,0 +1,108 @@ +package integration_v3 + +import ( + "github.com/opsgenie/opsgenie-go-sdk-v2/client" + "github.com/opsgenie/opsgenie-go-sdk-v2/og" +) + +type ListResult struct { + client.ResultMetadata + Integrations []GenericFields `json:"data"` +} + +type GenericFields struct { + Id string `json:"id"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + Type string `json:"type"` + TeamId string `json:"teamId"` + Version string `json:"version"` +} + +type GetResult struct { + client.ResultMetadata + Data map[string]interface{} `json:"data"` +} + +type CreateIntegrationResult struct { + client.ResultMetadata + GenericFields + ApiKey string `json:"apiKey"` +} + +type UpdateResult struct { + client.ResultMetadata + Data map[string]interface{} `json:"data"` +} + +type BasicResult struct { + client.ResultMetadata + ResultString string +} + +type AuthenticateResult struct { + client.ResultMetadata + Result string `json:"result"` +} + +type Action struct { + client.ResultMetadata + Type ActionType `json:"type"` + Id string `json:"id"` + Name string `json:"name,omitempty"` + Enabled bool `json:"enabled,omitempty"` + Order float32 `json:"order,omitempty"` + Direction string `json:"direction,omitempty"` + Domain string `json:"domain,omitempty"` + ActionGroupId string `json:"actionGroupId,omitempty"` + ActionMapping ActionMapping `json:"actionMapping,omitempty"` + Filter *og.Filter `json:"filter,omitempty"` + Mapping FieldMapping `json:"fieldMappings,omitempty"` + TypeSpecificProperties map[string]interface{} `json:"typeSpecificProperties,omitempty"` +} + +type CreateActionResult struct { + client.ResultMetadata + Id string `json:"id"` + Name string `json:"name"` + Enabled bool `json:"enabled"` + Order float32 `json:"order"` + Direction string `json:"direction"` + ActionGroupId string `json:"actionGroupId"` + ActionMapping ActionMapping `json:"actionMapping"` + Domain string `json:"domain,omitempty"` +} + +type UpdateActionResult struct { + client.ResultMetadata + Parent ParentIntegration `json:"data"` + ActionGroupId string `json:"actionGroupId"` + ActionMapping ActionMapping `json:"actionMapping"` + Domain string `json:"domain,omitempty"` +} + +type ListIntegrationActionsResult struct { + client.ResultMetadata + Incoming []Action `json:"incoming,omitempty"` + Outgoing []Action `json:"outgoing,omitempty"` +} + +type ListIntegrationActionGroupsResult struct { + client.ResultMetadata + ActionGroupsData []ActionGroup `json:"data"` +} + +type GetIntegrationActionGroupResult struct { + client.ResultMetadata + ActionGroupData ActionGroup `json:"data"` + ActionResultData []Action `json:"actions"` +} +type CreateIntegrationActionGroupsResult struct { + client.ResultMetadata + ActionGroupsData ActionGroup `json:"data"` +} + +type UpdateIntegrationActionGroupsResult struct { + client.ResultMetadata + ActionGroupsData ActionGroup `json:"data"` +}