From 44ad1da18062644ceb19d3667f38823f6471bfd4 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 20 Sep 2023 16:52:30 +0545 Subject: [PATCH] chore: use api.Context instead of duty.DBContext --- auth/kratos_client.go | 4 ++-- db/agents.go | 12 ++++++------ db/canaries.go | 3 +-- db/job_history.go | 6 +++--- db/people.go | 17 ++++++++--------- db/playbooks.go | 19 +++++++++---------- db/upstream.go | 6 +++--- events/pg_notify_router.go | 4 ++-- jobs/jobs.go | 2 +- playbook/pg_listeners.go | 4 ++-- 10 files changed, 37 insertions(+), 40 deletions(-) diff --git a/auth/kratos_client.go b/auth/kratos_client.go index 8a4daeed4..973dc7227 100644 --- a/auth/kratos_client.go +++ b/auth/kratos_client.go @@ -4,7 +4,7 @@ import ( "context" "os" - "github.com/flanksource/duty" + "github.com/flanksource/incident-commander/api" client "github.com/ory/client-go" ) @@ -101,7 +101,7 @@ func (k *KratosHandler) createAdminIdentity(ctx context.Context) (string, error) return createdIdentity.Id, nil } -func (k *KratosHandler) CreateAdminUser(ctx duty.DBContext) (string, error) { +func (k *KratosHandler) CreateAdminUser(ctx api.Context) (string, error) { var id string tx := ctx.DB().Raw(`SELECT id FROM identities WHERE traits->>'email' = ?`, AdminEmail).Scan(&id) if tx.Error != nil { diff --git a/db/agents.go b/db/agents.go index 6dcc4472b..6552c8e57 100644 --- a/db/agents.go +++ b/db/agents.go @@ -5,13 +5,13 @@ import ( "fmt" "github.com/flanksource/commons/collections" - "github.com/flanksource/duty" "github.com/flanksource/duty/models" + "github.com/flanksource/incident-commander/api" "github.com/google/uuid" "gorm.io/gorm" ) -func FindAgent(ctx duty.DBContext, name string) (*models.Agent, error) { +func FindAgent(ctx api.Context, name string) (*models.Agent, error) { var agent models.Agent err := ctx.DB().Where("name = ?", name).First(&agent).Error if err != nil { @@ -25,19 +25,19 @@ func FindAgent(ctx duty.DBContext, name string) (*models.Agent, error) { return &agent, nil } -func getAgent(ctx duty.DBContext, name string) (*models.Agent, error) { +func getAgent(ctx api.Context, name string) (*models.Agent, error) { var t models.Agent tx := ctx.DB().Where("name = ?", name).First(&t) return &t, tx.Error } -func createAgent(ctx duty.DBContext, name string) (*models.Agent, error) { +func createAgent(ctx api.Context, name string) (*models.Agent, error) { a := models.Agent{Name: name} tx := ctx.DB().Create(&a) return &a, tx.Error } -func GetOrCreateAgent(ctx duty.DBContext, name string) (*models.Agent, error) { +func GetOrCreateAgent(ctx api.Context, name string) (*models.Agent, error) { a, err := getAgent(ctx, name) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -53,7 +53,7 @@ func GetOrCreateAgent(ctx duty.DBContext, name string) (*models.Agent, error) { return a, nil } -func CreateAgent(ctx duty.DBContext, name string, personID *uuid.UUID, properties map[string]string) error { +func CreateAgent(ctx api.Context, name string, personID *uuid.UUID, properties map[string]string) error { properties = collections.MergeMap(properties, map[string]string{"type": "agent"}) a := models.Agent{ diff --git a/db/canaries.go b/db/canaries.go index 0d4a72a11..dfe23a1e0 100644 --- a/db/canaries.go +++ b/db/canaries.go @@ -3,12 +3,11 @@ package db import ( "time" - "github.com/flanksource/duty" "github.com/flanksource/incident-commander/api" "github.com/google/uuid" ) -func GetCanariesOfAgent(ctx duty.DBContext, agentID uuid.UUID, since time.Time) (*api.CanaryPullResponse, error) { +func GetCanariesOfAgent(ctx api.Context, agentID uuid.UUID, since time.Time) (*api.CanaryPullResponse, error) { var now time.Time if err := ctx.DB().Raw("SELECT NOW()").Scan(&now).Error; err != nil { return nil, err diff --git a/db/job_history.go b/db/job_history.go index 1a8c44be1..bbdf81b9c 100644 --- a/db/job_history.go +++ b/db/job_history.go @@ -1,12 +1,12 @@ package db import ( - "github.com/flanksource/duty" "github.com/flanksource/duty/models" + "github.com/flanksource/incident-commander/api" "github.com/google/uuid" ) -func PersistJobHistory(ctx duty.DBContext, h *models.JobHistory) error { +func PersistJobHistory(ctx api.Context, h *models.JobHistory) error { // Delete jobs which did not process anything if h.ID != uuid.Nil && (h.SuccessCount+h.ErrorCount) == 0 { return ctx.DB().Table("job_history").Delete(h).Error @@ -15,7 +15,7 @@ func PersistJobHistory(ctx duty.DBContext, h *models.JobHistory) error { return ctx.DB().Table("job_history").Save(h).Error } -func DeleteOldJobHistoryRows(ctx duty.DBContext, keepLatest int) error { +func DeleteOldJobHistoryRows(ctx api.Context, keepLatest int) error { return ctx.DB().Exec(` WITH ordered_history AS ( SELECT diff --git a/db/people.go b/db/people.go index 9148e9179..bcc230cf0 100644 --- a/db/people.go +++ b/db/people.go @@ -7,7 +7,6 @@ import ( "time" "github.com/flanksource/commons/collections" - "github.com/flanksource/duty" "github.com/flanksource/duty/models" "github.com/flanksource/incident-commander/api" "github.com/google/uuid" @@ -15,7 +14,7 @@ import ( "gorm.io/gorm/clause" ) -func UpdateUserProperties(ctx duty.DBContext, userID string, newProps api.PersonProperties) error { +func UpdateUserProperties(ctx api.Context, userID string, newProps api.PersonProperties) error { var current api.Person if err := ctx.DB().Table("people").Where("id = ?", userID).First(¤t).Error; err != nil { return err @@ -29,30 +28,30 @@ func UpdateUserProperties(ctx duty.DBContext, userID string, newProps api.Person return ctx.DB().Table("people").Where("id = ?", userID).Update("properties", props).Error } -func UpdateIdentityState(ctx duty.DBContext, id, state string) error { +func UpdateIdentityState(ctx api.Context, id, state string) error { return ctx.DB().Table("identities").Where("id = ?", id).Update("state", state).Error } -func GetUserByID(ctx duty.DBContext, id string) (api.Person, error) { +func GetUserByID(ctx api.Context, id string) (api.Person, error) { var user api.Person err := ctx.DB().Table("people").Where("id = ?", id).First(&user).Error return user, err } -func GetTeamsForUser(ctx duty.DBContext, id string) ([]models.Team, error) { +func GetTeamsForUser(ctx api.Context, id string) ([]models.Team, error) { var teams []models.Team err := ctx.DB().Raw("SELECT teams.* FROM teams LEFT JOIN team_members ON teams.id = team_members.team_id WHERE team_members.person_id = ?", id).Scan(&teams).Error return teams, err } -func GetUserByExternalID(ctx duty.DBContext, id string) (api.Person, error) { +func GetUserByExternalID(ctx api.Context, id string) (api.Person, error) { var user api.Person err := ctx.DB().Table("people").Where("external_id = ?", id).First(&user).Error return user, err } // CreateUser creates a new user and returns a copy -func CreateUser(ctx duty.DBContext, user api.Person) (api.Person, error) { +func CreateUser(ctx api.Context, user api.Person) (api.Person, error) { err := ctx.DB().Table("people").Create(&user).Error return user, err } @@ -63,7 +62,7 @@ type CreateUserRequest struct { Properties models.PersonProperties } -func CreatePerson(ctx duty.DBContext, name, email, personType string) (*models.Person, error) { +func CreatePerson(ctx api.Context, name, email, personType string) (*models.Person, error) { person := models.Person{Name: name, Email: email, Type: personType} if err := ctx.DB().Clauses(clause.Returning{}).Create(&person).Error; err != nil { return nil, err @@ -82,7 +81,7 @@ const ( saltLength = 12 ) -func CreateAccessToken(ctx duty.DBContext, personID uuid.UUID, name, password string, expiry time.Duration) (string, error) { +func CreateAccessToken(ctx api.Context, personID uuid.UUID, name, password string, expiry time.Duration) (string, error) { saltRaw := make([]byte, saltLength) if _, err := crand.Read(saltRaw); err != nil { return "", err diff --git a/db/playbooks.go b/db/playbooks.go index 7b3fda3af..f07539d5d 100644 --- a/db/playbooks.go +++ b/db/playbooks.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" - "github.com/flanksource/duty" "github.com/flanksource/duty/models" "github.com/flanksource/duty/types" "github.com/flanksource/incident-commander/api" @@ -14,7 +13,7 @@ import ( "gorm.io/gorm" ) -func FindPlaybooksForEvent(ctx duty.DBContext, eventClass, event string) ([]models.Playbook, error) { +func FindPlaybooksForEvent(ctx api.Context, eventClass, event string) ([]models.Playbook, error) { var playbooks []models.Playbook query := fmt.Sprintf(`SELECT * FROM playbooks WHERE spec->'on'->'%s' @> '[{"event": "%s"}]'`, eventClass, event) if err := ctx.DB().Raw(query).Scan(&playbooks).Error; err != nil { @@ -24,7 +23,7 @@ func FindPlaybooksForEvent(ctx duty.DBContext, eventClass, event string) ([]mode return playbooks, nil } -func FindPlaybook(ctx duty.DBContext, id uuid.UUID) (*models.Playbook, error) { +func FindPlaybook(ctx api.Context, id uuid.UUID) (*models.Playbook, error) { var p models.Playbook if err := ctx.DB().Where("id = ?", id).First(&p).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -38,7 +37,7 @@ func FindPlaybook(ctx duty.DBContext, id uuid.UUID) (*models.Playbook, error) { } // CanApprove returns true if the given person can approve runs of the given playbook. -func CanApprove(ctx duty.DBContext, personID, playbookID string) (bool, error) { +func CanApprove(ctx api.Context, personID, playbookID string) (bool, error) { query := ` WITH playbook_approvers AS ( SELECT id, @@ -66,7 +65,7 @@ func CanApprove(ctx duty.DBContext, personID, playbookID string) (bool, error) { return count > 0, nil } -func GetPlaybookRun(ctx duty.DBContext, id string) (*models.PlaybookRun, error) { +func GetPlaybookRun(ctx api.Context, id string) (*models.PlaybookRun, error) { var p models.PlaybookRun if err := ctx.DB().Where("id = ?", id).First(&p).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -80,7 +79,7 @@ func GetPlaybookRun(ctx duty.DBContext, id string) (*models.PlaybookRun, error) } // FindPlaybooksForCheck returns all the playbooks that match the given check type and tags. -func FindPlaybooksForCheck(ctx duty.DBContext, configType string, tags map[string]string) ([]models.Playbook, error) { +func FindPlaybooksForCheck(ctx api.Context, configType string, tags map[string]string) ([]models.Playbook, error) { joinQuery := `JOIN LATERAL jsonb_array_elements(playbooks."spec"->'checks') AS checks(ch) ON 1=1` var joinArgs []any if len(tags) != 0 { @@ -102,7 +101,7 @@ func FindPlaybooksForCheck(ctx duty.DBContext, configType string, tags map[strin } // FindPlaybooksForConfig returns all the playbooks that match the given config type and tags. -func FindPlaybooksForConfig(ctx duty.DBContext, configType string, tags map[string]string) ([]models.Playbook, error) { +func FindPlaybooksForConfig(ctx api.Context, configType string, tags map[string]string) ([]models.Playbook, error) { joinQuery := `JOIN LATERAL jsonb_array_elements(playbooks."spec"->'configs') AS configs(config) ON 1=1` var joinArgs []any @@ -125,7 +124,7 @@ func FindPlaybooksForConfig(ctx duty.DBContext, configType string, tags map[stri } // FindPlaybooksForComponent returns all the playbooks that match the given component type and tags. -func FindPlaybooksForComponent(ctx duty.DBContext, configType string, tags map[string]string) ([]models.Playbook, error) { +func FindPlaybooksForComponent(ctx api.Context, configType string, tags map[string]string) ([]models.Playbook, error) { joinQuery := `JOIN LATERAL jsonb_array_elements(playbooks."spec"->'components') AS components(component) ON 1=1` var joinArgs []any @@ -170,7 +169,7 @@ func DeletePlaybook(id string) error { // UpdatePlaybookRunStatusIfApproved updates the status of the playbook run to "pending" // if all the approvers have approved it. -func UpdatePlaybookRunStatusIfApproved(ctx duty.DBContext, playbookID string, approval v1.PlaybookApproval) error { +func UpdatePlaybookRunStatusIfApproved(ctx api.Context, playbookID string, approval v1.PlaybookApproval) error { if approval.Approvers.Empty() { return nil } @@ -201,6 +200,6 @@ func UpdatePlaybookRunStatusIfApproved(ctx duty.DBContext, playbookID string, ap return ctx.DB().Exec(query, approval.Approvers.Teams, approval.Approvers.People, models.PlaybookRunStatusScheduled, models.PlaybookRunStatusPending, playbookID).Error } -func SavePlaybookRunApproval(ctx duty.DBContext, approval models.PlaybookApproval) error { +func SavePlaybookRunApproval(ctx api.Context, approval models.PlaybookApproval) error { return ctx.DB().Create(&approval).Error } diff --git a/db/upstream.go b/db/upstream.go index 31cb9c950..9ae099505 100644 --- a/db/upstream.go +++ b/db/upstream.go @@ -5,13 +5,13 @@ import ( "strings" "github.com/flanksource/commons/logger" - "github.com/flanksource/duty" "github.com/flanksource/duty/upstream" + "github.com/flanksource/incident-commander/api" "github.com/google/uuid" "gorm.io/gorm/clause" ) -func GetAllResourceIDsOfAgent(ctx duty.DBContext, req upstream.PaginateRequest, agentID uuid.UUID) ([]string, error) { +func GetAllResourceIDsOfAgent(ctx api.Context, req upstream.PaginateRequest, agentID uuid.UUID) ([]string, error) { var response []string var err error @@ -38,7 +38,7 @@ func GetAllResourceIDsOfAgent(ctx duty.DBContext, req upstream.PaginateRequest, return response, err } -func InsertUpstreamMsg(ctx duty.DBContext, req *upstream.PushData) error { +func InsertUpstreamMsg(ctx api.Context, req *upstream.PushData) error { if len(req.Topologies) > 0 { if err := ctx.DB().Clauses(clause.OnConflict{UpdateAll: true}).Create(req.Topologies).Error; err != nil { return fmt.Errorf("error upserting topologies: %w", err) diff --git a/events/pg_notify_router.go b/events/pg_notify_router.go index f34465726..f77b393e0 100644 --- a/events/pg_notify_router.go +++ b/events/pg_notify_router.go @@ -2,8 +2,8 @@ package events import ( "github.com/flanksource/commons/logger" - "github.com/flanksource/duty" "github.com/flanksource/duty/duty/pg" + "github.com/flanksource/incident-commander/api" ) // pgNotifyRouter distributes the pgNotify event to multiple channels @@ -27,7 +27,7 @@ func (t *pgNotifyRouter) RegisterRoutes(routes []string) <-chan string { return pgNotifyChannel } -func (t *pgNotifyRouter) Run(ctx duty.DBContext, channel string) { +func (t *pgNotifyRouter) Run(ctx api.Context, channel string) { eventQueueNotifyChannel := make(chan string) go pg.Listen(ctx, channel, eventQueueNotifyChannel) diff --git a/jobs/jobs.go b/jobs/jobs.go index 980762a4d..69778e793 100644 --- a/jobs/jobs.go +++ b/jobs/jobs.go @@ -29,7 +29,7 @@ func ScheduleFunc(schedule string, fn func()) (any, error) { return FuncScheduler.AddFunc(schedule, fn) } -func Start(ctx duty.DBContext) { +func Start(ctx api.Context) { // Running first at startup and then with the schedule TeamComponentOwnershipRun() EvaluateEvidenceScripts() diff --git a/playbook/pg_listeners.go b/playbook/pg_listeners.go index 454486163..727dc513f 100644 --- a/playbook/pg_listeners.go +++ b/playbook/pg_listeners.go @@ -1,11 +1,11 @@ package playbook import ( - "github.com/flanksource/duty" "github.com/flanksource/duty/duty/pg" + "github.com/flanksource/incident-commander/api" ) -func ListenPlaybookPGNotify(ctx duty.DBContext) { +func ListenPlaybookPGNotify(ctx api.Context) { pgNotifyPlaybookSpecUpdated := make(chan string) go pg.Listen(ctx, "playbook_spec_updated", pgNotifyPlaybookSpecUpdated)