Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
add opsgenie integration
Browse files Browse the repository at this point in the history
  • Loading branch information
betorvs committed Jul 14, 2021
1 parent bfdeb29 commit 242deb6
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
8 changes: 8 additions & 0 deletions domain/opsgenie_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package domain
import (
"github.com/betorvs/sensubot/appcontext"
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
"github.com/opsgenie/opsgenie-go-sdk-v2/incident"
"github.com/opsgenie/opsgenie-go-sdk-v2/schedule"
)

// OpsgenieRepository interface
Expand All @@ -12,6 +14,12 @@ type OpsgenieRepository interface {
GetAlert(tinyID string) (*alert.GetAlertResult, error)
// ListAlert by query string and return (*alert.ListAlertResult, error
ListAlert(query string) (*alert.ListAlertResult, error)
// ListIncident by query string and return *incident.ListResult, error
ListIncident(query string) (*incident.ListResult, error)
// GetOnCall(team string) (*schedule.GetOnCallsResult, error)
GetOnCall(team string) (*schedule.GetOnCallsResult, error)
// ListSchedules() (*schedule.ListResult, error)
ListSchedules() (*schedule.ListResult, error)
}

// GetOpsgenieRepository func return OpsgenieRepository interface
Expand Down
84 changes: 80 additions & 4 deletions gateway/integrations/opsgenieclient/opsgenie_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
"github.com/betorvs/sensubot/config"
"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
"github.com/opsgenie/opsgenie-go-sdk-v2/incident"
"github.com/opsgenie/opsgenie-go-sdk-v2/schedule"
)

// OpsgenieRepository struct
type OpsgenieRepository struct {
Alert *alert.Client
Alert *alert.Client
Incident *incident.Client
Schedule *schedule.Client
}

// switchOpsgenieRegion func
Expand All @@ -40,7 +44,7 @@ func (repo OpsgenieRepository) GetAlert(tinyID string) (*alert.GetAlertResult, e
IdentifierValue: tinyID,
})
if err != nil {
return getResult, nil
return getResult, err
}
return getResult, nil
}
Expand All @@ -55,17 +59,89 @@ func (repo OpsgenieRepository) ListAlert(query string) (*alert.ListAlertResult,
Query: query,
})
if err != nil {
return listResult, nil
return listResult, err
}
return listResult, nil
}

// ListIncident func
func (repo OpsgenieRepository) ListIncident(query string) (*incident.ListResult, error) {
logLocal := config.GetLogger()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
logLocal.Debug("starting list incidents ", query)
listResult, err := repo.Incident.List(ctx, &incident.ListRequest{
Query: query,
})
if err != nil {
return listResult, err
}
return listResult, nil
}

// CreateIncident func
// func (repo OpsgenieRepository) CreateIncident() error {
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
// createResult, err := repo.Incident.
// // Create(ctx, &incident.CreateRequest{})
// }

// GetOnCall func
func (repo OpsgenieRepository) GetOnCall(team string) (*schedule.GetOnCallsResult, error) {
logLocal := config.GetLogger()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
logLocal.Debug("geting on calls ", team)
date := time.Now()
flat := true
scheduleResult, err := repo.Schedule.GetOnCalls(ctx, &schedule.GetOnCallsRequest{
Flat: &flat,
Date: &date,
ScheduleIdentifierType: schedule.Name,
ScheduleIdentifier: team,
})
if err != nil {
return scheduleResult, err
}
return scheduleResult, nil
}

// ListSchedules func
func (repo OpsgenieRepository) ListSchedules() (*schedule.ListResult, error) {
logLocal := config.GetLogger()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
logLocal.Debug("ListSchedules starts ")
expand := true
scheduleResult, err := repo.Schedule.List(ctx, &schedule.ListRequest{
Expand: &expand,
})
if err != nil {
return scheduleResult, err
}
return scheduleResult, nil

}

func New() appcontext.Component {
alertClient, _ := alert.NewClient(&client.Config{
ApiKey: config.Values.OpsgenieAPIKey,
OpsGenieAPIURL: switchOpsgenieRegion(),
})
return &OpsgenieRepository{Alert: alertClient}
incidentClient, _ := incident.NewClient(&client.Config{
ApiKey: config.Values.OpsgenieAPIKey,
OpsGenieAPIURL: switchOpsgenieRegion(),
})
scheduleClient, _ := schedule.NewClient(&client.Config{
ApiKey: config.Values.OpsgenieAPIKey,
OpsGenieAPIURL: switchOpsgenieRegion(),
})
return &OpsgenieRepository{
Alert: alertClient,
Incident: incidentClient,
Schedule: scheduleClient,
}
}

func init() {
Expand Down
18 changes: 18 additions & 0 deletions usecase/botrequest_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,32 @@ func requestToBot(message, role, displayName string) string {
}
switch command["integration"] {
case "sensu":
listResources := []string{"assets", "health", "checks", "hooks", "entities", "handlers", "filters", "mutators", "namespaces", "silenced", "events"}
if !utils.StringInSlice(command["resource"], listResources) {
localError := fmt.Sprintf("Resource %s not implemented", command["resource"])
logLocal.Debug("Role ", role, " Display Name ", displayName, " and response ", localError)
return localError
}
logLocal.Debug("using sensu integraton")
return requestSensu(command, role, displayName)

case "opsgenie":
listResources := []string{"alerts", "incidents"}
if !utils.StringInSlice(command["resource"], listResources) {
localError := fmt.Sprintf("Resource %s not implemented", command["resource"])
logLocal.Debug("Role ", role, " Display Name ", displayName, " and response ", localError)
return localError
}
logLocal.Debug("using opsgenie integraton")
return requestOpsgenie(command, role, displayName)

case "alertmanager":
listResources := []string{"alerts"}
if !utils.StringInSlice(command["resource"], listResources) {
localError := fmt.Sprintf("Resource %s not implemented", command["resource"])
logLocal.Debug("Role ", role, " Display Name ", displayName, " and response ", localError)
return localError
}
logLocal.Debug("using alertmanager integraton")
return requestAlertManager(command, role, displayName)
}
Expand Down
26 changes: 26 additions & 0 deletions usecase/opsgenie_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func requestOpsgenie(command map[string]string, role, displayName string) string
s += fmt.Sprintf("ID: %s, Alert Name: %s, Priority: %v \n", result.TinyId, result.Message, result.Priority)
s += fmt.Sprintf("Description:\n %s \n", result.Description)

case "oncall":
result, err := getOnCall(command["name"])
if err != nil {
logLocal.Error(err)
}
s += result

// end of command resource
}
// end of command verb
Expand All @@ -67,3 +74,22 @@ func getAlerts(tinyID string) (*alert.GetAlertResult, error) {
}
return result, nil
}

func getOnCall(team string) (string, error) {
repo := domain.GetOpsgenieRepository()
schedules, err := repo.ListSchedules()
if err != nil {
return "", err
}
var s string
for _, v := range schedules.Schedule {
result, err := repo.GetOnCall(v.Name)
if err != nil {
return "", err
}
s += fmt.Sprintf("%s", result.OnCallParticipants)

}

return s, nil
}

0 comments on commit 242deb6

Please sign in to comment.