Skip to content

Commit

Permalink
feat: and playbook action pushes
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed Jan 22, 2024
1 parent f48fd6c commit 97b748c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
const (
ResourceTypeCheckStatuses = "check_statuses"
ResourceTypeComponent = "components"
ResourceTypePlaybook = "playbook"
ResourceTypeUpstream = "upstream"
)

Expand Down
2 changes: 1 addition & 1 deletion models/playbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type PlaybookRunAction struct {
StartTime time.Time `json:"start_time,omitempty" time_format:"postgres_timestamp" gorm:"default:NOW(), NOT NULL"`
EndTime *time.Time `json:"end_time,omitempty" time_format:"postgres_timestamp"`
Result types.JSONMap `json:"result,omitempty" gorm:"default:null"`
Error string `json:"error,omitempty" gorm:"default:null"`
Error *string `json:"error,omitempty" gorm:"default:null"`
IsPushed bool `json:"is_pushed"`
AgentID *uuid.UUID `json:"agent_id,omitempty"`
}
Expand Down
10 changes: 9 additions & 1 deletion schema/playbooks.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ table "playbook_run_actions" {
default = "running"
}
column "playbook_run_id" {
null = false
null = true
type = uuid
comment = "a run id is mandatory except for an agent"
}
column "start_time" {
null = true
Expand Down Expand Up @@ -283,4 +284,11 @@ table "playbook_run_actions" {
on_update = NO_ACTION
on_delete = CASCADE
}
check "playbook_action_not_null_run_id" {
expr = <<EOF
(playbook_run_id IS NULL AND agent_id IS NOT NULL) OR
(playbook_run_id IS NOT NULL)
EOF
comment = "a run id is mandatory except for an agent"
}
}
12 changes: 12 additions & 0 deletions upstream/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,17 @@ func InsertUpstreamMsg(ctx context.Context, req *PushData) error {
}
}

for i := range req.PlaybookActions {
updates := map[string]any{
"result": req.PlaybookActions[i].Result,
"end_time": req.PlaybookActions[i].EndTime,
"status": req.PlaybookActions[i].Status,
"error": req.PlaybookActions[i].Error,
}
if err := db.Model(&models.PlaybookRunAction{}).Where("id = ?", req.PlaybookActions[i].ID).Updates(updates).Error; err != nil {
return fmt.Errorf("error updating playbook action [%s]: %w", req.PlaybookActions[i].ID.String(), err)
}
}

return nil
}
3 changes: 2 additions & 1 deletion upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type PushData struct {
ComponentRelationships []models.ComponentRelationship `json:"component_relationships,omitempty"`
ConfigComponentRelationships []models.ConfigComponentRelationship `json:"config_component_relationships,omitempty"`
Topologies []models.Topology `json:"topologies,omitempty"`
PlaybookActions []models.PlaybookRunAction `json:"playbook_actions,omitempty"`
}

func (p *PushData) String() string {
Expand Down Expand Up @@ -148,7 +149,7 @@ func (p *PushData) Attributes() map[string]any {
func (t *PushData) Count() int {
return len(t.Canaries) + len(t.Checks) + len(t.Components) + len(t.ConfigScrapers) +
len(t.ConfigAnalysis) + len(t.ConfigChanges) + len(t.ConfigItems) + len(t.CheckStatuses) +
len(t.ConfigRelationships) + len(t.ComponentRelationships) + len(t.ConfigComponentRelationships) + len(t.Topologies)
len(t.ConfigRelationships) + len(t.ComponentRelationships) + len(t.ConfigComponentRelationships) + len(t.Topologies) + len(t.PlaybookActions)
}

// ReplaceTopologyID replaces the topology_id for all the components
Expand Down

0 comments on commit 97b748c

Please sign in to comment.