node-api/apps/main.go

175 lines
3.5 KiB
Go
Raw Normal View History

2020-07-08 22:09:19 +00:00
package apps
2020-07-11 11:04:37 +00:00
import (
2020-07-13 22:01:42 +00:00
"github.com/rosti-cz/node-api/common"
2020-07-11 11:04:37 +00:00
)
2020-07-08 22:09:19 +00:00
func init() {
db := common.GetDBConnection()
db.AutoMigrate(Label{})
db.AutoMigrate(App{})
}
// Get returns one app
func Get(name string) (*App, error) {
var app App
db := common.GetDBConnection()
2020-07-16 21:24:09 +00:00
err := db.Preload("Labels").First(&app).Where("name = ?", name).Error
2020-07-08 22:09:19 +00:00
if err != nil {
return nil, err
}
return &app, nil
}
// List returns all apps located on this node
func List() (*[]App, error) {
var apps []App
db := common.GetDBConnection()
2020-07-16 21:24:09 +00:00
err := db.Preload("Labels").Find(&apps).Error
2020-07-08 22:09:19 +00:00
if err != nil {
return nil, err
}
return &apps, nil
}
// New creates new record about application in the database
2020-07-09 22:27:23 +00:00
func New(name string, SSHPort int, HTTPPort int, image string, CPU int, memory int) error {
2020-07-08 22:09:19 +00:00
app := App{
Name: name,
SSHPort: SSHPort,
HTTPPort: HTTPPort,
Image: image,
CPU: CPU,
Memory: memory,
}
db := common.GetDBConnection()
2020-07-09 22:27:23 +00:00
validationErrors := app.Validate()
if len(validationErrors) != 0 {
return ValidationError{
Errors: validationErrors,
}
}
2020-07-11 11:04:37 +00:00
if err := db.Create(&app).Error; err != nil {
2020-07-08 22:09:19 +00:00
return err
}
return nil
}
// Update changes value about app in the database
2020-07-11 11:04:37 +00:00
func Update(name string, SSHPort int, HTTPPort int, image string, CPU int, memory int) (*App, error) {
2020-07-08 22:09:19 +00:00
var app App
db := common.GetDBConnection()
err := db.First(&app).Where("name = ?", name).Error
if err != nil {
2020-07-11 11:04:37 +00:00
return &app, err
}
// Update affected fields
if image != "" {
app.Image = image
2020-07-08 22:09:19 +00:00
}
2020-07-11 11:04:37 +00:00
if CPU != 0 {
app.CPU = CPU
}
if memory != 0 {
app.Memory = memory
}
// SSH port and HTTP port cannot be turned off when they are once set
if SSHPort != 0 {
app.SSHPort = SSHPort
}
// SSH port and HTTP port cannot be turned off when they are once set
if HTTPPort != 0 {
app.HTTPPort = HTTPPort
}
2020-07-09 22:27:23 +00:00
validationErrors := app.Validate()
if len(validationErrors) != 0 {
2020-07-11 11:04:37 +00:00
return &app, ValidationError{
2020-07-09 22:27:23 +00:00
Errors: validationErrors,
}
}
2020-07-11 11:04:37 +00:00
// Apply the changes
err = db.Save(&app).Error
return &app, err
2020-07-08 22:09:19 +00:00
}
2020-07-16 17:05:38 +00:00
// UpdateState sets state
func UpdateState(name string, state string, CPUUsage float64, memory int, diskUsageBytes int, diskUsageInodes int) error {
db := common.GetDBConnection()
err := db.Model(&App{}).Updates(App{
State: state,
CPUUsage: CPUUsage,
MemoryUsage: memory,
DiskUsageBytes: diskUsageBytes,
DiskUsageInodes: diskUsageInodes,
}).Where("name = ?", name).Error
return err
}
2020-07-08 22:09:19 +00:00
// Delete removes records about one app from the database
func Delete(name string) error {
db := common.GetDBConnection()
err := db.Delete(App{}).Where("name = ?", name).Error
return err
}
2020-07-16 21:24:09 +00:00
// AddLabel adds label to existing application
func AddLabel(appName string, label string) error {
var app App
db := common.GetDBConnection()
err := db.First(&app).Where("name = ?", appName).Error
if err != nil {
return err
}
// Check if there is such label already
var count int
err = db.Model(&Label{}).Where("value = ? AND app_id = ?", label, app.ID).Count(&count).Error
if err != nil {
return err
}
if count > 0 {
return nil
}
// And create the label
return db.Create(&Label{AppID: app.ID, Value: label}).Error
}
// RemoveLabel removes label from existing application
func RemoveLabel(appName string, label string) error {
var app App
db := common.GetDBConnection()
err := db.First(&app).Where("name = ?", appName).Error
if err != nil {
return err
}
return db.Delete(&Label{}).Where("label = ? AND app_id = ?", label, app.ID).Error
}