Semaphore/db/Store.go

322 lines
8.8 KiB
Go
Raw Normal View History

package db
import (
"errors"
2020-12-04 23:22:05 +01:00
log "github.com/Sirupsen/logrus"
"time"
)
2020-12-04 23:22:05 +01:00
const databaseTimeFormat = "2006-01-02T15:04:05:99Z"
// GetParsedTime returns the timestamp as it will retrieved from the database
// This allows us to create timestamp consistency on return values from create requests
func GetParsedTime(t time.Time) time.Time {
parsedTime, err := time.Parse(databaseTimeFormat, t.Format(databaseTimeFormat))
if err != nil {
log.Error(err)
}
return parsedTime
}
type RetrieveQueryParams struct {
Offset int
Count int
SortBy string
SortInverted bool
}
2021-05-15 13:27:42 +02:00
type ObjectScope int
type ObjectProperties struct {
2021-05-16 23:44:42 +02:00
TableName string
IsGlobal bool // doesn't belong to other table, for example to project or user.
ForeignColumnName string
PrimaryColumnName string
SortableColumns []string
2021-06-24 21:53:36 +02:00
SortInverted bool
}
var ErrNotFound = errors.New("no rows in result set")
var ErrInvalidOperation = errors.New("invalid operation")
2021-05-06 14:41:31 +02:00
func ValidateUsername(login string) error {
return nil
}
2021-09-06 13:05:10 +02:00
type Transaction interface {}
type Store interface {
Connect() error
Close() error
Migrate() error
GetEnvironment(projectID int, environmentID int) (Environment, error)
GetEnvironments(projectID int, params RetrieveQueryParams) ([]Environment, error)
UpdateEnvironment(env Environment) error
CreateEnvironment(env Environment) (Environment, error)
DeleteEnvironment(projectID int, templateID int) error
DeleteEnvironmentSoft(projectID int, templateID int) error
2020-12-07 13:13:59 +01:00
GetInventory(projectID int, inventoryID int) (Inventory, error)
GetInventories(projectID int, params RetrieveQueryParams) ([]Inventory, error)
UpdateInventory(inventory Inventory) error
CreateInventory(inventory Inventory) (Inventory, error)
2020-12-07 20:48:52 +01:00
DeleteInventory(projectID int, inventoryID int) error
DeleteInventorySoft(projectID int, inventoryID int) error
GetRepository(projectID int, repositoryID int) (Repository, error)
GetRepositories(projectID int, params RetrieveQueryParams) ([]Repository, error)
UpdateRepository(repository Repository) error
CreateRepository(repository Repository) (Repository, error)
DeleteRepository(projectID int, repositoryID int) error
DeleteRepositorySoft(projectID int, repositoryID int) error
GetAccessKey(projectID int, accessKeyID int, deserializeSecret bool) (AccessKey, error)
2020-12-08 08:23:33 +01:00
GetAccessKeys(projectID int, params RetrieveQueryParams) ([]AccessKey, error)
2020-12-08 08:23:33 +01:00
UpdateAccessKey(accessKey AccessKey) error
CreateAccessKey(accessKey AccessKey) (AccessKey, error)
DeleteAccessKey(projectID int, accessKeyID int) error
DeleteAccessKeySoft(projectID int, accessKeyID int) error
GetUsers(params RetrieveQueryParams) ([]User, error)
2021-05-06 14:41:31 +02:00
CreateUserWithoutPassword(user User) (User, error)
CreateUser(user UserWithPwd) (User, error)
DeleteUser(userID int) error
// UpdateUser updates all fields of the entity except Pwd.
// Pwd should be present of you want update user password. Empty Pwd ignored.
UpdateUser(user UserWithPwd) error
SetUserPassword(userID int, password string) error
GetUser(userID int) (User, error)
2020-12-17 15:00:05 +01:00
GetUserByLoginOrEmail(login string, email string) (User, error)
GetProject(projectID int) (Project, error)
GetProjects(userID int) ([]Project, error)
CreateProject(project Project) (Project, error)
DeleteProject(projectID int) error
UpdateProject(project Project) error
GetTemplates(projectID int, params RetrieveQueryParams) ([]Template, error)
CreateTemplate(template Template) (Template, error)
UpdateTemplate(template Template) error
GetTemplate(projectID int, templateID int) (Template, error)
DeleteTemplate(projectID int, templateID int) error
2021-09-06 13:05:10 +02:00
GetSchedules() ([]Schedule, error)
GetTemplateSchedules(projectID int, templateID int) ([]Schedule, error)
CreateSchedule(schedule Schedule) (Schedule, error)
UpdateSchedule(schedule Schedule) error
GetSchedule(projectID int, scheduleID int) (Schedule, error)
DeleteSchedule(projectID int, scheduleID int) error
2020-12-17 15:00:05 +01:00
GetProjectUsers(projectID int, params RetrieveQueryParams) ([]User, error)
CreateProjectUser(projectUser ProjectUser) (ProjectUser, error)
2020-12-17 15:00:05 +01:00
DeleteProjectUser(projectID int, userID int) error
GetProjectUser(projectID int, userID int) (ProjectUser, error)
UpdateProjectUser(projectUser ProjectUser) error
CreateEvent(event Event) (Event, error)
GetUserEvents(userID int, params RetrieveQueryParams) ([]Event, error)
GetEvents(projectID int, params RetrieveQueryParams) ([]Event, error)
GetAPITokens(userID int) ([]APIToken, error)
CreateAPIToken(token APIToken) (APIToken, error)
GetAPIToken(tokenID string) (APIToken, error)
ExpireAPIToken(userID int, tokenID string) error
GetSession(userID int, sessionID int) (Session, error)
2021-03-12 18:41:41 +01:00
CreateSession(session Session) (Session, error)
ExpireSession(userID int, sessionID int) error
TouchSession(userID int, sessionID int) error
2021-03-12 18:41:41 +01:00
CreateTask(task Task) (Task, error)
UpdateTask(task Task) error
GetTemplateTasks(projectID int, templateID int, params RetrieveQueryParams) ([]TaskWithTpl, error)
GetProjectTasks(projectID int, params RetrieveQueryParams) ([]TaskWithTpl, error)
GetTask(projectID int, taskID int) (Task, error)
DeleteTaskWithOutputs(projectID int, taskID int) error
GetTaskOutputs(projectID int, taskID int) ([]TaskOutput, error)
CreateTaskOutput(output TaskOutput) (TaskOutput, error)
}
2020-12-07 20:48:52 +01:00
func FillTemplate(d Store, template *Template) (err error) {
if template.VaultPassID != nil {
template.VaultPass, err = d.GetAccessKey(template.ProjectID, *template.VaultPassID, true)
}
return
}
func FillInventory(d Store, inventory *Inventory) (err error) {
if inventory.SSHKeyID != nil {
inventory.SSHKey, err = d.GetAccessKey(inventory.ProjectID, *inventory.SSHKeyID, true)
}
if err != nil {
return
}
if inventory.BecomeKeyID != nil {
inventory.BecomeKey, err = d.GetAccessKey(inventory.ProjectID, *inventory.BecomeKeyID, true)
}
return
}
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 {
case "task":
task, err := d.GetTask(*evt.ProjectID, *evt.ObjectID)
if err != nil {
return "", err
}
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
}
var AccessKeyProps = ObjectProperties{
2021-05-16 23:44:42 +02:00
TableName: "access_key",
SortableColumns: []string{"name", "type"},
PrimaryColumnName: "id",
}
var GlobalAccessKeyProps = ObjectProperties{
2021-05-16 23:44:42 +02:00
IsGlobal: true,
TableName: "access_key",
SortableColumns: []string{"name", "type"},
ForeignColumnName: "ssh_key_id",
PrimaryColumnName: "id",
}
var EnvironmentProps = ObjectProperties{
2021-05-16 23:44:42 +02:00
TableName: "project__environment",
SortableColumns: []string{"name"},
ForeignColumnName: "environment_id",
PrimaryColumnName: "id",
}
var InventoryProps = ObjectProperties{
2021-05-16 23:44:42 +02:00
TableName: "project__inventory",
SortableColumns: []string{"name"},
ForeignColumnName: "inventory_id",
PrimaryColumnName: "id",
}
var RepositoryProps = ObjectProperties{
2021-05-16 23:44:42 +02:00
TableName: "project__repository",
ForeignColumnName: "repository_id",
PrimaryColumnName: "id",
}
2020-12-07 20:48:52 +01:00
var TemplateProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "project__template",
SortableColumns: []string{"name"},
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "id",
}
2021-09-06 13:05:10 +02:00
var ScheduleProps = ObjectProperties{
TableName: "project__schedule",
PrimaryColumnName: "id",
}
var ProjectUserProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "project__user",
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "user_id",
}
var ProjectProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "project",
IsGlobal: true,
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "id",
}
var UserProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "user",
IsGlobal: true,
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "id",
}
var SessionProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "session",
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "id",
}
var TokenProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "user__token",
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "id",
}
var TaskProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "task",
IsGlobal: true,
2021-05-16 23:44:42 +02:00
PrimaryColumnName: "id",
2021-06-24 21:53:36 +02:00
SortInverted: true,
}
var TaskOutputProps = ObjectProperties{
2021-06-24 19:45:28 +02:00
TableName: "task__output",
}