feat(scheduledtask): support sync cloudaccount

This commit is contained in:
rainzm
2021-11-15 19:01:10 +08:00
parent 6285fdb53a
commit 1b9d4a8f15
3 changed files with 54 additions and 31 deletions
@@ -21,11 +21,13 @@ const (
ST_STATUS_READY = "ready"
ST_STATUS_CREATE_FAILED = "create_failed"
ST_RESOURCE_SERVER = "server"
ST_RESOURCE_SERVER = "server"
ST_RESOURCE_CLOUDACCOUNT = "cloudaccount"
ST_RESOURCE_OPERATION_START = "start"
ST_RESOURCE_OPERATION_STOP = "stop"
ST_RESOURCE_OPERATION_RESTART = "restart"
ST_RESOURCE_OPERATION_SYNC = "sync"
ST_LABEL_ID = "id"
ST_LABEL_TAG = "tag"
@@ -35,4 +37,9 @@ const (
ST_ACTIVITY_STATUS_PART_SUCCEED = "part_succeed" // 部分成功
ST_ACTIVITY_STATUS_FAILED = "failed" // 失败
ST_ACTIVITY_STATUS_REJECT = "reject" // 拒绝
TIMER_TYPE_ONCE = "once"
TIMER_TYPE_DAY = "day"
TIMER_TYPE_WEEK = "week"
TIMER_TYPE_MONTH = "month"
)
+45 -29
View File
@@ -29,16 +29,17 @@ import (
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/compute"
comapi "yunion.io/x/onecloud/pkg/apis/compute"
api "yunion.io/x/onecloud/pkg/apis/scheduledtask"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
cop "yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/options"
sop "yunion.io/x/onecloud/pkg/scheduledtask/options"
"yunion.io/x/onecloud/pkg/util/httputils"
"yunion.io/x/onecloud/pkg/util/logclient"
"yunion.io/x/onecloud/pkg/util/stringutils2"
@@ -101,7 +102,7 @@ func (stm *SScheduledTaskManager) ListItemFilter(ctx context.Context, q *sqlchem
}
func (stm *SScheduledTaskManager) OrderByExtraFields(ctx context.Context, q *sqlchemy.SQuery,
userCred mcclient.TokenCredential, query api.ScalingPolicyListInput) (*sqlchemy.SQuery, error) {
userCred mcclient.TokenCredential, query api.ScheduledTaskListInput) (*sqlchemy.SQuery, error) {
return stm.SVirtualResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.VirtualResourceListInput)
}
@@ -159,10 +160,10 @@ func (stm *SScheduledTaskManager) ValidateCreateData(ctx context.Context, userCr
if !utils.IsInStringArray(input.ScheduledType, []string{api.ST_TYPE_TIMING, api.ST_TYPE_CYCLE}) {
return input, httperrors.NewInputParameterError("unkown scheduled type '%s'", input.ScheduledType)
}
if !utils.IsInStringArray(input.ResourceType, []string{api.ST_RESOURCE_SERVER}) {
if !utils.IsInStringArray(input.ResourceType, []string{api.ST_RESOURCE_SERVER, api.ST_RESOURCE_CLOUDACCOUNT}) {
return input, httperrors.NewInputParameterError("unkown resource type '%s'", input.ResourceType)
}
if !utils.IsInStringArray(input.Operation, []string{api.ST_RESOURCE_OPERATION_RESTART, api.ST_RESOURCE_OPERATION_STOP, api.ST_RESOURCE_OPERATION_START}) {
if !utils.IsInStringArray(input.Operation, []string{api.ST_RESOURCE_OPERATION_RESTART, api.ST_RESOURCE_OPERATION_STOP, api.ST_RESOURCE_OPERATION_START, api.ST_RESOURCE_OPERATION_SYNC}) {
return input, httperrors.NewInputParameterError("unkown resource operation '%s'", input.Operation)
}
if !utils.IsInStringArray(input.LabelType, []string{api.ST_LABEL_ID, api.ST_LABEL_TAG}) {
@@ -548,8 +549,9 @@ var timerQueue chan struct{}
func (stm *SScheduledTaskManager) Timer(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
if timerQueue == nil {
timerQueue = make(chan struct{}, cop.Options.ScheduledTaskQueueSize)
timerQueue = make(chan struct{}, sop.Options.ScheduledTaskQueueSize)
}
log.Infof("queueSize: %s", sop.Options.ScheduledTaskQueueSize)
// 60 is for fault tolerance
interval := 60 + 30
timeScope := stm.timeScope(time.Now(), time.Duration(interval)*time.Second)
@@ -603,6 +605,7 @@ func (stm *SScheduledTaskManager) Timer(ctx context.Context, userCred mcclient.T
func init() {
Register(ResourceServer, modules.Servers.ResourceManager)
Register(ResourceCloudAccount, modules.Cloudaccounts)
}
// Modules describe the correspondence between Resource and modulebase.ResourceManager,
@@ -618,7 +621,8 @@ func Register(resource Resource, manager modulebase.ResourceManager) {
type Resource string
const (
ResourceServer Resource = api.ST_RESOURCE_SERVER
ResourceServer Resource = api.ST_RESOURCE_SERVER
ResourceCloudAccount Resource = api.ST_RESOURCE_CLOUDACCOUNT
)
// ResourceOperation describe the operation for onecloud resource like create, update, delete and so on.
@@ -627,6 +631,7 @@ type ResourceOperation struct {
Operation string
StatusSuccess []string
Fail []ResourceOperationFail
Params *jsonutils.JSONDict
}
type ResourceOperationFail struct {
@@ -635,37 +640,54 @@ type ResourceOperationFail struct {
}
// It is clearer to write each ResourceOperation as a constant
var (
func init() {
ServerStart = ResourceOperation{
Resource: ResourceServer,
Operation: api.ST_RESOURCE_OPERATION_START,
StatusSuccess: []string{api.VM_RUNNING},
StatusSuccess: []string{comapi.VM_RUNNING},
Fail: []ResourceOperationFail{
{api.VM_START_FAILED, db.ACT_START_FAIL},
{comapi.VM_START_FAILED, db.ACT_START_FAIL},
},
}
ServerStop = ResourceOperation{
Resource: ResourceServer,
Operation: api.ST_RESOURCE_OPERATION_STOP,
StatusSuccess: []string{api.VM_READY},
StatusSuccess: []string{comapi.VM_READY},
Fail: []ResourceOperationFail{
{api.VM_STOP_FAILED, db.ACT_STOP_FAIL},
{comapi.VM_STOP_FAILED, db.ACT_STOP_FAIL},
},
}
ServerRestart = ResourceOperation{
Resource: ResourceServer,
Operation: api.ST_RESOURCE_OPERATION_RESTART,
StatusSuccess: []string{api.VM_RUNNING},
StatusSuccess: []string{comapi.VM_RUNNING},
Fail: []ResourceOperationFail{
{api.VM_START_FAILED, db.ACT_START_FAIL},
{api.VM_STOP_FAILED, db.ACT_STOP_FAIL},
{comapi.VM_START_FAILED, db.ACT_START_FAIL},
{comapi.VM_STOP_FAILED, db.ACT_STOP_FAIL},
},
}
ResourceOperationMap = map[string]ResourceOperation{
fmt.Sprintf("%s.%s", ResourceServer, api.ST_RESOURCE_OPERATION_START): ServerStart,
fmt.Sprintf("%s.%s", ResourceServer, api.ST_RESOURCE_OPERATION_STOP): ServerStop,
fmt.Sprintf("%s.%s", ResourceServer, api.ST_RESOURCE_OPERATION_RESTART): ServerRestart,
paramsAccoutSync := jsonutils.NewDict()
paramsAccoutSync.Add(jsonutils.JSONTrue, "full_sync")
paramsAccoutSync.Add(jsonutils.JSONFalse, "force")
CloudAccountSync = ResourceOperation{
Resource: ResourceCloudAccount,
Operation: api.ST_RESOURCE_OPERATION_SYNC,
Params: paramsAccoutSync,
}
ResourceOperationMap = map[string]ResourceOperation{
fmt.Sprintf("%s.%s", ResourceServer, api.ST_RESOURCE_OPERATION_START): ServerStart,
fmt.Sprintf("%s.%s", ResourceServer, api.ST_RESOURCE_OPERATION_STOP): ServerStop,
fmt.Sprintf("%s.%s", ResourceServer, api.ST_RESOURCE_OPERATION_RESTART): ServerRestart,
fmt.Sprintf("%s.%s", ResourceCloudAccount, api.ST_RESOURCE_OPERATION_SYNC): CloudAccountSync,
}
}
var (
ServerStart ResourceOperation
ServerStop ResourceOperation
ServerRestart ResourceOperation
CloudAccountSync ResourceOperation
ResourceOperationMap map[string]ResourceOperation
)
// Action itself is meaningless, a meaningful Action is generated by
@@ -676,10 +698,9 @@ var Action = SAction{timeout: 5 * time.Minute}
// SAction encapsulates action to for onecloud resources
type SAction struct {
operation ResourceOperation
session *mcclient.ClientSession
defautParams *jsonutils.JSONDict
timeout time.Duration
operation ResourceOperation
session *mcclient.ClientSession
timeout time.Duration
}
func (r SAction) ResourceOperation(oper ResourceOperation) SAction {
@@ -697,11 +718,6 @@ func (r SAction) Timeout(time time.Duration) SAction {
return r
}
func (r SAction) DefaultParams(dict *jsonutils.JSONDict) SAction {
r.defautParams = dict
return r
}
type WrapperListOptions struct {
options.BaseListOptions
}
@@ -744,7 +760,7 @@ func (r SAction) Apply(id string) (success bool, failReason string) {
_, err := resourceManager.PerformAction(session, id, action, params)
return err
}
err := requestFunc(r.session, id, r.defautParams)
err := requestFunc(r.session, id, r.operation.Params)
if err != nil {
clientErr, _ := err.(*httputils.JSONClientError)
return false, clientErr.Details
+1 -1
View File
@@ -24,7 +24,7 @@ import (
"yunion.io/x/log"
api "yunion.io/x/onecloud/pkg/apis/compute"
api "yunion.io/x/onecloud/pkg/apis/scheduledtask"
"yunion.io/x/onecloud/pkg/i18n"
"yunion.io/x/onecloud/pkg/util/bitmap"
)