From 328bb0153b1fe88c8addb4f950e7188b0026a446 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Mon, 27 Nov 2017 07:43:52 +0100 Subject: [PATCH] Add session model --- db.go | 23 +++++++++++++++++++++++ dbinit.go | 23 +++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/db.go b/db.go index 4fa7097..59e0180 100644 --- a/db.go +++ b/db.go @@ -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_-]*") diff --git a/dbinit.go b/dbinit.go index a076c2d..0893b93 100644 --- a/dbinit.go +++ b/dbinit.go @@ -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 {