/
router_version.go
92 lines (78 loc) · 2.91 KB
/
router_version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package models
import (
"database/sql"
"github.com/jinzhu/gorm"
)
type RouterVersionStatus string
const (
RouterVersionStatusPending RouterVersionStatus = "pending"
RouterVersionStatusFailed RouterVersionStatus = "failed"
RouterVersionStatusDeployed RouterVersionStatus = "deployed"
RouterVersionStatusUndeployed RouterVersionStatus = "undeployed"
)
// RouterVersion contains the configuration of a version of a router.
// Every change in configuration should always result in a new instance of
// RouterVersion.
type RouterVersion struct {
Model
// Router this RouterVersion is associated with.
RouterID ID `json:"-"`
Router *Router `json:"router" gorm:"association_autoupdate:false"`
// Version of Router configuration.
Version uint `json:"version"`
// Status of the RouterVersion. Indicates the deployment status of the configuration.
Status RouterVersionStatus `json:"status"`
// Last known error if the status is error
Error string `json:"error,omitempty"`
// Image of the router deployed
Image string `json:"image"`
// Downstream endpoints for the router
Routes Routes `json:"routes"`
// Default route
DefaultRouteID string `json:"default_route_id"`
// Rules for activating some routes based on request conditions.
TrafficRules TrafficRules `json:"rules,omitempty"`
// Configuration for the experiment engine queried by the router.
ExperimentEngine *ExperimentEngine `json:"experiment_engine"`
// Resource requests for deployment
ResourceRequest *ResourceRequest `json:"resource_request"`
// Request timeout as a valid quantity string
Timeout string `json:"timeout"`
// Logging configuration for the router
LogConfig *LogConfig `json:"log_config"`
// The enricher used by the router
EnricherID sql.NullInt32 `json:"-"`
Enricher *Enricher `json:"enricher,omitempty"`
// The ensembler used by the router
EnsemblerID sql.NullInt32 `json:"-"`
Ensembler *Ensembler `json:"ensembler,omitempty"`
// Monitoring URL used in the monitoring tab
MonitoringURL string `json:"monitoring_url" gorm:"-"`
}
// SetEnsemblerID Sets the id of the associated Ensembler
func (r *RouterVersion) SetEnsemblerID(ensemblerID ID) {
r.EnsemblerID = sql.NullInt32{Int32: int32(ensemblerID), Valid: true}
}
// SetEnricherID Sets the id of the associated Enricher
func (r *RouterVersion) SetEnricherID(enricherID ID) {
r.EnricherID = sql.NullInt32{Int32: int32(enricherID), Valid: true}
}
// BeforeCreate Sets version before creating
func (r *RouterVersion) BeforeCreate(tx *gorm.DB) error {
var latestVersion RouterVersion
err := tx.Select("router_versions.*").
Where("router_id = ?", r.RouterID).
Order("version desc").
FirstOrInit(&latestVersion, &RouterVersion{Version: 0}).Error
if err != nil {
return err
}
r.Version = latestVersion.Version + 1
return nil
}
func (r *RouterVersion) HasDockerConfig() bool {
if r.Ensembler != nil && r.Ensembler.DockerConfig != nil {
return true
}
return false
}