gocron/models/host.go

68 lines
1.7 KiB
Go
Raw Normal View History

2017-03-10 09:24:06 +00:00
package models
// 主机
type Host struct {
2017-04-02 02:19:52 +00:00
Id int16 `xorm:"smallint pk autoincr"`
Name string `xorm:"varchar(128) notnull"` // 主机名称
Alias string `xorm:"varchar(32) notnull default '' "` // 主机别名
Username string `xorm:"varchar(32) notnull default '' "` // ssh 用户名
Password string `xorm:"varchar(64) notnull default ''"` // ssh 密码
Port int `xorm:"notnull default 22"` // 主机端口
LoginType LoginType `xorm:"tinyint notnull default 1"` // ssh登录方式 1:公钥认证 2:账号密码
Remark string `xorm:"varchar(512) notnull default '' "` // 备注
Page int `xorm:"-"`
PageSize int `xorm:"-"`
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
type LoginType int8
2017-03-23 05:31:16 +00:00
const (
2017-04-02 02:19:52 +00:00
PublicKey = 1
2017-03-23 05:31:16 +00:00
UserPassword = 2
)
2017-03-10 09:24:06 +00:00
// 新增
2017-04-02 02:19:52 +00:00
func (host *Host) Create() (insertId int16, err error) {
_, err = Db.Insert(host)
2017-03-24 09:55:44 +00:00
if err == nil {
insertId = host.Id
}
return
2017-03-10 09:24:06 +00:00
}
// 更新
2017-04-02 02:19:52 +00:00
func (host *Host) Update(id int, data CommonMap) (int64, error) {
2017-03-10 09:24:06 +00:00
return Db.Table(host).ID(id).Update(data)
}
// 删除
2017-04-02 02:19:52 +00:00
func (host *Host) Delete(id int) (int64, error) {
2017-03-10 09:24:06 +00:00
return Db.Id(id).Delete(host)
}
2017-04-02 02:19:52 +00:00
func (host *Host) List() ([]Host, error) {
2017-03-10 09:24:06 +00:00
host.parsePageAndPageSize()
list := make([]Host, 0)
err := Db.Desc("id").Limit(host.PageSize, host.pageLimitOffset()).Find(&list)
return list, err
}
2017-04-02 02:19:52 +00:00
func (host *Host) Total() (int64, error) {
2017-03-10 09:24:06 +00:00
return Db.Count(host)
}
2017-04-02 02:19:52 +00:00
func (host *Host) parsePageAndPageSize() {
if host.Page <= 0 {
2017-03-10 09:24:06 +00:00
host.Page = Page
}
2017-04-02 02:19:52 +00:00
if host.PageSize >= 0 || host.PageSize > MaxPageSize {
2017-03-10 09:24:06 +00:00
host.PageSize = PageSize
}
}
2017-04-02 02:19:52 +00:00
func (host *Host) pageLimitOffset() int {
2017-03-10 09:24:06 +00:00
return (host.Page - 1) * host.PageSize
2017-04-02 02:19:52 +00:00
}