From 335c8eecc06a071e040f72ed4d29f515b6f444e6 Mon Sep 17 00:00:00 2001 From: rainzm Date: Thu, 15 Oct 2020 10:40:00 +0800 Subject: [PATCH] feat: change templateid as / in esxi --- pkg/cloudcommon/db/namevalidator.go | 4 ++++ pkg/compute/guestdrivers/esxi.go | 7 ++++++- pkg/compute/models/cachedimages.go | 6 +++++- pkg/multicloud/esxi/manager.go | 3 ++- pkg/multicloud/esxi/template.go | 19 ++++++++++++++++++- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/cloudcommon/db/namevalidator.go b/pkg/cloudcommon/db/namevalidator.go index e4d3aa18dc..f8af1ba840 100644 --- a/pkg/cloudcommon/db/namevalidator.go +++ b/pkg/cloudcommon/db/namevalidator.go @@ -87,6 +87,10 @@ func alterNameValidator(model IModel, name string) error { return nil } +func GenerateAlertName(model IModel, hint string) (string, error) { + return GenerateName2(model.GetModelManager(), nil, hint, model, 1) +} + func GenerateName(manager IModelManager, ownerId mcclient.IIdentityProvider, hint string) (string, error) { return GenerateName2(manager, ownerId, hint, nil, 1) } diff --git a/pkg/compute/guestdrivers/esxi.go b/pkg/compute/guestdrivers/esxi.go index 2ef110797f..6f5e569197 100644 --- a/pkg/compute/guestdrivers/esxi.go +++ b/pkg/compute/guestdrivers/esxi.go @@ -192,8 +192,13 @@ func (self *SESXiGuestDriver) GetJsonDescAtHost(ctx context.Context, userCred mc var hostIp string storageCacheHost, err := storageCaches[0].GetHost() if err != nil { - log.Errorf("fail to GetHost of storageCache %s", storageCaches[0].Id) + log.Errorf("unable to GetHost of storageCache %s: %v", storageCaches[0].Id, err) hostIp = storageCaches[0].ExternalId + } else if storageCacheHost == nil { + log.Errorf("unable to GetHost of storageCache %s: result is nil", storageCaches[0].Id) + hostIp = storageCaches[0].ExternalId + } else { + hostIp = storageCacheHost.AccessIp } hostIp = storageCacheHost.AccessIp imageInfo := SEsxiImageInfo{ diff --git a/pkg/compute/models/cachedimages.go b/pkg/compute/models/cachedimages.go index ddcbde5700..68f495b738 100644 --- a/pkg/compute/models/cachedimages.go +++ b/pkg/compute/models/cachedimages.go @@ -472,7 +472,11 @@ func (self *SCachedimage) canDeleteLastCache() bool { func (self *SCachedimage) syncWithCloudImage(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, image cloudprovider.ICloudImage, managerId string) error { diff, err := db.UpdateWithLock(ctx, self, func() error { - self.Name = image.GetName() + newName, err := db.GenerateAlertName(self, image.GetName()) + if err != nil { + return errors.Wrap(err, "GenerateAlertName") + } + self.Name = newName self.Size = image.GetSizeByte() self.ExternalId = image.GetGlobalId() self.ImageType = image.GetImageType() diff --git a/pkg/multicloud/esxi/manager.go b/pkg/multicloud/esxi/manager.go index 6c9b4018e0..e2fe25adef 100644 --- a/pkg/multicloud/esxi/manager.go +++ b/pkg/multicloud/esxi/manager.go @@ -351,7 +351,8 @@ func (cli *SESXiClient) scanAllMObjects(props []string, dst interface{}) error { func (cli *SESXiClient) SearchTemplateVM(id string) (*SVirtualMachine, error) { filter := property.Filter{} - filter["summary.config.uuid"] = id + uuid := toTemplateUuid(id) + filter["summary.config.uuid"] = uuid var movms []mo.VirtualMachine err := cli.scanMObjectsWithFilter(cli.client.ServiceContent.RootFolder, VIRTUAL_MACHINE_PROPS, &movms, filter) if err != nil { diff --git a/pkg/multicloud/esxi/template.go b/pkg/multicloud/esxi/template.go index 717957cc16..9963511140 100644 --- a/pkg/multicloud/esxi/template.go +++ b/pkg/multicloud/esxi/template.go @@ -16,6 +16,8 @@ package esxi import ( "context" + "fmt" + "strings" "time" "yunion.io/x/jsonutils" @@ -42,8 +44,23 @@ func NewVMTemplate(vm *SVirtualMachine, cache *SDatastoreImageCache) *SVMTemplat } } +const splitStr = "/" + +func toTemplateUuid(templateId string) string { + ids := strings.Split(templateId, splitStr) + if len(ids) == 1 { + return ids[0] + } + return ids[1] +} + +func toTemplateId(providerId string, templateUuid string) string { + return fmt.Sprintf("%s%s%s", providerId, splitStr, templateUuid) +} + func (t *SVMTemplate) GetId() string { - return t.uuid + providerId := t.vm.manager.cpcfg.Id + return toTemplateId(providerId, t.uuid) } func (t *SVMTemplate) UEFI() bool {