mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-20 17:36:01 +08:00
fix: 修复资源组云上同步异常问题
This commit is contained in:
@@ -25,8 +25,7 @@ const (
|
||||
)
|
||||
|
||||
type ExternalProjectDetails struct {
|
||||
apis.StatusStandaloneResourceDetails
|
||||
apis.ProjectizedResourceInfo
|
||||
apis.VirtualResourceDetails
|
||||
ManagedResourceInfo
|
||||
|
||||
SExternalProject
|
||||
|
||||
@@ -61,8 +61,7 @@ type CachedimageListInput struct {
|
||||
}
|
||||
|
||||
type ExternalProjectListInput struct {
|
||||
apis.StatusStandaloneResourceListInput
|
||||
apis.ProjectizedResourceListInput
|
||||
apis.VirtualResourceListInput
|
||||
apis.ExternalizedResourceBaseListInput
|
||||
|
||||
ManagedResourceListInput
|
||||
|
||||
@@ -242,15 +242,21 @@ func (self *SESXiGuestDriver) RequestDeployGuestOnHost(ctx context.Context, gues
|
||||
|
||||
action, _ := config.GetString("action")
|
||||
if action == "create" {
|
||||
extProj, name, err := account.GetExternalProject(ctx, task.GetUserCred(), guest.ProjectId)
|
||||
project, err := db.TenantCacheManager.FetchTenantById(ctx, guest.ProjectId)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get external project %s from account %s(%s) error: %v", guest.ProjectId, account.Name, account.Id, err)
|
||||
return errors.Wrapf(err, "FetchTenantById(%s)", guest.ProjectId)
|
||||
}
|
||||
|
||||
projects, err := account.GetExternalProjectsByProjectIdOrName(project.Id, project.Name)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetExternalProjectsByProjectIdOrName(%s,%s)", project.Id, project.Name)
|
||||
}
|
||||
|
||||
extProj := account.GetAvailableExternalProject(project, projects)
|
||||
if extProj != nil {
|
||||
config.Add(jsonutils.NewString(extProj.ExternalId), "desc", "group_id")
|
||||
}
|
||||
if len(name) > 0 {
|
||||
config.Add(jsonutils.NewString(name), "desc", "resource_pool")
|
||||
config.Add(jsonutils.NewString(extProj.Name), "desc", "resource_pool")
|
||||
} else {
|
||||
config.Add(jsonutils.NewString(project.Name), "desc", "resource_pool")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2774,6 +2774,22 @@ func (self *SCloudaccount) GetExternalProjects() ([]SExternalProject, error) {
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func (self *SCloudaccount) GetExternalProjectsByProjectIdOrName(projectId, name string) ([]SExternalProject, error) {
|
||||
projects := []SExternalProject{}
|
||||
q := ExternalProjectManager.Query().Equals("cloudaccount_id", self.Id)
|
||||
q = q.Filter(
|
||||
sqlchemy.OR(
|
||||
sqlchemy.Equals(q.Field("name"), name),
|
||||
sqlchemy.Equals(q.Field("tenant_id"), projectId),
|
||||
),
|
||||
)
|
||||
err := db.FetchModelObjects(ExternalProjectManager, q, &projects)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "db.FetchModelObjects")
|
||||
}
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func (manager *SCloudaccountManager) queryCloudAccountByCapability(region *SCloudregion, zone *SZone, domainId string, enabled tristate.TriState, capability string) *sqlchemy.SQuery {
|
||||
providers := CloudproviderManager.Query().SubQuery()
|
||||
q := manager.Query()
|
||||
@@ -2866,64 +2882,77 @@ func (account *SCloudaccount) GetUsages() []db.IUsage {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SCloudaccount) GetExternalProject(ctx context.Context, userCred mcclient.TokenCredential, id string) (*SExternalProject, string, error) {
|
||||
projects, err := self.GetExternalProjects()
|
||||
if err != nil {
|
||||
return nil, "", errors.Wrap(err, "GetExternalProjects")
|
||||
}
|
||||
for i := range projects {
|
||||
if projects[i].ProjectId == id && projects[i].Status == api.EXTERNAL_PROJECT_STATUS_AVAILABLE {
|
||||
return &projects[i], projects[i].Name, nil
|
||||
}
|
||||
}
|
||||
|
||||
project, err := db.TenantCacheManager.FetchById(id)
|
||||
if err != nil {
|
||||
return nil, "", errors.Wrap(err, "TenantCacheManager.FetchById")
|
||||
}
|
||||
|
||||
for i := range projects {
|
||||
if projects[i].Name == project.GetName() {
|
||||
if projects[i].Status != api.EXTERNAL_PROJECT_STATUS_AVAILABLE {
|
||||
return nil, "", fmt.Errorf("external project %s not available", projects[i].Name)
|
||||
func (self *SCloudaccount) GetAvailableExternalProject(local *db.STenant, projects []SExternalProject) *SExternalProject {
|
||||
var ret *SExternalProject = nil
|
||||
for i := 0; i < len(projects); i++ {
|
||||
if projects[i].Status == api.EXTERNAL_PROJECT_STATUS_AVAILABLE {
|
||||
if projects[i].ProjectId == local.Id {
|
||||
return &projects[i]
|
||||
}
|
||||
if projects[i].Name == local.Name {
|
||||
ret = &projects[i]
|
||||
}
|
||||
return &projects[i], project.GetName(), nil
|
||||
}
|
||||
}
|
||||
return nil, project.GetName(), cloudprovider.ErrNotFound
|
||||
return ret
|
||||
}
|
||||
|
||||
func (self *SCloudaccount) SyncProject(ctx context.Context, userCred mcclient.TokenCredential, id string) (string, error) {
|
||||
lockman.LockRawObject(ctx, self.Id, id)
|
||||
defer lockman.ReleaseRawObject(ctx, self.Id, id)
|
||||
|
||||
project, projectName, err := self.GetExternalProject(ctx, userCred, id)
|
||||
if err == nil {
|
||||
return project.ExternalId, nil
|
||||
}
|
||||
if err != cloudprovider.ErrNotFound {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(projectName) == 0 {
|
||||
return "", fmt.Errorf("empty project name")
|
||||
}
|
||||
// 若本地项目映射了多个云上项目,则在云上随机找一个项目
|
||||
// 若本地项目没有映射云上任何项目,则在云上新建一个同名项目
|
||||
// 若本地项目a映射云上项目b,但b项目不可用,则看云上是否有a项目,有则直接使用,若没有则在云上创建a-1, a-2类似项目
|
||||
func (self *SCloudaccount) SyncProject(ctx context.Context, userCred mcclient.TokenCredential, projectId string) (string, error) {
|
||||
lockman.LockRawObject(ctx, self.Id, projectId)
|
||||
defer lockman.ReleaseRawObject(ctx, self.Id, projectId)
|
||||
|
||||
provider, err := self.GetProvider()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "GetProvider")
|
||||
}
|
||||
iProject, err := provider.CreateIProject(projectName)
|
||||
|
||||
if !cloudprovider.IsSupportProject(provider) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
project, err := db.TenantCacheManager.FetchTenantById(ctx, projectId)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "FetchTenantById(%s)", projectId)
|
||||
}
|
||||
|
||||
projects, err := self.GetExternalProjectsByProjectIdOrName(projectId, project.Name)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "GetExternalProjectsByProjectIdOrName(%s,%s)", projectId, project.Name)
|
||||
}
|
||||
|
||||
extProj := self.GetAvailableExternalProject(project, projects)
|
||||
if extProj != nil {
|
||||
return extProj.ExternalId, nil
|
||||
}
|
||||
|
||||
retry := 1
|
||||
if len(projects) > 0 {
|
||||
retry = 10
|
||||
}
|
||||
|
||||
var iProject cloudprovider.ICloudProject = nil
|
||||
projectName := project.Name
|
||||
for i := 0; i < retry; i++ {
|
||||
iProject, err = provider.CreateIProject(projectName)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
projectName = fmt.Sprintf("%s-%d", project.Name, i)
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
|
||||
logclient.AddSimpleActionLog(self, logclient.ACT_CREATE, err, userCred, false)
|
||||
return "", errors.Wrap(err, "CreateIProject")
|
||||
}
|
||||
return "", nil
|
||||
return "", errors.Wrapf(err, "CreateIProject(%s)", projectName)
|
||||
}
|
||||
extProj, err := ExternalProjectManager.newFromCloudProject(ctx, userCred, self, iProject)
|
||||
|
||||
extProj, err = ExternalProjectManager.newFromCloudProject(ctx, userCred, self, project, iProject)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "newFromCloudProject")
|
||||
}
|
||||
|
||||
return extProj.ExternalId, nil
|
||||
}
|
||||
|
||||
@@ -36,8 +36,7 @@ import (
|
||||
)
|
||||
|
||||
type SExternalProjectManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
db.SProjectizedResourceBaseManager
|
||||
db.SVirtualResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
}
|
||||
@@ -46,7 +45,7 @@ var ExternalProjectManager *SExternalProjectManager
|
||||
|
||||
func init() {
|
||||
ExternalProjectManager = &SExternalProjectManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SVirtualResourceBaseManager: db.NewVirtualResourceBaseManager(
|
||||
SExternalProject{},
|
||||
"externalprojects_tbl",
|
||||
"externalproject",
|
||||
@@ -57,8 +56,7 @@ func init() {
|
||||
}
|
||||
|
||||
type SExternalProject struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SProjectizedResourceBase
|
||||
db.SVirtualResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
SManagedResourceBase
|
||||
|
||||
@@ -67,18 +65,13 @@ type SExternalProject struct {
|
||||
}
|
||||
|
||||
func (manager *SExternalProjectManager) AllowListItems(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsAdminAllowList(userCred, manager)
|
||||
return db.IsDomainAllowList(userCred, manager)
|
||||
}
|
||||
|
||||
func (self *SExternalProject) AllowUpdateItem(ctx context.Context, userCred mcclient.TokenCredential) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SExternalProject) getCloudProviderInfo() SCloudProviderInfo {
|
||||
provider := self.GetCloudprovider()
|
||||
return MakeCloudProviderInfo(nil, nil, provider)
|
||||
}
|
||||
|
||||
func (self *SExternalProject) GetExtraDetails(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
@@ -97,16 +90,28 @@ func (manager *SExternalProjectManager) FetchCustomizeColumns(
|
||||
isList bool,
|
||||
) []api.ExternalProjectDetails {
|
||||
rows := make([]api.ExternalProjectDetails, len(objs))
|
||||
|
||||
stdRows := manager.SStatusStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
manRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
projRows := manager.SProjectizedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
|
||||
virRows := manager.SVirtualResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
accountIds := make([]string, len(objs))
|
||||
for i := range rows {
|
||||
rows[i] = api.ExternalProjectDetails{
|
||||
StatusStandaloneResourceDetails: stdRows[i],
|
||||
ManagedResourceInfo: manRows[i],
|
||||
ProjectizedResourceInfo: projRows[i],
|
||||
VirtualResourceDetails: virRows[i],
|
||||
}
|
||||
proj := objs[i].(*SExternalProject)
|
||||
accountIds[i] = proj.CloudaccountId
|
||||
}
|
||||
accounts := make(map[string]SCloudaccount)
|
||||
err := db.FetchStandaloneObjectsByIds(CloudaccountManager, accountIds, &accounts)
|
||||
if err != nil {
|
||||
log.Errorf("FetchStandaloneObjectsByIds (%s) fail %s",
|
||||
CloudaccountManager.KeywordPlural(), err)
|
||||
return rows
|
||||
}
|
||||
|
||||
for i := range rows {
|
||||
if account, ok := accounts[accountIds[i]]; ok {
|
||||
rows[i].Account = account.Name
|
||||
rows[i].Brand = account.Brand
|
||||
rows[i].Provider = account.Provider
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +176,7 @@ func (manager *SExternalProjectManager) SyncProjects(ctx context.Context, userCr
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(added); i++ {
|
||||
_, err := manager.newFromCloudProject(ctx, userCred, account, added[i])
|
||||
_, err := manager.newFromCloudProject(ctx, userCred, account, nil, added[i])
|
||||
if err != nil {
|
||||
syncResult.AddError(err)
|
||||
} else {
|
||||
@@ -207,7 +212,7 @@ func (self *SExternalProject) SyncWithCloudProject(ctx context.Context, userCred
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *SExternalProjectManager) newFromCloudProject(ctx context.Context, userCred mcclient.TokenCredential, account *SCloudaccount, extProject cloudprovider.ICloudProject) (*SExternalProject, error) {
|
||||
func (manager *SExternalProjectManager) newFromCloudProject(ctx context.Context, userCred mcclient.TokenCredential, account *SCloudaccount, localProject *db.STenant, extProject cloudprovider.ICloudProject) (*SExternalProject, error) {
|
||||
project := SExternalProject{}
|
||||
project.SetModelManager(manager, &project)
|
||||
|
||||
@@ -218,7 +223,10 @@ func (manager *SExternalProjectManager) newFromCloudProject(ctx context.Context,
|
||||
project.CloudaccountId = account.Id
|
||||
project.DomainId = account.DomainId
|
||||
project.ProjectId = account.ProjectId
|
||||
if account.AutoCreateProject {
|
||||
if localProject != nil {
|
||||
project.DomainId = localProject.DomainId
|
||||
project.ProjectId = localProject.Id
|
||||
} else if account.AutoCreateProject {
|
||||
desc := fmt.Sprintf("auto create from cloud project %s (%s)", project.Name, project.ExternalId)
|
||||
domainId, projectId, err := getOrCreateTenant(ctx, project.Name, account.DomainId, "", desc)
|
||||
if err != nil {
|
||||
@@ -300,18 +308,14 @@ func (manager *SExternalProjectManager) ListItemFilter(
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
q, err = manager.SVirtualResourceBaseManager.ListItemFilter(ctx, q, userCred, query.VirtualResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemFilter")
|
||||
return nil, errors.Wrap(err, "SVirtualResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
q, err = manager.SProjectizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ProjectizedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SProjectizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
if len(query.Cloudprovider) > 0 {
|
||||
p, err := CloudproviderManager.FetchByIdOrName(userCred, query.Cloudprovider)
|
||||
@@ -351,18 +355,14 @@ func (manager *SExternalProjectManager) OrderByExtraFields(
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
q, err = manager.SVirtualResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.VirtualResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.OrderByExtraFields")
|
||||
return nil, errors.Wrap(err, "SVirtualResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SProjectizedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ProjectizedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SProjectizedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
@@ -370,7 +370,7 @@ func (manager *SExternalProjectManager) OrderByExtraFields(
|
||||
func (manager *SExternalProjectManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
q, err = manager.SVirtualResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
@@ -378,23 +378,15 @@ func (manager *SExternalProjectManager) QueryDistinctExtraField(q *sqlchemy.SQue
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
q, err = manager.SProjectizedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
func (manager *SExternalProjectManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential,
|
||||
keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
q, err := manager.SVirtualResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
q, err = manager.SProjectizedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SProjectizedResourceBaseManager.ListItemExportKeys")
|
||||
return nil, errors.Wrap(err, "SVirtualResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -27,6 +28,7 @@ import (
|
||||
|
||||
type SResourceGroup struct {
|
||||
multicloud.SResourceBase
|
||||
client *SAliyunClient
|
||||
|
||||
Status string
|
||||
AccountId string
|
||||
@@ -51,6 +53,14 @@ func (self *SResourceGroup) GetName() string {
|
||||
return self.Name
|
||||
}
|
||||
|
||||
func (self *SResourceGroup) Refresh() error {
|
||||
group, err := self.client.GetResourceGroup(self.Id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetResourceGroup")
|
||||
}
|
||||
return jsonutils.Update(self, group)
|
||||
}
|
||||
|
||||
func (self *SResourceGroup) GetStatus() string {
|
||||
switch self.Status {
|
||||
case "Creating":
|
||||
@@ -89,14 +99,14 @@ func (self *SAliyunClient) GetResourceGroups(pageNumber int, pageSize int) ([]SR
|
||||
}
|
||||
|
||||
func (self *SAliyunClient) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
|
||||
project, err := self.CreateProject(name)
|
||||
group, err := self.CreateResourceGroup(name)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "CreateProject")
|
||||
}
|
||||
return project, nil
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (self *SAliyunClient) CreateProject(name string) (*SResourceGroup, error) {
|
||||
func (self *SAliyunClient) CreateResourceGroup(name string) (*SResourceGroup, error) {
|
||||
params := map[string]string{
|
||||
"DisplayName": name,
|
||||
"Name": name,
|
||||
@@ -105,10 +115,30 @@ func (self *SAliyunClient) CreateProject(name string) (*SResourceGroup, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "CreateResourceGroup")
|
||||
}
|
||||
group := SResourceGroup{}
|
||||
err = resp.Unmarshal(&group, "ResourceGroup")
|
||||
group := &SResourceGroup{client: self}
|
||||
err = resp.Unmarshal(group, "ResourceGroup")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
return &group, nil
|
||||
err = cloudprovider.WaitStatus(group, api.EXTERNAL_PROJECT_STATUS_AVAILABLE, time.Second*5, time.Minute*3)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "WaitStatus")
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (self *SAliyunClient) GetResourceGroup(id string) (*SResourceGroup, error) {
|
||||
params := map[string]string{
|
||||
"ResourceGroupId": id,
|
||||
}
|
||||
resp, err := self.rmRequest("GetResourceGroup", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
group := &SResourceGroup{client: self}
|
||||
err = resp.Unmarshal(group, "ResourceGroup")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package shell
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/multicloud/aliyun"
|
||||
"yunion.io/x/onecloud/pkg/util/shellutils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
type ProjectListOptions struct {
|
||||
}
|
||||
shellutils.R(&ProjectListOptions{}, "project-list", "List project", func(cli *aliyun.SRegion, args *ProjectListOptions) error {
|
||||
project, err := cli.GetClient().GetIProjects()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(project, 0, 0, 0, nil)
|
||||
return nil
|
||||
})
|
||||
|
||||
type ProjectCreateOptions struct {
|
||||
NAME string
|
||||
}
|
||||
|
||||
shellutils.R(&ProjectCreateOptions{}, "project-create", "Create project", func(cli *aliyun.SRegion, args *ProjectCreateOptions) error {
|
||||
project, err := cli.GetClient().CreateProject(args.NAME)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(project)
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
@@ -32,4 +32,31 @@ func init() {
|
||||
printList(groups, 0, 0, 0, nil)
|
||||
return nil
|
||||
})
|
||||
|
||||
type ResourceGroupShowOptions struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
shellutils.R(&ResourceGroupShowOptions{}, "resource-group-show", "Show resource group", func(cli *aliyun.SRegion, args *ResourceGroupShowOptions) error {
|
||||
group, err := cli.GetClient().GetResourceGroup(args.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(group)
|
||||
return nil
|
||||
})
|
||||
|
||||
type ResourceGroupCreateOptions struct {
|
||||
NAME string
|
||||
}
|
||||
|
||||
shellutils.R(&ResourceGroupCreateOptions{}, "resource-group-create", "Create resource group", func(cli *aliyun.SRegion, args *ResourceGroupCreateOptions) error {
|
||||
group, err := cli.GetClient().CreateResourceGroup(args.NAME)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(group)
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -92,13 +92,13 @@ func (cluster *SCluster) CreateResourcePool(name string) (*mo.ResourcePool, erro
|
||||
return nil, errors.Wrap(cloudprovider.ErrNotFound, "AfterCreate")
|
||||
}
|
||||
|
||||
func (cluster *SCluster) SyncResourcePool(groupId string, name string) (*mo.ResourcePool, error) {
|
||||
func (cluster *SCluster) SyncResourcePool(name string) (*mo.ResourcePool, error) {
|
||||
pools, err := cluster.ListResourcePools()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ListResourcePools")
|
||||
}
|
||||
for i := range pools {
|
||||
if pools[i].Self.Value == groupId || pools[i].Entity().Name == name {
|
||||
if pools[i].Entity().Name == name {
|
||||
return &pools[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -654,7 +654,6 @@ type SCreateVMParam struct {
|
||||
Cdrom jsonutils.JSONObject
|
||||
Disks []SDiskInfo
|
||||
Nics []jsonutils.JSONObject
|
||||
GroupId string // resourcePoolId
|
||||
ResourcePool string
|
||||
}
|
||||
|
||||
@@ -842,7 +841,7 @@ func (self *SHost) DoCreateVM(ctx context.Context, ds *SDatastore, params SCreat
|
||||
return nil, errors.Wrap(err, "object.DataCenter.Folders")
|
||||
}
|
||||
vmFolder := folders.VmFolder
|
||||
resourcePool, err := self.SyncResourcePool(params.GroupId, params.ResourcePool)
|
||||
resourcePool, err := self.SyncResourcePool(params.ResourcePool)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SyncResourcePool")
|
||||
}
|
||||
@@ -983,9 +982,9 @@ func (host *SHost) CloneVM(ctx context.Context, from *SVirtualMachine, ds *SData
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "object.DataCenter.Folders")
|
||||
}
|
||||
resourcePool, err := host.GetResourcePool()
|
||||
resourcePool, err := host.SyncResourcePool(params.ResourcePool)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SHost.GetResourcePool")
|
||||
return nil, errors.Wrap(err, "SyncResourcePool")
|
||||
}
|
||||
|
||||
folderref := folders.VmFolder.Reference()
|
||||
@@ -1408,15 +1407,15 @@ func (host *SHost) GetCluster() (*SCluster, error) {
|
||||
return NewCluster(host.manager, cluster, host.datacenter), nil
|
||||
}
|
||||
|
||||
func (host *SHost) SyncResourcePool(groupId, name string) (*object.ResourcePool, error) {
|
||||
func (host *SHost) SyncResourcePool(name string) (*object.ResourcePool, error) {
|
||||
cluster, err := host.GetCluster()
|
||||
if err != nil {
|
||||
log.Errorf("failed to get host %s cluster info: %v", host.GetName(), err)
|
||||
return host.GetResourcePool()
|
||||
}
|
||||
pool, err := cluster.SyncResourcePool(groupId, name)
|
||||
pool, err := cluster.SyncResourcePool(name)
|
||||
if err != nil {
|
||||
log.Errorf("failed to sync resourcePool(%s, %s) for cluster %s error: %v", groupId, name, cluster.GetName(), err)
|
||||
log.Errorf("failed to sync resourcePool(%s) for cluster %s error: %v", name, cluster.GetName(), err)
|
||||
return host.GetResourcePool()
|
||||
}
|
||||
return object.NewResourcePool(host.manager.client.Client, pool.Reference()), nil
|
||||
|
||||
@@ -63,7 +63,6 @@ func init() {
|
||||
type ClusterPoolSyncOptions struct {
|
||||
DATACENTER string `help:"List clusters in datacenter"`
|
||||
CLUSTER string `help:"List cluster resource pool"`
|
||||
GroupId string `help:"Resource pool Id"`
|
||||
Name string `help:"Resource pool name"`
|
||||
}
|
||||
|
||||
@@ -76,7 +75,7 @@ func init() {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pool, err := cluster.SyncResourcePool(args.GroupId, args.Name)
|
||||
pool, err := cluster.SyncResourcePool(args.Name)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "SyncResourcePool")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user