Add session model

This commit is contained in:
Manfred Touron 2017-11-27 07:43:52 +01:00
parent 1ddd6867b6
commit 328bb0153b
2 changed files with 44 additions and 2 deletions

23
db.go
View file

@ -1,3 +1,4 @@
//go:generate stringer -type=SessionStatus
package main
import (
@ -21,6 +22,7 @@ type Config struct {
HostGroups []*HostGroup `json:"host_groups"`
ACLs []*ACL `json:"acls"`
Settings []*Setting `json:"settings"`
Sessions []*Session `json:"sessions"`
Date time.Time `json:"date"`
}
@ -109,6 +111,27 @@ type ACL struct {
Comment string `valid:"optional"`
}
type Session struct {
gorm.Model
StoppedAt time.Time `valid:"optional"`
Status string `valid:"required"`
User *User `gorm:"ForeignKey:UserID"`
Host *Host `gorm:"ForeignKey:HostID"`
UserID uint `valid:"optional"`
HostID uint `valid:"optional"`
ErrMsg string `valid:"optional"`
Comment string `valid:"optional"`
}
type SessionStatus string
const (
Unknown SessionStatus = "unknown"
Active = "active"
Closed = "closed"
Error = "error"
)
func init() {
unixUserRegexp := regexp.MustCompile("[a-z_][a-z0-9_-]*")

View file

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"time"
"github.com/go-gormigrate/gormigrate"
"github.com/jinzhu/gorm"
@ -24,8 +25,7 @@ func dbInit(db *gorm.DB) error {
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("settings").Error
},
},
{
}, {
ID: "2",
Migrate: func(tx *gorm.DB) error {
type SSHKey struct {
@ -292,6 +292,25 @@ func dbInit(db *gorm.DB) error {
Rollback: func(tx *gorm.DB) error {
return tx.Where("name = ?", "listhosts").Delete(&UserRole{}).Error
},
}, {
ID: "21",
Migrate: func(tx *gorm.DB) error {
type Session struct {
gorm.Model
StoppedAt time.Time `valid:"optional"`
Status string `valid:"required"`
User *User `gorm:"ForeignKey:UserID"`
Host *Host `gorm:"ForeignKey:HostID"`
UserID uint `valid:"optional"`
HostID uint `valid:"optional"`
ErrMsg string `valid:"optional"`
Comment string `valid:"optional"`
}
return tx.AutoMigrate(&Session{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("sessions").Error
},
},
})
if err := m.Migrate(); err != nil {