package model
import (
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
wmodel "github.com/goodrain/rainbond/worker/discover/model"
)
var _ ComponentOpReq = &ComponentStartReq{}
var _ ComponentOpReq = &ComponentStopReq{}
var _ ComponentOpReq = &ComponentBuildReq{}
var _ ComponentOpReq = &ComponentUpgradeReq{}
type BatchOpRequesters []ComponentOpReq
func (b BatchOpRequesters) ComponentIDs() []string {
var componentIDs []string
for _, item := range b {
componentIDs = append(componentIDs, item.GetComponentID())
}
return componentIDs
}
type ComponentOpReq interface {
GetComponentID() string
GetEventID() string
TaskBody(component *dbmodel.TenantServices) interface{}
BatchOpFailureItem() *ComponentOpResult
UpdateConfig(key, value string)
OpType() string
SetVersion(version string)
GetVersion() string
}
type BatchOpResult []*ComponentOpResult
type BatchOpResultItemStatus string
var (
BatchOpResultItemStatusFailure BatchOpResultItemStatus = "failure"
BatchOpResultItemStatusSuccess BatchOpResultItemStatus = "success"
)
type ComponentOpResult struct {
ServiceID string `json:"service_id"`
Operation string `json:"operation"`
EventID string `json:"event_id"`
Status BatchOpResultItemStatus `json:"status"`
ErrMsg string `json:"err_message"`
DeployVersion string `json:"deploy_version"`
}
func (b *ComponentOpResult) Success() {
b.Status = BatchOpResultItemStatusSuccess
}
type ComponentOpGeneralReq struct {
EventID string `json:"event_id"`
ServiceID string `json:"service_id"`
Configs map[string]string `json:"configs"`
DepServiceIDInBootSeq []string `json:"dep_service_ids_in_boot_seq"`
}
func (b *ComponentOpGeneralReq) UpdateConfig(key, value string) {
if b.Configs == nil {
b.Configs = make(map[string]string)
}
b.Configs[key] = value
}
type ComponentStartReq struct {
ComponentOpGeneralReq
}
func (s *ComponentStartReq) GetEventID() string {
if s.EventID == "" {
s.EventID = util.NewUUID()
}
return s.EventID
}
func (s *ComponentStartReq) GetVersion() string {
return ""
}
func (s *ComponentStartReq) SetVersion(string) {
}
func (s *ComponentStartReq) GetComponentID() string {
return s.ServiceID
}
func (s *ComponentStartReq) TaskBody(cpt *dbmodel.TenantServices) interface{} {
return &wmodel.StartTaskBody{
TenantID: cpt.TenantID,
ServiceID: cpt.ServiceID,
DeployVersion: cpt.DeployVersion,
EventID: s.GetEventID(),
Configs: s.Configs,
DepServiceIDInBootSeq: s.DepServiceIDInBootSeq,
}
}
func (s *ComponentStartReq) OpType() string {
return "start-service"
}
func (s *ComponentStartReq) BatchOpFailureItem() *ComponentOpResult {
return &ComponentOpResult{
ServiceID: s.ServiceID,
EventID: s.GetEventID(),
Operation: "start",
Status: BatchOpResultItemStatusFailure,
}
}
type ComponentStopReq struct {
ComponentStartReq
}
func (s *ComponentStopReq) OpType() string {
return "stop-service"
}
func (s *ComponentStopReq) BatchOpFailureItem() *ComponentOpResult {
return &ComponentOpResult{
ServiceID: s.ServiceID,
EventID: s.GetEventID(),
Operation: "stop",
Status: BatchOpResultItemStatusFailure,
}
}