diff --git a/README-CN.md b/README-CN.md index c79767aaa4..51d76acb83 100644 --- a/README-CN.md +++ b/README-CN.md @@ -69,7 +69,7 @@ Cloudpods提供了如下的功能: ## 快速开始 -我们可以通过以下简单两步将Cloudpods安装在一台至少8GiB内存和100GB硬盘的Linux主机上(目前CentOS 7和Debian 10经过充分测试) +我们可以通过以下简单三步将Cloudpods安装在一台至少8GiB内存和100GB硬盘的Linux主机上(目前CentOS 7和Debian 10经过充分测试) (下面假设该主机的IP为 *10.168.26.216*) @@ -87,7 +87,19 @@ $ ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.168.26.216 $ ssh root@10.168.26.216 "hostname" ``` -### 2. 安装Cloudpods +### 2. 安装git和相关工具 + +#### CentOS 7安装git和epel源 +```bash +yum install -y git epel-release +``` + +#### Debian 10安装git +```bash +apt install -y git +``` + +### 3. 安装Cloudpods 通过以下命令开始安装Cloudpods: diff --git a/README.md b/README.md index d17f5c5b76..97e9367690 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Cloudpods is a cloud-native open source unified multi/hybrid-cloud platform deve ## Quick start -You may install Cloudpods in a Linux box (currently CentOS 7 and Debian 10 are fully tested) with at least 8GiB RAM and 100GB storage by following two steps. +You may install Cloudpods in a Linux box (currently CentOS 7 and Debian 10 are fully tested) with at least 8GiB RAM and 100GB storage by following three steps. (Assuming that you install Cloudpods on a Linux box with IP *10.168.26.216*): @@ -90,7 +90,19 @@ $ ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.168.26.216 $ ssh root@10.168.26.216 "hostname" ``` -### 2. Install Cloudpods +### 2. Install git and relevant tools + +#### For CentOS 7 +```bash +yum install -y git epel-release +``` + +#### For Debian 10 +```bash +apt install -y git +``` + +### 3. Install Cloudpods Run the following commands to start installing Cloudpods. diff --git a/build/docker/Dockerfile.ansibleserver b/build/docker/Dockerfile.ansibleserver index c5d52aea1c..46cdd6b8f6 100644 --- a/build/docker/Dockerfile.ansibleserver +++ b/build/docker/Dockerfile.ansibleserver @@ -1,7 +1,7 @@ FROM registry.cn-beijing.aliyuncs.com/yunionio/ansibleserver-base:v1.0.3 # install playbook and telegraf install pkg -COPY --from=registry.cn-beijing.aliyuncs.com/yunionio/file-repo:v0.2.2 /opt/yunion/playbook /opt/yunion/playbook -COPY --from=registry.cn-beijing.aliyuncs.com/yunionio/file-repo:v0.2.2 /opt/yunion/ansible-install-pkg /opt/yunion/ansible-install-pkg +COPY --from=registry.cn-beijing.aliyuncs.com/yunionio/file-repo:v0.3.3 /opt/yunion/playbook /opt/yunion/playbook +COPY --from=registry.cn-beijing.aliyuncs.com/yunionio/file-repo:v0.3.3 /opt/yunion/ansible-install-pkg /opt/yunion/ansible-install-pkg ADD ./_output/alpine-build/bin/ansibleserver /opt/yunion/bin/ansibleserver diff --git a/build/docker/Dockerfile.file-repo b/build/docker/Dockerfile.file-repo index ce3f26a6b0..db99878ab2 100644 --- a/build/docker/Dockerfile.file-repo +++ b/build/docker/Dockerfile.file-repo @@ -11,10 +11,10 @@ RUN set -x \ # install default playbook and install pkg Run mkdir -p /opt/yunion/ansible-install-pkg -Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf-1.5.0~yn-1.aarch64.rpm -P /opt/yunion/ansible-install-pkg -Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf-1.5.0~yn-1.x86_64.rpm -P /opt/yunion/ansible-install-pkg -Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf_1.5.0~yn-1_amd64.deb -P /opt/yunion/ansible-install-pkg -Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf_1.5.0~yn-1_arm64.deb -P /opt/yunion/ansible-install-pkg +Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf-1.19.2-yn~fe11a96b-0.aarch64.rpm -P /opt/yunion/ansible-install-pkg +Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf-1.19.2-yn~fe11a96b-0.x86_64.rpm -P /opt/yunion/ansible-install-pkg +Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf_1.19.2-yn~fe11a96b-0_amd64.deb -P /opt/yunion/ansible-install-pkg +Run wget https://yunioniso.oss-cn-beijing.aliyuncs.com/rpms/telegraf/telegraf_1.19.2-yn~fe11a96b-0_arm64.deb -P /opt/yunion/ansible-install-pkg Run mkdir -p /opt/yunion/playbook Run mkdir /opt/yunion/playbook/monitor-agent diff --git a/build/docker/Dockerfile.glance b/build/docker/Dockerfile.glance index bdbe9e4eb7..dc506fe9a5 100644 --- a/build/docker/Dockerfile.glance +++ b/build/docker/Dockerfile.glance @@ -1,7 +1,7 @@ -FROM registry.cn-beijing.aliyuncs.com/yunionio/torrent:20200820 +FROM registry.cn-beijing.aliyuncs.com/yunionio/torrent:20210815.0 MAINTAINER "Zexi Li " RUN apk add --no-cache s3fs-fuse --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing RUN mkdir -p /opt/yunion/bin -ADD ./_output/alpine-build/bin/glance /opt/yunion/bin/glance \ No newline at end of file +ADD ./_output/alpine-build/bin/glance /opt/yunion/bin/glance diff --git a/build/docker/Dockerfile.onecloud-base b/build/docker/Dockerfile.onecloud-base index 06786ef1d1..21aec573f1 100644 --- a/build/docker/Dockerfile.onecloud-base +++ b/build/docker/Dockerfile.onecloud-base @@ -1,4 +1,5 @@ -FROM alpine:3.11 +# TODO: use 3.14 will lead 'apk add qemu-img fail on arm64' +FROM alpine:3.13.5 MAINTAINER "Zexi Li " diff --git a/build/docker/Dockerfile.torrent b/build/docker/Dockerfile.torrent index 68a9b39895..0c69009a22 100644 --- a/build/docker/Dockerfile.torrent +++ b/build/docker/Dockerfile.torrent @@ -1,15 +1,15 @@ -FROM registry.cn-beijing.aliyuncs.com/yunionio/onecloud-base:v0.2 +FROM registry.cn-beijing.aliyuncs.com/yunionio/onecloud-base:v0.3.5 -MAINTAINER "Yaoqi Wan " +MAINTAINER "Zexi Li " + +# HACK: use v3.14 alpine mirror to upgrade qemu-img +RUN echo http://dl-cdn.alpinelinux.org/alpine/v3.14/main > /etc/apk/repositories +RUN echo http://dl-cdn.alpinelinux.org/alpine/v3.14/community >> /etc/apk/repositories RUN apk update && \ - apk add --no-cache tzdata ca-certificates qemu-img && \ + apk add --no-cache qemu-img && \ rm -rf /var/cache/apk/* -RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime - -ENV TZ Asia/Shanghai - # add executable file torrent # make cmd/torrent RUN mkdir -p /opt/yunion/bin diff --git a/build/docker/Makefile b/build/docker/Makefile index 9831cbbc82..fb6bb214a8 100644 --- a/build/docker/Makefile +++ b/build/docker/Makefile @@ -7,6 +7,11 @@ debian10-base: docker buildx build --platform linux/arm64,linux/amd64 --push \ -t registry.cn-beijing.aliyuncs.com/yunionio/debian10-base:1.0 -f ./Dockerfile.debian-base . +ONECLOUD_BASE_VERSION = v0.3.5 + +onecloud-base: + $(DOCKER_BUILDX)/onecloud-base:$(ONECLOUD_BASE_VERSION) -f ./Dockerfile.onecloud-base . + CLIMC_BASE_VERSION = 20210701 climc-base: @@ -19,7 +24,7 @@ baremetal-base: $(DOCKER_BUILDX)/baremetal-base:$(BAREMETAL_BASE_VERSION) -f ./Dockerfile.baremetal-base . #docker push $(REGISTRY)/baremetal-base:$(BAREMETAL_BASE_VERSION) -FILE_REPO_VERSION = v0.2.2 +FILE_REPO_VERSION = v0.3.3 file-repo: $(DOCKER_BUILDX)/file-repo:$(FILE_REPO_VERSION) -f ./Dockerfile.file-repo . diff --git a/build/docker/multi-arch/Dockerfile.torrent b/build/docker/multi-arch/Dockerfile.torrent index 2e944799c3..25de595188 100644 --- a/build/docker/multi-arch/Dockerfile.torrent +++ b/build/docker/multi-arch/Dockerfile.torrent @@ -1,24 +1,24 @@ FROM registry.cn-beijing.aliyuncs.com/yunionio/alpine-build:1.0-5 as build ARG TARGETPLATFORM ARG BUILDPLATFORM + RUN mkdir -p /root/go/src/yunion.io/x/onecloud COPY . /root/go/src/yunion.io/x/onecloud +RUN cd /root/go/src/yunion.io/x/onecloud && make cmd/torrent -WORKDIR /root/go/src/yunion.io/x/onecloud -RUN make cmd/torrent +FROM registry.cn-beijing.aliyuncs.com/yunionio/onecloud-base:v0.3.5 -FROM registry.cn-beijing.aliyuncs.com/yunionio/onecloud-base:v0.2 +MAINTAINER "Zexi Li " -MAINTAINER "Yaoqi Wan " +# HACK: use v3.14 alpine mirror to upgrade qemu-img +RUN echo http://dl-cdn.alpinelinux.org/alpine/v3.14/main > /etc/apk/repositories +RUN echo http://dl-cdn.alpinelinux.org/alpine/v3.14/community >> /etc/apk/repositories RUN apk update && \ - apk add --no-cache tzdata ca-certificates qemu-img && \ + apk add --no-cache qemu-img && \ rm -rf /var/cache/apk/* -RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime - -ENV TZ Asia/Shanghai - +# TAG=20210815.0 # add executable file torrent # make cmd/torrent RUN mkdir -p /opt/yunion/bin diff --git a/build/docker/multi-arch/Dockerfile.torrent.dockerignore b/build/docker/multi-arch/Dockerfile.torrent.dockerignore new file mode 100644 index 0000000000..e2cb488007 --- /dev/null +++ b/build/docker/multi-arch/Dockerfile.torrent.dockerignore @@ -0,0 +1,2 @@ +_output/alpine-build/_cache + diff --git a/pkg/apis/compute/mongodb.go b/pkg/apis/compute/mongodb.go index c2e1b4206d..e45cba05ec 100644 --- a/pkg/apis/compute/mongodb.go +++ b/pkg/apis/compute/mongodb.go @@ -55,6 +55,7 @@ type MongoDBListInput struct { type MongoDBDetails struct { apis.VirtualResourceDetails CloudregionResourceInfo + ZoneResourceInfoBase ManagedResourceInfo VpcResourceInfoBase diff --git a/pkg/cloudevent/models/cloudproviders.go b/pkg/cloudevent/models/cloudproviders.go index 448e24d446..18245a80d5 100644 --- a/pkg/cloudevent/models/cloudproviders.go +++ b/pkg/cloudevent/models/cloudproviders.go @@ -303,8 +303,9 @@ func (self *SCloudprovider) GetNextTimeRange() (time.Time, time.Time, error) { } type SCloudproviderDelegate struct { - Id string - Name string + Id string + Name string + CloudaccountId string Enabled bool Status string @@ -317,6 +318,11 @@ type SCloudproviderDelegate struct { Provider string Brand string + Options struct { + cloudprovider.SApsaraEndpoints + cloudprovider.SHuaweiCloudStackEndpoints + } + ProxySetting proxyapi.SProxySetting } @@ -331,6 +337,16 @@ func (self *SCloudprovider) GetDelegate() (*SCloudproviderDelegate, error) { if err != nil { return nil, errors.Wrap(err, "result.Unmarshal") } + if provider.Provider == api.CLOUD_PROVIDER_APSARA || provider.Provider == api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK { + result, err := modules.Cloudaccounts.Get(s, provider.CloudaccountId, nil) + if err != nil { + return nil, errors.Wrapf(err, "modules.Cloudaccounts.Get") + } + err = result.Unmarshal(&provider.Options, "options") + if err != nil { + return nil, errors.Wrap(err, "result.Unmarshal") + } + } return provider, nil } @@ -391,6 +407,9 @@ func (self *SCloudprovider) GetProvider() (cloudprovider.ICloudProvider, error) Secret: passwd, ProxyFunc: proxyFunc, + + SHuaweiCloudStackEndpoints: delegate.Options.SHuaweiCloudStackEndpoints, + SApsaraEndpoints: delegate.Options.SApsaraEndpoints, }, ) } diff --git a/pkg/cloudid/models/cloudaccount.go b/pkg/cloudid/models/cloudaccount.go index 77cfe98c62..164c115ce3 100644 --- a/pkg/cloudid/models/cloudaccount.go +++ b/pkg/cloudid/models/cloudaccount.go @@ -45,6 +45,7 @@ import ( "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/logclient" + "yunion.io/x/onecloud/pkg/util/samlutils" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -349,9 +350,42 @@ func (manager *SCloudaccountManager) newFromICloudaccount(ctx context.Context, u if err != nil { return nil, errors.Wrap(err, "Insert") } + account.registerHuaweSaml(ctx) return account, nil } +func (self *SCloudaccount) registerHuaweSaml(ctx context.Context) error { + if self.Provider != computeapi.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK { + return nil + } + delegate, err := self.getCloudDelegate(ctx) + if err != nil { + return err + } + domain := delegate.Options.EndpointDomain + driv := SHuaweiSAMLDriver{ + EntityId: domain, + MetadataFileName: fmt.Sprintf("%s.xml", domain), + MetadataUrl: fmt.Sprintf("https://auth.%s/authui/saml/metadata.xml", domain), + } + metadata, err := GetMetadata(&driv) + if err != nil { + return errors.Wrapf(err, "GetMetadata") + } + ed, err := samlutils.ParseMetadata(metadata) + if err != nil { + return errors.Wrapf(err, "ParseMetadata") + } + + if FindDriver(ed.EntityId) != nil { + return nil + } + driv.EntityId = ed.EntityId + SamlIdpInstance().AddSPMetadata(metadata) + Register(&driv) + return nil +} + func (self *SCloudaccount) syncWithICloudaccount(ctx context.Context, userCred mcclient.TokenCredential, account SCloudaccount) error { _, err := db.UpdateWithLock(ctx, self, func() error { self.Name = account.Name @@ -366,6 +400,10 @@ func (self *SCloudaccount) syncWithICloudaccount(ctx context.Context, userCred m if err != nil { return errors.Wrap(err, "db.UpdateWithLock") } + err = self.registerHuaweSaml(ctx) + if err != nil { + log.Errorf("regiester %s saml sp error: %s", self.Name, err) + } self.StartSAMLProviderCreateTask(ctx, userCred) return nil } @@ -443,6 +481,10 @@ type SCloudDelegate struct { Provider string Brand string + Options struct { + cloudprovider.SHuaweiCloudStackEndpoints + cloudprovider.SApsaraEndpoints + } ProxySetting proxyapi.SProxySetting } @@ -526,6 +568,9 @@ func (account *SCloudDelegate) GetProvider() (cloudprovider.ICloudProvider, erro Secret: passwd, ProxyFunc: proxyFunc, + SApsaraEndpoints: account.Options.SApsaraEndpoints, + SHuaweiCloudStackEndpoints: account.Options.SHuaweiCloudStackEndpoints, + AccountId: account.Id, }) } diff --git a/pkg/cloudid/saml/providers/huawei/driver.go b/pkg/cloudid/models/huawei_driver.go similarity index 91% rename from pkg/cloudid/saml/providers/huawei/driver.go rename to pkg/cloudid/models/huawei_driver.go index fdf5616caf..181347e5b0 100644 --- a/pkg/cloudid/saml/providers/huawei/driver.go +++ b/pkg/cloudid/models/huawei_driver.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package huawei +package models import ( "context" @@ -21,7 +21,6 @@ import ( "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/cloudid/models" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/samlutils" @@ -38,15 +37,15 @@ func (d *SHuaweiSAMLDriver) GetIdpInitiatedLoginData(ctx context.Context, userCr func (d *SHuaweiSAMLDriver) GetSpInitiatedLoginData(ctx context.Context, userCred mcclient.TokenCredential, cloudAccountId string, sp *idp.SSAMLServiceProvider) (samlutils.SSAMLSpInitiatedLoginData, error) { data := samlutils.SSAMLSpInitiatedLoginData{} - _account, err := models.CloudaccountManager.FetchById(cloudAccountId) + _account, err := CloudaccountManager.FetchById(cloudAccountId) if err != nil { if errors.Cause(err) == sql.ErrNoRows { return data, httperrors.NewResourceNotFoundError2("cloudaccount", cloudAccountId) } return data, httperrors.NewGeneralError(err) } - account := _account.(*models.SCloudaccount) - if account.Provider != api.CLOUD_PROVIDER_HUAWEI { + account := _account.(*SCloudaccount) + if account.Provider != api.CLOUD_PROVIDER_HUAWEI && account.Provider != api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK { return data, httperrors.NewClientError("cloudaccount %s is %s not %s", account.Id, account.Provider, api.CLOUD_PROVIDER_HUAWEI) } if account.SAMLAuth.IsFalse() { @@ -70,7 +69,7 @@ func (d *SHuaweiSAMLDriver) GetSpInitiatedLoginData(ctx context.Context, userCre data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT data.AudienceRestriction = sp.GetEntityId() for k, v := range map[string][]string{ - "User": []string{userCred.GetUserName()}, + "User": {userCred.GetUserName()}, "Groups": groups, } { data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{ diff --git a/pkg/cloudid/models/huawei_facgory.go b/pkg/cloudid/models/huawei_facgory.go new file mode 100644 index 0000000000..75ccfc9ea1 --- /dev/null +++ b/pkg/cloudid/models/huawei_facgory.go @@ -0,0 +1,33 @@ +// 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 models + +type SHuaweiSAMLDriver struct { + EntityId string + MetadataFileName string + MetadataUrl string +} + +func (d *SHuaweiSAMLDriver) GetEntityID() string { + return d.EntityId +} + +func (d *SHuaweiSAMLDriver) GetMetadataFilename() string { + return d.MetadataFileName +} + +func (d *SHuaweiSAMLDriver) GetMetadataUrl() string { + return d.MetadataUrl +} diff --git a/pkg/cloudid/models/samldriver.go b/pkg/cloudid/models/samldriver.go index 20e371573c..4c49d6d2cb 100644 --- a/pkg/cloudid/models/samldriver.go +++ b/pkg/cloudid/models/samldriver.go @@ -16,8 +16,15 @@ package models import ( "context" + "io/ioutil" + "path" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/cloudid/options" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/samlutils" "yunion.io/x/onecloud/pkg/util/samlutils/idp" ) @@ -46,6 +53,10 @@ func Register(driver ICloudSAMLLoginDriver) { driverTable[driver.GetEntityID()] = driver } +func UnRegister(entityId string) { + delete(driverTable, entityId) +} + func FindDriver(entityId string) ICloudSAMLLoginDriver { if driver, ok := driverTable[entityId]; ok { return driver @@ -56,3 +67,26 @@ func FindDriver(entityId string) ICloudSAMLLoginDriver { func AllDrivers() map[string]ICloudSAMLLoginDriver { return driverTable } + +func GetMetadata(driver ICloudSAMLLoginDriver) ([]byte, error) { + filePath := path.Join(options.Options.CloudSAMLMetadataPath, driver.GetMetadataFilename()) + metaBytes, err := ioutil.ReadFile(filePath) + if err != nil || len(metaBytes) == 0 { + metaUrl := driver.GetMetadataUrl() + if len(metaUrl) > 0 { + log.Debugf("[%s] metadata file load failed, try download from %s", driver.GetEntityID(), metaUrl) + httpcli := httputils.GetDefaultClient() + resp, err := httpcli.Get(metaUrl) + if err != nil { + return nil, errors.Wrapf(err, "http get %s fail", metaUrl) + } + metaBytes, err = ioutil.ReadAll(resp.Body) + if err != nil { + return nil, errors.Wrapf(err, "read body %s fail", metaUrl) + } + } else { + return nil, errors.Wrapf(err, "read file %s fail", filePath) + } + } + return metaBytes, nil +} diff --git a/pkg/cloudid/saml/initidp.go b/pkg/cloudid/saml/initidp.go index cab8b41516..ee6998adc2 100644 --- a/pkg/cloudid/saml/initidp.go +++ b/pkg/cloudid/saml/initidp.go @@ -17,8 +17,6 @@ package saml import ( "context" "fmt" - "io/ioutil" - "path" "yunion.io/x/log" "yunion.io/x/pkg/errors" @@ -30,7 +28,6 @@ import ( "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/i18n" "yunion.io/x/onecloud/pkg/mcclient/auth" - "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/samlutils" "yunion.io/x/onecloud/pkg/util/samlutils/idp" ) @@ -85,25 +82,10 @@ func initSAMLIdp(app *appsrv.Application, prefix string) error { } idpInst := idp.NewIdpInstance(saml, spFunc, idpFunc, logoutFunc) - for entityId, drvFactory := range models.AllDrivers() { - filePath := path.Join(options.Options.CloudSAMLMetadataPath, drvFactory.GetMetadataFilename()) - metaBytes, err := ioutil.ReadFile(filePath) - if err != nil || len(metaBytes) == 0 { - metaUrl := drvFactory.GetMetadataUrl() - if len(metaUrl) > 0 { - log.Debugf("[%s] metadata file load failed, try download from %s", entityId, metaUrl) - httpcli := httputils.GetDefaultClient() - resp, err := httpcli.Get(metaUrl) - if err != nil { - return errors.Wrapf(err, "http get %s fail", metaUrl) - } - metaBytes, err = ioutil.ReadAll(resp.Body) - if err != nil { - return errors.Wrapf(err, "read body %s fail", metaUrl) - } - } else { - return errors.Wrapf(err, "read file %s fail", filePath) - } + for _, drvFactory := range models.AllDrivers() { + metaBytes, err := models.GetMetadata(drvFactory) + if err != nil { + return err } err = idpInst.AddSPMetadata(metaBytes) if err != nil { diff --git a/pkg/cloudid/saml/providers/huawei/factory.go b/pkg/cloudid/saml/providers/huawei/factory.go index 046c909f49..b281ccb490 100644 --- a/pkg/cloudid/saml/providers/huawei/factory.go +++ b/pkg/cloudid/saml/providers/huawei/factory.go @@ -19,20 +19,10 @@ import ( "yunion.io/x/onecloud/pkg/cloudprovider" ) -type SHuaweiSAMLDriver struct{} - -func (d *SHuaweiSAMLDriver) GetEntityID() string { - return cloudprovider.SAML_ENTITY_ID_HUAWEI_CLOUD -} - -func (d *SHuaweiSAMLDriver) GetMetadataFilename() string { - return "huawei.xml" -} - -func (d *SHuaweiSAMLDriver) GetMetadataUrl() string { - return "https://auth.huaweicloud.com/authui/saml/metadata.xml" -} - func init() { - models.Register(&SHuaweiSAMLDriver{}) + models.Register(&models.SHuaweiSAMLDriver{ + EntityId: cloudprovider.SAML_ENTITY_ID_HUAWEI_CLOUD, + MetadataFileName: "huawei.xml", + MetadataUrl: "https://auth.huaweicloud.com/authui/saml/metadata.xml", + }) } diff --git a/pkg/cloudmon/collectors/cloudmoninit.go b/pkg/cloudmon/collectors/cloudmoninit.go index 84d1a23fdf..344d94a869 100644 --- a/pkg/cloudmon/collectors/cloudmoninit.go +++ b/pkg/cloudmon/collectors/cloudmoninit.go @@ -35,6 +35,7 @@ import ( _ "yunion.io/x/onecloud/pkg/multicloud/esxi/provider" _ "yunion.io/x/onecloud/pkg/multicloud/google/provider" _ "yunion.io/x/onecloud/pkg/multicloud/huawei/provider" + _ "yunion.io/x/onecloud/pkg/multicloud/huaweistack/provider" _ "yunion.io/x/onecloud/pkg/multicloud/jdcloud/provider" _ "yunion.io/x/onecloud/pkg/multicloud/qcloud/provider" _ "yunion.io/x/onecloud/pkg/multicloud/zstack/provider" diff --git a/pkg/cloudmon/collectors/common/basemon.go b/pkg/cloudmon/collectors/common/basemon.go index 32f8867beb..5fcceb3da6 100644 --- a/pkg/cloudmon/collectors/common/basemon.go +++ b/pkg/cloudmon/collectors/common/basemon.go @@ -124,24 +124,30 @@ func (self *CloudReportBase) InitProviderInstance() (cloudprovider.ICloudProvide return nil, errors.Wrap(err, "getCloudAccount error") } endpoints := cloudprovider.SApsaraEndpoints{} + hwendpoints := cloudprovider.SHuaweiCloudStackEndpoints{} options, err := cloudAccout.Get("options") if err == nil { err := options.Unmarshal(&endpoints) if err != nil { log.Errorf("Unmarshal SApsaraEndpoints err:%v", err) } + err = options.Unmarshal(&hwendpoints) + if err != nil { + log.Errorf("Unmarshal SHuaweiCloudStackEndpoints err:%v", err) + } } else { log.Errorf("get cloudAccout options err:%v", err) } cfg := cloudprovider.ProviderConfig{ - Id: self.SProvider.Id, - Name: self.SProvider.Name, - URL: self.SProvider.AccessUrl, - Account: self.SProvider.Account, - Secret: secretDe, - Vendor: self.SProvider.Provider, - ProxyFunc: proxyFunc, - SApsaraEndpoints: endpoints, + Id: self.SProvider.Id, + Name: self.SProvider.Name, + URL: self.SProvider.AccessUrl, + Account: self.SProvider.Account, + Secret: secretDe, + Vendor: self.SProvider.Provider, + ProxyFunc: proxyFunc, + SApsaraEndpoints: endpoints, + SHuaweiCloudStackEndpoints: hwendpoints, } return cloudprovider.GetProvider(cfg) } @@ -169,7 +175,9 @@ func (self *CloudReportBase) GetAllRegionOfServers(servers []jsonutils.JSONObjec extranleIdMap[region_external_id] = "" region, err := providerInstance.GetIRegionById(region_external_id) if err != nil { - return nil, nil, err + name, _ := server.GetString("name") + log.Errorf("name:%s,region_external_id:%s,err:%v", name, region_external_id, err) + continue } regionServerList = append(regionServerList, region) regionServers := make([]jsonutils.JSONObject, 0) diff --git a/pkg/cloudmon/collectors/common/multiCloudMonUtils.go b/pkg/cloudmon/collectors/common/multiCloudMonUtils.go index afd1ee10bb..87e697ce25 100644 --- a/pkg/cloudmon/collectors/common/multiCloudMonUtils.go +++ b/pkg/cloudmon/collectors/common/multiCloudMonUtils.go @@ -81,7 +81,7 @@ type ReportOptions struct { Timeout int64 `help:"command timeout unit:second" default:"10"` SinceTime string `help:"sinceTime"` EndTime string `help:"endTime"` - Provider []string `help:"List objects from the provider" choices:"VMware|Aliyun|Qcloud|Azure|Aws|Huawei|ZStack|Google|Apsara|JDcloud|Ecloud" json:"provider,omitempty"` + Provider []string `help:"List objects from the provider" choices:"VMware|Aliyun|Qcloud|Azure|Aws|Huawei|ZStack|Google|Apsara|JDcloud|Ecloud|HuaweiCloudStack" json:"provider,omitempty"` PingProbeOptions } diff --git a/pkg/cloudmon/collectors/huaweimon/huaweicloudreport.go b/pkg/cloudmon/collectors/huaweimon/huaweicloudreport.go index 6533a19824..b04d29da56 100644 --- a/pkg/cloudmon/collectors/huaweimon/huaweicloudreport.go +++ b/pkg/cloudmon/collectors/huaweimon/huaweicloudreport.go @@ -26,6 +26,11 @@ import ( func init() { factory := SHwCloudReportFactory{} common.RegisterFactory(&factory) + + stackFactory := SHwStackCloudReportFactory{ + &SHwCloudReportFactory{}, + } + common.RegisterFactory(&stackFactory) } type SHwCloudReportFactory struct { @@ -97,3 +102,11 @@ func (self *SHwCloudReport) Report() error { } return nil } + +type SHwStackCloudReportFactory struct { + *SHwCloudReportFactory +} + +func (self *SHwStackCloudReportFactory) GetId() string { + return compute.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK +} diff --git a/pkg/cloudmon/collectors/huaweimon/hwservice.go b/pkg/cloudmon/collectors/huaweimon/hwservice.go index 194e5efc67..61b77c7169 100644 --- a/pkg/cloudmon/collectors/huaweimon/hwservice.go +++ b/pkg/cloudmon/collectors/huaweimon/hwservice.go @@ -22,16 +22,17 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudmon/collectors/common" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/multicloud/huawei" hw_moudules "yunion.io/x/onecloud/pkg/multicloud/huawei/client/modules" + "yunion.io/x/onecloud/pkg/multicloud/huaweistack" "yunion.io/x/onecloud/pkg/util/influxdb" ) func (self *SHwCloudReport) collectRegionMetricOfHost(region cloudprovider.ICloudRegion, servers []jsonutils.JSONObject) error { dataList := make([]influxdb.SMetricData, 0) - hwReg := region.(*huawei.SRegion) since, until, err := common.TimeRangeFromArgs(self.Args) if err != nil { return err @@ -52,8 +53,7 @@ func (self *SHwCloudReport) collectRegionMetricOfHost(region cloudprovider.IClou hwMeta.Dimensions = append(hwMeta.Dimensions, hw_moudules.SMetricDimension{Name: "instance_id", Value: instanceId}) metas = append(metas, hwMeta) } - - metricDatas, err := hwReg.GetMetricsData(metas, since, until) + metricDatas, err := self.GetMetricData(region, metas, since, until) if err != nil { log.Errorln(err) continue @@ -339,3 +339,16 @@ func (self *SHwCloudReport) collectMetricFromThisServer(server jsonutils.JSONObj self.AddMetricTag(&metric, common.OtherVmTags) return metric, nil } + +func (self *SHwCloudReport) GetMetricData(region cloudprovider.ICloudRegion, metrics []hw_moudules.SMetricMeta, + since time.Time, until time.Time) ([]hw_moudules.SMetricData, error) { + switch self.SProvider.Provider { + case compute.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: + hwReg := region.(*huaweistack.SRegion) + return hwReg.GetMetricsData(metrics, since, until) + default: + hwReg := region.(*huawei.SRegion) + return hwReg.GetMetricsData(metrics, since, until) + } + +} diff --git a/pkg/cloudprovider/cloudprovider.go b/pkg/cloudprovider/cloudprovider.go index c7958d36d6..fcda09d2f6 100644 --- a/pkg/cloudprovider/cloudprovider.go +++ b/pkg/cloudprovider/cloudprovider.go @@ -194,6 +194,7 @@ type ICloudProviderFactory interface { IsPublicCloud() bool IsOnPremise() bool + IsMultiTenant() bool IsSupportPrepaidResources() bool NeedSyncSkuFromCloud() bool @@ -628,6 +629,10 @@ func (factory *baseProviderFactory) IsOnPremise() bool { return false } +func (factory *baseProviderFactory) IsMultiTenant() bool { + return false +} + func (factory *baseProviderFactory) IsCloudeventRegional() bool { return false } @@ -766,6 +771,10 @@ func (factory *SPremiseBaseProviderFactory) IsOnPremise() bool { return true } +func (factory *SPremiseBaseProviderFactory) IsMultiTenant() bool { + return false +} + func (factory *SPremiseBaseProviderFactory) NeedSyncSkuFromCloud() bool { return false } @@ -774,6 +783,10 @@ type SPublicCloudBaseProviderFactory struct { baseProviderFactory } +func (factory *SPublicCloudBaseProviderFactory) IsMultiTenant() bool { + return true +} + func (factory *SPublicCloudBaseProviderFactory) IsPublicCloud() bool { return true } @@ -790,6 +803,10 @@ type SPrivateCloudBaseProviderFactory struct { baseProviderFactory } +func (factory *SPrivateCloudBaseProviderFactory) IsMultiTenant() bool { + return false +} + func (factory *SPrivateCloudBaseProviderFactory) IsPublicCloud() bool { return false } diff --git a/pkg/cloudprovider/endpoints.go b/pkg/cloudprovider/endpoints.go index 402cba09e0..ad9e769249 100644 --- a/pkg/cloudprovider/endpoints.go +++ b/pkg/cloudprovider/endpoints.go @@ -53,6 +53,11 @@ type SHuaweiCloudStackEndpoints struct { // required: true DefaultRegion string `default:"$HUAWEI_DEFAULT_REGION" metavar:"$HUAWEI_DEFAULT_REGION"` + // 默认DNS + // example: 10.125.0.26,10.125.0.27 + // required: false + DefaultSubnetDns string `default:"$HUAWEI_DEFAULT_SUBNET_DNS" metavar:"$HUAWEI_DEFAULT_SUBNET_DNS"` + // 弹性云服务 Ecs string `default:"$HUAWEI_ECS_ENDPOINT"` // 云容器服务 diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index d7f6c336e3..13e478578f 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -332,6 +332,7 @@ type ICloudVM interface { GetBootOrder() string GetVga() string GetVdi() string + GetOSArch() string GetOSType() string GetOSName() string GetBios() string @@ -800,6 +801,7 @@ type ICloudSku interface { GetPrepaidStatus() string GetPostpaidStatus() string + GetCpuArch() string GetCpuCoreCount() int GetMemorySizeMB() int diff --git a/pkg/compute/guestdrivers/baremetals.go b/pkg/compute/guestdrivers/baremetals.go index 1b47c0cb71..773114a677 100644 --- a/pkg/compute/guestdrivers/baremetals.go +++ b/pkg/compute/guestdrivers/baremetals.go @@ -163,9 +163,11 @@ func (self *SBaremetalGuestDriver) GetNamedNetworkConfiguration(guest *models.SG reuseAddr := false hn := host.GetAttach2Network(netConfig.Network) if hn != nil && options.Options.BaremetalServerReuseHostIp { - // try to reuse host network IP address - netConfig.Address = hn.IpAddr - reuseAddr = true + if netConfig.Address == "" || netConfig.Address == hn.IpAddr { + // try to reuse host network IP address + netConfig.Address = hn.IpAddr + reuseAddr = true + } } return net, nicConfs, api.IPAllocationStepup, reuseAddr, nil diff --git a/pkg/compute/guestdrivers/huaweistack.go b/pkg/compute/guestdrivers/huaweistack.go index c3aff11151..9840b6473e 100644 --- a/pkg/compute/guestdrivers/huaweistack.go +++ b/pkg/compute/guestdrivers/huaweistack.go @@ -45,6 +45,10 @@ func (self *SHuaweiCloudStackGuestDriver) GetProvider() string { return api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK } +func (self *SHuaweiCloudStackGuestDriver) DoScheduleSKUFilter() bool { + return false +} + func (self *SHuaweiCloudStackGuestDriver) GetComputeQuotaKeys(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys { keys := models.SComputeResourceKeys{} keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId) @@ -153,5 +157,5 @@ func (self *SHuaweiCloudStackGuestDriver) IsNeedInjectPasswordByCloudInit(desc * } func (self *SHuaweiCloudStackGuestDriver) IsSupportSetAutoRenew() bool { - return true + return false } diff --git a/pkg/compute/models/access_group_caches.go b/pkg/compute/models/access_group_caches.go index 46441cdb85..bd5dc5a61f 100644 --- a/pkg/compute/models/access_group_caches.go +++ b/pkg/compute/models/access_group_caches.go @@ -479,14 +479,6 @@ func (self *SAccessGroupCache) syncWithAccessGroup(ctx context.Context, userCred return group.SyncRules(ctx, userCred, src) } -func (self *SAccessGroupCache) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById(%s)", self.CloudregionId) - } - return region.(*SCloudregion), nil -} - func (self *SAccessGroupCache) GetIRegion() (cloudprovider.ICloudRegion, error) { provider, err := self.GetDriver() if err != nil { diff --git a/pkg/compute/models/app.go b/pkg/compute/models/app.go index 22145fa79f..a1537d69cf 100644 --- a/pkg/compute/models/app.go +++ b/pkg/compute/models/app.go @@ -375,14 +375,6 @@ func (a *SApp) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCre return nil, StartResourceSyncStatusTask(ctx, userCred, a, "AppSyncstatusTask", "") } -func (a *SApp) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(a.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById(%s)", a.CloudregionId) - } - return region.(*SCloudregion), nil -} - func (a *SApp) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := a.GetRegion() if err != nil { diff --git a/pkg/compute/models/buckets.go b/pkg/compute/models/buckets.go index d26310b6ab..92ce1bbaa1 100644 --- a/pkg/compute/models/buckets.go +++ b/pkg/compute/models/buckets.go @@ -379,14 +379,6 @@ func (bucket *SBucket) StartBucketDeleteTask(ctx context.Context, userCred mccli return nil } -func (bucket *SBucket) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(bucket.CloudregionId) - if err != nil { - return nil, errors.Wrap(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (bucket *SBucket) GetIRegion() (cloudprovider.ICloudRegion, error) { provider, err := bucket.GetDriver() if err != nil { diff --git a/pkg/compute/models/cloudproviderregions.go b/pkg/compute/models/cloudproviderregions.go index 7d87b895c1..86ab1cf55e 100644 --- a/pkg/compute/models/cloudproviderregions.go +++ b/pkg/compute/models/cloudproviderregions.go @@ -111,15 +111,6 @@ func (self *SCloudproviderregion) GetAccount() *SCloudaccount { return nil } -func (self *SCloudproviderregion) GetRegion() *SCloudregion { - regionObj, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - log.Errorf("CloudregionManager.FetchById(%s) fail %s", self.CloudregionId, err) - return nil - } - return regionObj.(*SCloudregion) -} - func (manager *SCloudproviderregionManager) FetchCustomizeColumns( ctx context.Context, userCred mcclient.TokenCredential, @@ -402,7 +393,10 @@ func (set SSyncResultSet) Add(manager db.IModelManager, result compare.SyncResul func (self *SCloudproviderregion) DoSync(ctx context.Context, userCred mcclient.TokenCredential, syncRange SSyncRange) error { syncResults := SSyncResultSet{} - localRegion := self.GetRegion() + localRegion, err := self.GetRegion() + if err != nil { + return errors.Wrapf(err, "GetRegion") + } provider := self.GetProvider() self.markSyncing(userCred) @@ -449,12 +443,14 @@ func (self *SCloudproviderregion) DoSync(ctx context.Context, userCred mcclient. } func (self *SCloudproviderregion) getSyncTaskKey() string { - region := self.GetRegion() + region, err := self.GetRegion() + if err != nil { + return self.CloudregionId + } if len(region.ExternalId) > 0 { return region.ExternalId - } else { - return self.CloudproviderId } + return self.CloudproviderId } func (self *SCloudproviderregion) submitSyncTask(ctx context.Context, userCred mcclient.TokenCredential, syncRange SSyncRange) { @@ -511,7 +507,7 @@ func (cpr *SCloudproviderregion) needAutoSyncInternal() bool { if intval > 24*3600 { // at least once everyday intval = 24 * 3600 } - region := cpr.GetRegion() + region, _ := cpr.GetRegion() log.Debugf("empty region %s! no need to check so frequently", region.GetName()) } if time.Now().Sub(cpr.LastSync) > time.Duration(intval)*time.Second { diff --git a/pkg/compute/models/cloudregionresource.go b/pkg/compute/models/cloudregionresource.go index 311051763e..495f84e38a 100644 --- a/pkg/compute/models/cloudregionresource.go +++ b/pkg/compute/models/cloudregionresource.go @@ -26,6 +26,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/stringutils2" @@ -51,13 +52,35 @@ func ValidateCloudregionResourceInput(userCred mcclient.TokenCredential, input a return regionObj.(*SCloudregion), input, nil } -func (self *SCloudregionResourceBase) GetRegion() *SCloudregion { +func (self *SCloudregionResourceBase) GetRegion() (*SCloudregion, error) { region, err := CloudregionManager.FetchById(self.CloudregionId) if err != nil { - log.Errorf("failed to find cloudregion %s error: %v", self.CloudregionId, err) - return nil + return nil, errors.Wrapf(err, "GetRegion(%s)", self.CloudregionId) } - return region.(*SCloudregion) + return region.(*SCloudregion), nil +} + +func (self *SCloudregionResourceBase) GetZoneBySuffix(suffix string) (*SZone, error) { + sq := ZoneManager.Query().SubQuery() + q := sq.Query().Filter( + sqlchemy.AND( + sqlchemy.Equals(sq.Field("cloudregion_id"), self.CloudregionId), + sqlchemy.Endswith(sq.Field("external_id"), suffix), + ), + ) + count, err := q.CountWithError() + if err != nil { + return nil, err + } + if count == 0 { + return nil, errors.Wrapf(cloudprovider.ErrNotFound, suffix) + } + if count > 1 { + return nil, errors.Wrapf(cloudprovider.ErrDuplicateId, suffix) + } + zone := &SZone{} + zone.SetModelManager(ZoneManager, zone) + return zone, q.First(zone) } func (manager *SCloudregionResourceBaseManager) FetchCustomizeColumns( diff --git a/pkg/compute/models/cloudregions.go b/pkg/compute/models/cloudregions.go index bc139ea3ac..a1589373e9 100644 --- a/pkg/compute/models/cloudregions.go +++ b/pkg/compute/models/cloudregions.go @@ -162,6 +162,29 @@ func (self *SCloudregion) GetZones() ([]SZone, error) { return zones, nil } +func (self *SCloudregion) GetZoneBySuffix(suffix string) (*SZone, error) { + sq := ZoneManager.Query().SubQuery() + q := sq.Query().Filter( + sqlchemy.AND( + sqlchemy.Equals(sq.Field("cloudregion_id"), self.Id), + sqlchemy.Endswith(sq.Field("external_id"), suffix), + ), + ) + count, err := q.CountWithError() + if err != nil { + return nil, err + } + if count == 0 { + return nil, errors.Wrapf(cloudprovider.ErrNotFound, suffix) + } + if count > 1 { + return nil, errors.Wrapf(cloudprovider.ErrDuplicateId, suffix) + } + zone := &SZone{} + zone.SetModelManager(ZoneManager, zone) + return zone, q.First(zone) +} + func (self *SCloudregion) GetGuestCount() (int, error) { return self.getGuestCountInternal(false) } @@ -493,7 +516,7 @@ func (self *SCloudregion) syncWithCloudRegion(ctx context.Context, userCred mccl self.IsEmulated = cloudRegion.IsEmulated() - if !factory.IsPublicCloud() && !factory.IsOnPremise() { + if !factory.IsPublicCloud() && !factory.IsOnPremise() && !factory.IsMultiTenant() { self.ManagerId = provider.Id } diff --git a/pkg/compute/models/cloudsync.go b/pkg/compute/models/cloudsync.go index 48863ae61b..1dec4e8db8 100644 --- a/pkg/compute/models/cloudsync.go +++ b/pkg/compute/models/cloudsync.go @@ -1166,7 +1166,10 @@ func syncDBInstanceBackups(ctx context.Context, userCred mcclient.TokenCredentia return errors.Wrapf(err, "GetIDBInstanceBackups") } - region := localInstance.GetRegion() + region, err := localInstance.GetRegion() + if err != nil { + return errors.Wrapf(err, "GetRegion") + } provider := localInstance.GetCloudprovider() result := DBInstanceBackupManager.SyncDBInstanceBackups(ctx, userCred, provider, localInstance, region, backups) diff --git a/pkg/compute/models/dbinstance_accounts.go b/pkg/compute/models/dbinstance_accounts.go index 5512f8deba..f91ed70186 100644 --- a/pkg/compute/models/dbinstance_accounts.go +++ b/pkg/compute/models/dbinstance_accounts.go @@ -328,9 +328,9 @@ func (manager *SDBInstanceAccountManager) ValidateCreateData(ctx context.Context if instance.Status != api.DBINSTANCE_RUNNING { return nil, httperrors.NewInputParameterError("DBInstance %s(%s) status is %s require status is %s", instance.Name, instance.Id, instance.Status, api.DBINSTANCE_RUNNING) } - region := instance.GetRegion() - if region == nil { - return nil, httperrors.NewInputParameterError("failed to found region for dbinstance %s(%s)", instance.Name, instance.Id) + region, err := instance.GetRegion() + if err != nil { + return nil, httperrors.NewGeneralError(errors.Wrapf(err, "GetRegion")) } for i, privilege := range input.Privileges { database, err := instance.GetDBInstanceDatabase(privilege.Database) @@ -417,7 +417,12 @@ func (self *SDBInstanceAccount) PerformGrantPrivilege(ctx context.Context, userC return nil, httperrors.NewInputParameterError("The account %s(%s) has permission %s to the database %s(%s)", self.Name, self.Id, privilege.Privilege, database.Name, database.Id) } - err = instance.GetRegion().GetDriver().ValidateDBInstanceAccountPrivilege(ctx, userCred, instance, self.Name, privilegeStr) + region, err := instance.GetRegion() + if err != nil { + return nil, httperrors.NewGeneralError(errors.Wrapf(err, "GetRegion")) + } + + err = region.GetDriver().ValidateDBInstanceAccountPrivilege(ctx, userCred, instance, self.Name, privilegeStr) if err != nil { return nil, err } @@ -447,13 +452,18 @@ func (self *SDBInstanceAccount) PerformSetPrivileges(ctx context.Context, userCr "input": map[string]string{}, } + region, err := instance.GetRegion() + if err != nil { + return nil, errors.Wrapf(err, "GetRegion") + } for i, privilege := range input.Privileges { database, err := instance.GetDBInstanceDatabase(privilege.Database) if err != nil { return nil, httperrors.NewInputParameterError("Failed to found database %s for dbinstance %s(%s): %v", privilege.Database, instance.Name, instance.Id, err) } input.Privileges[i].DBInstancedatabaseId = database.Id - err = instance.GetRegion().GetDriver().ValidateDBInstanceAccountPrivilege(ctx, userCred, instance, self.Name, privilege.Privilege) + + err = region.GetDriver().ValidateDBInstanceAccountPrivilege(ctx, userCred, instance, self.Name, privilege.Privilege) if err != nil { return nil, err } @@ -571,7 +581,11 @@ func (self *SDBInstanceAccount) PerformResetPassword(ctx context.Context, userCr return nil, err } } - err = instance.GetRegion().GetDriver().ValidateResetDBInstancePassword(ctx, userCred, instance, self.Name) + region, err := instance.GetRegion() + if err != nil { + return nil, err + } + err = region.GetDriver().ValidateResetDBInstancePassword(ctx, userCred, instance, self.Name) if err != nil { return nil, err } diff --git a/pkg/compute/models/dbinstance_backups.go b/pkg/compute/models/dbinstance_backups.go index 78cf62de55..5232e6d699 100644 --- a/pkg/compute/models/dbinstance_backups.go +++ b/pkg/compute/models/dbinstance_backups.go @@ -230,9 +230,9 @@ func (manager *SDBInstanceBackupManager) ValidateCreateData(ctx context.Context, if instance.Status != api.DBINSTANCE_RUNNING { return nil, httperrors.NewInputParameterError("DBInstance %s(%s) status is %s require status is %s", instance.Name, instance.Id, instance.Status, api.DBINSTANCE_RUNNING) } - region := instance.GetRegion() - if region == nil { - return nil, httperrors.NewInputParameterError("failed to found region for dbinstance %s(%s)", instance.Name, instance.Id) + region, err := instance.GetRegion() + if err != nil { + return nil, err } input.CloudregionId = region.Id input, err = region.GetDriver().ValidateCreateDBInstanceBackupData(ctx, userCred, ownerId, instance, input) @@ -347,9 +347,9 @@ func (self *SDBInstanceBackup) PerformSyncstatus(ctx context.Context, userCred m } func (backup *SDBInstanceBackup) GetIRegion() (cloudprovider.ICloudRegion, error) { - region := backup.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "no valid cloudregion") + region, err := backup.GetRegion() + if err != nil { + return nil, err } provider, err := backup.GetDriver() if err != nil { @@ -570,10 +570,6 @@ func (self *SDBInstanceBackup) GetCloudprovider() *SCloudprovider { return self.SManagedResourceBase.GetCloudprovider() } -func (self *SDBInstanceBackup) GetRegion() *SCloudregion { - return self.SCloudregionResourceBase.GetRegion() -} - func (manager *SDBInstanceBackupManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, diff --git a/pkg/compute/models/dbinstance_databases.go b/pkg/compute/models/dbinstance_databases.go index e0f9917987..ff87a0ceaf 100644 --- a/pkg/compute/models/dbinstance_databases.go +++ b/pkg/compute/models/dbinstance_databases.go @@ -237,9 +237,9 @@ func (manager *SDBInstanceDatabaseManager) ValidateCreateData(ctx context.Contex if instance.Status != api.DBINSTANCE_RUNNING { return nil, httperrors.NewInputParameterError("DBInstance %s(%s) status is %s require status is %s", instance.Name, instance.Id, instance.Status, api.DBINSTANCE_RUNNING) } - region := instance.GetRegion() - if region == nil { - return nil, httperrors.NewInputParameterError("failed to found region for dbinstance %s(%s)", instance.Name, instance.Id) + region, err := instance.GetRegion() + if err != nil { + return nil, err } for i, _account := range input.Accounts { account, err := instance.GetDBInstanceAccount(_account.Account) diff --git a/pkg/compute/models/dbinstance_skus.go b/pkg/compute/models/dbinstance_skus.go index 9f41d99d90..5115a2edad 100644 --- a/pkg/compute/models/dbinstance_skus.go +++ b/pkg/compute/models/dbinstance_skus.go @@ -652,9 +652,9 @@ func (manager *SDBInstanceSkuManager) ListItemExportKeys(ctx context.Context, func (self *SDBInstanceSku) GetZoneInfo() (cloudprovider.SZoneInfo, error) { zoneInfo := cloudprovider.SZoneInfo{ZoneId: self.ZoneId} - region := self.GetRegion() - if region == nil { - return zoneInfo, fmt.Errorf("empyt region for rds sku %s(%s)", self.Name, self.Id) + region, err := self.GetRegion() + if err != nil { + return zoneInfo, nil } var cloudZoneId = func(id string) (string, error) { if len(id) == 0 { diff --git a/pkg/compute/models/dbinstances.go b/pkg/compute/models/dbinstances.go index 9e065b554e..da6ac9e90f 100644 --- a/pkg/compute/models/dbinstances.go +++ b/pkg/compute/models/dbinstances.go @@ -979,7 +979,12 @@ func (self *SDBInstance) PerformRenew(ctx context.Context, userCred mcclient.Tok return nil, httperrors.NewInputParameterError("invalid duration %s: %s", durationStr, err) } - if !self.GetRegion().GetDriver().IsSupportedBillingCycle(bc, DBInstanceManager.KeywordPlural()) { + region, err := self.GetRegion() + if err != nil { + return nil, err + } + + if !region.GetDriver().IsSupportedBillingCycle(bc, DBInstanceManager.KeywordPlural()) { return nil, httperrors.NewInputParameterError("unsupported duration %s", durationStr) } @@ -1057,9 +1062,9 @@ func (self *SDBInstance) PerformPublicConnection(ctx context.Context, userCred m return nil, httperrors.NewInputParameterError("The extranet connection is not open") } - region := self.GetRegion() - if region == nil { - return nil, httperrors.NewGeneralError(fmt.Errorf("failed to found region for dbinstance %s(%s)", self.Name, self.Id)) + region, err := self.GetRegion() + if err != nil { + return nil, err } if !region.GetDriver().IsSupportDBInstancePublicConnection() { @@ -1585,9 +1590,9 @@ func (self *SDBInstance) SetZoneInfo(ctx context.Context, userCred mcclient.Toke } func (self *SDBInstance) SetZoneIds(extInstance cloudprovider.ICloudDBInstance) error { - region := self.GetRegion() - if region == nil { - return fmt.Errorf("failed found region for dbinstance %s", self.Name) + region, err := self.GetRegion() + if err != nil { + return err } zones, err := region.GetZones() if err != nil { @@ -1667,7 +1672,10 @@ func (self *SDBInstance) SyncWithCloudDBInstance(ctx context.Context, userCred m } } if len(self.VpcId) == 0 { - region := self.GetRegion() + region, err := self.GetRegion() + if err != nil { + return err + } vpc, err := VpcManager.GetOrCreateVpcForClassicNetwork(ctx, provider, region) if err != nil { log.Errorf("failed to create classic vpc for region %s error: %v", region.Name, err) @@ -1815,12 +1823,13 @@ func (man *SDBInstanceManager) TotalCount( return stat, err } -func (dbinstance *SDBInstance) GetQuotaKeys() quotas.IQuotaKeys { +func (self *SDBInstance) GetQuotaKeys() quotas.IQuotaKeys { + region, _ := self.GetRegion() return fetchRegionalQuotaKeys( rbacutils.ScopeProject, - dbinstance.GetOwnerId(), - dbinstance.GetRegion(), - dbinstance.GetCloudprovider(), + self.GetOwnerId(), + region, + self.GetCloudprovider(), ) } @@ -1836,14 +1845,14 @@ func (dbinstance *SDBInstance) GetUsages() []db.IUsage { } } -func (dbinstance *SDBInstance) GetIRegion() (cloudprovider.ICloudRegion, error) { - region := dbinstance.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "no valid cloudregion") - } - provider, err := dbinstance.GetDriver() +func (self *SDBInstance) GetIRegion() (cloudprovider.ICloudRegion, error) { + region, err := self.GetRegion() if err != nil { - return nil, errors.Wrap(err, "dbinstance.GetDriver") + return nil, err + } + provider, err := self.GetDriver() + if err != nil { + return nil, errors.Wrap(err, "self.GetDriver") } return provider.GetIRegionById(region.GetExternalId()) } diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index 67798aa0c8..f96b476832 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -1907,7 +1907,7 @@ func (self *SDisk) PerformPurge(ctx context.Context, userCred mcclient.TokenCred } provider := self.GetCloudprovider() - if provider != nil && provider.Provider == api.CLOUD_PROVIDER_HUAWEI { + if provider != nil && utils.IsInStringArray(provider.Provider, []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { cnt, err := self.GetSnapshotCount() if err != nil { return nil, httperrors.NewInternalServerError("GetSnapshotCount fail %s", err) @@ -1922,7 +1922,7 @@ func (self *SDisk) PerformPurge(ctx context.Context, userCred mcclient.TokenCred func (self *SDisk) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error { if !jsonutils.QueryBoolean(query, "delete_snapshots", false) { - if provider := self.GetCloudprovider(); provider != nil && provider.Provider == api.CLOUD_PROVIDER_HUAWEI { + if provider := self.GetCloudprovider(); provider != nil && utils.IsInStringArray(provider.Provider, []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { cnt, err := self.GetSnapshotCount() if err != nil { return httperrors.NewInternalServerError("GetSnapshotCount fail %s", err) diff --git a/pkg/compute/models/elastic_search.go b/pkg/compute/models/elastic_search.go index 2149850598..8e531ba21c 100644 --- a/pkg/compute/models/elastic_search.go +++ b/pkg/compute/models/elastic_search.go @@ -368,14 +368,6 @@ func (self *SElasticSearch) StartDeleteTask(ctx context.Context, userCred mcclie return nil } -func (self *SElasticSearch) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById(%s)", self.CloudregionId) - } - return region.(*SCloudregion), nil -} - func (self *SElasticSearch) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := self.GetRegion() if err != nil { diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 410bdd0f4d..272b83d279 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -152,7 +152,7 @@ func (manager *SElasticipManager) ListItemFilter( return nil, httperrors.NewGeneralError(err) } guest := serverObj.(*SGuest) - if guest.Hypervisor == api.HYPERVISOR_KVM || utils.IsInStringArray(guest.Hypervisor, api.PRIVATE_CLOUD_HYPERVISORS) { + if guest.Hypervisor == api.HYPERVISOR_KVM || (utils.IsInStringArray(guest.Hypervisor, api.PRIVATE_CLOUD_HYPERVISORS) && guest.Hypervisor != api.HYPERVISOR_HUAWEI_CLOUD_STACK) { zone, _ := guest.getZone() networks := NetworkManager.Query().SubQuery() wires := WireManager.Query().SubQuery() @@ -271,14 +271,6 @@ func (manager *SElasticipManager) QueryDistinctExtraField(q *sqlchemy.SQuery, fi return q, httperrors.ErrNotFound } -func (self *SElasticip) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (self *SElasticip) GetNetwork() (*SNetwork, error) { network, err := NetworkManager.FetchById(self.NetworkId) if err != nil { diff --git a/pkg/compute/models/filesystem.go b/pkg/compute/models/filesystem.go index 8bea01413d..90c177f268 100644 --- a/pkg/compute/models/filesystem.go +++ b/pkg/compute/models/filesystem.go @@ -526,14 +526,6 @@ func (self *SFileSystem) StartSyncstatus(ctx context.Context, userCred mcclient. return StartResourceSyncStatusTask(ctx, userCred, self, "FileSystemSyncstatusTask", parentTaskId) } -func (self *SFileSystem) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrap(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (self *SFileSystem) GetIRegion() (cloudprovider.ICloudRegion, error) { provider, err := self.GetDriver() if err != nil { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index fab04bda1c..431edf4809 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -2494,6 +2494,7 @@ func (self *SGuest) syncWithCloudVM(ctx context.Context, userCred mcclient.Token self.BootOrder = extVM.GetBootOrder() self.Vga = extVM.GetVga() self.Vdi = extVM.GetVdi() + self.OsArch = extVM.GetOSArch() self.OsType = extVM.GetOSType() self.Bios = extVM.GetBios() self.Machine = extVM.GetMachine() @@ -2573,6 +2574,7 @@ func (manager *SGuestManager) newCloudVM(ctx context.Context, userCred mcclient. guest.BootOrder = extVM.GetBootOrder() guest.Vga = extVM.GetVga() guest.Vdi = extVM.GetVdi() + guest.OsArch = extVM.GetOSArch() guest.OsType = extVM.GetOSType() guest.Bios = extVM.GetBios() guest.Machine = extVM.GetMachine() diff --git a/pkg/compute/models/instance_snapshots.go b/pkg/compute/models/instance_snapshots.go index f0ddd47852..9ad989de3c 100644 --- a/pkg/compute/models/instance_snapshots.go +++ b/pkg/compute/models/instance_snapshots.go @@ -458,10 +458,11 @@ func (self *SInstanceSnapshot) GetSnapshots() ([]SSnapshot, error) { } func (self *SInstanceSnapshot) GetQuotaKeys() quotas.IQuotaKeys { + region, _ := self.GetRegion() return fetchRegionalQuotaKeys( rbacutils.ScopeProject, self.GetOwnerId(), - self.GetRegion(), + region, self.GetCloudprovider(), ) } diff --git a/pkg/compute/models/kafka.go b/pkg/compute/models/kafka.go index e9b3b6f2d2..5cd5d8f9c4 100644 --- a/pkg/compute/models/kafka.go +++ b/pkg/compute/models/kafka.go @@ -365,14 +365,6 @@ func (self *SKafka) StartDeleteTask(ctx context.Context, userCred mcclient.Token return nil } -func (self *SKafka) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById(%s)", self.CloudregionId) - } - return region.(*SCloudregion), nil -} - func (self *SKafka) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := self.GetRegion() if err != nil { diff --git a/pkg/compute/models/loadbalancer_backendstatus.go b/pkg/compute/models/loadbalancer_backendstatus.go index 3eec506754..1ab16757e0 100644 --- a/pkg/compute/models/loadbalancer_backendstatus.go +++ b/pkg/compute/models/loadbalancer_backendstatus.go @@ -24,7 +24,6 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" - "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/influxdb" ) @@ -60,9 +59,9 @@ func (lbr *SLoadbalancerListenerRule) GetDetailsBackendStatus(ctx context.Contex if provider != nil { return jsonutils.NewArray(), nil } - lblis := lbr.GetLoadbalancerListener() - if lblis == nil { - return nil, httperrors.NewNotFoundError("find listener of listener rule %s(%s)", lbr.Name, lbr.Id) + lblis, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } pxname := fmt.Sprintf("backends_rule-%s", lbr.Id) return lbGetBackendGroupCheckStatus(ctx, userCred, lblis.LoadbalancerId, pxname, lbr.BackendGroupId) diff --git a/pkg/compute/models/loadbalancerbackendgroupresource.go b/pkg/compute/models/loadbalancerbackendgroupresource.go index 1c739f37fb..3c9abdc7d5 100644 --- a/pkg/compute/models/loadbalancerbackendgroupresource.go +++ b/pkg/compute/models/loadbalancerbackendgroupresource.go @@ -53,61 +53,60 @@ func ValidateLoadbalancerBackendgroupResourceInput(userCred mcclient.TokenCreden return lbbgObj.(*SLoadbalancerBackendGroup), input, nil } -func (self *SLoadbalancerBackendgroupResourceBase) GetLoadbalancerBackendGroup() *SLoadbalancerBackendGroup { - w, _ := LoadbalancerBackendGroupManager.FetchById(self.BackendGroupId) - if w != nil { - return w.(*SLoadbalancerBackendGroup) +func (self *SLoadbalancerBackendgroupResourceBase) GetLoadbalancerBackendGroup() (*SLoadbalancerBackendGroup, error) { + w, err := LoadbalancerBackendGroupManager.FetchById(self.BackendGroupId) + if err != nil { + return nil, err } - return nil + return w.(*SLoadbalancerBackendGroup), nil } -func (self *SLoadbalancerBackendgroupResourceBase) GetLoadbalancer() *SLoadbalancer { - lbbg := self.GetLoadbalancerBackendGroup() - if lbbg != nil { - return lbbg.GetLoadbalancer() +func (self *SLoadbalancerBackendgroupResourceBase) GetLoadbalancer() (*SLoadbalancer, error) { + lbbg, err := self.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - return nil + return lbbg.GetLoadbalancer() } -func (self *SLoadbalancerBackendgroupResourceBase) GetVpc() *SVpc { - lb := self.GetLoadbalancer() - if lb != nil { - return lb.GetVpc() +func (self *SLoadbalancerBackendgroupResourceBase) GetVpc() (*SVpc, error) { + lb, err := self.GetLoadbalancer() + if err != nil { + return nil, errors.Wrapf(err, "GetLoadbalancer") } - return nil + return lb.GetVpc() } -func (self *SLoadbalancerBackendgroupResourceBase) GetCloudprovider() *SCloudprovider { - lb := self.GetLoadbalancer() - if lb != nil { - return lb.GetCloudprovider() +func (self *SLoadbalancerBackendgroupResourceBase) GetCloudprovider() (*SCloudprovider, error) { + lb, err := self.GetLoadbalancer() + if err != nil { + return nil, err } - return nil + return lb.GetCloudprovider(), nil } func (self *SLoadbalancerBackendgroupResourceBase) GetProviderName() string { - lb := self.GetLoadbalancer() - if lb != nil { - return lb.SManagedResourceBase.GetProviderName() + lb, err := self.GetLoadbalancer() + if err != nil { + return "" } - return "" + return lb.SManagedResourceBase.GetProviderName() } -func (self *SLoadbalancerBackendgroupResourceBase) GetRegion() *SCloudregion { - vpc := self.GetVpc() - if vpc == nil { - return nil +func (self *SLoadbalancerBackendgroupResourceBase) GetRegion() (*SCloudregion, error) { + vpc, err := self.GetVpc() + if err != nil { + return nil, err } - region, _ := vpc.GetRegion() - return region + return vpc.GetRegion() } -func (self *SLoadbalancerBackendgroupResourceBase) GetZone() *SZone { - lb := self.GetLoadbalancer() - if lb != nil { - return lb.GetZone() +func (self *SLoadbalancerBackendgroupResourceBase) GetZone() (*SZone, error) { + lb, err := self.GetLoadbalancer() + if err != nil { + return nil, err } - return nil + return lb.GetZone() } func (manager *SLoadbalancerBackendgroupResourceBaseManager) FetchCustomizeColumns( diff --git a/pkg/compute/models/loadbalancerbackendgroups.go b/pkg/compute/models/loadbalancerbackendgroups.go index 96e6471312..fb2006695a 100644 --- a/pkg/compute/models/loadbalancerbackendgroups.go +++ b/pkg/compute/models/loadbalancerbackendgroups.go @@ -191,7 +191,7 @@ func (man *SLoadbalancerBackendGroupManager) ValidateCreateData(ctx context.Cont var ( lb = lbV.Model.(*SLoadbalancer) - lbRegion = lb.GetRegion() + lbRegion, _ = lb.GetRegion() lbIsManaged = lb.IsManaged() backends = []cloudprovider.SLoadbalancerBackend{} ) @@ -267,9 +267,9 @@ func (man *SLoadbalancerBackendGroupManager) ValidateCreateData(ctx context.Cont } } data.Set("backends", jsonutils.Marshal(backends)) - region := lb.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to find region for loadbalancer %s", lb.Name) + region, err := lb.GetRegion() + if err != nil { + return nil, err } return region.GetDriver().ValidateCreateLoadbalancerBackendGroupData(ctx, userCred, data, lb, backends) } @@ -294,27 +294,28 @@ func (lbbg *SLoadbalancerBackendGroup) GetLoadbalancerListeners() ([]SLoadbalanc return listeners, nil } -func (lbbg *SLoadbalancerBackendGroup) GetLoadbalancer() *SLoadbalancer { +func (lbbg *SLoadbalancerBackendGroup) GetLoadbalancer() (*SLoadbalancer, error) { lb, err := LoadbalancerManager.FetchById(lbbg.LoadbalancerId) if err != nil { - log.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) - return nil + return nil, err } - return lb.(*SLoadbalancer) + return lb.(*SLoadbalancer), nil } -func (llbg *SLoadbalancerBackendGroup) GetRegion() *SCloudregion { - if loadbalancer := llbg.GetLoadbalancer(); loadbalancer != nil { - return loadbalancer.GetRegion() +func (llbg *SLoadbalancerBackendGroup) GetRegion() (*SCloudregion, error) { + loadbalancer, err := llbg.GetLoadbalancer() + if err != nil { + return nil, err } - return nil + return loadbalancer.GetRegion() } func (lbbg *SLoadbalancerBackendGroup) GetIRegion() (cloudprovider.ICloudRegion, error) { - if loadbalancer := lbbg.GetLoadbalancer(); loadbalancer != nil { - return loadbalancer.GetIRegion() + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, errors.Wrapf(err, "GetLoadbalancer") } - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + return loadbalancer.GetIRegion() } func (lbbg *SLoadbalancerBackendGroup) GetBackends() ([]SLoadbalancerBackend, error) { @@ -554,7 +555,7 @@ func (lbbg *SLoadbalancerBackendGroup) StartOpenstackLoadBalancerBackendGroupCre } func (lbbg *SLoadbalancerBackendGroup) LBPendingDelete(ctx context.Context, userCred mcclient.TokenCredential) { - if lb := lbbg.GetLoadbalancer(); lb != nil && lb.BackendGroupId == lbbg.Id { + if lb, _ := lbbg.GetLoadbalancer(); lb != nil && lb.BackendGroupId == lbbg.Id { if _, err := db.UpdateWithLock(ctx, lb, func() error { lb.BackendGroupId = "" return nil @@ -635,7 +636,7 @@ func (lbbg *SLoadbalancerBackendGroup) GetBackendGroupParams() (*cloudprovider.S ListenerID: listenerId, } - loadbalancer := lbbg.GetLoadbalancer() + loadbalancer, _ := lbbg.GetLoadbalancer() if loadbalancer != nil { ret.VpcId = loadbalancer.VpcId ret.LoadbalancerID = loadbalancer.ExternalId @@ -745,14 +746,13 @@ func (lbbg *SLoadbalancerBackendGroup) GetAwsBackendGroupParams(lblis *SLoadbala ret.ListenerID = lblis.GetExternalId() - lb := lblis.GetLoadbalancer() + lb, _ := lblis.GetLoadbalancer() if lb != nil { - vpc := lb.GetVpc() - if vpc != nil { - ret.VpcId = vpc.GetExternalId() - } else { - return nil, fmt.Errorf("loadbalancer %s related vpc not found", lb.GetId()) + vpc, err := lb.GetVpc() + if err != nil { + return nil, errors.Wrapf(err, "GetVpc") } + ret.VpcId = vpc.GetExternalId() } ret.ListenType = lblis.ListenerType ret.ListenPort = lblis.ListenerPort @@ -860,9 +860,9 @@ func (lbbg *SLoadbalancerBackendGroup) GetICloudLoadbalancerBackendGroup() (clou return nil, fmt.Errorf("backendgroup %s has no external id", lbbg.GetId()) } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("backendgroup %s releated loadbalancer not found", lbbg.GetId()) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, errors.Wrapf(err, "GetLoadbalacer") } iregion, err := lb.GetIRegion() diff --git a/pkg/compute/models/loadbalancerbackends.go b/pkg/compute/models/loadbalancerbackends.go index 020d3c53e9..fe0e04bfda 100644 --- a/pkg/compute/models/loadbalancerbackends.go +++ b/pkg/compute/models/loadbalancerbackends.go @@ -174,9 +174,9 @@ func (man *SLoadbalancerBackendManager) QueryDistinctExtraField(q *sqlchemy.SQue } func (man *SLoadbalancerBackendManager) ValidateBackendVpc(lb *SLoadbalancer, guest *SGuest, backendgroup *SLoadbalancerBackendGroup) error { - region := lb.GetRegion() - if region == nil { - return httperrors.NewResourceNotFoundError("failed to find region for loadbalancer %s", lb.Name) + region, err := lb.GetRegion() + if err != nil { + return err } requireStatus := region.GetDriver().GetBackendStatusForAdd() if !utils.IsInStringArray(guest.Status, requireStatus) { @@ -187,7 +187,10 @@ func (man *SLoadbalancerBackendManager) ValidateBackendVpc(lb *SLoadbalancer, gu return httperrors.NewBadRequestError("%s", err) } if len(lb.VpcId) > 0 { - lbVpc := lb.GetVpc() + lbVpc, err := lb.GetVpc() + if err != nil { + return err + } if lbVpc != nil && !lbVpc.IsEmulated && vpc.Id != lb.VpcId { return httperrors.NewBadRequestError("guest %s(%s) vpc %s(%s) not same as loadbalancer vpc %s", guest.Name, guest.Id, vpc.Name, vpc.Id, lb.VpcId) } @@ -238,11 +241,14 @@ func (man *SLoadbalancerBackendManager) ValidateCreateData(ctx context.Context, backendType := backendTypeV.Value backendGroup := backendGroupV.Model.(*SLoadbalancerBackendGroup) - lb := backendGroup.GetLoadbalancer() + lb, err := backendGroup.GetLoadbalancer() + if err != nil { + return nil, err + } var backendModel db.IModel input := apis.VirtualResourceCreateInput{} - err := data.Unmarshal(&input) + err = data.Unmarshal(&input) if err != nil { return nil, httperrors.NewInternalServerError("unmarshal VirtualResourceCreateInput fail %s", err) } @@ -252,9 +258,9 @@ func (man *SLoadbalancerBackendManager) ValidateCreateData(ctx context.Context, } data.Update(jsonutils.Marshal(input)) - region := lb.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to find region for loadbalancer %s", lb.Name) + region, err := lb.GetRegion() + if err != nil { + return nil, err } ctx = context.WithValue(ctx, "ownerId", ownerId) @@ -266,20 +272,19 @@ func (lbb *SLoadbalancerBackend) AllowPerformStatus(ctx context.Context, userCre } func (lbb *SLoadbalancerBackend) GetCloudproviderId() string { - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { return lbbg.GetCloudproviderId() } return "" } -func (lbb *SLoadbalancerBackend) GetLoadbalancerBackendGroup() *SLoadbalancerBackendGroup { +func (lbb *SLoadbalancerBackend) GetLoadbalancerBackendGroup() (*SLoadbalancerBackendGroup, error) { backendgroup, err := LoadbalancerBackendGroupManager.FetchById(lbb.BackendGroupId) if err != nil { - log.Errorf("failed to find backendgroup for backend %s", lbb.Name) - return nil + return nil, errors.Wrapf(err, "GetLoadbalancerBackendGroup(%s)", lbb.BackendGroupId) } - return backendgroup.(*SLoadbalancerBackendGroup) + return backendgroup.(*SLoadbalancerBackendGroup), nil } func (lbb *SLoadbalancerBackend) GetGuest() *SGuest { @@ -290,18 +295,20 @@ func (lbb *SLoadbalancerBackend) GetGuest() *SGuest { return guest.(*SGuest) } -func (lbb *SLoadbalancerBackend) GetRegion() *SCloudregion { - if backendgroup := lbb.GetLoadbalancerBackendGroup(); backendgroup != nil { - return backendgroup.GetRegion() +func (lbb *SLoadbalancerBackend) GetRegion() (*SCloudregion, error) { + backendgroup, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - return nil + return backendgroup.GetRegion() } func (lbb *SLoadbalancerBackend) GetIRegion() (cloudprovider.ICloudRegion, error) { - if backendgroup := lbb.GetLoadbalancerBackendGroup(); backendgroup != nil { - return backendgroup.GetIRegion() + backendgroup, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - return nil, fmt.Errorf("failed to find region for backend %s", lbb.Name) + return backendgroup.GetIRegion() } func (man *SLoadbalancerBackendManager) GetGuestAddress(guest *SGuest) (string, error) { @@ -330,13 +337,13 @@ func (lbb *SLoadbalancerBackend) ValidateUpdateData(ctx context.Context, userCre } data.Update(jsonutils.Marshal(input)) - region := lbb.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to found region for loadbalancer backend %s", lbb.Name) + region, err := lbb.GetRegion() + if err != nil { + return nil, err } - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, httperrors.NewResourceNotFoundError("failed to found backendgroup for backend %s(%s)", lbb.Name, lbb.Id) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } data.Set("backend_id", jsonutils.NewString(lbb.BackendId)) diff --git a/pkg/compute/models/loadbalancercachedacls.go b/pkg/compute/models/loadbalancercachedacls.go index 2357340c0b..953cdb2489 100644 --- a/pkg/compute/models/loadbalancercachedacls.go +++ b/pkg/compute/models/loadbalancercachedacls.go @@ -22,6 +22,7 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/compare" + "yunion.io/x/pkg/utils" "yunion.io/x/sqlchemy" "yunion.io/x/onecloud/pkg/apis" @@ -124,7 +125,7 @@ func (man *SCachedLoadbalancerAclManager) ValidateCreateData(ctx context.Context } } - if providerV.Model.(*SCloudprovider).Provider == api.CLOUD_PROVIDER_HUAWEI { + if utils.IsInStringArray(providerV.Model.(*SCloudprovider).Provider, []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { listenerV := validators.NewModelIdOrNameValidator("listener", "loadbalancerlistener", ownerId) if err := listenerV.Validate(data); err != nil { return nil, err @@ -313,7 +314,7 @@ func (self *SCachedLoadbalancerAcl) syncRemoveCloudLoadbalanceAcl(ctx context.Co func (acl *SCachedLoadbalancerAcl) SyncWithCloudLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, extAcl cloudprovider.ICloudLoadbalancerAcl, projectId mcclient.IIdentityProvider) error { diff, err := db.UpdateWithLock(ctx, acl, func() error { // todo: 华为云acl没有name字段应此不需要同步名称 - if api.CLOUD_PROVIDER_HUAWEI != acl.GetProviderName() { + if !utils.IsInStringArray(acl.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { acl.Name = extAcl.GetName() } else { ext_listener_id := extAcl.GetAclListenerID() @@ -349,16 +350,16 @@ func (man *SCachedLoadbalancerAclManager) GetOrCreateCachedAcl(ctx context.Conte defer lockman.ReleaseClass(ctx, man, ownerProjId) listenerId := "" - if lblis.GetProviderName() == api.CLOUD_PROVIDER_HUAWEI { + if utils.IsInStringArray(lblis.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { listenerId = lblis.Id } if lblis.GetProviderName() == api.CLOUD_PROVIDER_OPENSTACK { listenerId = lblis.Id } - region := lblis.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "Loadbalancer listenser is not attached region") + region, err := lblis.GetRegion() + if err != nil { + return nil, err } lbacl, err := man.getLoadbalancerAclByRegion(provider, region.Id, acl.Id, listenerId) if err == nil { diff --git a/pkg/compute/models/loadbalancercachedcertificates.go b/pkg/compute/models/loadbalancercachedcertificates.go index 6041fd5662..32246a7874 100644 --- a/pkg/compute/models/loadbalancercachedcertificates.go +++ b/pkg/compute/models/loadbalancercachedcertificates.go @@ -255,9 +255,9 @@ func (man *SCachedLoadbalancerCertificateManager) GetOrCreateCachedCertificate(c lockman.LockClass(ctx, man, ownerProjId) defer lockman.ReleaseClass(ctx, man, ownerProjId) - region := lblis.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "loadbalancer listener is not attached to any region?") + region, err := lblis.GetRegion() + if err != nil { + return nil, err } lbcert, err := man.getLoadbalancerCertificateByRegion(provider, region.Id, cert.Id) if err == nil { diff --git a/pkg/compute/models/loadbalancerhuaweicachedlbb.go b/pkg/compute/models/loadbalancerhuaweicachedlbb.go index 106c90d663..003eedcb07 100644 --- a/pkg/compute/models/loadbalancerhuaweicachedlbb.go +++ b/pkg/compute/models/loadbalancerhuaweicachedlbb.go @@ -296,9 +296,9 @@ func (man *SHuaweiCachedLbManager) newFromCloudLoadbalancerBackend(ctx context.C } func newLocalBackendFromCloudLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, loadbalancerBackendgroup *SLoadbalancerBackendGroup, extLoadbalancerBackend cloudprovider.ICloudLoadbalancerBackend, syncOwnerId mcclient.IIdentityProvider) (*SLoadbalancerBackend, error) { - lbbgRegion := loadbalancerBackendgroup.GetRegion() - if lbbgRegion == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "loadbalancerBackendgroup is not attached to any region") + lbbgRegion, err := loadbalancerBackendgroup.GetRegion() + if err != nil { + return nil, err } lbbgProvider := loadbalancerBackendgroup.GetCloudprovider() if lbbgProvider == nil { diff --git a/pkg/compute/models/loadbalancerhuaweicachedlbbg.go b/pkg/compute/models/loadbalancerhuaweicachedlbbg.go index 80d33a86ad..00c82699df 100644 --- a/pkg/compute/models/loadbalancerhuaweicachedlbbg.go +++ b/pkg/compute/models/loadbalancerhuaweicachedlbbg.go @@ -29,7 +29,6 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" ) @@ -337,9 +336,9 @@ func (man *SHuaweiCachedLbbgManager) newFromCloudLoadbalancerBackendgroup(ctx co lbbg := &SHuaweiCachedLbbg{} lbbg.SetModelManager(man, lbbg) - region := lb.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "loadbalancer is not attached to any region") + region, err := lb.GetRegion() + if err != nil { + return nil, err } lbbg.ManagerId = provider.Id diff --git a/pkg/compute/models/loadbalancerlistenerresource.go b/pkg/compute/models/loadbalancerlistenerresource.go index ddbe3c4a0e..50226c759d 100644 --- a/pkg/compute/models/loadbalancerlistenerresource.go +++ b/pkg/compute/models/loadbalancerlistenerresource.go @@ -53,13 +53,12 @@ func ValidateLoadbalancerListenerResourceInput(userCred mcclient.TokenCredential return listenerObj.(*SLoadbalancerListener), input, nil } -func (self *SLoadbalancerListenerResourceBase) GetLoadbalancerListener() *SLoadbalancerListener { +func (self *SLoadbalancerListenerResourceBase) GetLoadbalancerListener() (*SLoadbalancerListener, error) { listener, err := LoadbalancerListenerManager.FetchById(self.ListenerId) if err != nil { - log.Errorf("failed to find LoadbalancerCluster %s error: %v", self.ListenerId, err) - return nil + return nil, errors.Wrapf(err, "GetLoadbalancerListener(%s)", self.ListenerId) } - return listener.(*SLoadbalancerListener) + return listener.(*SLoadbalancerListener), nil } func (self *SLoadbalancerListenerResourceBase) GetCloudproviderId() string { @@ -71,7 +70,7 @@ func (self *SLoadbalancerListenerResourceBase) GetCloudproviderId() string { } func (self *SLoadbalancerListenerResourceBase) GetCloudprovider() *SCloudprovider { - listener := self.GetLoadbalancerListener() + listener, _ := self.GetLoadbalancerListener() if listener != nil { return listener.GetCloudprovider() } @@ -79,7 +78,7 @@ func (self *SLoadbalancerListenerResourceBase) GetCloudprovider() *SCloudprovide } func (self *SLoadbalancerListenerResourceBase) GetProviderName() string { - listener := self.GetLoadbalancerListener() + listener, _ := self.GetLoadbalancerListener() if listener != nil { return listener.GetProviderName() } diff --git a/pkg/compute/models/loadbalancerlistenerrules.go b/pkg/compute/models/loadbalancerlistenerrules.go index 7582808512..26f5b814c5 100644 --- a/pkg/compute/models/loadbalancerlistenerrules.go +++ b/pkg/compute/models/loadbalancerlistenerrules.go @@ -494,9 +494,9 @@ func (man *SLoadbalancerListenerRuleManager) ValidateCreateData(ctx context.Cont } listener := listenerV.Model.(*SLoadbalancerListener) - region := listener.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to find region for loadbalancer listener %s", listener.Name) + region, err := listener.GetRegion() + if err != nil { + return nil, err } backendGroupV := validators.NewModelIdOrNameValidator("backend_group", "loadbalancerbackendgroup", ownerId) @@ -579,9 +579,9 @@ func (lbr *SLoadbalancerListenerRule) ValidateUpdateData(ctx context.Context, us } data.Update(jsonutils.Marshal(input)) - region := lbr.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to find region for loadbalancer listener rule %s", lbr.Name) + region, err := lbr.GetRegion() + if err != nil { + return nil, err } ctx = context.WithValue(ctx, "lbr", lbr) @@ -628,20 +628,12 @@ func (man *SLoadbalancerListenerRuleManager) FetchCustomizeColumns( return rows } -/*func (lbr *SLoadbalancerListenerRule) GetLoadbalancerListener() *SLoadbalancerListener { - listener, err := LoadbalancerListenerManager.FetchById(lbr.ListenerId) +func (lbr *SLoadbalancerListenerRule) GetRegion() (*SCloudregion, error) { + listener, err := lbr.GetLoadbalancerListener() if err != nil { - log.Errorf("failed to find listener for loadbalancer listener rule %s", lbr.Name) - return nil + return nil, err } - return listener.(*SLoadbalancerListener) -}*/ - -func (lbr *SLoadbalancerListenerRule) GetRegion() *SCloudregion { - if listener := lbr.GetLoadbalancerListener(); listener != nil { - return listener.GetRegion() - } - return nil + return listener.GetRegion() } func (lbr *SLoadbalancerListenerRule) GetLoadbalancerBackendGroup() *SLoadbalancerBackendGroup { @@ -740,7 +732,7 @@ func (lbr *SLoadbalancerListenerRule) constructFieldsFromCloudListenerRule(userC } if groupId := extRule.GetBackendGroupId(); len(groupId) > 0 { - if lbr.GetProviderName() == api.CLOUD_PROVIDER_HUAWEI { + if utils.IsInStringArray(lbr.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { group, err := db.FetchByExternalId(HuaweiCachedLbbgManager, groupId) if err != nil { if err == sql.ErrNoRows { @@ -782,7 +774,7 @@ func (lbr *SLoadbalancerListenerRule) updateCachedLoadbalancerBackendGroupAssoci } switch lbr.GetProviderName() { - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: _group, err := db.FetchByExternalId(HuaweiCachedLbbgManager, exteralLbbgId) if err != nil { if err == sql.ErrNoRows { diff --git a/pkg/compute/models/loadbalancerlisteners.go b/pkg/compute/models/loadbalancerlisteners.go index 850a738aee..52902d5d49 100644 --- a/pkg/compute/models/loadbalancerlisteners.go +++ b/pkg/compute/models/loadbalancerlisteners.go @@ -339,14 +339,11 @@ func (man *SLoadbalancerListenerManager) ValidateCreateData(ctx context.Context, data.Update(jsonutils.Marshal(input)) lb := lbV.Model.(*SLoadbalancer) - region := lb.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to find region for loadbalancer %s", lb.Name) + region, err := lb.GetRegion() + if err != nil { + return nil, err } - // if len(lb.ManagerId) > 0 { - // data.Set("manager_id", jsonutils.NewString(lb.ManagerId)) - // } return region.GetDriver().ValidateCreateLoadbalancerListenerData(ctx, userCred, ownerId, data, lb, backendGroupV.Model) } @@ -372,7 +369,7 @@ func (man *SLoadbalancerListenerManager) ValidateAcl(aclStatusV *validators.Vali return httperrors.NewMissingParameterError("acl_type") } } else { - if providerName != api.CLOUD_PROVIDER_HUAWEI { + if !utils.IsInStringArray(providerName, []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { data.Set("acl_id", jsonutils.NewString("")) data.Set("cached_acl_id", jsonutils.NewString("")) } @@ -452,9 +449,9 @@ func (lblis *SLoadbalancerListener) ValidateUpdateData(ctx context.Context, user } data.Update(jsonutils.Marshal(input)) - region := lblis.GetRegion() - if region == nil { - return nil, httperrors.NewResourceNotFoundError("failed to find region for loadbalancer listener %s", lblis.Name) + region, err := lblis.GetRegion() + if err != nil { + return nil, err } return region.GetDriver().ValidateUpdateLoadbalancerListenerData(ctx, userCred, data, lblis, backendGroupV.Model) @@ -669,7 +666,7 @@ func (lblis *SLoadbalancerListener) GetLoadbalancerListenerParams() (*cloudprovi listener.BackendGroupType = backendgroup.Type } - if loadbalancer := lblis.GetLoadbalancer(); loadbalancer != nil { + if loadbalancer, _ := lblis.GetLoadbalancer(); loadbalancer != nil { listener.LoadbalancerID = loadbalancer.ExternalId } @@ -734,7 +731,7 @@ func (lblis *SLoadbalancerListener) GetAwsLoadbalancerListenerParams() (*cloudpr return nil, err } - lb := lblis.GetLoadbalancer() + lb, _ := lblis.GetLoadbalancer() if lb != nil { listener.LoadbalancerID = lb.ExternalId } @@ -857,32 +854,32 @@ func (lblis *SLoadbalancerListener) GetLoadbalancerBackendGroup() *SLoadbalancer return group } -func (lblis *SLoadbalancerListener) GetLoadbalancer() *SLoadbalancer { +func (lblis *SLoadbalancerListener) GetLoadbalancer() (*SLoadbalancer, error) { _loadbalancer, err := LoadbalancerManager.FetchById(lblis.LoadbalancerId) if err != nil { - log.Errorf("failed to find loadbalancer for loadbalancer listener %s", lblis.Name) - return nil + return nil, err } loadbalancer := _loadbalancer.(*SLoadbalancer) if loadbalancer.PendingDeleted { - log.Errorf("loadbalancer %s(%s) has been deleted", loadbalancer.Name, loadbalancer.Id) - return nil + return nil, errors.Wrapf(cloudprovider.ErrNotFound, "pending deleted") } - return loadbalancer + return loadbalancer, nil } -func (lblis *SLoadbalancerListener) GetRegion() *SCloudregion { - if loadbalancer := lblis.GetLoadbalancer(); loadbalancer != nil { - return loadbalancer.GetRegion() +func (lblis *SLoadbalancerListener) GetRegion() (*SCloudregion, error) { + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } - return nil + return loadbalancer.GetRegion() } func (lblis *SLoadbalancerListener) GetIRegion() (cloudprovider.ICloudRegion, error) { - if loadbalancer := lblis.GetLoadbalancer(); loadbalancer != nil { - return loadbalancer.GetIRegion() + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + return loadbalancer.GetIRegion() } func (man *SLoadbalancerListenerManager) getLoadbalancerListenersByLoadbalancer(lb *SLoadbalancer) ([]SLoadbalancerListener, error) { @@ -1016,7 +1013,7 @@ func (lblis *SLoadbalancerListener) constructFieldsFromCloudListener(userCred mc switch lblis.ListenerType { case api.LB_LISTENER_TYPE_UDP: - if lblis.GetProviderName() != api.CLOUD_PROVIDER_HUAWEI { + if !utils.IsInStringArray(lblis.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { lblis.HealthCheckExp = extListener.GetHealthCheckExp() lblis.HealthCheckReq = extListener.GetHealthCheckReq() } @@ -1059,7 +1056,7 @@ func (lblis *SLoadbalancerListener) constructFieldsFromCloudListener(userCred mc groupId := extListener.GetBackendGroupId() switch lblis.GetProviderName() { - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: if len(groupId) > 0 { group, err := db.FetchByExternalIdAndManagerId(HuaweiCachedLbbgManager, groupId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { return q.Equals("manager_id", lb.ManagerId) @@ -1097,7 +1094,7 @@ func (lblis *SLoadbalancerListener) constructFieldsFromCloudListener(userCred mc } case api.CLOUD_PROVIDER_QCLOUD: if len(groupId) > 0 { - lb := lblis.GetLoadbalancer() + lb, _ := lblis.GetLoadbalancer() if forward, _ := lb.LBInfo.Int("Forward"); forward == 1 { // 应用型负载均衡 group, err := db.FetchByExternalIdAndManagerId(QcloudCachedLbbgManager, groupId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { @@ -1151,7 +1148,7 @@ func (lblis *SLoadbalancerListener) updateCachedLoadbalancerBackendGroupAssociat } switch lblis.GetProviderName() { - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: _group, err := db.FetchByExternalIdAndManagerId(HuaweiCachedLbbgManager, exteralLbbgId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { return q.Equals("manager_id", managerId) }) @@ -1177,7 +1174,7 @@ func (lblis *SLoadbalancerListener) updateCachedLoadbalancerBackendGroupAssociat } } case api.CLOUD_PROVIDER_QCLOUD: - lb := lblis.GetLoadbalancer() + lb, _ := lblis.GetLoadbalancer() if forward, _ := lb.LBInfo.Int("Forward"); forward == 1 { _group, err := db.FetchByExternalIdAndManagerId(QcloudCachedLbbgManager, exteralLbbgId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { return q.Equals("manager_id", managerId) diff --git a/pkg/compute/models/loadbalanceropenstackcachedlbbg.go b/pkg/compute/models/loadbalanceropenstackcachedlbbg.go index 20886ca513..5114913dfb 100644 --- a/pkg/compute/models/loadbalanceropenstackcachedlbbg.go +++ b/pkg/compute/models/loadbalanceropenstackcachedlbbg.go @@ -29,7 +29,6 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" ) @@ -363,9 +362,9 @@ func (man *SOpenstackCachedLbbgManager) newFromCloudLoadbalancerBackendgroup(ctx lbbg := &SOpenstackCachedLbbg{} lbbg.SetModelManager(man, lbbg) - region := lb.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "loadbalancer is not attached to any region") + region, err := lb.GetRegion() + if err != nil { + return nil, err } lbbg.ManagerId = provider.Id diff --git a/pkg/compute/models/loadbalancerqcloudcachedlbbg.go b/pkg/compute/models/loadbalancerqcloudcachedlbbg.go index 2693f3277c..ea98d77d44 100644 --- a/pkg/compute/models/loadbalancerqcloudcachedlbbg.go +++ b/pkg/compute/models/loadbalancerqcloudcachedlbbg.go @@ -30,7 +30,6 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" ) @@ -321,9 +320,9 @@ func (man *SQcloudCachedLbbgManager) newFromCloudLoadbalancerBackendgroup(ctx co lbbg := &SQcloudCachedLbbg{} lbbg.SetModelManager(man, lbbg) - region := lb.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "loadbalancer is not attached to any region") + region, err := lb.GetRegion() + if err != nil { + return nil, err } lbbg.ManagerId = provider.Id lbbg.CloudregionId = region.Id diff --git a/pkg/compute/models/loadbalancerresource.go b/pkg/compute/models/loadbalancerresource.go index 3ae1610b1d..73b892c5be 100644 --- a/pkg/compute/models/loadbalancerresource.go +++ b/pkg/compute/models/loadbalancerresource.go @@ -54,29 +54,29 @@ func ValidateLoadbalancerResourceInput(userCred mcclient.TokenCredential, input return lbObj.(*SLoadbalancer), input, nil } -func (self *SLoadbalancerResourceBase) GetLoadbalancer() *SLoadbalancer { - w, _ := LoadbalancerManager.FetchById(self.LoadbalancerId) - if w != nil { - return w.(*SLoadbalancer) +func (self *SLoadbalancerResourceBase) GetLoadbalancer() (*SLoadbalancer, error) { + w, err := LoadbalancerManager.FetchById(self.LoadbalancerId) + if err != nil { + return nil, err } - return nil + return w.(*SLoadbalancer), nil } -func (self *SLoadbalancerResourceBase) GetVpc() *SVpc { - lb := self.GetLoadbalancer() - if lb != nil { - return lb.GetVpc() +func (self *SLoadbalancerResourceBase) GetVpc() (*SVpc, error) { + lb, err := self.GetLoadbalancer() + if err != nil { + return nil, err } - return nil + return lb.GetVpc() } func (self *SLoadbalancerResourceBase) GetCloudprovider() *SCloudprovider { - vpc := self.GetVpc() + vpc, _ := self.GetVpc() if vpc != nil { return vpc.GetCloudprovider() } - lb := self.GetLoadbalancer() + lb, _ := self.GetLoadbalancer() if lb != nil { return lb.GetCloudprovider() } @@ -93,7 +93,7 @@ func (self *SLoadbalancerResourceBase) GetCloudproviderId() string { } func (self *SLoadbalancerResourceBase) GetProviderName() string { - vpc := self.GetVpc() + vpc, _ := self.GetVpc() if vpc != nil { return vpc.GetProviderName() } @@ -101,36 +101,35 @@ func (self *SLoadbalancerResourceBase) GetProviderName() string { } func (self *SLoadbalancerResourceBase) GetCloudaccount() *SCloudaccount { - vpc := self.GetVpc() + vpc, _ := self.GetVpc() if vpc != nil { return vpc.GetCloudaccount() } return nil } -func (self *SLoadbalancerResourceBase) GetRegion() *SCloudregion { - vpc := self.GetVpc() - if vpc == nil { - return nil +func (self *SLoadbalancerResourceBase) GetRegion() (*SCloudregion, error) { + vpc, err := self.GetVpc() + if err != nil { + return nil, err } - region, _ := vpc.GetRegion() - return region + return vpc.GetRegion() } func (self *SLoadbalancerResourceBase) GetRegionId() string { - region := self.GetRegion() + region, _ := self.GetRegion() if region != nil { return region.Id } return "" } -func (self *SLoadbalancerResourceBase) GetZone() *SZone { - lb := self.GetLoadbalancer() - if lb != nil { - return lb.GetZone() +func (self *SLoadbalancerResourceBase) GetZone() (*SZone, error) { + lb, err := self.GetLoadbalancer() + if err != nil { + return nil, err } - return nil + return lb.GetZone() } func (manager *SLoadbalancerResourceBaseManager) FetchCustomizeColumns( @@ -340,7 +339,7 @@ func (manager *SLoadbalancerResourceBaseManager) GetExportKeys() []string { } func (self *SLoadbalancerResourceBase) GetChangeOwnerCandidateDomainIds() []string { - lb := self.GetLoadbalancer() + lb, _ := self.GetLoadbalancer() if lb != nil { return lb.GetChangeOwnerCandidateDomainIds() } diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index e7a606ab32..b72b0abd94 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -420,22 +420,20 @@ func (lb *SLoadbalancer) GetCloudprovider() *SCloudprovider { return lb.SManagedResourceBase.GetCloudprovider() } -func (lb *SLoadbalancer) GetRegion() *SCloudregion { - return lb.SCloudregionResourceBase.GetRegion() -} - func (lb *SLoadbalancer) GetCloudproviderId() string { return lb.SManagedResourceBase.GetCloudproviderId() } -func (lb *SLoadbalancer) GetZone() *SZone { - zone, _ := lb.SZoneResourceBase.GetZone() - return zone +func (lb *SLoadbalancer) GetRegion() (*SCloudregion, error) { + return lb.SCloudregionResourceBase.GetRegion() } -func (lb *SLoadbalancer) GetVpc() *SVpc { - vpc, _ := lb.SVpcResourceBase.GetVpc() - return vpc +func (lb *SLoadbalancer) GetVpc() (*SVpc, error) { + return lb.SVpcResourceBase.GetVpc() +} + +func (lb *SLoadbalancer) GetZone() (*SZone, error) { + return lb.SZoneResourceBase.GetZone() } func (lb *SLoadbalancer) GetNetworks() ([]SNetwork, error) { @@ -462,9 +460,9 @@ func (lb *SLoadbalancer) GetIRegion() (cloudprovider.ICloudRegion, error) { if err != nil { return nil, errors.Wrap(err, "lb.GetDriver") } - region := lb.GetRegion() - if region == nil { - return nil, fmt.Errorf("failed to get region for lb %s", lb.Name) + region, err := lb.GetRegion() + if err != nil { + return nil, errors.Wrapf(err, "GetRegion") } return provider.GetIRegionById(region.ExternalId) } @@ -480,9 +478,9 @@ func (lb *SLoadbalancer) GetCreateLoadbalancerParams(iRegion cloudprovider.IClou params.Tags, _ = lb.GetAllUserMetadata() if len(lb.ZoneId) > 0 { - zone := lb.GetZone() - if zone == nil { - return nil, fmt.Errorf("failed to find zone for lb %s", lb.Name) + zone, err := lb.GetZone() + if err != nil { + return nil, err } iZone, err := iRegion.GetIZoneById(zone.ExternalId) if err != nil { @@ -507,10 +505,10 @@ func (lb *SLoadbalancer) GetCreateLoadbalancerParams(iRegion cloudprovider.IClou params.EgressMbps = lb.EgressMbps } - if lb.AddressType == api.LB_ADDR_TYPE_INTRANET || utils.IsInStringArray(lb.SManagedResourceBase.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_AWS, api.CLOUD_PROVIDER_QCLOUD}) { - vpc := lb.GetVpc() - if vpc == nil { - return nil, fmt.Errorf("failed to find vpc for lb %s", lb.Name) + if lb.AddressType == api.LB_ADDR_TYPE_INTRANET || utils.IsInStringArray(lb.SManagedResourceBase.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK, api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_AWS, api.CLOUD_PROVIDER_QCLOUD}) { + vpc, err := lb.GetVpc() + if err != nil { + return nil, err } iVpc, err := iRegion.GetIVpcById(vpc.ExternalId) if err != nil { @@ -519,7 +517,7 @@ func (lb *SLoadbalancer) GetCreateLoadbalancerParams(iRegion cloudprovider.IClou params.VpcID = iVpc.GetId() } - if lb.AddressType == api.LB_ADDR_TYPE_INTRANET || utils.IsInStringArray(lb.SManagedResourceBase.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_AWS}) { + if lb.AddressType == api.LB_ADDR_TYPE_INTRANET || utils.IsInStringArray(lb.SManagedResourceBase.GetProviderName(), []string{api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK, api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_AWS}) { networks, err := lb.GetNetworks() if err != nil { return nil, fmt.Errorf("failed to find network for lb %s: %s", lb.Name, err) @@ -722,7 +720,7 @@ func (lb *SLoadbalancer) ValidateDeleteCondition(ctx context.Context) error { } func (lb *SLoadbalancer) validatePurgeCondition(ctx context.Context) error { - region := lb.GetRegion() + region, _ := lb.GetRegion() if region != nil { if err := region.GetDriver().ValidateDeleteLoadbalancerCondition(ctx, lb); err != nil { return err @@ -1089,7 +1087,8 @@ func (self *SLoadbalancer) SyncLoadbalancerEip(ctx context.Context, userCred mcc // do nothing } else if eip == nil && extEip != nil { // add - neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, self.GetRegion(), provider.GetOwnerId()) + region, _ := self.GetRegion() + neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, region, provider.GetOwnerId()) if err != nil { log.Errorf("getEipByExtEip error %v", err) result.AddError(err) @@ -1120,7 +1119,8 @@ func (self *SLoadbalancer) SyncLoadbalancerEip(ctx context.Context, userCred mcc result.DeleteError(err) } else { result.Delete() - neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, self.GetRegion(), provider.GetOwnerId()) + region, _ := self.GetRegion() + neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, region, provider.GetOwnerId()) if err != nil { result.AddError(err) } else { @@ -1308,10 +1308,11 @@ func (man *SLoadbalancerManager) TotalCount( } func (lb *SLoadbalancer) GetQuotaKeys() quotas.IQuotaKeys { + region, _ := lb.GetRegion() return fetchRegionalQuotaKeys( rbacutils.ScopeProject, lb.GetOwnerId(), - lb.GetRegion(), + region, lb.GetCloudprovider(), ) } diff --git a/pkg/compute/models/mongodb.go b/pkg/compute/models/mongodb.go index 5fb36546b7..2c01d1eb51 100644 --- a/pkg/compute/models/mongodb.go +++ b/pkg/compute/models/mongodb.go @@ -17,7 +17,6 @@ package models import ( "context" "fmt" - "strings" "time" "yunion.io/x/jsonutils" @@ -247,8 +246,9 @@ func (manager *SMongoDBManager) FetchCustomizeColumns( manRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) regRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) - rdsIds := make([]string, len(rows)) vpcIds := make([]string, len(rows)) + netIds := make([]string, len(rows)) + zoneIds := make([]string, len(rows)) for i := range rows { rows[i] = api.MongoDBDetails{ VirtualResourceDetails: virtRows[i], @@ -256,8 +256,9 @@ func (manager *SMongoDBManager) FetchCustomizeColumns( CloudregionResourceInfo: regRows[i], } instance := objs[i].(*SMongoDB) - rdsIds[i] = instance.Id vpcIds[i] = instance.VpcId + netIds[i] = instance.NetworkId + zoneIds[i] = instance.ZoneId } vpcs := make(map[string]SVpc) @@ -268,11 +269,23 @@ func (manager *SMongoDBManager) FetchCustomizeColumns( return rows } + netMaps, err := db.FetchIdNameMap2(NetworkManager, netIds) + if err != nil { + return rows + } + + zoneMaps, err := db.FetchIdNameMap2(ZoneManager, zoneIds) + if err != nil { + return rows + } + for i := range rows { if vpc, ok := vpcs[vpcIds[i]]; ok { rows[i].Vpc = vpc.Name rows[i].VpcExtId = vpc.ExternalId } + rows[i].Network, _ = netMaps[netIds[i]] + rows[i].Zone, _ = zoneMaps[zoneIds[i]] } return rows @@ -477,6 +490,40 @@ func (self *SMongoDB) SyncWithCloudMongoDB(ctx context.Context, userCred mcclien self.MaintainTime = ext.GetMaintainTime() self.Status = ext.GetStatus() self.Port = ext.GetPort() + + if vpcId := ext.GetVpcId(); len(vpcId) > 0 { + vpc, err := db.FetchByExternalIdAndManagerId(VpcManager, vpcId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { + return q.Equals("manager_id", self.ManagerId) + }) + if err != nil { + log.Errorf("FetchVpcId(%s) error: %v", vpcId, err) + } else { + self.VpcId = vpc.GetId() + } + } + + if networkId := ext.GetNetworkId(); len(networkId) > 0 { + network, err := db.FetchByExternalIdAndManagerId(NetworkManager, networkId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { + wire := WireManager.Query().SubQuery() + vpc := VpcManager.Query().SubQuery() + return q.Join(wire, sqlchemy.Equals(wire.Field("id"), q.Field("wire_id"))). + Join(vpc, sqlchemy.Equals(vpc.Field("id"), wire.Field("vpc_id"))). + Filter(sqlchemy.Equals(vpc.Field("manager_id"), self.ManagerId)) + }) + if err == nil { + self.NetworkId = network.GetId() + } + } + + if zoneId := ext.GetZoneId(); len(zoneId) > 0 { + zone, err := self.GetZoneBySuffix(zoneId) + if err != nil { + log.Errorf("find zone %s error: %v", zoneId, err) + } else { + self.ZoneId = zone.Id + } + } + return nil }) if err != nil { @@ -511,12 +558,9 @@ func (self *SCloudregion) newFromCloudMongoDB(ctx context.Context, userCred mccl ins.ReplicationNum = ext.GetReplicationNum() if zoneId := ext.GetZoneId(); len(zoneId) > 0 { - zones, _ := self.GetZones() - for _, zone := range zones { - if strings.HasSuffix(zone.ExternalId, zoneId) { - ins.ZoneId = zone.Id - break - } + zone, err := self.GetZoneBySuffix(zoneId) + if err == nil { + ins.ZoneId = zone.Id } } @@ -609,10 +653,11 @@ func (man *SMongoDBManager) TotalCount( } func (self *SMongoDB) GetQuotaKeys() quotas.IQuotaKeys { + region, _ := self.GetRegion() return fetchRegionalQuotaKeys( rbacutils.ScopeProject, self.GetOwnerId(), - self.GetRegion(), + region, self.GetCloudprovider(), ) } @@ -630,9 +675,9 @@ func (self *SMongoDB) GetUsages() []db.IUsage { } func (self *SMongoDB) GetIRegion() (cloudprovider.ICloudRegion, error) { - region := self.GetRegion() - if region == nil { - return nil, errors.Wrap(httperrors.ErrInvalidStatus, "no valid cloudregion") + region, err := self.GetRegion() + if err != nil { + return nil, err } provider, err := self.GetDriver() if err != nil { diff --git a/pkg/compute/models/mount_targets.go b/pkg/compute/models/mount_targets.go index 925ae9ff99..273ac38c36 100644 --- a/pkg/compute/models/mount_targets.go +++ b/pkg/compute/models/mount_targets.go @@ -362,7 +362,7 @@ func (self *SMountTarget) ValidateDeleteCondition(ctx context.Context) error { if err != nil { return httperrors.NewGeneralError(errors.Wrapf(err, "GetRegion")) } - if region.Provider == api.CLOUD_PROVIDER_HUAWEI { + if utils.IsInStringArray(region.Provider, []string{api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK}) { return httperrors.NewNotSupportedError("not allow to delete") } return self.SStatusStandaloneResourceBase.ValidateDeleteCondition(ctx) diff --git a/pkg/compute/models/purge.go b/pkg/compute/models/purge.go index beb4db9763..c5effca85d 100644 --- a/pkg/compute/models/purge.go +++ b/pkg/compute/models/purge.go @@ -551,7 +551,7 @@ func (lbbg *SLoadbalancerBackendGroup) purgeCachedlbbg(ctx context.Context, user switch lbbg.GetProviderName() { case api.CLOUD_PROVIDER_AWS: return lbbg.purgeAwsCachedlbbg(ctx, userCred) - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: return lbbg.purgeHuaweiCachedlbbg(ctx, userCred) } @@ -632,7 +632,7 @@ func (lbb *SLoadbalancerBackend) purgeCachedlbb(ctx context.Context, userCred mc switch lbb.GetProviderName() { case api.CLOUD_PROVIDER_AWS: return lbb.purgeAwsCachedlbb(ctx, userCred) - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: return lbb.purgeHuaweiCachedlbb(ctx, userCred) } diff --git a/pkg/compute/models/secgroupcache.go b/pkg/compute/models/secgroupcache.go index 8232118700..a3f6cbbc19 100644 --- a/pkg/compute/models/secgroupcache.go +++ b/pkg/compute/models/secgroupcache.go @@ -806,9 +806,9 @@ func (self *SSecurityGroupCache) GetSecuritRuleSet() (cloudprovider.SecurityRule } func (self *SSecurityGroupCache) SyncRules() error { - region := self.GetRegion() - if region == nil { - return fmt.Errorf("failed to get region for secgroupcache %s(%s)", self.Name, self.Id) + region, err := self.GetRegion() + if err != nil { + return err } iSecgroup, err := self.GetISecurityGroup() if err != nil { diff --git a/pkg/compute/models/skus.go b/pkg/compute/models/skus.go index 702ede8141..5642af8601 100644 --- a/pkg/compute/models/skus.go +++ b/pkg/compute/models/skus.go @@ -400,14 +400,6 @@ func (self *SServerSku) GetPrivateCloudproviders() ([]SCloudprovider, error) { return providers, nil } -func (self *SServerSku) GetRegion() (*SCloudregion, error) { - regionObj, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, err - } - return regionObj.(*SCloudregion), nil -} - func (self *SServerSkuManager) ClearSchedDescCache(wait bool) error { s := auth.GetAdminSession(context.Background(), options.Options.Region, "") _, err := modules.SchedManager.SyncSku(s, true) @@ -1091,6 +1083,7 @@ func (self *SServerSku) constructSku(extSku cloudprovider.ICloudSku) { self.PrepaidStatus = extSku.GetPrepaidStatus() self.PostpaidStatus = extSku.GetPostpaidStatus() + self.CpuArch = extSku.GetCpuArch() self.CpuCoreCount = extSku.GetCpuCoreCount() self.MemorySizeMB = extSku.GetMemorySizeMB() diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index 19f0fa9125..33d5d522c8 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -355,9 +355,9 @@ func (manager *SSnapshotManager) ValidateCreateData( input.StorageId = disk.StorageId } input.ManagerId = storage.ManagerId - region, _ := storage.GetRegion() - if region == nil { - return input, httperrors.NewInputParameterError("failed to found region for disk's storage %s(%s)", storage.Name, storage.Id) + region, err := storage.GetRegion() + if err != nil { + return input, err } input.CloudregionId = region.Id @@ -641,7 +641,7 @@ func (self *SSnapshot) GetStorageType() string { } func (self *SSnapshot) GetRegionDriver() IRegionDriver { - cloudRegion := self.GetRegion() + cloudRegion, _ := self.GetRegion() if cloudRegion != nil { return cloudRegion.GetDriver() } @@ -1003,19 +1003,15 @@ func (manager *SSnapshotManager) SyncSnapshots(ctx context.Context, userCred mcc return syncResult } -func (self *SSnapshot) GetRegion() *SCloudregion { - return CloudregionManager.FetchRegionById(self.CloudregionId) -} - func (self *SSnapshot) GetISnapshotRegion() (cloudprovider.ICloudRegion, error) { provider, err := self.GetDriver() if err != nil { return nil, err } - region := self.GetRegion() - if region == nil { - return nil, fmt.Errorf("fail to find region for snapshot") + region, err := self.GetRegion() + if err != nil { + return nil, err } return provider.GetIRegionById(region.GetExternalId()) } @@ -1040,7 +1036,7 @@ func (self *SSnapshot) PerformPurge(ctx context.Context, userCred mcclient.Token } func (self *SSnapshot) getCloudProviderInfo() SCloudProviderInfo { - region := self.GetRegion() + region, _ := self.GetRegion() provider := self.GetCloudprovider() return MakeCloudProviderInfo(region, nil, provider) } @@ -1066,7 +1062,7 @@ func (manager *SSnapshotManager) CleanupSnapshots(ctx context.Context, userCred } snapshot.SetModelManager(manager, snapshot) - region := snapshot.GetRegion() + region, _ := snapshot.GetRegion() if err = manager.StartSnapshotCleanupTask(ctx, userCred, region, now); err != nil { log.Errorf("Start snaphsot cleanup task failed %s", err) return @@ -1087,12 +1083,13 @@ func (manager *SSnapshotManager) StartSnapshotCleanupTask( return nil } -func (snapshot *SSnapshot) GetQuotaKeys() quotas.IQuotaKeys { +func (self *SSnapshot) GetQuotaKeys() quotas.IQuotaKeys { + region, _ := self.GetRegion() return fetchRegionalQuotaKeys( rbacutils.ScopeProject, - snapshot.GetOwnerId(), - snapshot.GetRegion(), - snapshot.GetCloudprovider(), + self.GetOwnerId(), + region, + self.GetCloudprovider(), ) } diff --git a/pkg/compute/models/storagecaches.go b/pkg/compute/models/storagecaches.go index c662f86f1e..1169497219 100644 --- a/pkg/compute/models/storagecaches.go +++ b/pkg/compute/models/storagecaches.go @@ -145,11 +145,11 @@ func (self *SStoragecache) GetHost() (*SHost, error) { func (self *SStoragecache) GetRegion() (*SCloudregion, error) { host, err := self.GetHost() if err != nil { - return nil, err + return nil, errors.Wrapf(err, "GetHost") } - region, _ := host.GetRegion() - if region == nil { - return nil, fmt.Errorf("failed to get region for host %s(%s)", host.Name, host.Id) + region, err := host.GetRegion() + if err != nil { + return nil, errors.Wrapf(err, "GetRegion") } return region, nil } diff --git a/pkg/compute/models/waf_instances.go b/pkg/compute/models/waf_instances.go index f22e696e1f..4dd3b7654b 100644 --- a/pkg/compute/models/waf_instances.go +++ b/pkg/compute/models/waf_instances.go @@ -385,14 +385,6 @@ func (self *SWafInstance) syncRemove(ctx context.Context, userCred mcclient.Toke return self.RealDelete(ctx, userCred) } -func (self *SWafInstance) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (self *SWafInstance) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := self.GetRegion() if err != nil { diff --git a/pkg/compute/models/waf_ipset_caches.go b/pkg/compute/models/waf_ipset_caches.go index 1aad5f3943..09dd7b27ff 100644 --- a/pkg/compute/models/waf_ipset_caches.go +++ b/pkg/compute/models/waf_ipset_caches.go @@ -213,14 +213,6 @@ func (self *SWafIPSetCache) StartDeleteTask(ctx context.Context, userCred mcclie return task.ScheduleRun(nil) } -func (self *SWafIPSetCache) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (self *SWafIPSetCache) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := self.GetRegion() if err != nil { diff --git a/pkg/compute/models/waf_regexset_caches.go b/pkg/compute/models/waf_regexset_caches.go index 5cf654d80f..66eca3c725 100644 --- a/pkg/compute/models/waf_regexset_caches.go +++ b/pkg/compute/models/waf_regexset_caches.go @@ -213,14 +213,6 @@ func (self *SWafRegexSetCache) StartDeleteTask(ctx context.Context, userCred mcc return task.ScheduleRun(nil) } -func (self *SWafRegexSetCache) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (self *SWafRegexSetCache) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := self.GetRegion() if err != nil { diff --git a/pkg/compute/models/waf_rule_group_caches.go b/pkg/compute/models/waf_rule_group_caches.go index 0d19f0115c..c014f955bf 100644 --- a/pkg/compute/models/waf_rule_group_caches.go +++ b/pkg/compute/models/waf_rule_group_caches.go @@ -213,14 +213,6 @@ func (self *SWafRuleGroupCache) StartDeleteTask(ctx context.Context, userCred mc return task.ScheduleRun(nil) } -func (self *SWafRuleGroupCache) GetRegion() (*SCloudregion, error) { - region, err := CloudregionManager.FetchById(self.CloudregionId) - if err != nil { - return nil, errors.Wrapf(err, "CloudregionManager.FetchById") - } - return region.(*SCloudregion), nil -} - func (self *SWafRuleGroupCache) GetIRegion() (cloudprovider.ICloudRegion, error) { region, err := self.GetRegion() if err != nil { diff --git a/pkg/compute/models/zones.go b/pkg/compute/models/zones.go index b8135d8ffb..bb48661f9b 100644 --- a/pkg/compute/models/zones.go +++ b/pkg/compute/models/zones.go @@ -86,7 +86,7 @@ func (manager *SZoneManager) AllowListItems(ctx context.Context, userCred mcclie func (zone *SZone) ValidateDeleteCondition(ctx context.Context) error { usage := zone.GeneralUsage() if !usage.IsEmpty() { - return httperrors.NewNotEmptyError("not empty zone") + return httperrors.NewNotEmptyError("not empty zone: %s", zone.Id) } return zone.SStandaloneResourceBase.ValidateDeleteCondition(ctx) } diff --git a/pkg/compute/regiondrivers/aliyun.go b/pkg/compute/regiondrivers/aliyun.go index 0f16d1d8bf..6822e49f61 100644 --- a/pkg/compute/regiondrivers/aliyun.go +++ b/pkg/compute/regiondrivers/aliyun.go @@ -303,7 +303,7 @@ func (self *SAliyunRegionDriver) ValidateCreateLoadbalancerBackendData(ctx conte } hostRegion, _ := host.GetRegion() - lbRegion := lb.GetRegion() + lbRegion, _ := lb.GetRegion() if hostRegion.Id != lbRegion.Id { return nil, httperrors.NewInputParameterError("region of host %q (%s) != region of loadbalancer %q (%s))", host.Name, host.ZoneId, lb.Name, lb.ZoneId) @@ -611,7 +611,7 @@ func (self *SAliyunRegionDriver) ValidateCreateLoadbalancerListenerData(ctx cont } // check scheduler limiations - cloudregion := lb.GetRegion() + cloudregion, _ := lb.GetRegion() if cloudregion == nil { return nil, httperrors.NewResourceNotFoundError("failed to find loadbalancer's %s(%s) region", lb.Name, lb.Id) } @@ -707,9 +707,9 @@ func (self *SAliyunRegionDriver) ValidateUpdateLoadbalancerListenerData(ctx cont listenerType, _ := data.GetString("listener_type") - lb := lblis.GetLoadbalancer() - if lb == nil { - return nil, httperrors.NewInternalServerError("failed to found loadbalancer for listener %s(%s)", lblis.Name, lblis.Id) + lb, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } egressMbps := 5000 @@ -748,7 +748,7 @@ func (self *SAliyunRegionDriver) ValidateUpdateLoadbalancerListenerData(ctx cont if len(lb.LoadbalancerSpec) == 0 { return nil, httperrors.NewInputParameterError("The specified Scheduler %s is invalid for performance sharing loadbalancer", scheduler) } - cloudregion := lb.GetRegion() + cloudregion, _ := lb.GetRegion() if cloudregion == nil { return nil, httperrors.NewResourceNotFoundError("failed to find loadbalancer's %s(%s) region", lb.Name, lb.Id) } @@ -810,7 +810,7 @@ func (self *SAliyunRegionDriver) ValidateUpdateLoadbalancerListenerData(ctx cont } } - lb := backendgroup.GetLoadbalancer() + lb, _ := backendgroup.GetLoadbalancer() if tlsCipherPolicy, _ := data.GetString("tls_cipher_policy"); len(tlsCipherPolicy) > 0 && len(lb.LoadbalancerSpec) == 0 { data.Set("tls_cipher_policy", jsonutils.NewString("")) } diff --git a/pkg/compute/regiondrivers/aws.go b/pkg/compute/regiondrivers/aws.go index 358c2bcf44..e1865630be 100644 --- a/pkg/compute/regiondrivers/aws.go +++ b/pkg/compute/regiondrivers/aws.go @@ -565,9 +565,9 @@ func (self *SAwsRegionDriver) validateUpdateNetworkListenerData(ctx context.Cont func (self *SAwsRegionDriver) ValidateUpdateLoadbalancerListenerData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, lblis *models.SLoadbalancerListener, backendGroup db.IModel) (*jsonutils.JSONDict, error) { ownerId := lblis.GetOwnerId() - lb := lblis.GetLoadbalancer() - if lb == nil { - return nil, httperrors.NewResourceNotFoundError("loadbalancer listener %s related loadbalancer %s not found", lblis.Id, lblis.LoadbalancerId) + lb, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } if lb.LoadbalancerSpec == api.LB_AWS_SPEC_APPLICATION { @@ -898,13 +898,13 @@ func (self *SAwsRegionDriver) RequestCreateLoadbalancerBackendGroup(ctx context. func (self *SAwsRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } cachedlbbgs, err := models.AwsCachedLbbgManager.GetCachedBackendGroups(lbbg.GetId()) @@ -1004,9 +1004,9 @@ func (self *SAwsRegionDriver) RequestDeleteLoadbalancerBackend(ctx context.Conte func (self *SAwsRegionDriver) RequestCreateLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } { @@ -1109,13 +1109,13 @@ func (self *SAwsRegionDriver) RequestCreateLoadbalancerListener(ctx context.Cont func (self *SAwsRegionDriver) RequestCreateLoadbalancerListenerRule(ctx context.Context, userCred mcclient.TokenCredential, lbr *models.SLoadbalancerListenerRule, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1164,9 +1164,9 @@ func (self *SAwsRegionDriver) RequestDeleteLoadbalancerBackendGroup(ctx context. if err != nil { return nil, errors.Wrap(err, "AwsRegionDriver.RequestDeleteLoadbalancerBackendGroup.GetIRegion") } - loadbalancer := lbbg.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId) if err != nil { @@ -1355,9 +1355,9 @@ func (self *SAwsRegionDriver) RequestSyncLoadbalancerListener(ctx context.Contex if err != nil { return nil, errors.Wrap(err, "awsRegionDriver.RequestSyncLoadbalancerListener.GetAwsLoadbalancerListenerParams") } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1389,9 +1389,9 @@ func (self *SAwsRegionDriver) RequestSyncLoadbalancerBackendGroup(ctx context.Co return errors.Wrap(err, "AwsRegionDriver.RequestSyncLoadbalancerBackendGroup.GetLoadbalancerBackendGroup") } - lb := lblis.GetLoadbalancer() - if lb == nil { - return errors.Wrap(fmt.Errorf("listener %s related loadbalancer not found", lblis.GetId()), "AwsRegionDriver.RequestSyncLoadbalancerBackendGroup.GetLoadbalancer") + lb, err := lblis.GetLoadbalancer() + if err != nil { + return err } cachedLbbg, err := models.AwsCachedLbbgManager.GetUsableCachedBackendGroup(lb.GetId(), lblis.BackendGroupId, lblis.ListenerType, lblis.HealthCheckType, lblis.HealthCheckInterval) diff --git a/pkg/compute/regiondrivers/google.go b/pkg/compute/regiondrivers/google.go index 81d27e0747..c527246bc1 100644 --- a/pkg/compute/regiondrivers/google.go +++ b/pkg/compute/regiondrivers/google.go @@ -259,7 +259,9 @@ func (self *SGoogleRegionDriver) RequestCreateDBInstanceBackup(ctx context.Conte return nil, errors.Wrap(err, "iRds.GetIDBInstanceBackups") } - result := models.DBInstanceBackupManager.SyncDBInstanceBackups(ctx, userCred, backup.GetCloudprovider(), instance, backup.GetRegion(), backups) + region, _ := backup.GetRegion() + + result := models.DBInstanceBackupManager.SyncDBInstanceBackups(ctx, userCred, backup.GetCloudprovider(), instance, region, backups) log.Infof("SyncDBInstanceBackups for dbinstance %s(%s) result: %s", instance.Name, instance.Id, result.Result()) instance.SetStatus(userCred, api.DBINSTANCE_RUNNING, "") return nil, nil diff --git a/pkg/compute/regiondrivers/huawei.go b/pkg/compute/regiondrivers/huawei.go index 6ebd2adbb2..1afc19d033 100644 --- a/pkg/compute/regiondrivers/huawei.go +++ b/pkg/compute/regiondrivers/huawei.go @@ -663,9 +663,9 @@ func (self *SHuaWeiRegionDriver) createLoadbalancerBackendGroup(ctx context.Cont if err != nil { return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId) if err != nil { @@ -725,7 +725,7 @@ func (self *SHuaWeiRegionDriver) RequestCreateLoadbalancerBackendGroup(ctx conte } rule = _rule.(*models.SLoadbalancerListenerRule) - listener = rule.GetLoadbalancerListener() + listener, _ = rule.GetLoadbalancerListener() } else { _listener, err := db.FetchById(models.LoadbalancerListenerManager, listenerId) if err != nil { @@ -898,7 +898,7 @@ func (self *SHuaWeiRegionDriver) RequestSyncLoadbalancerBackendGroup(ctx context return nil, err } - lb := lbbg.GetLoadbalancer() + lb, _ := lbbg.GetLoadbalancer() ilb, err := iRegion.GetILoadBalancerById(lb.GetExternalId()) if err != nil { return nil, err @@ -1230,9 +1230,9 @@ func (self *SHuaWeiRegionDriver) RequestCreateLoadbalancerListener(ctx context.C if err != nil { return nil, errors.Wrap(err, "huaweiRegionDriver.RequestCreateLoadbalancerListener.GetHuaweiLoadbalancerListenerParams") } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1375,9 +1375,9 @@ func (self *SHuaWeiRegionDriver) RequestSyncLoadbalancerListener(ctx context.Con if err != nil { return nil, err } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1617,9 +1617,9 @@ func (self *SHuaWeiRegionDriver) RequestDeleteLoadbalancerListener(ctx context.C if jsonutils.QueryBoolean(task.GetParams(), "purge", false) { return nil, nil } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1745,9 +1745,9 @@ func (self *SHuaWeiRegionDriver) RequestDeleteLoadbalancerBackendGroup(ctx conte if err != nil { return nil, errors.Wrap(err, "huaweiRegionDriver.RequestDeleteLoadbalancerBackendGroup.") } - loadbalancer := lbbg.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId) if err != nil { @@ -1923,7 +1923,8 @@ func (self *SHuaWeiRegionDriver) RequestCreateLoadbalancer(ctx context.Context, } } - if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil, lb.GetCloudprovider(), lb.GetRegion()); err != nil { + region, _ := lb.GetRegion() + if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil, lb.GetCloudprovider(), region); err != nil { return nil, errors.Wrap(err, "Huawei.RequestCreateLoadbalancer.SyncWithCloudLoadbalancer") } lbbgs, err := iLoadbalancer.GetILoadBalancerBackendGroups() @@ -2020,13 +2021,13 @@ func (self *SHuaWeiRegionDriver) RequestSyncLoadbalancerBackend(ctx context.Cont func (self *SHuaWeiRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } cachedlbbgs, err := models.HuaweiCachedLbbgManager.GetCachedBackendGroups(lbbg.GetId()) @@ -2077,13 +2078,13 @@ func (self *SHuaWeiRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Co func (self *SHuaWeiRegionDriver) RequestCreateLoadbalancerListenerRule(ctx context.Context, userCred mcclient.TokenCredential, lbr *models.SLoadbalancerListenerRule, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { diff --git a/pkg/compute/regiondrivers/huaweistack.go b/pkg/compute/regiondrivers/huaweistack.go index 49a237f8df..de9b420ed7 100644 --- a/pkg/compute/regiondrivers/huaweistack.go +++ b/pkg/compute/regiondrivers/huaweistack.go @@ -17,3 +17,7 @@ func init() { func (self *SHuaweiCloudStackRegionDriver) GetProvider() string { return api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK } + +func (self *SHuaweiCloudStackRegionDriver) IsSupportedElasticcache() bool { + return false +} diff --git a/pkg/compute/regiondrivers/kvm.go b/pkg/compute/regiondrivers/kvm.go index 23d3a88a7b..b06679d97e 100644 --- a/pkg/compute/regiondrivers/kvm.go +++ b/pkg/compute/regiondrivers/kvm.go @@ -239,7 +239,7 @@ func (self *SKVMRegionDriver) ValidateCreateLoadbalancerBackendData(ctx context. return nil, httperrors.NewInputParameterError("error loadbalancer of backend group %s", backendGroup.GetId()) } var ( - lbRegion = lb.GetRegion() + lbRegion, _ = lb.GetRegion() hostRegion, _ = host.GetRegion() ) if lbRegion.Id != hostRegion.Id { @@ -408,7 +408,7 @@ func (self *SKVMRegionDriver) ValidateUpdateLoadbalancerListenerRuleData(ctx con if redirectType != api.LB_REDIRECT_OFF { if redirectType == api.LB_REDIRECT_RAW { var ( - lblis = lbr.GetLoadbalancerListener() + lblis, _ = lbr.GetLoadbalancerListener() listenerType = lblis.ListenerType ) scheme, host, path := redirectSchemeV.Value, redirectHostV.Value, redirectPathV.Value diff --git a/pkg/compute/regiondrivers/managedvirtual.go b/pkg/compute/regiondrivers/managedvirtual.go index 1d1c10f799..762f8c2c5d 100644 --- a/pkg/compute/regiondrivers/managedvirtual.go +++ b/pkg/compute/regiondrivers/managedvirtual.go @@ -92,7 +92,7 @@ func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerBacken return nil, fmt.Errorf("error loadbalancer of backend group %s", backendGroup.GetId()) } hostRegion, _ := host.GetRegion() - lbRegion := lb.GetRegion() + lbRegion, _ := lb.GetRegion() if hostRegion.Id != lbRegion.Id { return nil, httperrors.NewInputParameterError("region of host %q (%s) != region of loadbalancer %q (%s))", host.Name, host.ZoneId, lb.Name, lb.ZoneId) @@ -242,7 +242,8 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancer(ctx co if err := db.SetExternalId(lb, userCred, iLoadbalancer.GetGlobalId()); err != nil { return nil, err } - if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil, lb.GetCloudprovider(), lb.GetRegion()); err != nil { + region, _ := lb.GetRegion() + if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil, lb.GetCloudprovider(), region); err != nil { return nil, err } //公网lb,需要同步public ip @@ -567,9 +568,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerBackend if err != nil { return nil, err } - loadbalancer := lbbg.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId) if err != nil { @@ -612,9 +613,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerBackend if err != nil { return nil, err } - loadbalancer := lbbg.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId) if err != nil { @@ -662,13 +663,13 @@ func (self *SManagedVirtualizationRegionDriver) RequestPullLoadbalancerBackendGr func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := lb.GetIRegion() if err != nil { @@ -703,13 +704,13 @@ func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerBackend if jsonutils.QueryBoolean(task.GetParams(), "purge", false) { return nil, nil } - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := lb.GetIRegion() if err != nil { @@ -742,13 +743,13 @@ func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerBackend func (self *SManagedVirtualizationRegionDriver) RequestSyncLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := lb.GetIRegion() if err != nil { @@ -859,9 +860,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerListene if err != nil { return nil, errors.Wrapf(err, "lblis.GetLoadbalancerListenerParams") } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -888,9 +889,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerListene if jsonutils.QueryBoolean(task.GetParams(), "purge", false) { return nil, nil } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -924,9 +925,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerListene func (self *SManagedVirtualizationRegionDriver) RequestStartLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1027,7 +1028,10 @@ func (self *SManagedVirtualizationRegionDriver) RequestSyncLoadbalancerListener( if err != nil { return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetParams") } - loadbalancer := lblis.GetLoadbalancer() + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err + } if loadbalancer == nil { return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) } @@ -1056,9 +1060,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestSyncLoadbalancerListener( func (self *SManagedVirtualizationRegionDriver) RequestStopLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1079,9 +1083,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestStopLoadbalancerListener( func (self *SManagedVirtualizationRegionDriver) RequestSyncstatusLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1106,13 +1110,13 @@ func (self *SManagedVirtualizationRegionDriver) RequestSyncstatusLoadbalancerLis func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerListenerRule(ctx context.Context, userCred mcclient.TokenCredential, lbr *models.SLoadbalancerListenerRule, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1156,13 +1160,13 @@ func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerListene if jsonutils.QueryBoolean(task.GetParams(), "purge", false) { return nil, nil } - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1610,7 +1614,10 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateDBInstance(ctx cont log.Errorf("failed to sync project %s for create %s rds %s error: %v", dbinstance.ProjectId, _cloudprovider.Provider, dbinstance.Name, err) } - region := dbinstance.GetRegion() + region, err := dbinstance.GetRegion() + if err != nil { + return nil, err + } err = region.GetDriver().InitDBInstanceUser(ctx, dbinstance, task, &desc) if err != nil { @@ -1769,7 +1776,10 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateDBInstanceFromBacku log.Errorf("failed to sync project %s for create %s rds %s error: %v", rds.ProjectId, _cloudprovider.Provider, rds.Name, err) } - region := rds.GetRegion() + region, err := rds.GetRegion() + if err != nil { + return nil, err + } err = region.GetDriver().InitDBInstanceUser(ctx, rds, task, &desc) if err != nil { diff --git a/pkg/compute/regiondrivers/openstack.go b/pkg/compute/regiondrivers/openstack.go index bc17c95958..0f85d74f99 100644 --- a/pkg/compute/regiondrivers/openstack.go +++ b/pkg/compute/regiondrivers/openstack.go @@ -174,7 +174,8 @@ func (self *SOpenStackRegionDriver) RequestCreateLoadbalancer(ctx context.Contex return nil, errors.Wrap(fmt.Errorf("status error"), "check status") } - if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil, lb.GetCloudprovider(), lb.GetRegion()); err != nil { + region, _ := lb.GetRegion() + if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil, lb.GetCloudprovider(), region); err != nil { return nil, err } @@ -425,9 +426,9 @@ func (self *SOpenStackRegionDriver) RequestCreateLoadbalancerListener(ctx contex if err != nil { return nil, errors.Wrapf(err, "lblis.GetLoadbalancerListenerParams") } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -643,9 +644,9 @@ func (self *SOpenStackRegionDriver) RequestSyncLoadbalancerListener(ctx context. if err != nil { return nil, err } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -722,9 +723,9 @@ func (self *SOpenStackRegionDriver) RequestDeleteLoadbalancerListener(ctx contex if jsonutils.QueryBoolean(task.GetParams(), "purge", false) { return nil, nil } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1004,7 +1005,7 @@ func (self *SOpenStackRegionDriver) ValidateUpdateLoadbalancerListenerRuleData(c if redirectType != api.LB_REDIRECT_OFF { if redirectType == api.LB_REDIRECT_RAW { var ( - lblis = lbr.GetLoadbalancerListener() + lblis, _ = lbr.GetLoadbalancerListener() listenerType = lblis.ListenerType ) scheme, host, path := redirectSchemeV.Value, redirectHostV.Value, redirectPathV.Value @@ -1034,13 +1035,13 @@ func (self *SOpenStackRegionDriver) ValidateUpdateLoadbalancerListenerRuleData(c func (self *SOpenStackRegionDriver) RequestCreateLoadbalancerListenerRule(ctx context.Context, userCred mcclient.TokenCredential, lbr *models.SLoadbalancerListenerRule, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1099,13 +1100,13 @@ func (self *SOpenStackRegionDriver) RequestDeleteLoadbalancerListenerRule(ctx co if jsonutils.QueryBoolean(task.GetParams(), "purge", false) { return nil, nil } - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1315,9 +1316,9 @@ func (self *SOpenStackRegionDriver) createLoadbalancerBackendGroup(ctx context.C if err != nil { return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId) if err != nil { @@ -1526,7 +1527,7 @@ func (self *SOpenStackRegionDriver) RequestCreateLoadbalancerBackendGroup(ctx co } rule = _rule.(*models.SLoadbalancerListenerRule) - listener = rule.GetLoadbalancerListener() + listener, _ = rule.GetLoadbalancerListener() } else { _listener, err := db.FetchById(models.LoadbalancerListenerManager, listenerId) if err != nil { @@ -1604,7 +1605,7 @@ func (self *SOpenStackRegionDriver) RequestSyncLoadbalancerBackendGroup(ctx cont return nil, err } - lb := lbbg.GetLoadbalancer() + lb, _ := lbbg.GetLoadbalancer() ilb, err := iRegion.GetILoadBalancerById(lb.GetExternalId()) if err != nil { return nil, err @@ -1754,9 +1755,9 @@ func (self *SOpenStackRegionDriver) RequestDeleteLoadbalancerBackendGroup(ctx co if err != nil { return nil, errors.Wrap(err, "openstackRegionDriver.RequestDeleteLoadbalancerBackendGroup.") } - loadbalancer := lbbg.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId) if err != nil { @@ -1933,13 +1934,13 @@ func (self *SOpenStackRegionDriver) ValidateUpdateLoadbalancerBackendData(ctx co func (self *SOpenStackRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } cachedlbbgs, err := models.OpenstackCachedLbbgManager.GetCachedBackendGroups(lbbg.GetId()) diff --git a/pkg/compute/regiondrivers/qcloud.go b/pkg/compute/regiondrivers/qcloud.go index 8aa4d452a3..bfbbe0e7af 100644 --- a/pkg/compute/regiondrivers/qcloud.go +++ b/pkg/compute/regiondrivers/qcloud.go @@ -260,9 +260,9 @@ func (self *SQcloudRegionDriver) RequestCreateLoadbalancerBackendGroup(ctx conte return nil, nil } - loadbalancer := lbbg.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + loadbalancer, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId) if err != nil { @@ -298,13 +298,13 @@ func (self *SQcloudRegionDriver) RequestCreateLoadbalancerBackendGroup(ctx conte func (self *SQcloudRegionDriver) RequestCreateLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - lbbg := lbb.GetLoadbalancerBackendGroup() - if lbbg == nil { - return nil, fmt.Errorf("failed to find lbbg for backend %s", lbb.Name) + lbbg, err := lbb.GetLoadbalancerBackendGroup() + if err != nil { + return nil, err } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } cachedlbbgs, err := models.QcloudCachedLbbgManager.GetCachedBackendGroups(lbbg.GetId()) @@ -478,9 +478,9 @@ func (self *SQcloudRegionDriver) createLoadbalancerBackendGroup(ctx context.Cont if err != nil { return nil, errors.Wrap(err, "SQcloudRegionDriver.createLoadbalancerBackendGroup.GetIRegion") } - lb := lbbg.GetLoadbalancer() - if lb == nil { - return nil, fmt.Errorf("failed to find loadbalancer for backendgroup %s", lbbg.Name) + lb, err := lbbg.GetLoadbalancer() + if err != nil { + return nil, err } iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId) if err != nil { @@ -489,9 +489,9 @@ func (self *SQcloudRegionDriver) createLoadbalancerBackendGroup(ctx context.Cont var ilbbg cloudprovider.ICloudLoadbalancerBackendGroup if lbr != nil { - l := lbr.GetLoadbalancerListener() - if l == nil { - return nil, fmt.Errorf("could not create loadbalancer backendgroup, loadbalancer listener rule %s related listener not found", lbr.GetName()) + l, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } ilblis, err := iLoadbalancer.GetILoadBalancerListenerById(l.ExternalId) @@ -588,9 +588,9 @@ func (self *SQcloudRegionDriver) RequestCreateLoadbalancerListener(ctx context.C if err != nil { return nil, errors.Wrap(err, "qcloudRegionDriver.RequestCreateLoadbalancerListener.GetQcloudLoadbalancerListenerParams") } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -676,13 +676,13 @@ func (self *SQcloudRegionDriver) GetLoadbalancerListenerRuleInputParams(lblis *m func (self *SQcloudRegionDriver) RequestCreateLoadbalancerListenerRule(ctx context.Context, userCred mcclient.TokenCredential, lbr *models.SLoadbalancerListenerRule, task taskman.ITask) error { taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - listener := lbr.GetLoadbalancerListener() - if listener == nil { - return nil, fmt.Errorf("failed to find listener for listnener rule %s", lbr.Name) + listener, err := lbr.GetLoadbalancerListener() + if err != nil { + return nil, err } - loadbalancer := listener.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name) + loadbalancer, err := listener.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { @@ -1226,7 +1226,7 @@ func (self *SQcloudRegionDriver) RequestSyncLoadbalancerBackendGroup(ctx context return nil, errors.Wrap(err, "QcloudRegionDriver.RequestSyncLoadbalancerbackendGroup.GetIRegion") } - lb := lbbg.GetLoadbalancer() + lb, _ := lbbg.GetLoadbalancer() ilb, err := iRegion.GetILoadBalancerById(lb.GetExternalId()) if err != nil { return nil, errors.Wrap(err, "QcloudRegionDriver.RequestSyncLoadbalancerbackendGroup.GetILoadBalancerById") @@ -1355,9 +1355,9 @@ func (self *SQcloudRegionDriver) RequestSyncLoadbalancerListener(ctx context.Con if err != nil { return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetParams") } - loadbalancer := lblis.GetLoadbalancer() - if loadbalancer == nil { - return nil, fmt.Errorf("failed to find loadbalancer for lblis %s", lblis.Name) + loadbalancer, err := lblis.GetLoadbalancer() + if err != nil { + return nil, err } iRegion, err := loadbalancer.GetIRegion() if err != nil { diff --git a/pkg/compute/tasks/cloud_account_sync_skus_task.go b/pkg/compute/tasks/cloud_account_sync_skus_task.go index 7baafc8c57..0925fca623 100644 --- a/pkg/compute/tasks/cloud_account_sync_skus_task.go +++ b/pkg/compute/tasks/cloud_account_sync_skus_task.go @@ -67,7 +67,7 @@ func (self *CloudAccountSyncSkusTask) OnInit(ctx context.Context, obj db.IStanda _regions := provider.(*models.SCloudprovider).GetCloudproviderRegions() for i := range _regions { - region := _regions[i].GetRegion() + region, _ := _regions[i].GetRegion() regions = append(regions, *region) } } else { @@ -76,7 +76,7 @@ func (self *CloudAccountSyncSkusTask) OnInit(ctx context.Context, obj db.IStanda ids := []string{} _regions := provider.GetCloudproviderRegions() for i := range _regions { - region := _regions[i].GetRegion() + region, _ := _regions[i].GetRegion() if region != nil && !utils.IsInStringArray(region.GetId(), ids) { regions = append(regions, *region) ids = append(ids, region.GetId()) diff --git a/pkg/compute/tasks/dbinstance_backup_create_task.go b/pkg/compute/tasks/dbinstance_backup_create_task.go index 024da30f6d..7e5fac9ffc 100644 --- a/pkg/compute/tasks/dbinstance_backup_create_task.go +++ b/pkg/compute/tasks/dbinstance_backup_create_task.go @@ -40,9 +40,9 @@ func (self *DBInstanceBackupCreateTask) taskFailed(ctx context.Context, backup * backup.SetStatus(self.UserCred, api.DBINSTANCE_BACKUP_CREATE_FAILED, err.Error()) db.OpsLog.LogEvent(backup, db.ACT_CREATE, err, self.GetUserCred()) logclient.AddActionLogWithStartable(self, backup, logclient.ACT_CREATE, err, self.UserCred, false) - instance, _ := backup.GetDBInstance() - if instance != nil { - instance.SetStatus(self.UserCred, api.DBINSTANCE_BACKING_UP_FAILED, err.Error()) + rds, _ := backup.GetDBInstance() + if rds != nil { + rds.SetStatus(self.UserCred, api.DBINSTANCE_BACKING_UP_FAILED, err.Error()) } self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) } @@ -53,14 +53,20 @@ func (self *DBInstanceBackupCreateTask) OnInit(ctx context.Context, obj db.IStan } func (self *DBInstanceBackupCreateTask) CreateDBInstanceBackup(ctx context.Context, backup *models.SDBInstanceBackup) { - instance, err := backup.GetDBInstance() + rds, err := backup.GetDBInstance() if err != nil { self.taskFailed(ctx, backup, errors.Wrap(err, "backup.GetDBInstance")) return } self.SetStage("OnCreateDBInstanceBackupComplete", nil) - err = instance.GetRegion().GetDriver().RequestCreateDBInstanceBackup(ctx, self.UserCred, instance, backup, self) + region, err := rds.GetRegion() + if err != nil { + self.taskFailed(ctx, backup, errors.Wrapf(err, "GetRegion")) + return + } + + err = region.GetDriver().RequestCreateDBInstanceBackup(ctx, self.UserCred, rds, backup, self) if err != nil { self.taskFailed(ctx, backup, errors.Wrap(err, "RequestCreateDBInstanceBackup")) return diff --git a/pkg/compute/tasks/dbinstance_backup_syncstatus_task.go b/pkg/compute/tasks/dbinstance_backup_syncstatus_task.go index a8f301ad48..80217eda08 100644 --- a/pkg/compute/tasks/dbinstance_backup_syncstatus_task.go +++ b/pkg/compute/tasks/dbinstance_backup_syncstatus_task.go @@ -46,14 +46,14 @@ func (self *DBInstanceBackupSyncstatusTask) taskFailed(ctx context.Context, back func (self *DBInstanceBackupSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { backup := obj.(*models.SDBInstanceBackup) - region := backup.GetRegion() - if region == nil { - self.taskFailed(ctx, backup, fmt.Errorf("failed to found cloudregion for backup storage %s(%s)", backup.Name, backup.Id)) + region, err := backup.GetRegion() + if err != nil { + self.taskFailed(ctx, backup, errors.Wrapf(err, "GetRegion")) return } self.SetStage("OnDBInstanceBackupSyncStatusComplete", nil) - err := region.GetDriver().RequestSyncDBInstanceBackupStatus(ctx, self.GetUserCred(), backup, self) + err = region.GetDriver().RequestSyncDBInstanceBackupStatus(ctx, self.GetUserCred(), backup, self) if err != nil { self.taskFailed(ctx, backup, errors.Wrap(err, "RequestSyncDBInstanceBackupStatus")) return diff --git a/pkg/compute/tasks/dbinstance_change_config.go b/pkg/compute/tasks/dbinstance_change_config.go index 9cc9d51561..6c4f232c12 100644 --- a/pkg/compute/tasks/dbinstance_change_config.go +++ b/pkg/compute/tasks/dbinstance_change_config.go @@ -37,12 +37,12 @@ func init() { taskman.RegisterTask(DBInstanceChangeConfigTask{}) } -func (self *DBInstanceChangeConfigTask) taskFailed(ctx context.Context, dbinstance *models.SDBInstance, err error) { - dbinstance.SetStatus(self.UserCred, api.DBINSTANCE_CHANGE_CONFIG_FAILED, err.Error()) - db.OpsLog.LogEvent(dbinstance, db.ACT_CHANGE_CONFIG, err, self.GetUserCred()) - logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_CHANGE_CONFIG, err, self.UserCred, false) +func (self *DBInstanceChangeConfigTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) { + rds.SetStatus(self.UserCred, api.DBINSTANCE_CHANGE_CONFIG_FAILED, err.Error()) + db.OpsLog.LogEvent(rds, db.ACT_CHANGE_CONFIG, err, self.GetUserCred()) + logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CHANGE_CONFIG, err, self.UserCred, false) notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionChangeConfig, IsFail: true, }) @@ -59,7 +59,13 @@ func (self *DBInstanceChangeConfigTask) OnInit(ctx context.Context, obj db.IStan return } - err = rds.GetRegion().GetDriver().RequestChangeDBInstanceConfig(ctx, self.UserCred, rds, input, self) + region, err := rds.GetRegion() + if err != nil { + self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegion")) + return + } + + err = region.GetDriver().RequestChangeDBInstanceConfig(ctx, self.UserCred, rds, input, self) if err != nil { self.taskFailed(ctx, rds, err) return @@ -67,14 +73,14 @@ func (self *DBInstanceChangeConfigTask) OnInit(ctx context.Context, obj db.IStan } func (self *DBInstanceChangeConfigTask) OnDBInstanceChangeConfigComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - dbinstance := obj.(*models.SDBInstance) - logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_CHANGE_CONFIG, nil, self.UserCred, true) + rds := obj.(*models.SDBInstance) + logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CHANGE_CONFIG, nil, self.UserCred, true) notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionChangeConfig, }) self.SetStage("OnSyncDBInstanceStatusComplete", nil) - models.StartResourceSyncStatusTask(ctx, self.UserCred, dbinstance, "DBInstanceSyncStatusTask", self.GetTaskId()) + models.StartResourceSyncStatusTask(ctx, self.UserCred, rds, "DBInstanceSyncStatusTask", self.GetTaskId()) } func (self *DBInstanceChangeConfigTask) OnDBInstanceChangeConfigCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { diff --git a/pkg/compute/tasks/dbinstance_create_task.go b/pkg/compute/tasks/dbinstance_create_task.go index e987edcd2c..4e7162ad85 100644 --- a/pkg/compute/tasks/dbinstance_create_task.go +++ b/pkg/compute/tasks/dbinstance_create_task.go @@ -19,6 +19,7 @@ import ( "fmt" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -36,12 +37,12 @@ func init() { taskman.RegisterTask(DBInstanceCreateTask{}) } -func (self *DBInstanceCreateTask) taskFailed(ctx context.Context, dbinstance *models.SDBInstance, err error) { - dbinstance.SetStatus(self.UserCred, api.DBINSTANCE_CREATE_FAILED, err.Error()) - db.OpsLog.LogEvent(dbinstance, db.ACT_CREATE, err, self.GetUserCred()) - logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_CREATE, err, self.UserCred, false) +func (self *DBInstanceCreateTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) { + rds.SetStatus(self.UserCred, api.DBINSTANCE_CREATE_FAILED, err.Error()) + db.OpsLog.LogEvent(rds, db.ACT_CREATE, err, self.GetUserCred()) + logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CREATE, err, self.UserCred, false) notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionCreate, IsFail: true, }) @@ -49,14 +50,17 @@ func (self *DBInstanceCreateTask) taskFailed(ctx context.Context, dbinstance *mo } func (self *DBInstanceCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - dbinstance := obj.(*models.SDBInstance) - self.CreateDBInstance(ctx, dbinstance) + rds := obj.(*models.SDBInstance) + self.CreateDBInstance(ctx, rds) } func (self *DBInstanceCreateTask) CreateDBInstance(ctx context.Context, rds *models.SDBInstance) { - region := rds.GetRegion() + region, err := rds.GetRegion() + if err != nil { + self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegion")) + return + } self.SetStage("OnCreateDBInstanceComplete", nil) - var err error if len(rds.DBInstancebackupId) > 0 { err = region.GetDriver().RequestCreateDBInstanceFromBackup(ctx, self.UserCred, rds, self) } else { @@ -69,22 +73,22 @@ func (self *DBInstanceCreateTask) CreateDBInstance(ctx context.Context, rds *mod } func (self *DBInstanceCreateTask) OnCreateDBInstanceComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - dbinstance := obj.(*models.SDBInstance) - logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_CREATE, nil, self.UserCred, true) + rds := obj.(*models.SDBInstance) + logclient.AddActionLogWithStartable(self, rds, logclient.ACT_CREATE, nil, self.UserCred, true) self.SetStage("OnSyncDBInstanceStatusComplete", nil) - models.StartResourceSyncStatusTask(ctx, self.UserCred, dbinstance, "DBInstanceSyncStatusTask", self.GetTaskId()) + models.StartResourceSyncStatusTask(ctx, self.UserCred, rds, "DBInstanceSyncStatusTask", self.GetTaskId()) } func (self *DBInstanceCreateTask) OnCreateDBInstanceCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - dbinstance := obj.(*models.SDBInstance) - self.taskFailed(ctx, dbinstance, fmt.Errorf("%s", data.String())) + rds := obj.(*models.SDBInstance) + self.taskFailed(ctx, rds, fmt.Errorf("%s", data.String())) } func (self *DBInstanceCreateTask) OnSyncDBInstanceStatusComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - dbinstance := obj.(*models.SDBInstance) - //notifyclient.NotifyWebhook(ctx, self.UserCred, dbinstance, notifyclient.ActionCreate) + rds := obj.(*models.SDBInstance) + //notifyclient.NotifyWebhook(ctx, self.UserCred, rds, notifyclient.ActionCreate) notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionCreate, }) self.SetStageComplete(ctx, nil) diff --git a/pkg/compute/tasks/dbinstance_delete_task.go b/pkg/compute/tasks/dbinstance_delete_task.go index 50627d8090..9d6c86902b 100644 --- a/pkg/compute/tasks/dbinstance_delete_task.go +++ b/pkg/compute/tasks/dbinstance_delete_task.go @@ -37,12 +37,12 @@ func init() { taskman.RegisterTask(DBInstanceDeleteTask{}) } -func (self *DBInstanceDeleteTask) taskFailed(ctx context.Context, dbinstance *models.SDBInstance, err error) { - dbinstance.SetStatus(self.UserCred, api.DBINSTANCE_DELETE_FAILED, err.Error()) - db.OpsLog.LogEvent(dbinstance, db.ACT_DELETE, err, self.GetUserCred()) - logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_DELETE, err, self.UserCred, false) +func (self *DBInstanceDeleteTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) { + rds.SetStatus(self.UserCred, api.DBINSTANCE_DELETE_FAILED, err.Error()) + db.OpsLog.LogEvent(rds, db.ACT_DELETE, err, self.GetUserCred()) + logclient.AddActionLogWithStartable(self, rds, logclient.ACT_DELETE, err, self.UserCred, false) notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionDelete, IsFail: true, }) @@ -50,57 +50,62 @@ func (self *DBInstanceDeleteTask) taskFailed(ctx context.Context, dbinstance *mo } func (self *DBInstanceDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - dbinstance := obj.(*models.SDBInstance) - self.DeleteDBInstance(ctx, dbinstance) + rds := obj.(*models.SDBInstance) + self.DeleteDBInstance(ctx, rds) } -func (self *DBInstanceDeleteTask) DeleteDBInstance(ctx context.Context, dbinstance *models.SDBInstance) { - idbinstance, err := dbinstance.GetIDBInstance() +func (self *DBInstanceDeleteTask) DeleteDBInstance(ctx context.Context, rds *models.SDBInstance) { + irds, err := rds.GetIDBInstance() if err != nil { if errors.Cause(err) == cloudprovider.ErrNotFound { - self.DeleteDBInstanceComplete(ctx, dbinstance) + self.DeleteDBInstanceComplete(ctx, rds) return } - self.taskFailed(ctx, dbinstance, err) + self.taskFailed(ctx, rds, err) return } if !jsonutils.QueryBoolean(self.Params, "purge", false) { - err = idbinstance.Delete() + err = irds.Delete() if err != nil { - self.taskFailed(ctx, dbinstance, err) + self.taskFailed(ctx, rds, err) return } } - self.DeleteDBInstanceComplete(ctx, dbinstance) + self.DeleteDBInstanceComplete(ctx, rds) } -func (self *DBInstanceDeleteTask) DeleteDBInstanceComplete(ctx context.Context, dbinstance *models.SDBInstance) { - if !dbinstance.GetRegion().GetDriver().IsSupportKeepDBInstanceManualBackup() || jsonutils.QueryBoolean(self.Params, "purge", false) { - err := dbinstance.PurgeBackups(ctx, self.UserCred, api.BACKUP_MODE_MANUAL) +func (self *DBInstanceDeleteTask) DeleteDBInstanceComplete(ctx context.Context, rds *models.SDBInstance) { + region, err := rds.GetRegion() + if err != nil { + self.taskFailed(ctx, rds, errors.Wrapf(err, "GetRegion")) + return + } + if !region.GetDriver().IsSupportKeepDBInstanceManualBackup() || jsonutils.QueryBoolean(self.Params, "purge", false) { + err := rds.PurgeBackups(ctx, self.UserCred, api.BACKUP_MODE_MANUAL) if err != nil { - self.taskFailed(ctx, dbinstance, errors.Wrap(err, "dbinstance.PurgeManualBackups")) + self.taskFailed(ctx, rds, errors.Wrap(err, "rds.PurgeManualBackups")) return } - err = dbinstance.Purge(ctx, self.UserCred) + err = rds.Purge(ctx, self.UserCred) if err != nil { - self.taskFailed(ctx, dbinstance, errors.Wrap(err, "dbinstance.Purge")) + self.taskFailed(ctx, rds, errors.Wrap(err, "rds.Purge")) return } - //notifyclient.NotifyWebhook(ctx, self.UserCred, dbinstance, notifyclient.ActionDelete) + //notifyclient.NotifyWebhook(ctx, self.UserCred, rds, notifyclient.ActionDelete) notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionDelete, }) self.SetStageComplete(ctx, nil) return } - self.DeleteBackups(ctx, dbinstance, nil) - //notifyclient.NotifyWebhook(ctx, self.UserCred, dbinstance, notifyclient.ActionDelete) + self.DeleteBackups(ctx, rds, nil) + //notifyclient.NotifyWebhook(ctx, self.UserCred, rds, notifyclient.ActionDelete) notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{ - Obj: dbinstance, + Obj: rds, Action: notifyclient.ActionDelete, }) } diff --git a/pkg/compute/tasks/dbinstance_remote_update_task.go b/pkg/compute/tasks/dbinstance_remote_update_task.go index b5e18e3994..2aa6fc13c4 100644 --- a/pkg/compute/tasks/dbinstance_remote_update_task.go +++ b/pkg/compute/tasks/dbinstance_remote_update_task.go @@ -18,6 +18,7 @@ import ( "context" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -33,34 +34,41 @@ func init() { taskman.RegisterTask(DBInstanceRemoteUpdateTask{}) } -func (self *DBInstanceRemoteUpdateTask) taskFail(ctx context.Context, dbinstance *models.SDBInstance, reason jsonutils.JSONObject) { - dbinstance.SetStatus(self.UserCred, api.DBINSTANCE_UPDATE_TAGS_FAILED, reason.String()) - self.SetStageFailed(ctx, reason) +func (self *DBInstanceRemoteUpdateTask) taskFail(ctx context.Context, rds *models.SDBInstance, err error) { + rds.SetStatus(self.UserCred, api.DBINSTANCE_UPDATE_TAGS_FAILED, err.Error()) + self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) } func (self *DBInstanceRemoteUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { - instance := obj.(*models.SDBInstance) + rds := obj.(*models.SDBInstance) self.SetStage("OnRemoteUpdateComplete", nil) replaceTags := jsonutils.QueryBoolean(self.Params, "replace_tags", false) - if err := instance.GetRegion().GetDriver().RequestRemoteUpdateDBInstance(ctx, self.GetUserCred(), instance, replaceTags, self); err != nil { - self.taskFail(ctx, instance, jsonutils.NewString(err.Error())) + region, err := rds.GetRegion() + if err != nil { + self.taskFail(ctx, rds, errors.Wrapf(err, "GetRegion")) + return + } + + if err := region.GetDriver().RequestRemoteUpdateDBInstance(ctx, self.GetUserCred(), rds, replaceTags, self); err != nil { + self.taskFail(ctx, rds, err) + return } } -func (self *DBInstanceRemoteUpdateTask) OnRemoteUpdateComplete(ctx context.Context, dbinstance *models.SDBInstance, data jsonutils.JSONObject) { +func (self *DBInstanceRemoteUpdateTask) OnRemoteUpdateComplete(ctx context.Context, rds *models.SDBInstance, data jsonutils.JSONObject) { self.SetStage("OnSyncStatusComplete", nil) - models.StartResourceSyncStatusTask(ctx, self.UserCred, dbinstance, "DBInstanceSyncStatusTask", self.GetTaskId()) + models.StartResourceSyncStatusTask(ctx, self.UserCred, rds, "DBInstanceSyncStatusTask", self.GetTaskId()) } -func (self *DBInstanceRemoteUpdateTask) OnRemoteUpdateCompleteFailed(ctx context.Context, dbinstance *models.SDBInstance, data jsonutils.JSONObject) { - self.taskFail(ctx, dbinstance, data) +func (self *DBInstanceRemoteUpdateTask) OnRemoteUpdateCompleteFailed(ctx context.Context, rds *models.SDBInstance, data jsonutils.JSONObject) { + self.taskFail(ctx, rds, errors.Errorf(data.String())) } -func (self *DBInstanceRemoteUpdateTask) OnSyncStatusComplete(ctx context.Context, dbinstance *models.SDBInstance, data jsonutils.JSONObject) { +func (self *DBInstanceRemoteUpdateTask) OnSyncStatusComplete(ctx context.Context, rds *models.SDBInstance, data jsonutils.JSONObject) { self.SetStageComplete(ctx, nil) } -func (self *DBInstanceRemoteUpdateTask) OnSyncStatusCompleteFailed(ctx context.Context, dbinstance *models.SDBInstance, data jsonutils.JSONObject) { +func (self *DBInstanceRemoteUpdateTask) OnSyncStatusCompleteFailed(ctx context.Context, rds *models.SDBInstance, data jsonutils.JSONObject) { self.SetStageFailed(ctx, data) } diff --git a/pkg/compute/tasks/loadbalancer_backend_create_task.go b/pkg/compute/tasks/loadbalancer_backend_create_task.go index 73716734b9..f2bc0fceb2 100644 --- a/pkg/compute/tasks/loadbalancer_backend_create_task.go +++ b/pkg/compute/tasks/loadbalancer_backend_create_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -41,7 +40,7 @@ func (self *LoadbalancerBackendCreateTask) taskFail(ctx context.Context, lbb *mo db.OpsLog.LogEvent(lbb, db.ACT_ALLOCATE_FAIL, reason, self.UserCred) logclient.AddActionLogWithStartable(self, lbb, logclient.ACT_CREATE, reason, self.UserCred, false) notifyclient.NotifySystemErrorWithCtx(ctx, lbb.Id, lbb.Name, api.LB_CREATE_FAILED, reason.String()) - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { logclient.AddActionLogWithStartable(self, lbbg, logclient.ACT_LB_ADD_BACKEND, reason, self.UserCred, false) } @@ -50,9 +49,9 @@ func (self *LoadbalancerBackendCreateTask) taskFail(ctx context.Context, lbb *mo func (self *LoadbalancerBackendCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbb := obj.(*models.SLoadbalancerBackend) - region := lbb.GetRegion() - if region == nil { - self.taskFail(ctx, lbb, jsonutils.NewString(fmt.Sprintf("failed to find region for lbb %s", lbb.Name))) + region, err := lbb.GetRegion() + if err != nil { + self.taskFail(ctx, lbb, jsonutils.NewString(err.Error())) return } @@ -66,7 +65,7 @@ func (self *LoadbalancerBackendCreateTask) OnLoadbalancerBackendCreateComplete(c lbb.SetStatus(self.GetUserCred(), api.LB_STATUS_ENABLED, "") db.OpsLog.LogEvent(lbb, db.ACT_ALLOCATE, lbb.GetShortDesc(ctx), self.UserCred) logclient.AddActionLogWithStartable(self, lbb, logclient.ACT_CREATE, nil, self.UserCred, true) - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { logclient.AddActionLogWithStartable(self, lbbg, logclient.ACT_LB_ADD_BACKEND, nil, self.UserCred, true) } diff --git a/pkg/compute/tasks/loadbalancer_backend_delete_task.go b/pkg/compute/tasks/loadbalancer_backend_delete_task.go index f6cb9dbf0b..02fc67d113 100644 --- a/pkg/compute/tasks/loadbalancer_backend_delete_task.go +++ b/pkg/compute/tasks/loadbalancer_backend_delete_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -40,7 +39,7 @@ func (self *LoadbalancerBackendDeleteTask) taskFail(ctx context.Context, lbb *mo lbb.SetStatus(self.GetUserCred(), api.LB_STATUS_DELETE_FAILED, reason.String()) db.OpsLog.LogEvent(lbb, db.ACT_DELOCATE_FAIL, reason, self.UserCred) logclient.AddActionLogWithStartable(self, lbb, logclient.ACT_DELOCATE, reason, self.UserCred, false) - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { logclient.AddActionLogWithStartable(self, lbbg, logclient.ACT_LB_REMOVE_BACKEND, reason, self.UserCred, false) } @@ -49,9 +48,9 @@ func (self *LoadbalancerBackendDeleteTask) taskFail(ctx context.Context, lbb *mo func (self *LoadbalancerBackendDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbb := obj.(*models.SLoadbalancerBackend) - region := lbb.GetRegion() - if region == nil { - self.taskFail(ctx, lbb, jsonutils.NewString(fmt.Sprintf("failed to find region for lbb %s", lbb.Name))) + region, err := lbb.GetRegion() + if err != nil { + self.taskFail(ctx, lbb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerBackendDeleteComplete", nil) @@ -64,7 +63,7 @@ func (self *LoadbalancerBackendDeleteTask) OnLoadbalancerBackendDeleteComplete(c lbb.DoPendingDelete(ctx, self.GetUserCred()) db.OpsLog.LogEvent(lbb, db.ACT_DELETE, lbb.GetShortDesc(ctx), self.UserCred) logclient.AddActionLogWithStartable(self, lbb, logclient.ACT_DELOCATE, nil, self.UserCred, true) - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { logclient.AddActionLogWithStartable(self, lbbg, logclient.ACT_LB_REMOVE_BACKEND, nil, self.UserCred, true) } diff --git a/pkg/compute/tasks/loadbalancer_backend_sync_task.go b/pkg/compute/tasks/loadbalancer_backend_sync_task.go index 72f1066f26..6b6b81cde9 100644 --- a/pkg/compute/tasks/loadbalancer_backend_sync_task.go +++ b/pkg/compute/tasks/loadbalancer_backend_sync_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -41,7 +40,7 @@ func (self *LoadbalancerBackendSyncTask) taskFail(ctx context.Context, lbb *mode db.OpsLog.LogEvent(lbb, db.ACT_SYNC_CONF, reason, self.UserCred) logclient.AddActionLogWithStartable(self, lbb, logclient.ACT_SYNC_CONF, reason, self.UserCred, false) notifyclient.NotifySystemErrorWithCtx(ctx, lbb.Id, lbb.Name, api.LB_SYNC_CONF_FAILED, reason.String()) - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { logclient.AddActionLogWithStartable(self, lbbg, logclient.ACL_LB_SYNC_BACKEND_CONF, reason, self.UserCred, false) } @@ -50,9 +49,9 @@ func (self *LoadbalancerBackendSyncTask) taskFail(ctx context.Context, lbb *mode func (self *LoadbalancerBackendSyncTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbb := obj.(*models.SLoadbalancerBackend) - region := lbb.GetRegion() - if region == nil { - self.taskFail(ctx, lbb, jsonutils.NewString(fmt.Sprintf("failed to find region for lbb %s", lbb.Name))) + region, err := lbb.GetRegion() + if err != nil { + self.taskFail(ctx, lbb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerBackendCreateComplete", nil) @@ -65,7 +64,7 @@ func (self *LoadbalancerBackendSyncTask) OnLoadbalancerBackendCreateComplete(ctx lbb.SetStatus(self.GetUserCred(), api.LB_STATUS_ENABLED, "") db.OpsLog.LogEvent(lbb, db.ACT_SYNC_CONF, lbb.GetShortDesc(ctx), self.UserCred) logclient.AddActionLogWithStartable(self, lbb, logclient.ACT_SYNC_CONF, nil, self.UserCred, true) - lbbg := lbb.GetLoadbalancerBackendGroup() + lbbg, _ := lbb.GetLoadbalancerBackendGroup() if lbbg != nil { logclient.AddActionLogWithStartable(self, lbbg, logclient.ACL_LB_SYNC_BACKEND_CONF, nil, self.UserCred, true) } diff --git a/pkg/compute/tasks/loadbalancer_backendgroup_create_task.go b/pkg/compute/tasks/loadbalancer_backendgroup_create_task.go index c89427c6b3..2f7c000361 100644 --- a/pkg/compute/tasks/loadbalancer_backendgroup_create_task.go +++ b/pkg/compute/tasks/loadbalancer_backendgroup_create_task.go @@ -62,9 +62,9 @@ func (self *LoadbalancerLoadbalancerBackendGroupCreateTask) taskFail(ctx context func (self *LoadbalancerLoadbalancerBackendGroupCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbbg := obj.(*models.SLoadbalancerBackendGroup) - region := lbbg.GetRegion() - if region == nil { - self.taskFail(ctx, lbbg, jsonutils.NewString(fmt.Sprintf("failed to find region for lb backendgroup %s", lbbg.Name))) + region, err := lbbg.GetRegion() + if err != nil { + self.taskFail(ctx, lbbg, jsonutils.NewString(err.Error())) return } backends := []cloudprovider.SLoadbalancerBackend{} @@ -90,9 +90,9 @@ func (self *LoadbalancerLoadbalancerBackendGroupCreateTask) OnLoadbalancerBacken func (self *HuaweiLoadbalancerLoadbalancerBackendGroupCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbbg := obj.(*models.SLoadbalancerBackendGroup) - region := lbbg.GetRegion() - if region == nil { - self.taskFail(ctx, lbbg, jsonutils.NewString(fmt.Sprintf("failed to find region for lb backendgroup %s", lbbg.Name))) + region, err := lbbg.GetRegion() + if err != nil { + self.taskFail(ctx, lbbg, jsonutils.NewString(err.Error())) return } @@ -118,9 +118,9 @@ func (self *HuaweiLoadbalancerLoadbalancerBackendGroupCreateTask) OnInit(ctx con func (self *AwsLoadbalancerLoadbalancerBackendGroupCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbbg := obj.(*models.SLoadbalancerBackendGroup) - region := lbbg.GetRegion() - if region == nil { - self.taskFail(ctx, lbbg, jsonutils.NewString(fmt.Sprintf("failed to find region for lb backendgroup %s", lbbg.Name))) + region, err := lbbg.GetRegion() + if err != nil { + self.taskFail(ctx, lbbg, jsonutils.NewString(err.Error())) return } @@ -145,9 +145,9 @@ func (self *AwsLoadbalancerLoadbalancerBackendGroupCreateTask) OnInit(ctx contex func (self *OpenstackLoadbalancerLoadbalancerBackendGroupCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbbg := obj.(*models.SLoadbalancerBackendGroup) - region := lbbg.GetRegion() - if region == nil { - self.taskFail(ctx, lbbg, jsonutils.NewString(fmt.Sprintf("failed to find region for lb backendgroup %s", lbbg.Name))) + region, err := lbbg.GetRegion() + if err != nil { + self.taskFail(ctx, lbbg, jsonutils.NewString(err.Error())) return } diff --git a/pkg/compute/tasks/loadbalancer_backendgroup_delete_task.go b/pkg/compute/tasks/loadbalancer_backendgroup_delete_task.go index c04dd92cc1..58aba397ab 100644 --- a/pkg/compute/tasks/loadbalancer_backendgroup_delete_task.go +++ b/pkg/compute/tasks/loadbalancer_backendgroup_delete_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerBackendGroupDeleteTask) taskFail(ctx context.Context, lb func (self *LoadbalancerBackendGroupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbbg := obj.(*models.SLoadbalancerBackendGroup) - region := lbbg.GetRegion() - if region == nil { - self.taskFail(ctx, lbbg, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lbbg.Name))) + region, err := lbbg.GetRegion() + if err != nil { + self.taskFail(ctx, lbbg, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerBackendGroupDeleteComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_create_task.go b/pkg/compute/tasks/loadbalancer_create_task.go index a42fbba0d2..54c078b1e9 100644 --- a/pkg/compute/tasks/loadbalancer_create_task.go +++ b/pkg/compute/tasks/loadbalancer_create_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -51,9 +50,9 @@ func (self *LoadbalancerCreateTask) taskFail(ctx context.Context, lb *models.SLo func (self *LoadbalancerCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lb := obj.(*models.SLoadbalancer) - region := lb.GetRegion() - if region == nil { - self.taskFail(ctx, lb, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lb.Name))) + region, err := lb.GetRegion() + if err != nil { + self.taskFail(ctx, lb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerCreateComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_delete_task.go b/pkg/compute/tasks/loadbalancer_delete_task.go index cd0700b485..5eba8bff04 100644 --- a/pkg/compute/tasks/loadbalancer_delete_task.go +++ b/pkg/compute/tasks/loadbalancer_delete_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -50,9 +49,9 @@ func (self *LoadbalancerDeleteTask) taskFail(ctx context.Context, lb *models.SLo func (self *LoadbalancerDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lb := obj.(*models.SLoadbalancer) - region := lb.GetRegion() - if region == nil { - self.taskFail(ctx, lb, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lb.Name))) + region, err := lb.GetRegion() + if err != nil { + self.taskFail(ctx, lb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerDeleteComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_listener_create_task.go b/pkg/compute/tasks/loadbalancer_listener_create_task.go index fa9e86bfd1..89ab83de30 100644 --- a/pkg/compute/tasks/loadbalancer_listener_create_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_create_task.go @@ -17,7 +17,6 @@ package tasks import ( "context" "database/sql" - "fmt" "yunion.io/x/jsonutils" @@ -39,7 +38,7 @@ func init() { func getOnLoadbalancerListenerCreateCompleteFunc(provider string) func(ctx context.Context, lblis *models.SLoadbalancerListener, data jsonutils.JSONObject, self *LoadbalancerListenerCreateTask) { switch provider { - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: return onHuaweiLoadbalancerListenerCreateComplete case api.CLOUD_PROVIDER_OPENSTACK: return onOpenstackLoadbalancerListenerCreateComplete @@ -169,9 +168,9 @@ func (self *LoadbalancerListenerCreateTask) taskFail(ctx context.Context, lblis func (self *LoadbalancerListenerCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lblis := obj.(*models.SLoadbalancerListener) - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerCreateComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_listener_delete_task.go b/pkg/compute/tasks/loadbalancer_listener_delete_task.go index bc53b45e87..0793e9bd84 100644 --- a/pkg/compute/tasks/loadbalancer_listener_delete_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_delete_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -50,9 +49,9 @@ func (self *LoadbalancerListenerDeleteTask) taskFail(ctx context.Context, lblis func (self *LoadbalancerListenerDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lblis := obj.(*models.SLoadbalancerListener) - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerDeleteComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_listener_rule_create_task.go b/pkg/compute/tasks/loadbalancer_listener_rule_create_task.go index fdb0f052fc..dea06e11b2 100644 --- a/pkg/compute/tasks/loadbalancer_listener_rule_create_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_rule_create_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -38,7 +37,7 @@ func init() { func getOnPrepareLoadbalancerBackendgroupFunc(provider string) func(ctx context.Context, region *models.SCloudregion, lbr *models.SLoadbalancerListenerRule, data jsonutils.JSONObject, self *LoadbalancerListenerRuleCreateTask) { switch provider { - case api.CLOUD_PROVIDER_HUAWEI: + case api.CLOUD_PROVIDER_HUAWEI, api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK: return onHuaiweiPrepareLoadbalancerBackendgroup case api.CLOUD_PROVIDER_AWS: return onAwsPrepareLoadbalancerBackendgroup @@ -61,9 +60,9 @@ func onHuaiweiPrepareLoadbalancerBackendgroup(ctx context.Context, region *model return } - lblis := lbr.GetLoadbalancerListener() - if lblis == nil { - self.taskFail(ctx, lbr, jsonutils.NewString("huawei loadbalancer listener rule releated listener not found")) + lblis, err := lbr.GetLoadbalancerListener() + if err != nil { + self.taskFail(ctx, lbr, jsonutils.NewString(err.Error())) return } @@ -114,9 +113,9 @@ func onAwsPrepareLoadbalancerBackendgroup(ctx context.Context, region *models.SC return } - lblis := lbr.GetLoadbalancerListener() - if lblis == nil { - self.taskFail(ctx, lbr, jsonutils.NewString("aws loadbalancer listener rule releated listener not found")) + lblis, err := lbr.GetLoadbalancerListener() + if err != nil { + self.taskFail(ctx, lbr, jsonutils.NewString(err.Error())) return } @@ -156,9 +155,9 @@ func onOpenstackPrepareLoadbalancerBackendgroup(ctx context.Context, region *mod return } - lblis := lbr.GetLoadbalancerListener() - if lblis == nil { - self.taskFail(ctx, lbr, jsonutils.NewString("openstack loadbalancer listener rule releated listener not found")) + lblis, err := lbr.GetLoadbalancerListener() + if err != nil { + self.taskFail(ctx, lbr, jsonutils.NewString(err.Error())) return } @@ -207,7 +206,7 @@ func (self *LoadbalancerListenerRuleCreateTask) taskFail(ctx context.Context, lb db.OpsLog.LogEvent(lbr, db.ACT_ALLOCATE_FAIL, reason, self.UserCred) logclient.AddActionLogWithStartable(self, lbr, logclient.ACT_CREATE, reason, self.UserCred, false) notifyclient.NotifySystemErrorWithCtx(ctx, lbr.Id, lbr.Name, api.LB_CREATE_FAILED, reason.String()) - lblis := lbr.GetLoadbalancerListener() + lblis, _ := lbr.GetLoadbalancerListener() if lblis != nil { logclient.AddActionLogWithStartable(self, lblis, logclient.ACT_LB_ADD_LISTENER_RULE, reason, self.UserCred, false) } @@ -216,9 +215,9 @@ func (self *LoadbalancerListenerRuleCreateTask) taskFail(ctx context.Context, lb func (self *LoadbalancerListenerRuleCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbr := obj.(*models.SLoadbalancerListenerRule) - region := lbr.GetRegion() - if region == nil { - self.taskFail(ctx, lbr, jsonutils.NewString(fmt.Sprintf("failed to find region for lbr %s", lbr.Name))) + region, err := lbr.GetRegion() + if err != nil { + self.taskFail(ctx, lbr, jsonutils.NewString(err.Error())) return } @@ -231,9 +230,9 @@ func (self *LoadbalancerListenerRuleCreateTask) OnPrepareLoadbalancerBackendgrou } func (self *LoadbalancerListenerRuleCreateTask) OnCreateLoadbalancerListenerRule(ctx context.Context, lbr *models.SLoadbalancerListenerRule, data jsonutils.JSONObject) { - region := lbr.GetRegion() - if region == nil { - self.taskFail(ctx, lbr, jsonutils.NewString(fmt.Sprintf("failed to find region for lbr %s", lbr.Name))) + region, err := lbr.GetRegion() + if err != nil { + self.taskFail(ctx, lbr, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerRuleCreateComplete", nil) @@ -251,7 +250,7 @@ func (self *LoadbalancerListenerRuleCreateTask) OnLoadbalancerListenerRuleCreate db.OpsLog.LogEvent(lbr, db.ACT_ALLOCATE, lbr.GetShortDesc(ctx), self.UserCred) logclient.AddActionLogWithStartable(self, lbr, logclient.ACT_CREATE, nil, self.UserCred, true) notifyclient.NotifyWebhook(ctx, self.UserCred, lbr, notifyclient.ActionCreate) - lblis := lbr.GetLoadbalancerListener() + lblis, _ := lbr.GetLoadbalancerListener() if lblis != nil { logclient.AddActionLogWithStartable(self, lblis, logclient.ACT_LB_ADD_LISTENER_RULE, nil, self.UserCred, true) } diff --git a/pkg/compute/tasks/loadbalancer_listener_rule_delete_task.go b/pkg/compute/tasks/loadbalancer_listener_rule_delete_task.go index 2e51b19918..e0e8a3fa77 100644 --- a/pkg/compute/tasks/loadbalancer_listener_rule_delete_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_rule_delete_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerListenerRuleDeleteTask) taskFail(ctx context.Context, lb func (self *LoadbalancerListenerRuleDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lbr := obj.(*models.SLoadbalancerListenerRule) - region := lbr.GetRegion() - if region == nil { - self.taskFail(ctx, lbr, jsonutils.NewString(fmt.Sprintf("failed to find region for lbr %s", lbr.Name))) + region, err := lbr.GetRegion() + if err != nil { + self.taskFail(ctx, lbr, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerRuleDeleteComplete", nil) @@ -61,7 +60,7 @@ func (self *LoadbalancerListenerRuleDeleteTask) OnLoadbalancerListenerRuleDelete db.OpsLog.LogEvent(lbr, db.ACT_DELETE, lbr.GetShortDesc(ctx), self.UserCred) logclient.AddActionLogWithStartable(self, lbr, logclient.ACT_DELOCATE, nil, self.UserCred, true) notifyclient.NotifyWebhook(ctx, self.UserCred, lbr, notifyclient.ActionDelete) - lblis := lbr.GetLoadbalancerListener() + lblis, _ := lbr.GetLoadbalancerListener() if lblis != nil { logclient.AddActionLogWithStartable(self, lblis, logclient.ACT_LB_REMOVE_LISTENER_RULE, nil, self.UserCred, true) } diff --git a/pkg/compute/tasks/loadbalancer_listener_start_task.go b/pkg/compute/tasks/loadbalancer_listener_start_task.go index 8eda66a1eb..f19ccbf96f 100644 --- a/pkg/compute/tasks/loadbalancer_listener_start_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_start_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerListenerStartTask) taskFail(ctx context.Context, lblis * func (self *LoadbalancerListenerStartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lblis := obj.(*models.SLoadbalancerListener) - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerStartComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_listener_stop_task.go b/pkg/compute/tasks/loadbalancer_listener_stop_task.go index 20ddcfc85f..8a98563d5a 100644 --- a/pkg/compute/tasks/loadbalancer_listener_stop_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_stop_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerListenerStopTask) taskFail(ctx context.Context, lblis *m func (self *LoadbalancerListenerStopTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lblis := obj.(*models.SLoadbalancerListener) - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerStopComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_listener_sync_task.go b/pkg/compute/tasks/loadbalancer_listener_sync_task.go index c2f6e0c2c8..d04252c447 100644 --- a/pkg/compute/tasks/loadbalancer_listener_sync_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_sync_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,25 +45,26 @@ func (self *LoadbalancerListenerSyncTask) taskFail(ctx context.Context, lblis *m func (self *LoadbalancerListenerSyncTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lblis := obj.(*models.SLoadbalancerListener) - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerBackendgroupSyncComplete", nil) driver := region.GetDriver() userCred := self.GetUserCred() - err := driver.RequestSyncLoadbalancerBackendGroup(ctx, userCred, lblis, self) + err = driver.RequestSyncLoadbalancerBackendGroup(ctx, userCred, lblis, self) if err != nil { self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) + return } } func (self *LoadbalancerListenerSyncTask) OnLoadbalancerBackendgroupSyncComplete(ctx context.Context, lblis *models.SLoadbalancerListener, data jsonutils.JSONObject) { - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerSyncComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_listener_syncstatus_task.go b/pkg/compute/tasks/loadbalancer_listener_syncstatus_task.go index 687c4e3883..eb73234f91 100644 --- a/pkg/compute/tasks/loadbalancer_listener_syncstatus_task.go +++ b/pkg/compute/tasks/loadbalancer_listener_syncstatus_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerListenerSyncstatusTask) taskFail(ctx context.Context, lb func (self *LoadbalancerListenerSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lblis := obj.(*models.SLoadbalancerListener) - region := lblis.GetRegion() - if region == nil { - self.taskFail(ctx, lblis, jsonutils.NewString(fmt.Sprintf("failed to find region for lblis %s", lblis.Name))) + region, err := lblis.GetRegion() + if err != nil { + self.taskFail(ctx, lblis, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerListenerSyncstatusComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_remote_update_task.go b/pkg/compute/tasks/loadbalancer_remote_update_task.go index 8904d99912..fc209c1fa7 100644 --- a/pkg/compute/tasks/loadbalancer_remote_update_task.go +++ b/pkg/compute/tasks/loadbalancer_remote_update_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -41,9 +40,9 @@ func (self *LoadbalancerRemoteUpdateTask) taskFail(ctx context.Context, lb *mode func (self *LoadbalancerRemoteUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lb := obj.(*models.SLoadbalancer) - region := lb.GetRegion() - if region == nil { - self.taskFail(ctx, lb, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lb.Name))) + region, err := lb.GetRegion() + if err != nil { + self.taskFail(ctx, lb, jsonutils.NewString(err.Error())) return } self.SetStage("OnRemoteUpdateComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_start_task.go b/pkg/compute/tasks/loadbalancer_start_task.go index 93697d1f23..70cebe00df 100644 --- a/pkg/compute/tasks/loadbalancer_start_task.go +++ b/pkg/compute/tasks/loadbalancer_start_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerStartTask) taskFail(ctx context.Context, lb *models.SLoa func (self *LoadbalancerStartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lb := obj.(*models.SLoadbalancer) - region := lb.GetRegion() - if region == nil { - self.taskFail(ctx, lb, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lb.Name))) + region, err := lb.GetRegion() + if err != nil { + self.taskFail(ctx, lb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerStartComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_stop_task.go b/pkg/compute/tasks/loadbalancer_stop_task.go index 30ee07599e..42a489d02e 100644 --- a/pkg/compute/tasks/loadbalancer_stop_task.go +++ b/pkg/compute/tasks/loadbalancer_stop_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerStopTask) taskFail(ctx context.Context, lb *models.SLoad func (self *LoadbalancerStopTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lb := obj.(*models.SLoadbalancer) - region := lb.GetRegion() - if region == nil { - self.taskFail(ctx, lb, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lb.Name))) + region, err := lb.GetRegion() + if err != nil { + self.taskFail(ctx, lb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerStopComplete", nil) diff --git a/pkg/compute/tasks/loadbalancer_syncstatus_task.go b/pkg/compute/tasks/loadbalancer_syncstatus_task.go index 0fbb34b3bb..7d5eed9476 100644 --- a/pkg/compute/tasks/loadbalancer_syncstatus_task.go +++ b/pkg/compute/tasks/loadbalancer_syncstatus_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -46,9 +45,9 @@ func (self *LoadbalancerSyncstatusTask) taskFail(ctx context.Context, lb *models func (self *LoadbalancerSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { lb := obj.(*models.SLoadbalancer) - region := lb.GetRegion() - if region == nil { - self.taskFail(ctx, lb, jsonutils.NewString(fmt.Sprintf("failed to find region for lb %s", lb.Name))) + region, err := lb.GetRegion() + if err != nil { + self.taskFail(ctx, lb, jsonutils.NewString(err.Error())) return } self.SetStage("OnLoadbalancerSyncstatusComplete", nil) diff --git a/pkg/compute/tasks/snapshot_syncstatus_task.go b/pkg/compute/tasks/snapshot_syncstatus_task.go index 3c54710272..3e7d070f63 100644 --- a/pkg/compute/tasks/snapshot_syncstatus_task.go +++ b/pkg/compute/tasks/snapshot_syncstatus_task.go @@ -16,7 +16,6 @@ package tasks import ( "context" - "fmt" "yunion.io/x/jsonutils" @@ -45,14 +44,14 @@ func (self *SnapshotSyncstatusTask) taskFailed(ctx context.Context, snapshot *mo func (self *SnapshotSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { snapshot := obj.(*models.SSnapshot) - region := snapshot.GetRegion() - if region == nil { - self.taskFailed(ctx, snapshot, jsonutils.NewString(fmt.Sprintf("failed to found cloudregion for snapshot %s(%s)", snapshot.Name, snapshot.Id))) + region, err := snapshot.GetRegion() + if err != nil { + self.taskFailed(ctx, snapshot, jsonutils.NewString(err.Error())) return } self.SetStage("OnSnapshotSyncStatusComplete", nil) - err := region.GetDriver().RequestSyncSnapshotStatus(ctx, self.GetUserCred(), snapshot, self) + err = region.GetDriver().RequestSyncSnapshotStatus(ctx, self.GetUserCred(), snapshot, self) if err != nil { self.taskFailed(ctx, snapshot, jsonutils.NewString(err.Error())) return diff --git a/pkg/mcclient/options/k8s/cluster.go b/pkg/mcclient/options/k8s/cluster.go index 7cfe6e424f..3f2217d063 100644 --- a/pkg/mcclient/options/k8s/cluster.go +++ b/pkg/mcclient/options/k8s/cluster.go @@ -414,6 +414,7 @@ type ClusterComponentMonitorGrafanaOAuth struct { } type ClusterComponentMonitorGrafana struct { + Disable bool `help:"Disable grafana component" json:"disable"` AdminUser string `help:"Grafana admin user" default:"admin" json:"adminUser"` AdminPassword string `help:"Grafana admin user password" json:"adminPassword"` Storage ClusterComponentStorage `help:"Storage setting"` @@ -436,6 +437,7 @@ type ObjectStoreConfig struct { } type ClusterComponentMonitorLoki struct { + Disable bool `help:"Disable loki component" json:"disable"` Storage ClusterComponentStorage `help:"Storage setting" json:"storage"` ObjectStoreConfig ObjectStoreConfig `json:"objectStoreConfig"` } @@ -445,11 +447,13 @@ type MonitorPrometheusThanosSidecar struct { } type ClusterComponentMonitorPrometheus struct { + Disable bool `help:"Disable prometheus component" json:"disable"` Storage ClusterComponentStorage `help:"Storage setting" json:"storage"` Thanos MonitorPrometheusThanosSidecar `json:"thanosSidecar"` } type ClusterComponentMonitorPromtail struct { + Disable bool `help:"Disable promtail component" json:"disable"` } type ClusterComponentMonitorSetting struct { diff --git a/pkg/monitor/dbinit/metric_dbinit.go b/pkg/monitor/dbinit/metric_dbinit.go index 4ec976bd02..1d0c31b552 100644 --- a/pkg/monitor/dbinit/metric_dbinit.go +++ b/pkg/monitor/dbinit/metric_dbinit.go @@ -49,7 +49,7 @@ func GetRegistryMetricInput() (metricInitInputs []monitor.MetricCreateInput) { if metricInitInputMap == nil { metricInitInputMap = make(map[string]monitor.MetricCreateInput) } - for name, _ := range metricInitInputMap { + for name := range metricInitInputMap { metricInitInputs = append(metricInitInputs, metricInitInputMap[name]) } return @@ -280,7 +280,7 @@ func init() { newMetricFieldCreateInput("balance", "balance", monitor.METRIC_UNIT_RMB, 1), }) - // cpu + // agent_cpu RegistryMetricCreateInput("agent_cpu", "CPU usage", monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 1, []monitor.MetricFieldCreateInput{ newMetricFieldCreateInput("usage_active", "CPU active state utilization rate", monitor.METRIC_UNIT_PERCENT, 1), @@ -294,7 +294,7 @@ func init() { newMetricFieldCreateInput("usage_softirq", "CPU softirq usage", monitor.METRIC_UNIT_PERCENT, 9), }) - // disk + // agent_disk RegistryMetricCreateInput("agent_disk", "Disk usage", monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 3, []monitor.MetricFieldCreateInput{ @@ -307,7 +307,7 @@ func init() { newMetricFieldCreateInput("inodes_total", "Total inodes", monitor.METRIC_UNIT_COUNT, 7), }) - // diskio + // agent_diskio RegistryMetricCreateInput("agent_diskio", "Disk traffic and timing", monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 4, []monitor.MetricFieldCreateInput{ newMetricFieldCreateInput("read_bps", "Disk read rate", monitor.METRIC_UNIT_BPS, 1), @@ -324,7 +324,7 @@ func init() { newMetricFieldCreateInput("iops_in_progress", "Number of I / O requests issued but not yet completed", monitor.METRIC_UNIT_COUNT, 12), }) - // mem + // agent_mem RegistryMetricCreateInput("agent_mem", "Memory", monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 2, []monitor.MetricFieldCreateInput{ newMetricFieldCreateInput("used_percent", "Used memory rate", monitor.METRIC_UNIT_PERCENT, 1), @@ -340,17 +340,31 @@ func init() { newMetricFieldCreateInput("total", "Total memory", monitor.METRIC_UNIT_BYTE, 10), }) - // net + // agent_net RegistryMetricCreateInput("agent_net", "Network interface and protocol usage", monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 5, []monitor.MetricFieldCreateInput{ - newMetricFieldCreateInput("bytes_sent", "The total number of bytes sent by the network interface", monitor.METRIC_UNIT_BYTE, 1), - newMetricFieldCreateInput("bytes_recv", "The total number of bytes received by the network interface", monitor.METRIC_UNIT_BYTE, 2), - newMetricFieldCreateInput("packets_sent", "The total number of packets sent by the network interface", monitor.METRIC_UNIT_COUNT, 3), - newMetricFieldCreateInput("packets_recv", "The total number of packets received by the network interface", monitor.METRIC_UNIT_COUNT, 4), - newMetricFieldCreateInput("err_in", "The total number of receive errors detected by the network interface", monitor.METRIC_UNIT_COUNT, 5), - newMetricFieldCreateInput("err_out", "The total number of transmission errors detected by the network interface", monitor.METRIC_UNIT_COUNT, 6), - newMetricFieldCreateInput("drop_in", "The total number of received packets dropped by the network interface", monitor.METRIC_UNIT_COUNT, 7), - newMetricFieldCreateInput("drop_out", "The total number of transmission packets dropped by the network interface", monitor.METRIC_UNIT_COUNT, 8), + newMetricFieldCreateInput("bps_sent", "Send traffic per second", monitor.METRIC_UNIT_BPS, 1), + newMetricFieldCreateInput("bps_recv", "Received traffic per second", monitor.METRIC_UNIT_BPS, 2), + newMetricFieldCreateInput("bytes_sent", "The total number of bytes sent by the network interface", monitor.METRIC_UNIT_BYTE, 3), + newMetricFieldCreateInput("bytes_recv", "The total number of bytes received by the network interface", monitor.METRIC_UNIT_BYTE, 4), + newMetricFieldCreateInput("packets_sent", "The total number of packets sent by the network interface", monitor.METRIC_UNIT_COUNT, 5), + newMetricFieldCreateInput("packets_recv", "The total number of packets received by the network interface", monitor.METRIC_UNIT_COUNT, 6), + newMetricFieldCreateInput("err_in", "The total number of receive errors detected by the network interface", monitor.METRIC_UNIT_COUNT, 7), + newMetricFieldCreateInput("err_out", "The total number of transmission errors detected by the network interface", monitor.METRIC_UNIT_COUNT, 8), + newMetricFieldCreateInput("drop_in", "The total number of received packets dropped by the network interface", monitor.METRIC_UNIT_COUNT, 9), + newMetricFieldCreateInput("drop_out", "The total number of transmission packets dropped by the network interface", monitor.METRIC_UNIT_COUNT, 10), + }) + + // agent lm-sensors temperature + RegistryMetricCreateInput("agent_sensors", "Collect lm-sensors metrics", + monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 6, []monitor.MetricFieldCreateInput{ + newMetricFieldCreateInput("temp_input", "lm-sensors temperature input", "", 1), + }) + + // agent smartctl device temperature + RegistryMetricCreateInput("agent_smart_device", "Collect smartctl metrics", + monitor.METRIC_RES_TYPE_AGENT, monitor.METRIC_DATABASE_TELE, 7, []monitor.MetricFieldCreateInput{ + newMetricFieldCreateInput("temp_c", "Disk device temperature ", "", 1), }) RegistryMetricCreateInput("storage", "Storage usage", diff --git a/pkg/multicloud/apsara/instancetype.go b/pkg/multicloud/apsara/instancetype.go index de42f724ce..abd064dbdb 100644 --- a/pkg/multicloud/apsara/instancetype.go +++ b/pkg/multicloud/apsara/instancetype.go @@ -117,6 +117,10 @@ func (self *SInstanceType) GetPostpaidStatus() string { return api.SkuStatusAvailable } +func (self *SInstanceType) GetCpuArch() string { + return "" +} + func (self *SInstanceType) GetCpuCoreCount() int { return int(self.CpuCoreCount) } diff --git a/pkg/multicloud/apsara/provider/provider.go b/pkg/multicloud/apsara/provider/provider.go index 5dbb18b27b..a87f1cb3da 100644 --- a/pkg/multicloud/apsara/provider/provider.go +++ b/pkg/multicloud/apsara/provider/provider.go @@ -39,6 +39,10 @@ func (self *SApsaraProviderFactory) GetName() string { return apsara.CLOUD_PROVIDER_APSARA_CN } +func (self *SApsaraProviderFactory) IsMultiTenant() bool { + return true +} + func (self *SApsaraProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) { output := cloudprovider.SCloudaccount{} if len(input.AccessKeyId) == 0 { diff --git a/pkg/multicloud/azure/classic_disk.go b/pkg/multicloud/azure/classic_disk.go index fd8bbabf08..40d19b53b3 100644 --- a/pkg/multicloud/azure/classic_disk.go +++ b/pkg/multicloud/azure/classic_disk.go @@ -102,6 +102,9 @@ func (self *SClassicDisk) GetDiskSizeMB() int { if self.Properties.DiskSizeGB > 0 { return int(self.Properties.DiskSizeGB * 1024) } + if self.Properties.DiskSize > 0 { + return int(self.Properties.DiskSize * 1024) + } return 0 } diff --git a/pkg/multicloud/azure/classic_instancesize.go b/pkg/multicloud/azure/classic_instancesize.go index b9b4a66714..0472386a48 100644 --- a/pkg/multicloud/azure/classic_instancesize.go +++ b/pkg/multicloud/azure/classic_instancesize.go @@ -76,4 +76,69 @@ var CLASSIC_VM_SIZES = map[string]ClassicVMSize{ "Standard_G3": {MemoryInMB: 112 * 1024, NumberOfCores: 8, StorageSize: 1, MaxNic: 4}, "Standard_G4": {MemoryInMB: 224 * 1024, NumberOfCores: 16, StorageSize: 3, MaxNic: 8}, "Standard_G5": {MemoryInMB: 448 * 1024, NumberOfCores: 32, StorageSize: 6, MaxNic: 8}, + + "Standard_A0": {MemoryInMB: 768, NumberOfCores: 1}, + "Standard_A1": {MemoryInMB: 1792, NumberOfCores: 1}, + "Standard_A2": {MemoryInMB: 3584, NumberOfCores: 2}, + "Standard_A3": {MemoryInMB: 7168, NumberOfCores: 4}, + "Standard_A5": {MemoryInMB: 14336, NumberOfCores: 2}, + "Standard_A4": {MemoryInMB: 14336, NumberOfCores: 8}, + "Standard_A6": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_A7": {MemoryInMB: 57344, NumberOfCores: 8}, + "Basic_A0": {MemoryInMB: 768, NumberOfCores: 1}, + "Basic_A1": {MemoryInMB: 1792, NumberOfCores: 1}, + "Basic_A2": {MemoryInMB: 3584, NumberOfCores: 2}, + "Basic_A3": {MemoryInMB: 7168, NumberOfCores: 4}, + "Basic_A4": {MemoryInMB: 14336, NumberOfCores: 8}, + "Standard_D2_v2_Promo": {MemoryInMB: 7168, NumberOfCores: 2}, + "Standard_D3_v2_Promo": {MemoryInMB: 14336, NumberOfCores: 4}, + "Standard_D4_v2_Promo": {MemoryInMB: 28672, NumberOfCores: 8}, + "Standard_D5_v2_Promo": {MemoryInMB: 57344, NumberOfCores: 16}, + "Standard_D11_v2_Promo": {MemoryInMB: 14336, NumberOfCores: 2}, + "Standard_D12_v2_Promo": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_D13_v2_Promo": {MemoryInMB: 57344, NumberOfCores: 8}, + "Standard_D14_v2_Promo": {MemoryInMB: 114688, NumberOfCores: 16}, + "Standard_F1": {MemoryInMB: 2048, NumberOfCores: 1}, + "Standard_F2": {MemoryInMB: 4096, NumberOfCores: 2}, + "Standard_F4": {MemoryInMB: 8192, NumberOfCores: 4}, + "Standard_F8": {MemoryInMB: 16384, NumberOfCores: 8}, + "Standard_F16": {MemoryInMB: 32768, NumberOfCores: 16}, + "Standard_DS1_v2": {MemoryInMB: 3584, NumberOfCores: 1}, + "Standard_DS2_v2": {MemoryInMB: 7168, NumberOfCores: 2}, + "Standard_DS3_v2": {MemoryInMB: 14336, NumberOfCores: 4}, + "Standard_DS4_v2": {MemoryInMB: 28672, NumberOfCores: 8}, + "Standard_DS5_v2": {MemoryInMB: 57344, NumberOfCores: 16}, + "Standard_DS11-1_v2": {MemoryInMB: 14336, NumberOfCores: 2}, + "Standard_DS11_v2": {MemoryInMB: 14336, NumberOfCores: 2}, + "Standard_DS12-1_v2": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_DS12-2_v2": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_DS12_v2": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_DS13-2_v2": {MemoryInMB: 57344, NumberOfCores: 8}, + "Standard_DS13-4_v2": {MemoryInMB: 57344, NumberOfCores: 8}, + "Standard_DS13_v2": {MemoryInMB: 57344, NumberOfCores: 8}, + "Standard_DS14-4_v2": {MemoryInMB: 114688, NumberOfCores: 16}, + "Standard_DS14-8_v2": {MemoryInMB: 114688, NumberOfCores: 16}, + "Standard_DS14_v2": {MemoryInMB: 114688, NumberOfCores: 16}, + "Standard_DS15_v2": {MemoryInMB: 143360, NumberOfCores: 20}, + "Standard_DS2_v2_Promo": {MemoryInMB: 7168, NumberOfCores: 2}, + "Standard_DS3_v2_Promo": {MemoryInMB: 14336, NumberOfCores: 4}, + "Standard_DS4_v2_Promo": {MemoryInMB: 28672, NumberOfCores: 8}, + "Standard_DS5_v2_Promo": {MemoryInMB: 57344, NumberOfCores: 16}, + "Standard_DS11_v2_Promo": {MemoryInMB: 14336, NumberOfCores: 2}, + "Standard_DS12_v2_Promo": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_DS13_v2_Promo": {MemoryInMB: 57344, NumberOfCores: 8}, + "Standard_DS14_v2_Promo": {MemoryInMB: 114688, NumberOfCores: 16}, + "Standard_F1s": {MemoryInMB: 2048, NumberOfCores: 1}, + "Standard_F2s": {MemoryInMB: 4096, NumberOfCores: 2}, + "Standard_F4s": {MemoryInMB: 8192, NumberOfCores: 4}, + "Standard_F8s": {MemoryInMB: 16384, NumberOfCores: 8}, + "Standard_F16s": {MemoryInMB: 32768, NumberOfCores: 16}, + "Standard_DS1": {MemoryInMB: 3584, NumberOfCores: 1}, + "Standard_DS2": {MemoryInMB: 7168, NumberOfCores: 2}, + "Standard_DS3": {MemoryInMB: 14336, NumberOfCores: 4}, + "Standard_DS4": {MemoryInMB: 28672, NumberOfCores: 8}, + "Standard_DS11": {MemoryInMB: 14336, NumberOfCores: 2}, + "Standard_DS12": {MemoryInMB: 28672, NumberOfCores: 4}, + "Standard_DS13": {MemoryInMB: 57344, NumberOfCores: 8}, + "Standard_DS14": {MemoryInMB: 114688, NumberOfCores: 16}, } diff --git a/pkg/multicloud/azure/dbinstance.go b/pkg/multicloud/azure/dbinstance.go index f1a8eb0028..af82711e0f 100644 --- a/pkg/multicloud/azure/dbinstance.go +++ b/pkg/multicloud/azure/dbinstance.go @@ -296,6 +296,8 @@ func (rds *SDBInstance) GetVmemSizeMB() int { return rds.Sku.Capacity * 2 * 1024 case "General Purpose": return rds.Sku.Capacity * 5 * 1024 + case "GeneralPurpose": + return int(float32(rds.Sku.Capacity) * 5.2 * 1024) case "Memory Optimized": return rds.Sku.Capacity * 10 * 1024 default: diff --git a/pkg/multicloud/cloudpods/sku.go b/pkg/multicloud/cloudpods/sku.go index cb5e78a86d..66be3b9929 100644 --- a/pkg/multicloud/cloudpods/sku.go +++ b/pkg/multicloud/cloudpods/sku.go @@ -61,6 +61,10 @@ func (self *SServerSku) GetPostpaidStatus() string { return self.PostpaidStatus } +func (self *SServerSku) GetCpuArch() string { + return "x86" +} + func (self *SServerSku) GetCpuCoreCount() int { return self.CpuCoreCount } diff --git a/pkg/multicloud/huawei/bucket.go b/pkg/multicloud/huawei/bucket.go index 66ca5e237b..ec1dadb1b7 100644 --- a/pkg/multicloud/huawei/bucket.go +++ b/pkg/multicloud/huawei/bucket.go @@ -86,6 +86,7 @@ func (b *SBucket) GetStorageClass() string { output, err := obscli.GetBucketStoragePolicy(b.Name) if err != nil { log.Errorf("obscli.GetBucketStoragePolicy error %s", err) + return "" } return output.StorageClass } diff --git a/pkg/multicloud/huawei/image.go b/pkg/multicloud/huawei/image.go index 54e508b8ae..5b985608e2 100644 --- a/pkg/multicloud/huawei/image.go +++ b/pkg/multicloud/huawei/image.go @@ -167,7 +167,11 @@ func (self *SImage) GetSizeByte() int64 { func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { if self.imgInfo == nil { - imgInfo := imagetools.NormalizeImageInfo(self.ImageSourceType, self.OSType, self.OSType, self.Platform, "") + arch := "x86" + if strings.ToLower(self.SupportArm) == "true" { + arch = "arm" + } + imgInfo := imagetools.NormalizeImageInfo(self.ImageSourceType, arch, self.OSType, self.Platform, "") self.imgInfo = &imgInfo } diff --git a/pkg/multicloud/huawei/instance.go b/pkg/multicloud/huawei/instance.go index 879f2fc15c..89622945ab 100644 --- a/pkg/multicloud/huawei/instance.go +++ b/pkg/multicloud/huawei/instance.go @@ -29,6 +29,7 @@ import ( "yunion.io/x/pkg/util/osprofile" "yunion.io/x/pkg/utils" + "yunion.io/x/onecloud/pkg/apis" billing_api "yunion.io/x/onecloud/pkg/apis/billing" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -113,6 +114,7 @@ type SInstance struct { Status string `json:"status"` Progress string `json:"progress"` HostID string `json:"hostId"` + Image Image `json:"image"` Updated string `json:"updated"` Created time.Time `json:"created"` Metadata VMMetadata `json:"metadata"` @@ -476,6 +478,26 @@ func (self *SInstance) GetVdi() string { return "vnc" } +func (self *SInstance) GetOSArch() string { + if len(self.Image.ID) > 0 { + image, err := self.host.zone.region.GetImage(self.Image.ID) + if err == nil { + return image.GetOsArch() + } + + log.Debugf("GetOSArch.GetImage %s: %s", self.Image.ID, err) + } + + t := self.GetInstanceType() + if len(t) > 0 { + if strings.HasPrefix(t, "k") { + return apis.OS_ARCH_AARCH64 + } + } + + return apis.OS_ARCH_X86 +} + func (self *SInstance) GetOSType() string { return osprofile.NormalizeOSType(self.Metadata.OSType) } diff --git a/pkg/multicloud/huawei/traces.go b/pkg/multicloud/huawei/traces.go index 4ac06d8e10..3d6daa3d96 100644 --- a/pkg/multicloud/huawei/traces.go +++ b/pkg/multicloud/huawei/traces.go @@ -54,12 +54,12 @@ type SEvent struct { } func (event *SEvent) GetName() string { - if len(event.ResourceId) > 0 { - return event.ResourceId - } if len(event.ResourceName) > 0 { return event.ResourceName } + if len(event.ResourceId) > 0 { + return event.ResourceId + } return event.TraceName } diff --git a/pkg/multicloud/huaweistack/bucket.go b/pkg/multicloud/huaweistack/bucket.go index 0158963ed3..3ff8bef53a 100644 --- a/pkg/multicloud/huaweistack/bucket.go +++ b/pkg/multicloud/huaweistack/bucket.go @@ -77,6 +77,12 @@ func (b *SBucket) GetCreateAt() time.Time { return b.CreationDate } +/* + service returned error: Status=405 Method Not Allowed, Code=MethodNotAllowed, + Message=The specified method is not allowed against this resource., + RequestId=00000175B0E9D138440B9EF092DF8A7A +https://support.huaweicloud.com/productdesc-modelarts/modelarts_01_0017.html +*/ func (b *SBucket) GetStorageClass() string { obscli, err := b.region.getOBSClient() if err != nil { @@ -86,6 +92,7 @@ func (b *SBucket) GetStorageClass() string { output, err := obscli.GetBucketStoragePolicy(b.Name) if err != nil { log.Errorf("obscli.GetBucketStoragePolicy error %s", err) + return "" } return output.StorageClass } @@ -593,7 +600,8 @@ func (b *SBucket) GetWebsiteConf() (cloudprovider.SBucketWebsiteConf, error) { } result.Index = out.IndexDocument.Suffix result.ErrorDocument = out.ErrorDocument.Key - result.Url = fmt.Sprintf("https://%s.obs-website.%s.myhuaweicloud.com", b.Name, b.region.GetId()) + endpoint := b.region.client.cpcfg.GetEndpoint("obs-website", b.region.GetId()) + result.Url = fmt.Sprintf("https://%s.%s", endpoint) return result, nil } diff --git a/pkg/multicloud/huaweistack/client/client.go b/pkg/multicloud/huaweistack/client/client.go index aa7e79b3f4..d808e837bc 100644 --- a/pkg/multicloud/huaweistack/client/client.go +++ b/pkg/multicloud/huaweistack/client/client.go @@ -28,7 +28,6 @@ type Client struct { // 标记初始化状态 init bool - Balances *modules.SBalanceManager Bandwidths *modules.SBandwidthManager Credentials *modules.SCredentialManager Disks *modules.SDiskManager @@ -53,7 +52,6 @@ type Client struct { ElbL7policies *modules.SElbL7policiesManager ElbPolicies *modules.SElbPoliciesManager ElbWhitelist *modules.SElbWhitelistManager - Orders *modules.SOrderManager Port *modules.SPortManager Projects *modules.SProjectManager Regions *modules.SRegionManager @@ -153,7 +151,6 @@ func (self *Client) SetHttpClient(httpClient *http.Client) { self.ElbL7policies.SetHttpClient(httpClient) self.ElbPolicies.SetHttpClient(httpClient) self.ElbWhitelist.SetHttpClient(httpClient) - self.Orders.SetHttpClient(httpClient) self.SecurityGroupRules.SetHttpClient(httpClient) self.SecurityGroups.SetHttpClient(httpClient) self.NovaSecurityGroups.SetHttpClient(httpClient) @@ -161,7 +158,6 @@ func (self *Client) SetHttpClient(httpClient *http.Client) { self.Users.SetHttpClient(httpClient) self.Interface.SetHttpClient(httpClient) self.Jobs.SetHttpClient(httpClient) - self.Balances.SetHttpClient(httpClient) self.Bandwidths.SetHttpClient(httpClient) self.Port.SetHttpClient(httpClient) self.Flavors.SetHttpClient(httpClient) @@ -239,7 +235,6 @@ func (self *Client) initManagers() { self.ElbL7policies = modules.NewElbL7policiesManager(self.cfg) self.ElbPolicies = modules.NewElbPoliciesManager(self.cfg) self.ElbWhitelist = modules.NewElbWhitelistManager(self.cfg) - self.Orders = modules.NewOrderManager(self.cfg) self.SecurityGroupRules = modules.NewSecgroupRuleManager(self.cfg) self.SecurityGroups = modules.NewSecurityGroupManager(self.cfg) self.NovaSecurityGroups = modules.NewNovaSecurityGroupManager(self.cfg) @@ -247,7 +242,6 @@ func (self *Client) initManagers() { self.Users = modules.NewUserManager(self.cfg) self.Interface = modules.NewInterfaceManager(self.cfg) self.Jobs = modules.NewJobManager(self.cfg) - self.Balances = modules.NewBalanceManager(self.cfg) self.Bandwidths = modules.NewBandwidthManager(self.cfg) self.Credentials = modules.NewCredentialManager(self.cfg) self.Port = modules.NewPortManager(self.cfg) diff --git a/pkg/multicloud/huaweistack/client/modules/manager_resource.go b/pkg/multicloud/huaweistack/client/modules/manager_resource.go index 376c33e4fb..b50c322aba 100644 --- a/pkg/multicloud/huaweistack/client/modules/manager_resource.go +++ b/pkg/multicloud/huaweistack/client/modules/manager_resource.go @@ -28,8 +28,6 @@ import ( type ServiceNameType string -const HuaWeiDefaultDomain = "myhuaweicloud.com" - const ( ServiceNameECS ServiceNameType = "ecs" // 弹性云服务 ServiceNameCCE ServiceNameType = "cce" // 云容器服务 diff --git a/pkg/multicloud/huaweistack/client/modules/mod_balances.go b/pkg/multicloud/huaweistack/client/modules/mod_balances.go deleted file mode 100644 index 23dbce872e..0000000000 --- a/pkg/multicloud/huaweistack/client/modules/mod_balances.go +++ /dev/null @@ -1,72 +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 modules - -import ( - "fmt" - - "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/manager" - "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/responses" -) - -/* -https://support.huaweicloud.com/api-oce/zh-cn_topic_0075195195.html -客户运营能力API的Endpoint为“bss.cn-north-1.myhuaweicloud.com”。该Endpoint为全局Endpoint,中国站所有区域均可使用。 -如何获取合作伙伴ID https://support.huaweicloud.com/bpconsole_faq/zh-cn_topic_0081005893.html -注意事项: -客户查询自身的账户余额的时候,只允许使用客户自身的AK/SK或者Token调用。 -*/ -type SBalanceManager struct { - domainId string // 租户ID - SResourceManager -} - -type balanceCtx struct { - domainId string -} - -// https://support.huaweicloud.com/api-bpconsole/zh-cn_topic_0075213309.html -// 这个manager非常特殊。url hardcode -func (self *balanceCtx) GetPath() string { - return fmt.Sprintf("%s/customer/account-mgr", self.domainId) -} - -// 这个manager非常特殊。只有List 和 SetDomainId方法可用。其他方法未验证 -func NewBalanceManager(cfg manager.IManagerConfig) *SBalanceManager { - return &SBalanceManager{SResourceManager: SResourceManager{ - SBaseManager: NewBaseManager(cfg), - ServiceName: ServiceNameBSS, - Region: "cn-north-1", - ProjectId: "", - version: "v1.0", - Keyword: "account_balance", - KeywordPlural: "account_balances", - - ResourceKeyword: "balances", - }} -} - -func (self *SBalanceManager) List(querys map[string]string) (*responses.ListResult, error) { - if len(self.domainId) == 0 { - return nil, fmt.Errorf("domainId is emtpy.Use SetDomainId method to set.") - } - - ctx := &balanceCtx{domainId: self.domainId} - return self.ListInContext(ctx, querys) -} - -func (self *SBalanceManager) SetDomainId(domainId string) { - self.domainId = domainId -} diff --git a/pkg/multicloud/huaweistack/client/modules/mod_ces.go b/pkg/multicloud/huaweistack/client/modules/mod_ces.go index 58a0795a25..6872f65590 100644 --- a/pkg/multicloud/huaweistack/client/modules/mod_ces.go +++ b/pkg/multicloud/huaweistack/client/modules/mod_ces.go @@ -21,6 +21,7 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/httperrors" + hw_mod "yunion.io/x/onecloud/pkg/multicloud/huawei/client/modules" "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/manager" "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/requests" ) @@ -42,41 +43,8 @@ func NewCloudEyeManager(cfg manager.IManagerConfig) *SCloudEyeManager { }} } -type SMetricDimension struct { - Name string `json:"name"` - Value string `json:"value"` -} - -type SDatapoint struct { - Timestamp int64 `json:"timestamp"` - Max float64 `json:"max,omitzero"` - Min float64 `json:"min,omitzero"` - Average float64 `json:"average,omitzero"` - Sum float64 `json:"sum,omitzero"` - Variance float64 `json:"variance,omitzero"` -} - -type SMetricData struct { - SMetricMeta - - Datapoints []SDatapoint -} - -type SMetricMeta struct { - SMetric - - Unit string `json:"unit"` -} - -type SMetric struct { - MetricName string `json:"metric_name"` - Namespace string `json:"namespace"` - - Dimensions []SMetricDimension `json:"dimensions"` -} - -func (ces *SCloudEyeManager) ListMetrics() ([]SMetricMeta, error) { - metrics := make([]SMetricMeta, 0) +func (ces *SCloudEyeManager) ListMetrics() ([]hw_mod.SMetricMeta, error) { + metrics := make([]hw_mod.SMetricMeta, 0) next := "" for { marker, data, err := ces.listMetricsInternal(next) @@ -92,7 +60,7 @@ func (ces *SCloudEyeManager) ListMetrics() ([]SMetricMeta, error) { return metrics, nil } -func (ces *SCloudEyeManager) listMetricsInternal(start string) (string, []SMetricMeta, error) { +func (ces *SCloudEyeManager) listMetricsInternal(start string) (string, []hw_mod.SMetricMeta, error) { request := requests.NewResourceRequest(ces.GetEndpoint(), "GET", string(ces.ServiceName), ces.version, ces.Region, ces.ProjectId, ces.ResourceKeyword) request.AddQueryParam("limit", "1000") if len(start) > 0 { @@ -103,7 +71,7 @@ func (ces *SCloudEyeManager) listMetricsInternal(start string) (string, []SMetri return "", nil, errors.Wrap(err, "ces.jsonRequest") } marker, _ := resp.GetString("meta_data", "marker") - metrics := make([]SMetricMeta, 0) + metrics := make([]hw_mod.SMetricMeta, 0) err = resp.Unmarshal(&metrics, "metrics") if err != nil { return "", nil, errors.Wrap(err, "resp.Unmarshal metrics") @@ -112,7 +80,7 @@ func (ces *SCloudEyeManager) listMetricsInternal(start string) (string, []SMetri } type SBatchQueryMetricDataInput struct { - Metrics []SMetric `json:"metrics"` + Metrics []hw_mod.SMetric `json:"metrics"` From int64 `json:"from"` To int64 `json:"to"` @@ -120,11 +88,11 @@ type SBatchQueryMetricDataInput struct { Filter string `json:"filter"` } -func (ces *SCloudEyeManager) GetMetricsData(metrics []SMetricMeta, since time.Time, until time.Time) ([]SMetricData, error) { +func (ces *SCloudEyeManager) GetMetricsData(metrics []hw_mod.SMetricMeta, since time.Time, until time.Time) ([]hw_mod.SMetricData, error) { if len(metrics) > 10 { return nil, errors.Wrap(httperrors.ErrTooLarge, "request more than 10 metrics") } - metricReq := make([]SMetric, len(metrics)) + metricReq := make([]hw_mod.SMetric, len(metrics)) for i := range metrics { metricReq[i] = metrics[i].SMetric } @@ -143,7 +111,7 @@ func (ces *SCloudEyeManager) GetMetricsData(metrics []SMetricMeta, since time.Ti return nil, errors.Wrap(err, "ces.jsonRequest") } //log.Debugf("%s", resp) - result := make([]SMetricData, 0) + result := make([]hw_mod.SMetricData, 0) err = resp.Unmarshal(&result, "metrics") if err != nil { return nil, errors.Wrap(err, "resp.Unmarshal") diff --git a/pkg/multicloud/huaweistack/client/modules/mod_orders.go b/pkg/multicloud/huaweistack/client/modules/mod_orders.go deleted file mode 100644 index ebcfea08f3..0000000000 --- a/pkg/multicloud/huaweistack/client/modules/mod_orders.go +++ /dev/null @@ -1,104 +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 modules - -import ( - "fmt" - - "yunion.io/x/jsonutils" - - "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/manager" - "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/responses" -) - -// domian 客户账号ID https://support.huaweicloud.com/oce_faq/zh-cn_topic_0113714840.html -type SOrderManager struct { - orderCtx manager.IManagerContext - SResourceManager -} - -type orderCtx struct { - domainId string -} - -// {domain_id}/common/ -// 这个manager非常特殊。url hardcode -func (self *orderCtx) GetPath() string { - return fmt.Sprintf("%s/common", self.domainId) -} - -// 客户运营能力API的Endpoint为“bss.cn-north-1.myhuaweicloud.com”。该Endpoint为全局Endpoint,中国站所有区域均可使用。 -// https://support.huaweicloud.com/api-oce/zh-cn_topic_0084961226.html -func NewOrderManager(cfg manager.IManagerConfig) *SOrderManager { - return &SOrderManager{SResourceManager: SResourceManager{ - SBaseManager: NewBaseManager(cfg), - ServiceName: ServiceNameBSS, - Region: "cn-north-1", - ProjectId: "", - version: "v1.0", - Keyword: "", - KeywordPlural: "", - - ResourceKeyword: "order-mgr", - }} -} - -func (self *SOrderManager) SetDomainId(domainId string) error { - if len(domainId) == 0 { - return fmt.Errorf("SetDomainId domain id should not be emtpy") - } - - self.orderCtx = &orderCtx{domainId: domainId} - return nil -} - -// 查询客户包周期资源列表 https://support.huaweicloud.com/api-oce/zh-cn_topic_0084961226.html -func (self *SOrderManager) List(querys map[string]string) (*responses.ListResult, error) { - return nil, fmt.Errorf("Not Suppport List Order") -} - -// 查询订单的资源开通详情 https://support.huaweicloud.com/api-oce/api_order_00001.html -func (self *SOrderManager) Get(id string, querys map[string]string) (jsonutils.JSONObject, error) { - if self.orderCtx == nil { - return nil, fmt.Errorf("domainId is emtpy.Use SetDomainId method to set.") - } - - // !!!特殊调用 - return self.GetInContextWithSpec(self.orderCtx, "orders-resource", id, querys, "") -} - -func (self *SOrderManager) PerformAction(action string, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { - request := self.newRequest("POST", id, action, self.orderCtx) - request.SetContent([]byte(getContent(params))) - - return self._do(request, "") -} - -func (self *SOrderManager) GetPeriodResourceList(querys map[string]string) (*responses.ListResult, error) { - if self.orderCtx == nil { - return nil, fmt.Errorf("domainId is emtpy.Use SetDomainId method to set.") - } - - return self.ListInContextWithSpec(self.orderCtx, "resources/detail", querys, "data") -} - -// https://support.huaweicloud.com/api-bpconsole/zh-cn_topic_0082522029.html -func (self *SOrderManager) RenewPeriodResource(params jsonutils.JSONObject) (jsonutils.JSONObject, error) { - if self.orderCtx == nil { - return nil, fmt.Errorf("domainId is emtpy.Use SetDomainId method to set.") - } - - return self.CreateInContextWithSpec(self.orderCtx, "resources/renew", params, "order_ids") -} diff --git a/pkg/multicloud/huaweistack/cloudgroup.go b/pkg/multicloud/huaweistack/cloudgroup.go index bb3aabc060..c6c3b12608 100644 --- a/pkg/multicloud/huaweistack/cloudgroup.go +++ b/pkg/multicloud/huaweistack/cloudgroup.go @@ -257,7 +257,11 @@ func (self *SHuaweiClient) DetachGroupRole(groupId, roleId string) error { if err != nil { return errors.Wrap(err, "newGeneralAPIClient") } - err = client.Groups.DeleteRole(self.ownerId, groupId, roleId) + role, err := self.GetRole(roleId) + if err != nil { + return errors.Wrapf(err, "GetRole(%s)", roleId) + } + err = client.Groups.DeleteRole(self.ownerId, groupId, role.Id) if err != nil { return errors.Wrapf(err, "DeleteRole") } @@ -266,7 +270,7 @@ func (self *SHuaweiClient) DetachGroupRole(groupId, roleId string) error { return errors.Wrapf(err, "GetProjects") } for _, project := range projects { - err = client.Groups.DeleteProjectRole(project.ID, groupId, roleId) + err = client.Groups.DeleteProjectRole(project.ID, groupId, role.Id) if err != nil { return errors.Wrapf(err, "DeleteProjectRole") } @@ -274,12 +278,29 @@ func (self *SHuaweiClient) DetachGroupRole(groupId, roleId string) error { return nil } +func (self *SHuaweiClient) GetRole(name string) (*SRole, error) { + roles, err := self.GetRoles("", "") + if err != nil { + return nil, errors.Wrapf(err, "GetRoles(%s)", name) + } + for i := range roles { + if roles[i].DisplayName == name { + return &roles[i], nil + } + } + return nil, errors.Wrapf(cloudprovider.ErrNotFound, name) +} + func (self *SHuaweiClient) AttachGroupRole(groupId, roleId string) error { client, err := self.newGeneralAPIClient() if err != nil { return errors.Wrap(err, "newGeneralAPIClient") } - err = client.Groups.AddRole(self.ownerId, groupId, roleId) + role, err := self.GetRole(roleId) + if err != nil { + return errors.Wrapf(err, "GetRole(%s)", roleId) + } + err = client.Groups.AddRole(self.ownerId, groupId, role.Id) if err != nil { return errors.Wrapf(err, "AddRole") } @@ -288,7 +309,7 @@ func (self *SHuaweiClient) AttachGroupRole(groupId, roleId string) error { return errors.Wrapf(err, "GetProjects") } for _, project := range projects { - err = client.Groups.AddProjectRole(project.ID, groupId, roleId) + err = client.Groups.AddProjectRole(project.ID, groupId, role.Id) if err != nil { return errors.Wrapf(err, "AddProjectRole") } diff --git a/pkg/multicloud/huaweistack/dbinstance.go b/pkg/multicloud/huaweistack/dbinstance.go index f015e6e44f..41298389e1 100644 --- a/pkg/multicloud/huaweistack/dbinstance.go +++ b/pkg/multicloud/huaweistack/dbinstance.go @@ -161,11 +161,7 @@ func (rds *SDBInstance) GetStatus() string { } func (rds *SDBInstance) GetBillingType() string { - _, err := rds.region.GetOrderResourceDetail(fmt.Sprintf("%s.vm", rds.Id)) - if err != nil { - return billing_api.BILLING_TYPE_POSTPAID - } - return billing_api.BILLING_TYPE_PREPAID + return billing_api.BILLING_TYPE_POSTPAID } func (rds *SDBInstance) GetSecurityGroupIds() ([]string, error) { @@ -185,11 +181,7 @@ func (rds *SDBInstance) fetchFlavor() error { } func (rds *SDBInstance) GetExpiredAt() time.Time { - order, err := rds.region.GetOrderResourceDetail(fmt.Sprintf("%s.vm", rds.Id)) - if err != nil { - return time.Time{} - } - return order.ExpireTime + return time.Time{} } func (rds *SDBInstance) GetStorageType() string { @@ -697,5 +689,5 @@ func (region *SRegion) RecoveryDBInstanceFromBackup(target, origin string, backu } func (rds *SDBInstance) Renew(bc billing.SBillingCycle) error { - return rds.region.RenewInstance(rds.Id, bc) + return cloudprovider.ErrNotSupported } diff --git a/pkg/multicloud/huaweistack/disk.go b/pkg/multicloud/huaweistack/disk.go index 8d388c75db..d966ce9dc5 100644 --- a/pkg/multicloud/huaweistack/disk.go +++ b/pkg/multicloud/huaweistack/disk.go @@ -185,28 +185,8 @@ func (self *SDisk) IsEmulated() bool { return false } -func (self *SDisk) getResourceDetails() *SResourceDetail { - if self.details != nil { - return self.details - } - - res, err := self.storage.zone.region.GetOrderResourceDetail(self.GetId()) - if err != nil { - log.Debugln(err) - return nil - } - - self.details = &res - return self.details -} - func (self *SDisk) GetBillingType() string { - details := self.getResourceDetails() - if details == nil { - return billing_api.BILLING_TYPE_POSTPAID - } else { - return billing_api.BILLING_TYPE_PREPAID - } + return billing_api.BILLING_TYPE_POSTPAID } func (self *SDisk) GetCreatedAt() time.Time { @@ -215,11 +195,6 @@ func (self *SDisk) GetCreatedAt() time.Time { func (self *SDisk) GetExpiredAt() time.Time { var expiredTime time.Time - details := self.getResourceDetails() - if details != nil { - expiredTime = details.ExpireTime - } - return expiredTime } diff --git a/pkg/multicloud/huaweistack/elasticcache_instance.go b/pkg/multicloud/huaweistack/elasticcache_instance.go index 24feb34a6e..3218b09b68 100644 --- a/pkg/multicloud/huaweistack/elasticcache_instance.go +++ b/pkg/multicloud/huaweistack/elasticcache_instance.go @@ -163,15 +163,6 @@ func (self *SElasticcache) GetCreatedAt() time.Time { func (self *SElasticcache) GetExpiredAt() time.Time { var expiredTime time.Time - if self.ChargingMode == 1 { - res, err := self.region.GetOrderResourceDetail(self.GetId()) - if err != nil { - log.Debugln(err) - } - - expiredTime = res.ExpireTime - } - return expiredTime } diff --git a/pkg/multicloud/huaweistack/huawei.go b/pkg/multicloud/huaweistack/huawei.go index cb66b66acd..6137439a7b 100644 --- a/pkg/multicloud/huaweistack/huawei.go +++ b/pkg/multicloud/huaweistack/huawei.go @@ -33,10 +33,8 @@ import ( /* 待解决问题: -1.同步的子账户中有一条空记录.需要查原因 -2.安全组同步需要进一步确认 -3.实例接口需要进一步确认 -4.BGP type 目前是hard code在代码中。需要考虑从cloudmeta服务中查询 +2.VM密码登录不成功(ubuntu不行,centos可以) +3.实例绑定eip 查不出来eip? */ const ( @@ -73,6 +71,7 @@ func NewHuaweiClientConfig(accessKey, accessSecret, projectId string, endpoints accessSecret: accessSecret, endpoints: endpoints, } + return cfg } @@ -244,7 +243,7 @@ func getOBSEndpoint(regionId string) string { } func (client *SHuaweiClient) getOBSClient(regionId string) (*obs.ObsClient, error) { - endpoint := getOBSEndpoint(regionId) + endpoint := client.cpcfg.SHuaweiCloudStackEndpoints.GetEndpoint("obs", regionId) return obs.New(client.accessKey, client.accessSecret, endpoint) } @@ -322,11 +321,7 @@ func (self *SHuaweiClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) if strings.ToLower(project.Name) == "mos" { continue } - // https://www.huaweicloud.com/notice/2018/20190618171312411.html - expiredAt, _ := timeutils.ParseTimeStr("2020-09-16 00:00:00") - if !self.ownerCreateTime.IsZero() && self.ownerCreateTime.After(expiredAt) && strings.ToLower(project.Name) == "cn-north-1" { - continue - } + s := cloudprovider.SSubAccount{ Name: fmt.Sprintf("%s-%s", self.cpcfg.Name, project.Name), Account: fmt.Sprintf("%s/%s", self.accessKey, project.ID), @@ -438,42 +433,6 @@ type SBalance struct { MeasureUnit int64 `json:"measure_unit"` } -// 这里的余额指的是所有租户的总余额 -func (self *SHuaweiClient) QueryAccountBalance() (*SAccountBalance, error) { - domains, err := self.getEnabledDomains() - if err != nil { - return nil, err - } - - result := &SAccountBalance{} - for _, domain := range domains { - balances, err := self.queryDomainBalances(domain.ID) - if err != nil { - return nil, err - } - for _, balance := range balances { - result.AvailableAmount += balance.Amount - result.CreditAmount += balance.CreditAmount - result.DesignatedAmount += balance.DesignatedAmount - } - } - - return result, nil -} - -// https://support.huaweicloud.com/api-bpconsole/zh-cn_topic_0075213309.html -func (self *SHuaweiClient) queryDomainBalances(domainId string) ([]SBalance, error) { - huawei, _ := self.newGeneralAPIClient() - huawei.Balances.SetDomainId(domainId) - balances := make([]SBalance, 0) - err := doListAll(huawei.Balances.List, nil, &balances) - if err != nil { - return nil, err - } - - return balances, nil -} - func (self *SHuaweiClient) GetVersion() string { return HUAWEI_API_VERSION } @@ -554,6 +513,10 @@ func (self *SHuaweiClient) GetOwnerId() (string, error) { return ret.DomainId, nil } +func (self *SHuaweiClient) GetSamlEntityId() string { + return fmt.Sprintf("auth.%s", self.cpcfg.EndpointDomain) +} + func (self *SHuaweiClient) initOwner() error { ownerId, err := self.GetOwnerId() if err != nil { diff --git a/pkg/multicloud/huaweistack/image.go b/pkg/multicloud/huaweistack/image.go index d1bd0dadf5..60348ff0a5 100644 --- a/pkg/multicloud/huaweistack/image.go +++ b/pkg/multicloud/huaweistack/image.go @@ -167,7 +167,11 @@ func (self *SImage) GetSizeByte() int64 { func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { if self.imgInfo == nil { - imgInfo := imagetools.NormalizeImageInfo(self.ImageSourceType, self.OSType, self.OSType, self.Platform, "") + arch := "x86" + if strings.ToLower(self.SupportArm) == "true" { + arch = "arm" + } + imgInfo := imagetools.NormalizeImageInfo(self.ImageSourceType, arch, self.OSType, self.Platform, "") self.imgInfo = &imgInfo } @@ -273,10 +277,6 @@ func excludeImage(image SImage) bool { return true } - if len(image.SupportArm) > 0 { - return true - } - return false } @@ -421,15 +421,14 @@ func formatVersion(osDist string, osVersion string) (string, error) { return "", err } -// todo: 如何保持同步更新 // https://support.huaweicloud.com/api-ims/zh-cn_topic_0031617666.html func stdVersion(osDist string, osVersion string, osArch string) (string, error) { // 架构 arch := "" switch osArch { - case "64", apis.OS_ARCH_X86_64: + case "64", apis.OS_ARCH_X86_64, apis.OS_ARCH_AARCH64, apis.OS_ARCH_ARM: arch = "64bit" - case "32", apis.OS_ARCH_X86_32: + case "32", apis.OS_ARCH_X86_32, apis.OS_ARCH_AARCH32: arch = "32bit" default: return "", fmt.Errorf("unsupported arch %s.reference: https://support.huaweicloud.com/api-ims/zh-cn_topic_0031617666.html", osArch) diff --git a/pkg/multicloud/huaweistack/instance.go b/pkg/multicloud/huaweistack/instance.go index ebea15d913..4993457b89 100644 --- a/pkg/multicloud/huaweistack/instance.go +++ b/pkg/multicloud/huaweistack/instance.go @@ -29,6 +29,7 @@ import ( "yunion.io/x/pkg/util/osprofile" "yunion.io/x/pkg/utils" + "yunion.io/x/onecloud/pkg/apis" billing_api "yunion.io/x/onecloud/pkg/apis/billing" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -354,15 +355,6 @@ func (self *SInstance) GetCreatedAt() time.Time { // charging_mode “0”:按需计费 “1”:按包年包月计费 func (self *SInstance) GetExpiredAt() time.Time { var expiredTime time.Time - if self.Metadata.ChargingMode == "1" { - res, err := self.host.zone.region.GetOrderResourceDetail(self.GetId()) - if err != nil { - log.Debugln(err) - } - - expiredTime = res.ExpireTime - } - return expiredTime } @@ -476,6 +468,23 @@ func (self *SInstance) GetVdi() string { return "vnc" } +func (self *SInstance) GetOSArch() string { + if flavor, err := self.host.zone.region.GetICloudSku(self.Flavor.ID); err == nil { + return flavor.GetCpuArch() + } else { + log.Debugf("GetOSArch.GetICloudSku %s: %s", self.Flavor.ID, err) + } + + t := self.GetInstanceType() + if len(t) > 0 { + if strings.HasPrefix(t, "k") { + return apis.OS_ARCH_AARCH64 + } + } + + return apis.OS_ARCH_X86 +} + func (self *SInstance) GetOSType() string { return osprofile.NormalizeOSType(self.Metadata.OSType) } @@ -765,7 +774,7 @@ func (self *SInstance) CreateDisk(ctx context.Context, sizeMb int, uuid string, } func (self *SInstance) Renew(bc billing.SBillingCycle) error { - return self.host.zone.region.RenewInstance(self.GetId(), bc) + return cloudprovider.ErrNotSupported } // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0094148850.html @@ -955,30 +964,7 @@ func (self *SRegion) CreateInstance(name string, imageId string, instanceType st ids, err = self.GetAllSubTaskEntityIDs(self.ecsClient.Servers.ServiceType(), _id, "server_id") } else { // 包年包月 - err = cloudprovider.WaitCreated(10*time.Second, 300*time.Second, func() bool { - log.Debugf("WaitCreated %s", _id) - order, e := self.GetOrder(_id) - if e != nil { - log.Debugf(e.Error()) - return false - } - - if order.TotalSize == 0 { - return false - } - - ids, err = self.getAllResIdsByType(_id, RESOURCE_TYPE_VM) - if err != nil { - log.Debugln(err) - return false - } - - if len(ids) > 0 { - return true - } - - return false - }) + return "", errors.Wrap(cloudprovider.ErrNotSupported, "CreateInstance") } if err != nil { @@ -1321,43 +1307,6 @@ func (self *SRegion) DetachDisk(instanceId string, diskId string) error { return err } -// // https://support.huaweicloud.com/api-bpconsole/zh-cn_topic_0082522029.html -// 只支持传入主资源ID, 根据“查询客户包周期资源列表”接口响应参数中的“is_main_resource”来标识。 -// expire_mode 0:进入宽限期 1:转按需 2:自动退订 3:自动续订(当前只支持ECS、EVS和VPC) -func (self *SRegion) RenewInstance(instanceId string, bc billing.SBillingCycle) error { - params := jsonutils.NewDict() - res := jsonutils.NewArray() - res.Add(jsonutils.NewString(instanceId)) - params.Add(res, "resource_ids") - params.Add(jsonutils.NewInt(EXPIRE_MODE_AUTO_UNSUBSCRIBE), "expire_mode") // 自动退订 - params.Add(jsonutils.NewInt(AUTO_PAY_TRUE), "isAutoPay") // 自动支付 - month := int64(bc.GetMonths()) - year := int64(bc.GetYears()) - - if month >= 1 && month <= 11 { - params.Add(jsonutils.NewInt(PERIOD_TYPE_MONTH), "period_type") - params.Add(jsonutils.NewInt(month), "period_num") - } else if year >= 1 && year <= 3 { - params.Add(jsonutils.NewInt(PERIOD_TYPE_YEAR), "period_type") - params.Add(jsonutils.NewInt(year), "period_num") - } else { - return fmt.Errorf("invalid renew period %d month,must be 1~11 month or 1~3 year", month) - } - - domainId, err := self.getDomianId() - if err != nil { - return err - } - - err = self.ecsClient.Orders.SetDomainId(domainId) - if err != nil { - return err - } - - _, err = self.ecsClient.Orders.RenewPeriodResource(params) - return err -} - // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0065817702.html func (self *SRegion) GetInstanceSecrityGroupIds(instanceId string) ([]string, error) { if len(instanceId) == 0 { @@ -1379,20 +1328,6 @@ func (self *SRegion) GetInstanceSecrityGroupIds(instanceId string) ([]string, er return securitygroupIds, nil } -// https://support.huaweicloud.com/api-oce/zh-cn_topic_0082522030.html -func (self *SRegion) UnsubscribeInstance(instanceId string, domianId string) (jsonutils.JSONObject, error) { - unsubObj := jsonutils.NewDict() - unsubObj.Add(jsonutils.NewInt(1), "unSubType") - unsubObj.Add(jsonutils.NewInt(5), "unsubscribeReasonType") - unsubObj.Add(jsonutils.NewString("no reason"), "unsubscribeReason") - resList := jsonutils.NewArray() - resList.Add(jsonutils.NewString(instanceId)) - unsubObj.Add(resList, "resourceIds") - - self.ecsClient.Orders.SetDomainId(domianId) - return self.ecsClient.Orders.PerformAction("resources/delete", "", unsubObj) -} - func (self *SInstance) GetProjectId() string { return self.EnterpriseProjectId } diff --git a/pkg/multicloud/huaweistack/instancetype.go b/pkg/multicloud/huaweistack/instancetype.go index fe83a5327c..c9d276b63a 100644 --- a/pkg/multicloud/huaweistack/instancetype.go +++ b/pkg/multicloud/huaweistack/instancetype.go @@ -17,6 +17,8 @@ package huaweistack import ( "strconv" "strings" + + "yunion.io/x/onecloud/pkg/apis" ) // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html @@ -35,8 +37,9 @@ type SInstanceType struct { } type OSExtraSpecs struct { - EcsPerformancetype string `json:"ecs:performancetype"` - EcsGeneration string `json:"ecs:generation"` + EcsPerformancetype string `json:"ecs:performancetype"` + EcsGeneration string `json:"ecs:generation"` + EcsInstanceArchitecture string `json:"ecs:instance_architecture"` } var FLAVOR_FAMILY_CATEGORY_MAP = map[string]string{ @@ -111,16 +114,6 @@ func getFlavorLocalCategory(family string) string { } } -// https://support.huaweicloud.com/productdesc-ecs/ecs_01_0066.html -// https://support.huaweicloud.com/ecs_faq/ecs_faq_0105.html -func GetCpuArch(flavorId string) string { - if strings.HasPrefix(flavorId, "k") { - return "aarch64" - } - - return "x86" -} - func (self *SInstanceType) GetId() string { return self.ID } @@ -177,9 +170,29 @@ func (self *SInstanceType) GetPostpaidStatus() string { return "available" } +// https://support.huaweicloud.com/productdesc-ecs/ecs_01_0066.html +// https://support.huaweicloud.com/ecs_faq/ecs_faq_0105.html +func (self *SInstanceType) GetCpuArch() string { + if len(self.OSExtraSpecs.EcsInstanceArchitecture) > 0 { + if strings.ToLower(self.OSExtraSpecs.EcsInstanceArchitecture) == "arm64" { + return apis.OS_ARCH_AARCH64 + } + + if strings.HasPrefix(self.OSExtraSpecs.EcsInstanceArchitecture, "arm") { + return apis.OS_ARCH_AARCH64 + } + } + + if strings.HasPrefix(self.ID, "k") { + return apis.OS_ARCH_AARCH64 + } + + return apis.OS_ARCH_X86 +} + func (self *SInstanceType) GetCpuCoreCount() int { count, err := strconv.Atoi(self.Vcpus) - if err != nil { + if err == nil { return count } return 0 diff --git a/pkg/multicloud/huaweistack/monitor.go b/pkg/multicloud/huaweistack/monitor.go index da138a4a6f..d0600131ef 100644 --- a/pkg/multicloud/huaweistack/monitor.go +++ b/pkg/multicloud/huaweistack/monitor.go @@ -17,7 +17,7 @@ package huaweistack import ( "time" - "yunion.io/x/onecloud/pkg/multicloud/huaweistack/client/modules" + "yunion.io/x/onecloud/pkg/multicloud/huawei/client/modules" ) func (r *SRegion) GetMetrics() ([]modules.SMetricMeta, error) { diff --git a/pkg/multicloud/huaweistack/order.go b/pkg/multicloud/huaweistack/order.go index 198641903d..1b783b24d5 100644 --- a/pkg/multicloud/huaweistack/order.go +++ b/pkg/multicloud/huaweistack/order.go @@ -16,12 +16,7 @@ package huaweistack import ( "fmt" - "strings" "time" - - "yunion.io/x/log" - - "yunion.io/x/onecloud/pkg/cloudprovider" ) type SOrder struct { @@ -73,121 +68,3 @@ func (self *SRegion) getDomianId() (string, error) { return domains[0].ID, nil } - -/* -获取订单信息 https://support.huaweicloud.com/api-oce/api_order_00001.html -*/ -func (self *SRegion) GetOrder(orderId string) (SOrder, error) { - var order SOrder - domain, err := self.getDomianId() - if err != nil { - return order, err - } - - err = self.ecsClient.Orders.SetDomainId(domain) - if err != nil { - return order, err - } - - err = DoGet(self.ecsClient.Orders.Get, orderId, nil, &order) - return order, err -} - -/* -获取订单资源详情列表 https://support.huaweicloud.com/api-oce/zh-cn_topic_0084961226.html -*/ -func (self *SRegion) GetOrderResources(orderId string, resource_ids []string, only_main_resource bool) ([]SResourceDetail, error) { - domain, err := self.getDomianId() - if err != nil { - return nil, err - } - - err = self.ecsClient.Orders.SetDomainId(domain) - if err != nil { - return nil, err - } - - resources := make([]SResourceDetail, 0) - queries := map[string]string{"customer_id": domain} - if len(orderId) > 0 { - queries["order_id"] = orderId - } - - if len(resource_ids) > 0 { - queries["resource_ids"] = strings.Join(resource_ids, ",") - } - - if only_main_resource { - queries["only_main_resource"] = "1" - } - - err = doListAll(self.ecsClient.Orders.GetPeriodResourceList, queries, &resources) - return resources, err -} - -/* -获取资源详情 https://support.huaweicloud.com/api-oce/zh-cn_topic_0084961226.html -*/ -func (self *SRegion) GetOrderResourceDetail(resourceId string) (SResourceDetail, error) { - var res SResourceDetail - if len(resourceId) == 0 { - return res, fmt.Errorf("GetOrderResourceDetail resource id should not be empty") - } - - resources, err := self.GetOrderResources("", []string{resourceId}, false) - if err != nil { - return res, err - } - - switch len(resources) { - case 0: - return res, cloudprovider.ErrNotFound - case 1: - return resources[0], nil - default: - return res, fmt.Errorf("%d resources with id %s found, Expect 1", len(resources), resourceId) - } -} - -func (self *SRegion) GetAllResByOrderId(orderId string) ([]SResource, error) { - order, err := self.GetOrder(orderId) - if err != nil { - return nil, err - } - - log.Debugf("GetAllResByOrderId %#v", order.Resources) - return order.Resources, nil -} - -func (self *SRegion) getAllResByType(orderId string, resourceType string) ([]SResource, error) { - res, err := self.GetAllResByOrderId(orderId) - if err != nil { - return nil, err - } - - ret := make([]SResource, 0) - for i := range res { - r := res[i] - if r.ResourceType == resourceType { - ret = append(ret, r) - } - } - - return ret, nil -} - -func (self *SRegion) getAllResIdsByType(orderId string, resourceType string) ([]string, error) { - res, err := self.getAllResByType(orderId, resourceType) - if err != nil { - return nil, err - } - - ids := make([]string, 0) - for _, r := range res { - if len(r.ResourceID) > 0 { - ids = append(ids, r.ResourceID) - } - } - - return ids, nil -} diff --git a/pkg/multicloud/huaweistack/provider/provider.go b/pkg/multicloud/huaweistack/provider/provider.go index ad58464315..f4eef53006 100644 --- a/pkg/multicloud/huaweistack/provider/provider.go +++ b/pkg/multicloud/huaweistack/provider/provider.go @@ -181,6 +181,10 @@ func (self *SHuaweiCloudStackProviderFactory) GetClientRC(info cloudprovider.SPr }, nil } +func (self *SHuaweiCloudStackProviderFactory) IsMultiTenant() bool { + return true +} + func init() { factory := SHuaweiCloudStackProviderFactory{} cloudprovider.RegisterFactory(&factory) @@ -300,7 +304,7 @@ func (self *SHuaweiCloudStackProvider) GetIClouduserByName(name string) (cloudpr } func (self *SHuaweiCloudStackProvider) GetSamlEntityId() string { - return cloudprovider.SAML_ENTITY_ID_HUAWEI_CLOUD + return self.client.GetSamlEntityId() } func (self *SHuaweiCloudStackProvider) GetICloudSAMLProviders() ([]cloudprovider.ICloudSAMLProvider, error) { diff --git a/pkg/multicloud/huaweistack/region.go b/pkg/multicloud/huaweistack/region.go index dcdad7a068..b76a62dbea 100644 --- a/pkg/multicloud/huaweistack/region.go +++ b/pkg/multicloud/huaweistack/region.go @@ -52,6 +52,7 @@ type SRegion struct { izones []cloudprovider.ICloudZone ivpcs []cloudprovider.ICloudVpc + iskus []cloudprovider.ICloudSku storageCache *SStoragecache } @@ -572,18 +573,8 @@ func (self *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP ctype = InternetChargeByBandwidth } - // todo: 如何避免hardcode。集成到cloudmeta服务中? if len(eip.BGPType) == 0 { - switch self.GetId() { - case "cn-north-1", "cn-east-2", "cn-south-1": - eip.BGPType = "5_sbgp" - case "cn-northeast-1": - eip.BGPType = "5_telcom" - case "cn-north-4", "ap-southeast-1", "ap-southeast-2", "eu-west-0": - eip.BGPType = "5_bgp" - default: - eip.BGPType = "5_bgp" - } + eip.BGPType = "5_bgp" } // 华为云EIP名字最大长度64 @@ -989,7 +980,37 @@ func (region *SRegion) GetIBucketByName(name string) (cloudprovider.ICloudBucket } func (self *SRegion) GetSkus(zoneId string) ([]cloudprovider.ICloudSku, error) { - return nil, cloudprovider.ErrNotImplemented + if self.iskus != nil { + return self.iskus, nil + } + + ret := make([]cloudprovider.ICloudSku, 0) + flavors, err := self.fetchInstanceTypes(zoneId) + if err != nil { + return nil, errors.Wrap(err, "fetchInstanceTypes") + } + + for i := range flavors { + ret = append(ret, &flavors[i]) + } + + self.iskus = ret + return ret, nil +} + +func (self *SRegion) GetICloudSku(skuId string) (cloudprovider.ICloudSku, error) { + skus, err := self.GetSkus("") + if err != nil { + return nil, err + } + + for i := range skus { + if skus[i].GetId() == skuId { + return skus[i], nil + } + } + + return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetICloudSku") } func (self *SRegion) GetIElasticcaches() ([]cloudprovider.ICloudElasticcache, error) { @@ -1044,24 +1065,7 @@ func (self *SRegion) GetZoneSupportedDiskTypes(zoneId string) ([]string, error) } func (self *SRegion) GetISkus() ([]cloudprovider.ICloudSku, error) { - izones, err := self.GetIZones() - if err != nil { - return nil, errors.Wrap(err, "GetIZones") - } - - ret := make([]cloudprovider.ICloudSku, 0) - for i := range izones { - flavors, err := self.fetchInstanceTypes(izones[i].GetId()) - if err != nil { - return nil, errors.Wrap(err, "fetchInstanceTypes") - } - - for i := range flavors { - ret = append(ret, &flavors[i]) - } - } - - return ret, nil + return self.GetSkus("") } func (self *SRegion) GetEndpoints() ([]jsonutils.JSONObject, error) { diff --git a/pkg/multicloud/huaweistack/roles.go b/pkg/multicloud/huaweistack/roles.go index 3de9f80617..267ce49580 100644 --- a/pkg/multicloud/huaweistack/roles.go +++ b/pkg/multicloud/huaweistack/roles.go @@ -49,7 +49,7 @@ func (role *SRole) GetPolicyType() string { } func (role *SRole) GetGlobalId() string { - return role.Id + return role.DisplayName } func (role *SRole) UpdateDocument(document *jsonutils.JSONDict) error { @@ -82,7 +82,7 @@ func (self *SHuaweiClient) GetRoles(domainId, name string) ([]SRole, error) { params["domain_id"] = self.ownerId } if len(name) > 0 { - params["name"] = name + params["display_name"] = name } client, err := self.newGeneralAPIClient() diff --git a/pkg/multicloud/huaweistack/saml_provider.go b/pkg/multicloud/huaweistack/saml_provider.go index c0b5a6eb46..1ad909b3ec 100644 --- a/pkg/multicloud/huaweistack/saml_provider.go +++ b/pkg/multicloud/huaweistack/saml_provider.go @@ -66,7 +66,7 @@ func (self *SAMLProvider) GetStatus() string { } func (self *SAMLProvider) GetAuthUrl() string { - return fmt.Sprintf("https://auth.huaweicloud.com/authui/federation/websso?domain_id=%s&idp=%s&protocol=saml", self.client.ownerId, self.Id) + return fmt.Sprintf("https://auth.%s/authui/federation/websso?domain_id=%s&idp=%s&protocol=saml", self.client.cpcfg.EndpointDomain, self.client.ownerId, self.Id) } func (self *SAMLProvider) Delete() error { diff --git a/pkg/multicloud/huaweistack/shell/instance.go b/pkg/multicloud/huaweistack/shell/instance.go index b559b9cdd6..f9ce186c22 100644 --- a/pkg/multicloud/huaweistack/shell/instance.go +++ b/pkg/multicloud/huaweistack/shell/instance.go @@ -153,19 +153,6 @@ func init() { DOMAIN string `help:"domain ID"` } - shellutils.R(&InstanceOrderUnsubscribeOptions{}, "instance-order-unsubscribe", "Unsubscribe a prepaid server", func(cli *huawei.SRegion, args *InstanceOrderUnsubscribeOptions) error { - instance, e := cli.GetInstanceByID(args.ID) - if e != nil { - return e - } - - _, err := cli.UnsubscribeInstance(instance.GetId(), args.DOMAIN) - if err != nil { - return err - } - return nil - }) - type InstanceSaveImageOptions struct { ID string `help:"Instance ID"` IMAGE_NAME string `help:"Image name"` diff --git a/pkg/multicloud/huaweistack/shell/order.go b/pkg/multicloud/huaweistack/shell/order.go deleted file mode 100644 index bde61f3df0..0000000000 --- a/pkg/multicloud/huaweistack/shell/order.go +++ /dev/null @@ -1,36 +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 ( - huawei "yunion.io/x/onecloud/pkg/multicloud/huaweistack" - "yunion.io/x/onecloud/pkg/util/shellutils" -) - -func init() { - type OrderListOptions struct { - OrderId string `help:"Order Id"` - ResourceIds []string `help:"ResourceIds"` - MainResource bool `help:"Main resource"` - } - shellutils.R(&OrderListOptions{}, "order-list", "List order", func(cli *huawei.SRegion, args *OrderListOptions) error { - orders, err := cli.GetOrderResources(args.OrderId, args.ResourceIds, args.MainResource) - if err != nil { - return err - } - printList(orders, 0, 0, 0, nil) - return nil - }) -} diff --git a/pkg/multicloud/huaweistack/shell/role.go b/pkg/multicloud/huaweistack/shell/role.go index 19c6d2de91..0707532c2f 100644 --- a/pkg/multicloud/huaweistack/shell/role.go +++ b/pkg/multicloud/huaweistack/shell/role.go @@ -15,6 +15,13 @@ package shell import ( + "fmt" + "io/ioutil" + + "yunion.io/x/jsonutils" + + cloudid_api "yunion.io/x/onecloud/pkg/apis/cloudid" + api "yunion.io/x/onecloud/pkg/apis/compute" huawei "yunion.io/x/onecloud/pkg/multicloud/huaweistack" "yunion.io/x/onecloud/pkg/util/shellutils" ) @@ -32,4 +39,37 @@ func init() { printList(roles, 0, 0, 0, nil) return nil }) + + shellutils.R(&RoleListOptions{}, "cloud-policy-export", "Export cloudpolicy", func(cli *huawei.SRegion, args *RoleListOptions) error { + roles, err := cli.GetClient().GetRoles(args.DomainId, args.Name) + if err != nil { + return err + } + type sRule struct { + Name string + Id string + ExternalId string + CloudEnv string + Provider string + Description string + Document jsonutils.JSONDict + PolicyType string + Status string + } + ret := []sRule{} + for i := range roles { + ret = append(ret, sRule{ + Name: roles[i].DisplayName, + Id: roles[i].Id, + ExternalId: roles[i].DisplayName, + CloudEnv: api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK, + Provider: api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK, + Description: roles[i].DescriptionCn, + Document: roles[i].Policy, + PolicyType: cloudid_api.CLOUD_POLICY_TYPE_SYSTEM, + Status: cloudid_api.CLOUD_POLICY_STATUS_AVAILABLE, + }) + } + return ioutil.WriteFile(fmt.Sprintf("%s.json", api.CLOUD_PROVIDER_HUAWEI_CLOUD_STACK), []byte(jsonutils.Marshal(ret).PrettyString()), 0644) + }) } diff --git a/pkg/multicloud/huaweistack/shell/business.go b/pkg/multicloud/huaweistack/shell/skus.go similarity index 72% rename from pkg/multicloud/huaweistack/shell/business.go rename to pkg/multicloud/huaweistack/shell/skus.go index e08454ee0a..55909a8d2d 100644 --- a/pkg/multicloud/huaweistack/shell/business.go +++ b/pkg/multicloud/huaweistack/shell/skus.go @@ -20,14 +20,15 @@ import ( ) func init() { - type AccountBalanceOptions struct { + type SkuListOptions struct { } - shellutils.R(&AccountBalanceOptions{}, "balance", "Get account balance", func(cli *huawei.SRegion, args *AccountBalanceOptions) error { - result, err := cli.GetClient().QueryAccountBalance() - if err != nil { - return err + shellutils.R(&SkuListOptions{}, "sku-list", "List skus", func(cli *huawei.SRegion, args *SkuListOptions) error { + skus, e := cli.GetISkus() + if e != nil { + return e } - printObject(result) + + printList(skus, 0, 0, 0, []string{}) return nil }) } diff --git a/pkg/multicloud/huaweistack/storagecache.go b/pkg/multicloud/huaweistack/storagecache.go index 25a7a222bf..b8f2bb4c13 100644 --- a/pkg/multicloud/huaweistack/storagecache.go +++ b/pkg/multicloud/huaweistack/storagecache.go @@ -38,7 +38,8 @@ import ( type SStoragecache struct { multicloud.SResourceBase multicloud.HuaweiTags - region *SRegion + region *SRegion + iimages []cloudprovider.ICloudImage } func GetBucketName(regionId string, imageId string) string { @@ -69,22 +70,43 @@ func (self *SStoragecache) IsEmulated() bool { return false } +func (self *SStoragecache) fetchImages() error { + imagesGold, err := self.region.GetImages("", ImageOwnerPublic, "", EnvFusionCompute) + if err != nil { + return err + } + + imagesSelf, err := self.region.GetImages("", ImageOwnerSelf, "", EnvFusionCompute) + if err != nil { + return err + } + + self.iimages = make([]cloudprovider.ICloudImage, len(imagesGold)+len(imagesSelf)) + for i := range imagesGold { + imagesGold[i].storageCache = self + self.iimages[i] = &imagesGold[i] + } + + l := len(imagesGold) + for i := range imagesSelf { + imagesSelf[i].storageCache = self + self.iimages[i+l] = &imagesSelf[i] + } + return nil +} + func (self *SStoragecache) GetICloudImages() ([]cloudprovider.ICloudImage, error) { - return nil, cloudprovider.ErrNotImplemented + if self.iimages == nil { + err := self.fetchImages() + if err != nil { + return nil, err + } + } + return self.iimages, nil } func (self *SStoragecache) GetICustomizedCloudImages() ([]cloudprovider.ICloudImage, error) { - imagesSelf, err := self.region.GetImages("", ImageOwnerSelf, "", EnvFusionCompute) - if err != nil { - return nil, errors.Wrapf(err, "GetImages") - } - - ret := []cloudprovider.ICloudImage{} - for i := range imagesSelf { - imagesSelf[i].storageCache = self - ret = append(ret, &imagesSelf[i]) - } - return ret, nil + return nil, cloudprovider.ErrNotImplemented } func (self *SStoragecache) GetIImageById(extId string) (cloudprovider.ICloudImage, error) { diff --git a/pkg/multicloud/huaweistack/traces.go b/pkg/multicloud/huaweistack/traces.go index 0919b37071..dfb3b7413e 100644 --- a/pkg/multicloud/huaweistack/traces.go +++ b/pkg/multicloud/huaweistack/traces.go @@ -54,12 +54,12 @@ type SEvent struct { } func (event *SEvent) GetName() string { - if len(event.ResourceId) > 0 { - return event.ResourceId - } if len(event.ResourceName) > 0 { return event.ResourceName } + if len(event.ResourceId) > 0 { + return event.ResourceId + } return event.TraceName } diff --git a/pkg/multicloud/huaweistack/wire.go b/pkg/multicloud/huaweistack/wire.go index 94e20e2eb8..9c63f3fdd9 100644 --- a/pkg/multicloud/huaweistack/wire.go +++ b/pkg/multicloud/huaweistack/wire.go @@ -16,6 +16,7 @@ package huaweistack import ( "fmt" + "strings" "time" "yunion.io/x/jsonutils" @@ -184,6 +185,19 @@ func (self *SRegion) createNetwork(vpcId string, name string, cidr string, desc subnetObj.Add(jsonutils.NewString(vpcId), "vpc_id") subnetObj.Add(jsonutils.NewString(cidr), "cidr") subnetObj.Add(jsonutils.NewString(gateway), "gateway_ip") + // hard code for hcso + // https://support.huaweicloud.com/dns_faq/dns_faq_002.html + // https://support.huaweicloud.com/api-dns/dns_api_69001.html + if self.client != nil && len(self.client.cpcfg.SHuaweiCloudStackEndpoints.DefaultSubnetDns) > 0 { + dns := strings.Split(self.client.cpcfg.SHuaweiCloudStackEndpoints.DefaultSubnetDns, ",") + if len(dns) > 0 && len(dns[0]) > 0 { + subnetObj.Add(jsonutils.NewString(dns[0]), "primary_dns") + } + + if len(dns) > 1 && len(dns[1]) > 0 { + subnetObj.Add(jsonutils.NewString(dns[1]), "secondary_dns") + } + } params.Add(subnetObj, "subnet") subnet := SNetwork{} diff --git a/pkg/multicloud/instance_base.go b/pkg/multicloud/instance_base.go index ac08778036..f47802c0b5 100644 --- a/pkg/multicloud/instance_base.go +++ b/pkg/multicloud/instance_base.go @@ -70,3 +70,7 @@ func (self *SInstanceBase) SaveImage(opts *cloudprovider.SaveImageOptions) (clou func (self *SInstanceBase) AllocatePublicIpAddress() (string, error) { return "", errors.Wrapf(cloudprovider.ErrNotImplemented, "AllocatePublicIpAddress") } + +func (self *SInstanceBase) GetOSArch() string { + return "" +} diff --git a/pkg/multicloud/openstack/flavor.go b/pkg/multicloud/openstack/flavor.go index 1d5213bef2..abc19322a4 100644 --- a/pkg/multicloud/openstack/flavor.go +++ b/pkg/multicloud/openstack/flavor.go @@ -208,6 +208,10 @@ func (flavor *SFlavor) GetPostpaidStatus() string { return api.SkuStatusAvailable } +func (flavor *SFlavor) GetCpuArch() string { + return "" +} + func (flavor *SFlavor) GetCpuCoreCount() int { return int(flavor.Vcpus) } diff --git a/pkg/multicloud/qcloud/mongodb.go b/pkg/multicloud/qcloud/mongodb.go index 999d8db944..3cc71ded60 100644 --- a/pkg/multicloud/qcloud/mongodb.go +++ b/pkg/multicloud/qcloud/mongodb.go @@ -162,6 +162,12 @@ func (self *SMongoDB) GetVmemSizeMb() int { } func (self *SMongoDB) GetReplicationNum() int { + switch self.GetCategory() { + case api.MONGO_DB_CATEGORY_SHARDING: + return self.ReplicationSetNum + case api.MONGO_DB_CATEGORY_REPLICATE: + return 3 + } return self.ReplicationSetNum } @@ -170,11 +176,7 @@ func (self *SMongoDB) GetDiskSizeMb() int { } func (self *SMongoDB) GetZoneId() string { - zone, err := self.region.getZoneById(self.Zone) - if err != nil { - return "" - } - return zone.GetGlobalId() + return self.Zone } func (self *SMongoDB) GetBillingType() string { @@ -315,7 +317,8 @@ func (self *SRegion) GetICloudMongoDBs() ([]cloudprovider.ICloudMongoDB, error) func (self *SRegion) GetMongoDBBackups(id string) ([]cloudprovider.SMongoDBBackup, error) { params := map[string]string{ - "InstanceId": id, + "BackupMethod": "2", + "InstanceId": id, } resp, err := self.mongodbRequest("DescribeDBBackups", params) if err != nil { diff --git a/pkg/multicloud/qcloud/shell/mongodb.go b/pkg/multicloud/qcloud/shell/mongodb.go index 66beb5a982..5f68bdbadd 100644 --- a/pkg/multicloud/qcloud/shell/mongodb.go +++ b/pkg/multicloud/qcloud/shell/mongodb.go @@ -50,4 +50,13 @@ func init() { return cli.DeleteMongoDB(args.ID) }) + shellutils.R(&MongoDBIdOptions{}, "mongodb-backup-list", "List mongodb backups", func(cli *qcloud.SRegion, args *MongoDBIdOptions) error { + backups, err := cli.GetMongoDBBackups(args.ID) + if err != nil { + return err + } + printList(backups, 0, 0, 0, []string{}) + return nil + }) + } diff --git a/pkg/multicloud/zstack/offering.go b/pkg/multicloud/zstack/offering.go index 5c5d11e356..f6d0839eb1 100644 --- a/pkg/multicloud/zstack/offering.go +++ b/pkg/multicloud/zstack/offering.go @@ -166,6 +166,10 @@ func (offering *SInstanceOffering) GetPrepaidStatus() string { return api.SkuStatusSoldout } +func (offering *SInstanceOffering) GetCpuArch() string { + return "" +} + func (offering *SInstanceOffering) GetPostpaidStatus() string { return api.SkuStatusAvailable } diff --git a/pkg/util/netutils2/netutils.go b/pkg/util/netutils2/netutils.go index bc0a198027..7c1c519bda 100644 --- a/pkg/util/netutils2/netutils.go +++ b/pkg/util/netutils2/netutils.go @@ -234,7 +234,7 @@ func AddNicRoutes(routes *[][]string, nicDesc *types.SServerNic, mainIp string, } if len(nicDesc.Routes) > 0 { extendRoutes(routes, nicDesc.Routes) - } else if len(nicDesc.Gateway) > 0 && isExitAddress(nicDesc.Ip) && + } else if len(nicDesc.Gateway) > 0 && !isExitAddress(nicDesc.Ip) && nicCnt == 2 && nicDesc.Ip != mainIp && isExitAddress(mainIp) { for _, pref := range GetPrivatePrefixes(privatePrefixes) { addRoute(routes, pref, nicDesc.Gateway) diff --git a/pkg/util/netutils2/netutils_test.go b/pkg/util/netutils2/netutils_test.go index 479ecb45f0..de70e5baac 100644 --- a/pkg/util/netutils2/netutils_test.go +++ b/pkg/util/netutils2/netutils_test.go @@ -16,6 +16,8 @@ package netutils2 import ( "testing" + + "yunion.io/x/onecloud/pkg/cloudcommon/types" ) func TestNetlen2Mask(t *testing.T) { @@ -135,3 +137,71 @@ func TestMyDefault(t *testing.T) { } } } + +func TestGetMainNicFromDeployApi(t *testing.T) { + nics1 := []*types.SServerNic{ + { + Ip: "10.168.222.19", + Gateway: "10.168.222.1", + }, + { + Ip: "114.114.114.114", + Gateway: "114.114.114.1", + }, + } + nics2 := []*types.SServerNic{ + { + Ip: "10.168.222.19", + }, + { + Ip: "114.114.114.114", + Gateway: "114.114.114.1", + }, + } + nics3 := []*types.SServerNic{ + { + Ip: "10.168.222.19", + Gateway: "10.168.222.1", + }, + { + Ip: "114.114.114.114", + }, + } + nics4 := []*types.SServerNic{ + { + Ip: "10.168.222.19", + }, + { + Ip: "114.114.114.114", + }, + } + cases := []struct { + nics []*types.SServerNic + want *types.SServerNic + }{ + { + nics1, + nics1[1], + }, + { + nics2, + nics2[1], + }, + { + nics3, + nics3[0], + }, + { + nics4, + nics4[1], + }, + } + for _, c := range cases { + got, err := GetMainNicFromDeployApi(c.nics) + if err != nil { + t.Errorf("error %s", err) + } else if got != c.want { + t.Errorf("error: got %v want %v", got, c.want) + } + } +}