diff --git a/db/Store.go b/db/Store.go index 7d35fcba..1818f5ca 100644 --- a/db/Store.go +++ b/db/Store.go @@ -134,67 +134,67 @@ type Store interface { CreateTaskOutput(output TaskOutput) (TaskOutput, error) } -var AccessKeyObject = ObjectProperties{ +var AccessKeyProps = ObjectProperties{ TableName: "access_key", SortableColumns: []string{"name", "type"}, TemplateColumnName: "ssh_key_id", } -var GlobalAccessKeyObject = ObjectProperties{ +var GlobalAccessKeyProps = ObjectProperties{ IsGlobal: true, TableName: "access_key", SortableColumns: []string{"name", "type"}, TemplateColumnName: "ssh_key_id", } -var EnvironmentObject = ObjectProperties{ +var EnvironmentProps = ObjectProperties{ TableName: "project__environment", SortableColumns: []string{"name"}, } -var InventoryObject = ObjectProperties{ +var InventoryProps = ObjectProperties{ TableName: "project__inventory", SortableColumns: []string{"name"}, TemplateColumnName: "inventory_id", } -var RepositoryObject = ObjectProperties{ +var RepositoryProps = ObjectProperties{ TableName: "project__repository", TemplateColumnName: "repository_id", } -var TemplateObject = ObjectProperties{ +var TemplateProps = ObjectProperties{ TableName: "project__template", SortableColumns: []string{"name"}, } -var ProjectUserObject = ObjectProperties{ +var ProjectUserProps = ObjectProperties{ TableName: "project__user", } -var ProjectObject = ObjectProperties{ +var ProjectProps = ObjectProperties{ TableName: "project", IsGlobal: true, } -var UserObject = ObjectProperties{ +var UserProps = ObjectProperties{ TableName: "user", IsGlobal: true, } -var SessionObject = ObjectProperties{ +var SessionProps = ObjectProperties{ TableName: "session", } -var TokenObject = ObjectProperties{ +var TokenProps = ObjectProperties{ TableName: "user__token", } -var TaskObject = ObjectProperties{ +var TaskProps = ObjectProperties{ TableName: "task", IsGlobal: true, } -var TaskOutputObject = ObjectProperties{ +var TaskOutputProps = ObjectProperties{ TableName: "task__output", } diff --git a/db/bolt/BoltDb.go b/db/bolt/BoltDb.go index e21172b0..1e1b7527 100644 --- a/db/bolt/BoltDb.go +++ b/db/bolt/BoltDb.go @@ -169,9 +169,14 @@ func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.R continue } - obj := reflect.New(objType).Elem() - err = json.Unmarshal(v, &obj) - if err == nil { + tmp := reflect.New(objType) + ptr := tmp.Interface() + str := string(v) + fmt.Println(str) + err = json.Unmarshal(v, ptr) + obj := reflect.ValueOf(ptr).Elem().Interface() + + if err != nil { return err } @@ -181,7 +186,8 @@ func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.R } } - objectsValue.Set(reflect.Append(objectsValue, obj)) + newObjectValues := reflect.Append(objectsValue, reflect.ValueOf(obj)) + objectsValue.Set(newObjectValues) n++ @@ -249,7 +255,7 @@ func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProperties, objectID } return d.db.Update(func (tx *bbolt.Tx) error { - b := tx.Bucket(makeBucketId(db.InventoryObject, bucketID)) + b := tx.Bucket(makeBucketId(db.InventoryProps, bucketID)) if b == nil { return db.ErrNotFound } @@ -276,6 +282,8 @@ func (d *BoltDb) updateObject(bucketID int, props db.ObjectProperties, object in return db.ErrNotFound } + // TODO: marshal by tag db + str, err := json.Marshal(object) if err != nil { return err @@ -293,29 +301,25 @@ func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object in return err2 } - idValue := reflect.ValueOf(object).FieldByName("ID") - var objectID objectID + objPtr := reflect.ValueOf(&object).Elem() - switch idValue.Kind() { - case reflect.Int: - case reflect.Int8: - case reflect.Int16: - case reflect.Int32: - case reflect.Int64: - case reflect.Uint: - case reflect.Uint8: - case reflect.Uint16: - case reflect.Uint32: - case reflect.Uint64: + tmpObj := reflect.New(objPtr.Elem().Type()).Elem() + tmpObj.Set(objPtr.Elem()) + + idValue := tmpObj.FieldByName("ID") + var objectID objectID + idKind := idValue.Kind() + switch { + case idKind >= reflect.Int && idKind <= reflect.Uint64: if idValue.Int() == 0 { id, err2 := b.NextSequence() if err2 != nil { return err2 } idValue.SetInt(int64(id)) - objectID = intObjectID(id) } - case reflect.String: + objectID = intObjectID(idValue.Int()) + case idKind == reflect.String: if idValue.String() == "" { return fmt.Errorf("object ID can not be empty string") } @@ -328,6 +332,8 @@ func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object in return fmt.Errorf("object ID can not be nil") } + + objPtr.Set(tmpObj) str, err2 := json.Marshal(object) if err2 != nil { return err2 diff --git a/db/bolt/access_key.go b/db/bolt/access_key.go index f2982f2a..112fd999 100644 --- a/db/bolt/access_key.go +++ b/db/bolt/access_key.go @@ -6,56 +6,56 @@ import ( func (d *BoltDb) GetAccessKey(projectID int, accessKeyID int) (db.AccessKey, error) { var key db.AccessKey - err := d.getObject(projectID, db.AccessKeyObject, intObjectID(accessKeyID), &key) + err := d.getObject(projectID, db.AccessKeyProps, intObjectID(accessKeyID), &key) return key, err } func (d *BoltDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) { var keys []db.AccessKey - err := d.getObjects(projectID, db.AccessKeyObject, params, nil, &keys) + err := d.getObjects(projectID, db.AccessKeyProps, params, nil, &keys) return keys, err } func (d *BoltDb) UpdateAccessKey(key db.AccessKey) error { - return d.updateObject(*key.ProjectID, db.AccessKeyObject, key) + return d.updateObject(*key.ProjectID, db.AccessKeyProps, key) } func (d *BoltDb) CreateAccessKey(key db.AccessKey) (db.AccessKey, error) { - newKey, err := d.createObject(*key.ProjectID, db.GlobalAccessKeyObject, key) + newKey, err := d.createObject(*key.ProjectID, db.GlobalAccessKeyProps, key) return newKey.(db.AccessKey), err } func (d *BoltDb) DeleteAccessKey(projectID int, accessKeyID int) error { - return d.deleteObject(projectID, db.AccessKeyObject, intObjectID(accessKeyID)) + return d.deleteObject(projectID, db.AccessKeyProps, intObjectID(accessKeyID)) } func (d *BoltDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error { - return d.deleteObjectSoft(projectID, db.AccessKeyObject, intObjectID(accessKeyID)) + return d.deleteObjectSoft(projectID, db.AccessKeyProps, intObjectID(accessKeyID)) } func (d *BoltDb) GetGlobalAccessKey(accessKeyID int) (key db.AccessKey, err error) { - err = d.getObject(0, db.GlobalAccessKeyObject, intObjectID(accessKeyID), &key) + err = d.getObject(0, db.GlobalAccessKeyProps, intObjectID(accessKeyID), &key) return } func (d *BoltDb) GetGlobalAccessKeys(params db.RetrieveQueryParams) (keys []db.AccessKey, err error) { - err = d.getObjects(0, db.GlobalAccessKeyObject, params, nil, &keys) + err = d.getObjects(0, db.GlobalAccessKeyProps, params, nil, &keys) return } func (d *BoltDb) UpdateGlobalAccessKey(key db.AccessKey) error { - return d.updateObject(0, db.AccessKeyObject, key) + return d.updateObject(0, db.AccessKeyProps, key) } func (d *BoltDb) CreateGlobalAccessKey(key db.AccessKey) (db.AccessKey, error) { - newKey, err := d.createObject(0, db.GlobalAccessKeyObject, key) + newKey, err := d.createObject(0, db.GlobalAccessKeyProps, key) return newKey.(db.AccessKey), err } func (d *BoltDb) DeleteGlobalAccessKey(accessKeyID int) error { - return d.deleteObject(0, db.GlobalAccessKeyObject, intObjectID(accessKeyID)) + return d.deleteObject(0, db.GlobalAccessKeyProps, intObjectID(accessKeyID)) } func (d *BoltDb) DeleteGlobalAccessKeySoft(accessKeyID int) error { - return d.deleteObjectSoft(0, db.GlobalAccessKeyObject, intObjectID(accessKeyID)) + return d.deleteObjectSoft(0, db.GlobalAccessKeyProps, intObjectID(accessKeyID)) } diff --git a/db/bolt/environment.go b/db/bolt/environment.go index e027da8c..1eb1c627 100644 --- a/db/bolt/environment.go +++ b/db/bolt/environment.go @@ -3,28 +3,28 @@ package bolt import "github.com/ansible-semaphore/semaphore/db" func (d *BoltDb) GetEnvironment(projectID int, environmentID int) (environment db.Environment, err error) { - err = d.getObject(projectID, db.EnvironmentObject, intObjectID(environmentID), &environment) + err = d.getObject(projectID, db.EnvironmentProps, intObjectID(environmentID), &environment) return } func (d *BoltDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) (environment []db.Environment, err error) { - err = d.getObjects(projectID, db.EnvironmentObject, params, nil, &environment) + err = d.getObjects(projectID, db.EnvironmentProps, params, nil, &environment) return } func (d *BoltDb) UpdateEnvironment(env db.Environment) error { - return d.updateObject(env.ProjectID, db.EnvironmentObject, &env) + return d.updateObject(env.ProjectID, db.EnvironmentProps, &env) } func (d *BoltDb) CreateEnvironment(env db.Environment) (db.Environment, error) { - newEnv, err := d.createObject(env.ProjectID, db.EnvironmentObject, env) + newEnv, err := d.createObject(env.ProjectID, db.EnvironmentProps, env) return newEnv.(db.Environment), err } func (d *BoltDb) DeleteEnvironment(projectID int, environmentID int) error { - return d.deleteObject(projectID, db.EnvironmentObject, intObjectID(environmentID)) + return d.deleteObject(projectID, db.EnvironmentProps, intObjectID(environmentID)) } func (d *BoltDb) DeleteEnvironmentSoft(projectID int, environmentID int) error { - return d.deleteObjectSoft(projectID, db.EnvironmentObject, intObjectID(environmentID)) + return d.deleteObjectSoft(projectID, db.EnvironmentProps, intObjectID(environmentID)) } diff --git a/db/bolt/inventory.go b/db/bolt/inventory.go index c2981292..4dbbd3c5 100644 --- a/db/bolt/inventory.go +++ b/db/bolt/inventory.go @@ -6,7 +6,7 @@ import ( func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) { - err = d.getObject(projectID, db.InventoryObject, intObjectID(inventoryID), &inventory) + err = d.getObject(projectID, db.InventoryProps, intObjectID(inventoryID), &inventory) if err != nil { return @@ -27,24 +27,24 @@ func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inve } func (d *BoltDb) GetInventories(projectID int, params db.RetrieveQueryParams) (inventories []db.Inventory, err error) { - err = d.getObjects(projectID, db.AccessKeyObject, params, nil, &inventories) + err = d.getObjects(projectID, db.AccessKeyProps, params, nil, &inventories) return } func (d *BoltDb) DeleteInventory(projectID int, inventoryID int) error { - return d.deleteObject(projectID, db.InventoryObject, intObjectID(inventoryID)) + return d.deleteObject(projectID, db.InventoryProps, intObjectID(inventoryID)) } func (d *BoltDb) DeleteInventorySoft(projectID int, inventoryID int) error { - return d.deleteObjectSoft(projectID, db.InventoryObject, intObjectID(inventoryID)) + return d.deleteObjectSoft(projectID, db.InventoryProps, intObjectID(inventoryID)) } func (d *BoltDb) UpdateInventory(inventory db.Inventory) error { - return d.updateObject(inventory.ProjectID, db.InventoryObject, inventory) + return d.updateObject(inventory.ProjectID, db.InventoryProps, inventory) } func (d *BoltDb) CreateInventory(inventory db.Inventory) (db.Inventory, error) { - newInventory, err := d.createObject(inventory.ProjectID, db.InventoryObject, inventory) + newInventory, err := d.createObject(inventory.ProjectID, db.InventoryProps, inventory) return newInventory.(db.Inventory), err } diff --git a/db/bolt/project.go b/db/bolt/project.go index 41fb3407..38b29905 100644 --- a/db/bolt/project.go +++ b/db/bolt/project.go @@ -8,7 +8,7 @@ import ( func (d *BoltDb) CreateProject(project db.Project) (db.Project, error) { project.Created = time.Now() - newProject, err := d.createObject(0, db.ProjectObject, project) + newProject, err := d.createObject(0, db.ProjectProps, project) if err != nil { return db.Project{}, err @@ -20,7 +20,7 @@ func (d *BoltDb) CreateProject(project db.Project) (db.Project, error) { func (d *BoltDb) GetProjects(userID int) (projects []db.Project, err error) { var allProjects []db.Project - err = d.getObjects(0, db.ProjectObject, db.RetrieveQueryParams{}, nil, &allProjects) + err = d.getObjects(0, db.ProjectProps, db.RetrieveQueryParams{}, nil, &allProjects) if err != nil { return @@ -40,14 +40,14 @@ func (d *BoltDb) GetProjects(userID int) (projects []db.Project, err error) { } func (d *BoltDb) GetProject(projectID int) (project db.Project, err error) { - err = d.getObject(0, db.ProjectObject, intObjectID(projectID), &project) + err = d.getObject(0, db.ProjectProps, intObjectID(projectID), &project) return } func (d *BoltDb) DeleteProject(projectID int) error { - return d.deleteObject(0, db.ProjectObject, intObjectID(projectID)) + return d.deleteObject(0, db.ProjectProps, intObjectID(projectID)) } func (d *BoltDb) UpdateProject(project db.Project) error { - return d.updateObject(0, db.ProjectObject, project) + return d.updateObject(0, db.ProjectProps, project) } diff --git a/db/bolt/repository.go b/db/bolt/repository.go index 7d969dc3..defecd68 100644 --- a/db/bolt/repository.go +++ b/db/bolt/repository.go @@ -5,29 +5,29 @@ import ( ) func (d *BoltDb) GetRepository(projectID int, repositoryID int) (repository db.Repository, err error) { - err = d.getObject(projectID, db.RepositoryObject, intObjectID(repositoryID), &repository) + err = d.getObject(projectID, db.RepositoryProps, intObjectID(repositoryID), &repository) return } func (d *BoltDb) GetRepositories(projectID int, params db.RetrieveQueryParams) (repositories []db.Repository, err error) { - err = d.getObjects(projectID, db.RepositoryObject, params, nil, &repositories) + err = d.getObjects(projectID, db.RepositoryProps, params, nil, &repositories) return } func (d *BoltDb) UpdateRepository(repository db.Repository) error { - return d.updateObject(repository.ProjectID, db.RepositoryObject, repository) + return d.updateObject(repository.ProjectID, db.RepositoryProps, repository) } func (d *BoltDb) CreateRepository(repository db.Repository) (db.Repository, error) { - newRepo, err := d.createObject(repository.ProjectID, db.RepositoryObject, repository) + newRepo, err := d.createObject(repository.ProjectID, db.RepositoryProps, repository) return newRepo.(db.Repository), err } func (d *BoltDb) DeleteRepository(projectID int, repositoryId int) error { - return d.deleteObject(projectID, db.RepositoryObject, intObjectID(repositoryId)) + return d.deleteObject(projectID, db.RepositoryProps, intObjectID(repositoryId)) } func (d *BoltDb) DeleteRepositorySoft(projectID int, repositoryId int) error { - return d.deleteObjectSoft(projectID, db.RepositoryObject, intObjectID(repositoryId)) + return d.deleteObjectSoft(projectID, db.RepositoryProps, intObjectID(repositoryId)) } diff --git a/db/bolt/session.go b/db/bolt/session.go index d582f695..b57e55e0 100644 --- a/db/bolt/session.go +++ b/db/bolt/session.go @@ -17,7 +17,7 @@ type globalToken struct { func (d *BoltDb) CreateSession(session db.Session) (db.Session, error) { - newSession, err := d.createObject(session.UserID, db.SessionObject, session) + newSession, err := d.createObject(session.UserID, db.SessionProps, session) if err != nil { return db.Session{}, err } @@ -26,7 +26,7 @@ func (d *BoltDb) CreateSession(session db.Session) (db.Session, error) { func (d *BoltDb) CreateAPIToken(token db.APIToken) (db.APIToken, error) { // create token in bucket "token_" - newToken, err := d.createObject(token.UserID, db.TokenObject, token) + newToken, err := d.createObject(token.UserID, db.TokenProps, token) if err != nil { return db.APIToken{}, err } @@ -46,50 +46,50 @@ func (d *BoltDb) GetAPIToken(tokenID string) (token db.APIToken, err error) { if err != nil { return } - err = d.getObject(t.UserID, db.TokenObject, strObjectID(tokenID), &token) + err = d.getObject(t.UserID, db.TokenProps, strObjectID(tokenID), &token) return } func (d *BoltDb) ExpireAPIToken(userID int, tokenID string) (err error) { var token db.APIToken - err = d.getObject(userID, db.TokenObject, strObjectID(tokenID), &token) + err = d.getObject(userID, db.TokenProps, strObjectID(tokenID), &token) if err != nil { return } token.Expired = true - err = d.updateObject(userID, db.TokenObject, token) + err = d.updateObject(userID, db.TokenProps, token) return } func (d *BoltDb) GetSession(userID int, sessionID int) (session db.Session, err error) { - err = d.getObject(userID, db.SessionObject, intObjectID(sessionID), &session) + err = d.getObject(userID, db.SessionProps, intObjectID(sessionID), &session) return } func (d *BoltDb) ExpireSession(userID int, sessionID int) (err error) { var session db.Session - err = d.getObject(userID, db.SessionObject, intObjectID(sessionID), &session) + err = d.getObject(userID, db.SessionProps, intObjectID(sessionID), &session) if err != nil { return } session.Expired = true - err = d.updateObject(userID, db.SessionObject, session) + err = d.updateObject(userID, db.SessionProps, session) return } func (d *BoltDb) TouchSession(userID int, sessionID int) (err error) { var session db.Session - err = d.getObject(userID, db.SessionObject, intObjectID(sessionID), &session) + err = d.getObject(userID, db.SessionProps, intObjectID(sessionID), &session) if err != nil { return } session.LastActive = time.Now() - err = d.updateObject(userID, db.SessionObject, session) + err = d.updateObject(userID, db.SessionProps, session) return } func (d *BoltDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) { - err = d.getObjects(userID, db.SessionObject, db.RetrieveQueryParams{}, nil, &tokens) + err = d.getObjects(userID, db.SessionProps, db.RetrieveQueryParams{}, nil, &tokens) return } diff --git a/db/bolt/task.go b/db/bolt/task.go index 838470d8..b593babc 100644 --- a/db/bolt/task.go +++ b/db/bolt/task.go @@ -8,16 +8,16 @@ import ( func (d *BoltDb) CreateTask(task db.Task) (newTask db.Task, err error) { task.Created = time.Now() - err = d.getObject(0, db.TaskObject, intObjectID(task.ID), &newTask) + err = d.getObject(0, db.TaskProps, intObjectID(task.ID), &newTask) return } func (d *BoltDb) UpdateTask(task db.Task) error { - return d.updateObject(0, db.TaskObject, task) + return d.updateObject(0, db.TaskProps, task) } func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) { - newOutput, err := d.createObject(output.TaskID, db.TaskOutputObject, output) + newOutput, err := d.createObject(output.TaskID, db.TaskOutputProps, output) if err != nil { return db.TaskOutput{}, err } @@ -25,7 +25,7 @@ func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) { } func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) { - err = d.getObjects(0, db.TaskObject, params, func (tsk interface{}) bool { + err = d.getObjects(0, db.TaskProps, params, func (tsk interface{}) bool { task := tsk.(db.TaskWithTpl) if task.ProjectID != projectID { @@ -70,7 +70,7 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer } func (d *BoltDb) GetTask(projectID int, taskID int) (task db.Task, err error) { - err = d.getObject(0, db.TaskObject, intObjectID(taskID), &task) + err = d.getObject(0, db.TaskProps, intObjectID(taskID), &task) if err != nil { return } @@ -97,13 +97,13 @@ func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) { return } - err = d.deleteObject(0, db.TaskObject, intObjectID(taskID)) + err = d.deleteObject(0, db.TaskProps, intObjectID(taskID)) if err != nil { return } _ = d.db.Update(func (tx *bbolt.Tx) error { - return tx.DeleteBucket(makeBucketId(db.TaskOutputObject, taskID)) + return tx.DeleteBucket(makeBucketId(db.TaskOutputProps, taskID)) }) return @@ -117,7 +117,7 @@ func (d *BoltDb) GetTaskOutputs(projectID int, taskID int) (outputs []db.TaskOut return } - err = d.getObjects(taskID, db.TaskOutputObject, db.RetrieveQueryParams{}, nil, &outputs) + err = d.getObjects(taskID, db.TaskOutputProps, db.RetrieveQueryParams{}, nil, &outputs) return } diff --git a/db/bolt/template.go b/db/bolt/template.go index 56b046fc..fa46189e 100644 --- a/db/bolt/template.go +++ b/db/bolt/template.go @@ -5,24 +5,24 @@ import ( ) func (d *BoltDb) CreateTemplate(template db.Template) (db.Template, error) { - newTemplate, err := d.createObject(template.ProjectID, db.TemplateObject, template) + newTemplate, err := d.createObject(template.ProjectID, db.TemplateProps, template) return newTemplate.(db.Template), err } func (d *BoltDb) UpdateTemplate(template db.Template) error { - return d.updateObject(template.ProjectID, db.TemplateObject, template) + return d.updateObject(template.ProjectID, db.TemplateProps, template) } func (d *BoltDb) GetTemplates(projectID int, params db.RetrieveQueryParams) (templates []db.Template, err error) { - err = d.getObjects(projectID, db.TemplateObject, params, nil, &templates) + err = d.getObjects(projectID, db.TemplateProps, params, nil, &templates) return } func (d *BoltDb) GetTemplate(projectID int, templateID int) (template db.Template, err error) { - err = d.getObject(projectID, db.TemplateObject, intObjectID(templateID), &template) + err = d.getObject(projectID, db.TemplateProps, intObjectID(templateID), &template) return } func (d *BoltDb) DeleteTemplate(projectID int, templateID int) error { - return d.deleteObject(projectID, db.TemplateObject, intObjectID(templateID)) + return d.deleteObject(projectID, db.TemplateProps, intObjectID(templateID)) } diff --git a/db/bolt/user.go b/db/bolt/user.go index 3ddfa8be..6217d0fe 100644 --- a/db/bolt/user.go +++ b/db/bolt/user.go @@ -17,7 +17,7 @@ func (d *BoltDb) CreateUserWithoutPassword(user db.User) (newUser db.User, err e user.Password = "" user.Created = db.GetParsedTime(time.Now()) - usr, err := d.createObject(0, db.UserObject, user) + usr, err := d.createObject(0, db.UserProps, user) if err != nil { return @@ -43,18 +43,18 @@ func (d *BoltDb) CreateUser(user db.UserWithPwd) (newUser db.User, err error) { user.Password = string(pwdHash) user.Created = db.GetParsedTime(time.Now()) - usr, err := d.createObject(0, db.UserObject, user) + usr, err := d.createObject(0, db.UserProps, user) if err != nil { return } - newUser = usr.(db.User) + newUser = usr.(db.UserWithPwd).User return } func (d *BoltDb) DeleteUser(userID int) error { - return d.deleteObject(0, db.UserObject, intObjectID(userID)) + return d.deleteObject(0, db.UserProps, intObjectID(userID)) } func (d *BoltDb) UpdateUser(user db.UserWithPwd) error { @@ -77,7 +77,7 @@ func (d *BoltDb) UpdateUser(user db.UserWithPwd) error { user.Password = password - return d.updateObject(0, db.UserObject, user) + return d.updateObject(0, db.UserProps, user) } func (d *BoltDb) SetUserPassword(userID int, password string) error { @@ -90,11 +90,11 @@ func (d *BoltDb) SetUserPassword(userID int, password string) error { return err } user.Password = string(pwdHash) - return d.updateObject(0, db.UserObject, user) + return d.updateObject(0, db.UserProps, user) } func (d *BoltDb) CreateProjectUser(projectUser db.ProjectUser) (db.ProjectUser, error) { - newProjectUser, err := d.createObject(projectUser.ProjectID, db.ProjectUserObject, projectUser) + newProjectUser, err := d.createObject(projectUser.ProjectID, db.ProjectUserProps, projectUser) if err != nil { return db.ProjectUser{}, err @@ -104,37 +104,37 @@ func (d *BoltDb) CreateProjectUser(projectUser db.ProjectUser) (db.ProjectUser, } func (d *BoltDb) GetProjectUser(projectID, userID int) (user db.ProjectUser, err error) { - err = d.getObject(projectID, db.ProjectUserObject, intObjectID(userID), &user) + err = d.getObject(projectID, db.ProjectUserProps, intObjectID(userID), &user) return } func (d *BoltDb) GetProjectUsers(projectID int, params db.RetrieveQueryParams) (users []db.User, err error) { - err = d.getObjects(projectID, db.ProjectUserObject, params, nil, &users) + err = d.getObjects(projectID, db.ProjectUserProps, params, nil, &users) return } func (d *BoltDb) UpdateProjectUser(projectUser db.ProjectUser) error { - return d.updateObject(projectUser.ProjectID, db.ProjectUserObject, projectUser) + return d.updateObject(projectUser.ProjectID, db.ProjectUserProps, projectUser) } func (d *BoltDb) DeleteProjectUser(projectID, userID int) error { - return d.deleteObject(projectID, db.ProjectUserObject, intObjectID(userID)) + return d.deleteObject(projectID, db.ProjectUserProps, intObjectID(userID)) } //GetUser retrieves a user from the database by ID func (d *BoltDb) GetUser(userID int) (user db.User, err error) { - err = d.getObject(0, db.UserObject, intObjectID(userID), &user) + err = d.getObject(0, db.UserProps, intObjectID(userID), &user) return } func (d *BoltDb) GetUsers(params db.RetrieveQueryParams) (users []db.User, err error) { - err = d.getObjects(0, db.UserObject, params, nil, &users) + err = d.getObjects(0, db.UserProps, params, nil, &users) return } func (d *BoltDb) GetUserByLoginOrEmail(login string, email string) (existingUser db.User, err error) { var users []db.User - err = d.getObjects(0, db.UserObject, db.RetrieveQueryParams{}, nil, &users) + err = d.getObjects(0, db.UserProps, db.RetrieveQueryParams{}, nil, &users) if err != nil { return } diff --git a/db/sql/access_key.go b/db/sql/access_key.go index 20f05fbe..27bf218c 100644 --- a/db/sql/access_key.go +++ b/db/sql/access_key.go @@ -4,13 +4,13 @@ import "github.com/ansible-semaphore/semaphore/db" func (d *SqlDb) GetAccessKey(projectID int, accessKeyID int) (db.AccessKey, error) { var key db.AccessKey - err := d.getObject(projectID, db.AccessKeyObject, accessKeyID, &key) + err := d.getObject(projectID, db.AccessKeyProps, accessKeyID, &key) return key, err } func (d *SqlDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) { var keys []db.AccessKey - err := d.getObjects(projectID, db.AccessKeyObject, params, &keys) + err := d.getObjects(projectID, db.AccessKeyProps, params, &keys) return keys, err } @@ -51,23 +51,23 @@ func (d *SqlDb) CreateAccessKey(key db.AccessKey) (newKey db.AccessKey, err erro } func (d *SqlDb) DeleteAccessKey(projectID int, accessKeyID int) error { - return d.deleteObject(projectID, db.AccessKeyObject, accessKeyID) + return d.deleteObject(projectID, db.AccessKeyProps, accessKeyID) } func (d *SqlDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error { - return d.deleteObjectSoft(projectID, db.AccessKeyObject, accessKeyID) + return d.deleteObjectSoft(projectID, db.AccessKeyProps, accessKeyID) } func (d *SqlDb) GetGlobalAccessKey(accessKeyID int) (db.AccessKey, error) { var key db.AccessKey - err := d.getObject(0, db.GlobalAccessKeyObject, accessKeyID, &key) + err := d.getObject(0, db.GlobalAccessKeyProps, accessKeyID, &key) return key, err } func (d *SqlDb) GetGlobalAccessKeys(params db.RetrieveQueryParams) ([]db.AccessKey, error) { var keys []db.AccessKey - err := d.getObjects(0, db.GlobalAccessKeyObject, params, &keys) + err := d.getObjects(0, db.GlobalAccessKeyProps, params, &keys) return keys, err } @@ -106,9 +106,9 @@ func (d *SqlDb) CreateGlobalAccessKey(key db.AccessKey) (newKey db.AccessKey, er } func (d *SqlDb) DeleteGlobalAccessKey(accessKeyID int) error { - return d.deleteObject(0, db.GlobalAccessKeyObject, accessKeyID) + return d.deleteObject(0, db.GlobalAccessKeyProps, accessKeyID) } func (d *SqlDb) DeleteGlobalAccessKeySoft(accessKeyID int) error { - return d.deleteObjectSoft(0, db.GlobalAccessKeyObject, accessKeyID) + return d.deleteObjectSoft(0, db.GlobalAccessKeyProps, accessKeyID) } diff --git a/db/sql/environment.go b/db/sql/environment.go index 172fb7a3..263a1f2e 100644 --- a/db/sql/environment.go +++ b/db/sql/environment.go @@ -4,13 +4,13 @@ import "github.com/ansible-semaphore/semaphore/db" func (d *SqlDb) GetEnvironment(projectID int, environmentID int) (db.Environment, error) { var environment db.Environment - err := d.getObject(projectID, db.EnvironmentObject, environmentID, &environment) + err := d.getObject(projectID, db.EnvironmentProps, environmentID, &environment) return environment, err } func (d *SqlDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) ([]db.Environment, error) { var environment []db.Environment - err := d.getObjects(projectID, db.EnvironmentObject, params, &environment) + err := d.getObjects(projectID, db.EnvironmentProps, params, &environment) return environment, err } @@ -47,9 +47,9 @@ func (d *SqlDb) CreateEnvironment(env db.Environment) (newEnv db.Environment, er } func (d *SqlDb) DeleteEnvironment(projectID int, environmentID int) error { - return d.deleteObject(projectID, db.EnvironmentObject, environmentID) + return d.deleteObject(projectID, db.EnvironmentProps, environmentID) } func (d *SqlDb) DeleteEnvironmentSoft(projectID int, environmentID int) error { - return d.deleteObjectSoft(projectID, db.EnvironmentObject, environmentID) + return d.deleteObjectSoft(projectID, db.EnvironmentProps, environmentID) } diff --git a/db/sql/inventory.go b/db/sql/inventory.go index 47e3707e..61ef3d26 100644 --- a/db/sql/inventory.go +++ b/db/sql/inventory.go @@ -3,7 +3,7 @@ package sql import "github.com/ansible-semaphore/semaphore/db" func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inventory, err error) { - err = d.getObject(projectID, db.InventoryObject, inventoryID, &inventory) + err = d.getObject(projectID, db.InventoryProps, inventoryID, &inventory) if err != nil { return } @@ -24,16 +24,16 @@ func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inven func (d *SqlDb) GetInventories(projectID int, params db.RetrieveQueryParams) ([]db.Inventory, error) { var inventories []db.Inventory - err := d.getObjects(projectID, db.InventoryObject, params, &inventories) + err := d.getObjects(projectID, db.InventoryProps, params, &inventories) return inventories, err } func (d *SqlDb) DeleteInventory(projectID int, inventoryID int) error { - return d.deleteObject(projectID, db.InventoryObject, inventoryID) + return d.deleteObject(projectID, db.InventoryProps, inventoryID) } func (d *SqlDb) DeleteInventorySoft(projectID int, inventoryID int) error { - return d.deleteObjectSoft(projectID, db.InventoryObject, inventoryID) + return d.deleteObjectSoft(projectID, db.InventoryProps, inventoryID) } func (d *SqlDb) UpdateInventory(inventory db.Inventory) error { diff --git a/db/sql/repository.go b/db/sql/repository.go index 979fabb1..93939c7d 100644 --- a/db/sql/repository.go +++ b/db/sql/repository.go @@ -7,7 +7,7 @@ import ( func (d *SqlDb) GetRepository(projectID int, repositoryID int) (db.Repository, error) { var repository db.Repository - err := d.getObject(projectID, db.RepositoryObject, repositoryID, &repository) + err := d.getObject(projectID, db.RepositoryProps, repositoryID, &repository) if err != nil { return repository, err @@ -85,10 +85,10 @@ func (d *SqlDb) CreateRepository(repository db.Repository) (newRepo db.Repositor } func (d *SqlDb) DeleteRepository(projectID int, repositoryId int) error { - return d.deleteObject(projectID, db.RepositoryObject, repositoryId) + return d.deleteObject(projectID, db.RepositoryProps, repositoryId) } func (d *SqlDb) DeleteRepositorySoft(projectID int, repositoryId int) error { - return d.deleteObjectSoft(projectID, db.RepositoryObject, repositoryId) + return d.deleteObjectSoft(projectID, db.RepositoryProps, repositoryId) } diff --git a/util/config.go b/util/config.go index 2eb0feac..b36115c1 100644 --- a/util/config.go +++ b/util/config.go @@ -43,6 +43,7 @@ type DbDriver int const ( DbDriverMySQL DbDriver = iota + DbDriverBolt ) type DbConfig struct { @@ -63,6 +64,7 @@ type ldapMappings struct { //ConfigType mapping between Config and the json file that sets it type ConfigType struct { MySQL DbConfig `json:"mysql"` + BoltDb DbConfig `json:"bolt"` // Format `:port_num` eg, :3000 // if : is missing it will be corrected @@ -279,6 +281,8 @@ func (d *DbConfig) HasSupportMultipleDatabases() bool { func (d *DbConfig) GetConnectionString(includeDbName bool) (connectionString string, err error) { switch d.Dialect { + case DbDriverBolt: + connectionString = d.Hostname case DbDriverMySQL: if includeDbName { connectionString = fmt.Sprintf( @@ -305,6 +309,9 @@ func (conf *ConfigType) GetDBConfig() (dbConfig DbConfig, err error) { case conf.MySQL.isPresent(): dbConfig = conf.MySQL dbConfig.Dialect = DbDriverMySQL + case conf.BoltDb.isPresent(): + dbConfig = conf.BoltDb + dbConfig.Dialect = DbDriverBolt default: err = errors.New("database configuration not found") } @@ -320,6 +327,14 @@ func (conf *ConfigType) GenerateCookieSecrets() { conf.CookieEncryption = base64.StdEncoding.EncodeToString(encryption) } +func (conf *ConfigType) ScanBoltDb() { + fmt.Print(" > DB filename (default /tmp/boltdb): ") + ScanErrorChecker(fmt.Scanln(&conf.BoltDb.Hostname)) + if len(conf.BoltDb.Hostname) == 0 { + conf.BoltDb.Hostname = "/tmp/boltdb" + } +} + func (conf *ConfigType) ScanMySQL() { fmt.Print(" > DB Hostname (default 127.0.0.1:3306): ") @@ -349,12 +364,15 @@ func (conf *ConfigType) Scan() { db := 1 fmt.Println(" > DB") fmt.Println(" 1 - MySQL") + fmt.Println(" 2 - bbolt") fmt.Print(" (default 1): ") ScanErrorChecker(fmt.Scanln(&db)) switch db { case 1: conf.ScanMySQL() + case 2: + conf.ScanBoltDb() } fmt.Print(" > Playbook path (default /tmp/semaphore): ")