2020-12-04 23:41:26 +01:00
|
|
|
package db
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Event represents information generated by ansible or api action captured to the database during execution
|
2016-04-16 21:42:57 +02:00
|
|
|
type Event struct {
|
2024-02-11 20:52:14 +01:00
|
|
|
ID int `db:"id" json:"-"`
|
|
|
|
UserID *int `db:"user_id" json:"user_id"`
|
|
|
|
ProjectID *int `db:"project_id" json:"project_id"`
|
|
|
|
IntegrationID *int `db:"integration_id" json:"integration_id"`
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2021-10-13 16:07:22 +02:00
|
|
|
ObjectID *int `db:"object_id" json:"object_id"`
|
|
|
|
ObjectType *EventObjectType `db:"object_type" json:"object_type"`
|
|
|
|
Description *string `db:"description" json:"description"`
|
|
|
|
Created time.Time `db:"created" json:"created"`
|
2016-04-16 21:42:57 +02:00
|
|
|
|
|
|
|
ObjectName string `db:"-" json:"object_name"`
|
|
|
|
ProjectName *string `db:"project_name" json:"project_name"`
|
2021-08-25 06:27:16 +02:00
|
|
|
Username *string `db:"-" json:"username"`
|
2016-04-16 21:42:57 +02:00
|
|
|
}
|
2021-10-12 12:25:43 +02:00
|
|
|
|
2021-10-13 16:07:22 +02:00
|
|
|
type EventObjectType string
|
|
|
|
|
|
|
|
const (
|
2024-02-11 20:52:14 +01:00
|
|
|
EventTask EventObjectType = "task"
|
|
|
|
EventEnvironment EventObjectType = "environment"
|
|
|
|
EventInventory EventObjectType = "inventory"
|
|
|
|
EventKey EventObjectType = "key"
|
|
|
|
EventProject EventObjectType = "project"
|
|
|
|
EventRepository EventObjectType = "repository"
|
|
|
|
EventSchedule EventObjectType = "schedule"
|
|
|
|
EventTemplate EventObjectType = "template"
|
|
|
|
EventUser EventObjectType = "user"
|
|
|
|
EventView EventObjectType = "view"
|
|
|
|
EventIntegration EventObjectType = "integration"
|
|
|
|
EventIntegrationExtractValue EventObjectType = "integrationextractvalue"
|
|
|
|
EventIntegrationMatcher EventObjectType = "integrationmatcher"
|
2024-12-05 16:45:40 +01:00
|
|
|
|
|
|
|
EventTerraformInventoryAlias EventObjectType = "terraform_inventory_alias"
|
2021-10-13 16:07:22 +02:00
|
|
|
)
|
|
|
|
|
2021-10-12 12:25:43 +02:00
|
|
|
func FillEvents(d Store, events []Event) (err error) {
|
|
|
|
usernames := make(map[int]string)
|
|
|
|
|
|
|
|
for i, evt := range events {
|
|
|
|
var objName string
|
|
|
|
objName, err = getEventObjectName(d, evt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if objName != "" {
|
|
|
|
events[i].ObjectName = objName
|
|
|
|
}
|
|
|
|
|
|
|
|
if evt.UserID == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var username string
|
|
|
|
|
|
|
|
username, ok := usernames[*evt.UserID]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
username, err = getEventUsername(d, evt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
usernames[*evt.UserID] = username
|
|
|
|
}
|
|
|
|
|
|
|
|
events[i].Username = &username
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventObjectName(d Store, evt Event) (string, error) {
|
|
|
|
if evt.ObjectID == nil || evt.ObjectType == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
switch *evt.ObjectType {
|
2021-10-13 16:07:22 +02:00
|
|
|
case EventTask:
|
2021-10-12 12:25:43 +02:00
|
|
|
task, err := d.GetTask(*evt.ProjectID, *evt.ObjectID)
|
|
|
|
if err != nil {
|
2022-10-21 23:36:05 +02:00
|
|
|
// Task can be deleted, it is ok, just return empty string
|
|
|
|
return "", nil
|
2021-10-12 12:25:43 +02:00
|
|
|
}
|
|
|
|
return task.Playbook, nil
|
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEventUsername(d Store, evt Event) (username string, err error) {
|
|
|
|
if evt.UserID == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := d.GetUser(*evt.UserID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.Username, nil
|
|
|
|
}
|