mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/yunionio/cloudpods.git
synced 2026-09-21 00:24:07 +08:00
1301 lines
46 KiB
Go
1301 lines
46 KiB
Go
// 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 regiondrivers
|
||
|
||
import (
|
||
"context"
|
||
"database/sql"
|
||
"fmt"
|
||
"time"
|
||
|
||
"yunion.io/x/jsonutils"
|
||
"yunion.io/x/log"
|
||
"yunion.io/x/pkg/errors"
|
||
"yunion.io/x/pkg/utils"
|
||
|
||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
|
||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||
"yunion.io/x/onecloud/pkg/compute/models"
|
||
"yunion.io/x/onecloud/pkg/httperrors"
|
||
"yunion.io/x/onecloud/pkg/mcclient"
|
||
"yunion.io/x/onecloud/pkg/util/rand"
|
||
)
|
||
|
||
type SManagedVirtualizationRegionDriver struct {
|
||
SVirtualizationRegionDriver
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
return self.ValidateManagerId(ctx, userCred, data)
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateManagerId(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
if managerId, _ := data.GetString("manager_id"); len(managerId) == 0 {
|
||
return nil, httperrors.NewMissingParameterError("manager")
|
||
}
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerAclData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
return self.ValidateManagerId(ctx, userCred, data)
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerCertificateData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
return self.ValidateManagerId(ctx, userCred, data)
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateUpdateLoadbalancerCertificateData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerBackendData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, backendType string, lb *models.SLoadbalancer, backendGroup *models.SLoadbalancerBackendGroup, backend db.IModel) (*jsonutils.JSONDict, error) {
|
||
if backendType != api.LB_BACKEND_GUEST {
|
||
return nil, httperrors.NewUnsupportOperationError("internal error: unexpected backend type %s", backendType)
|
||
}
|
||
guest := backend.(*models.SGuest)
|
||
host := guest.GetHost()
|
||
if host == nil {
|
||
return nil, fmt.Errorf("error getting host of guest %s", guest.GetId())
|
||
}
|
||
if lb == nil {
|
||
return nil, fmt.Errorf("error loadbalancer of backend group %s", backendGroup.GetId())
|
||
}
|
||
hostRegion := host.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)
|
||
}
|
||
address, err := models.LoadbalancerBackendManager.GetGuestAddress(guest)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
data.Set("address", jsonutils.NewString(address))
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateUpdateLoadbalancerBackendData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, lbbg *models.SLoadbalancerBackendGroup) (*jsonutils.JSONDict, error) {
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerBackendGroupData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, lb *models.SLoadbalancer, backends []cloudprovider.SLoadbalancerBackend) (*jsonutils.JSONDict, error) {
|
||
for _, backend := range backends {
|
||
if len(backend.ExternalID) == 0 {
|
||
return nil, httperrors.NewInputParameterError("invalid guest %s", backend.Name)
|
||
}
|
||
}
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerListenerRuleData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, data *jsonutils.JSONDict, backendGroup db.IModel) (*jsonutils.JSONDict, error) {
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateUpdateLoadbalancerListenerRuleData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, backendGroup db.IModel) (*jsonutils.JSONDict, error) {
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateLoadbalancerListenerData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, data *jsonutils.JSONDict, lb *models.SLoadbalancer, backendGroup db.IModel) (*jsonutils.JSONDict, error) {
|
||
_, err := self.ValidateManagerId(ctx, userCred, data)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if aclStatus, _ := data.GetString("acl_status"); aclStatus == api.LB_BOOL_ON {
|
||
aclId, _ := data.GetString("acl_id")
|
||
if len(aclId) == 0 {
|
||
return nil, httperrors.NewMissingParameterError("acl")
|
||
}
|
||
_, err = models.LoadbalancerAclManager.FetchById(aclId)
|
||
if err != nil {
|
||
if err == sql.ErrNoRows {
|
||
return nil, httperrors.NewResourceNotFoundError("failed to find acl %s", aclId)
|
||
}
|
||
return nil, httperrors.NewGeneralError(err)
|
||
}
|
||
}
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateUpdateLoadbalancerListenerData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, lblis *models.SLoadbalancerListener, backendGroup db.IModel) (*jsonutils.JSONDict, error) {
|
||
if listenerType, _ := data.GetString("listener_type"); len(listenerType) > 0 && lblis.ListenerType != listenerType {
|
||
return nil, httperrors.NewInputParameterError("cannot change loadbalancer listener listener_type")
|
||
}
|
||
if listenerPort, _ := data.Int("listener_port"); listenerPort != 0 && listenerPort != int64(lblis.ListenerPort) {
|
||
return nil, httperrors.NewInputParameterError("cannot change loadbalancer listener listener_port")
|
||
}
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateDeleteLoadbalancerCondition(ctx context.Context, lb *models.SLoadbalancer) error {
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateDeleteLoadbalancerBackendCondition(ctx context.Context, lbb *models.SLoadbalancerBackend) error {
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateDeleteLoadbalancerBackendGroupCondition(ctx context.Context, lbbg *models.SLoadbalancerBackendGroup) error {
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) GetBackendStatusForAdd() []string {
|
||
return []string{api.VM_RUNNING}
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancer(ctx context.Context, userCred mcclient.TokenCredential, lb *models.SLoadbalancer, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
params, err := lb.GetCreateLoadbalancerParams(iRegion)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.CreateILoadBalancer(params)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := db.SetExternalId(lb, userCred, iLoadbalancer.GetGlobalId()); err != nil {
|
||
return nil, err
|
||
}
|
||
if err := lb.SyncWithCloudLoadbalancer(ctx, userCred, iLoadbalancer, nil); err != nil {
|
||
return nil, err
|
||
}
|
||
//公网lb,需要同步public ip
|
||
if lb.AddressType == api.LB_ADDR_TYPE_INTERNET {
|
||
publicIp, err := iLoadbalancer.GetIEIP()
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "iLoadbalancer.GetIEIP()")
|
||
}
|
||
lb.SyncLoadbalancerEip(ctx, userCred, lb.GetCloudprovider(), publicIp)
|
||
}
|
||
|
||
lbbgs, err := iLoadbalancer.GetILoadBalancerBackendGroups()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(lbbgs) > 0 {
|
||
provider := lb.GetCloudprovider()
|
||
if provider == nil {
|
||
return nil, fmt.Errorf("failed to find cloudprovider for lb %s", lb.Name)
|
||
}
|
||
models.LoadbalancerBackendGroupManager.SyncLoadbalancerBackendgroups(ctx, userCred, provider, lb, lbbgs, &models.SSyncRange{})
|
||
}
|
||
return nil, nil
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestStartLoadbalancer(ctx context.Context, userCred mcclient.TokenCredential, lb *models.SLoadbalancer, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancer.Start()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestStopLoadbalancer(ctx context.Context, userCred mcclient.TokenCredential, lb *models.SLoadbalancer, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancer.Stop()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestSyncstatusLoadbalancer(ctx context.Context, userCred mcclient.TokenCredential, lb *models.SLoadbalancer, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
status := iLoadbalancer.GetStatus()
|
||
if utils.IsInStringArray(status, []string{api.LB_STATUS_ENABLED, api.LB_STATUS_DISABLED}) {
|
||
return nil, lb.SetStatus(userCred, status, "")
|
||
}
|
||
return nil, fmt.Errorf("Unknown loadbalancer status %s", status)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancer(ctx context.Context, userCred mcclient.TokenCredential, lb *models.SLoadbalancer, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
if jsonutils.QueryBoolean(task.GetParams(), "purge", false) {
|
||
return nil, nil
|
||
}
|
||
|
||
if len(lb.ExternalId) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancer.Delete()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) createLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, lbacl *models.SCachedLoadbalancerAcl) (jsonutils.JSONObject, error) {
|
||
iRegion, err := lbacl.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
acl := &cloudprovider.SLoadbalancerAccessControlList{
|
||
Name: lbacl.Name,
|
||
Entrys: []cloudprovider.SLoadbalancerAccessControlListEntry{},
|
||
}
|
||
|
||
lblis, err := lbacl.GetListener()
|
||
if err == nil {
|
||
if api.LB_BOOL_ON == lblis.AclStatus {
|
||
acl.AccessControlEnable = true
|
||
}
|
||
} else {
|
||
return nil, fmt.Errorf("regionDriver.createLoadbalancerAcl %s", err)
|
||
}
|
||
|
||
_originAcl, err := db.FetchById(models.LoadbalancerAclManager, lbacl.AclId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.FetchAcl")
|
||
}
|
||
|
||
originAcl := _originAcl.(*models.SLoadbalancerAcl)
|
||
if originAcl.AclEntries != nil {
|
||
for _, entry := range *originAcl.AclEntries {
|
||
acl.Entrys = append(acl.Entrys, cloudprovider.SLoadbalancerAccessControlListEntry{CIDR: entry.Cidr, Comment: entry.Comment})
|
||
}
|
||
}
|
||
iLoadbalancerAcl, err := iRegion.CreateILoadBalancerAcl(acl)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := db.SetExternalId(lbacl, userCred, iLoadbalancerAcl.GetGlobalId()); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, lbacl.SyncWithCloudLoadbalancerAcl(ctx, userCred, iLoadbalancerAcl, lbacl.GetOwnerId())
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, lbacl *models.SCachedLoadbalancerAcl, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
return self.createLoadbalancerAcl(ctx, userCred, lbacl)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) syncLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, lbacl *models.SCachedLoadbalancerAcl) (jsonutils.JSONObject, error) {
|
||
iRegion, err := lbacl.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
acl := &cloudprovider.SLoadbalancerAccessControlList{
|
||
Name: lbacl.Name,
|
||
Entrys: []cloudprovider.SLoadbalancerAccessControlListEntry{},
|
||
}
|
||
|
||
lblis, err := lbacl.GetListener()
|
||
if err == nil {
|
||
if api.LB_BOOL_ON == lblis.AclStatus {
|
||
acl.AccessControlEnable = true
|
||
}
|
||
} else {
|
||
return nil, fmt.Errorf("regionDriver.syncLoadbalancerAcl %s", err)
|
||
}
|
||
|
||
if lbacl.AclEntries != nil {
|
||
for _, entry := range *lbacl.AclEntries {
|
||
acl.Entrys = append(acl.Entrys, cloudprovider.SLoadbalancerAccessControlListEntry{CIDR: entry.Cidr, Comment: entry.Comment})
|
||
}
|
||
}
|
||
lockman.LockRawObject(ctx, "acl", lbacl.Id)
|
||
defer lockman.ReleaseRawObject(ctx, "acl", lbacl.Id)
|
||
|
||
iLoadbalancerAcl, err := iRegion.GetILoadBalancerAclById(lbacl.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancerAcl.Sync(acl)
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestSyncLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, lbacl *models.SCachedLoadbalancerAcl, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
return self.syncLoadbalancerAcl(ctx, userCred, lbacl)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) deleteLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, lbacl *models.SCachedLoadbalancerAcl, task taskman.ITask) (jsonutils.JSONObject, error) {
|
||
if jsonutils.QueryBoolean(task.GetParams(), "purge", false) {
|
||
return nil, nil
|
||
}
|
||
iRegion, err := lbacl.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if len(lbacl.ExternalId) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
iLoadbalancerAcl, err := iRegion.GetILoadBalancerAclById(lbacl.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancerAcl.Delete()
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerAcl(ctx context.Context, userCred mcclient.TokenCredential, lbacl *models.SCachedLoadbalancerAcl, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
return self.deleteLoadbalancerAcl(ctx, userCred, lbacl, task)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) createLoadbalancerCertificate(ctx context.Context, userCred mcclient.TokenCredential, lbcert *models.SCachedLoadbalancerCertificate) (jsonutils.JSONObject, error) {
|
||
iRegion, err := lbcert.GetIRegion()
|
||
if err != nil {
|
||
return nil, errors.Wrapf(err, "lbcert.GetIRegion")
|
||
}
|
||
certificate := &cloudprovider.SLoadbalancerCertificate{
|
||
Name: fmt.Sprintf("%s-%s", lbcert.Name, rand.String(4)),
|
||
PrivateKey: lbcert.PrivateKey,
|
||
Certificate: lbcert.Certificate,
|
||
}
|
||
iLoadbalancerCert, err := iRegion.CreateILoadBalancerCertificate(certificate)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "iRegion.CreateILoadBalancerCertificate")
|
||
}
|
||
lbcert.SetModelManager(models.CachedLoadbalancerCertificateManager, lbcert)
|
||
if err := db.SetExternalId(lbcert, userCred, iLoadbalancerCert.GetGlobalId()); err != nil {
|
||
return nil, errors.Wrap(err, "db.SetExternalId")
|
||
}
|
||
return nil, lbcert.SyncWithCloudLoadbalancerCertificate(ctx, userCred, iLoadbalancerCert, lbcert.GetOwnerId())
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerCertificate(ctx context.Context, userCred mcclient.TokenCredential, lbcert *models.SCachedLoadbalancerCertificate, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
return self.createLoadbalancerCertificate(ctx, userCred, lbcert)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerCertificate(ctx context.Context, userCred mcclient.TokenCredential, lbcert *models.SCachedLoadbalancerCertificate, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
if jsonutils.QueryBoolean(task.GetParams(), "purge", false) {
|
||
return nil, nil
|
||
}
|
||
iRegion, err := lbcert.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancerCert, err := iRegion.GetILoadBalancerCertificateById(lbcert.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancerCert.Delete()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerBackendGroup(ctx context.Context, userCred mcclient.TokenCredential, lbbg *models.SLoadbalancerBackendGroup, backends []cloudprovider.SLoadbalancerBackend, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
iRegion, err := lbbg.GetIRegion()
|
||
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)
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
group := &cloudprovider.SLoadbalancerBackendGroup{
|
||
Name: lbbg.Name,
|
||
GroupType: lbbg.Type,
|
||
Backends: backends,
|
||
}
|
||
iLoadbalancerBackendGroup, err := iLoadbalancer.CreateILoadBalancerBackendGroup(group)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := db.SetExternalId(lbbg, userCred, iLoadbalancerBackendGroup.GetGlobalId()); err != nil {
|
||
return nil, err
|
||
}
|
||
iBackends, err := iLoadbalancerBackendGroup.GetILoadbalancerBackends()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(iBackends) > 0 {
|
||
provider := loadbalancer.GetCloudprovider()
|
||
if provider == nil {
|
||
return nil, fmt.Errorf("failed to find cloudprovider for lb %s", loadbalancer.Name)
|
||
}
|
||
models.LoadbalancerBackendManager.SyncLoadbalancerBackends(ctx, userCred, provider, lbbg, iBackends, &models.SSyncRange{})
|
||
}
|
||
return nil, nil
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerBackendGroup(ctx context.Context, userCred mcclient.TokenCredential, lbbg *models.SLoadbalancerBackendGroup, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
if jsonutils.QueryBoolean(task.GetParams(), "purge", false) {
|
||
return nil, nil
|
||
}
|
||
iRegion, err := lbbg.GetIRegion()
|
||
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)
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
if len(lbbg.ExternalId) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
iLoadbalancerBackendGroup, err := iLoadbalancer.GetILoadBalancerBackendGroupById(lbbg.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
err = iLoadbalancerBackendGroup.Delete()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
cachedLbbgs, err := models.AwsCachedLbbgManager.GetCachedBackendGroups(lbbg.GetId())
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
for i := range cachedLbbgs {
|
||
err = cachedLbbgs[i].Delete(ctx, userCred)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
|
||
return nil, nil
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestSyncLoadbalancerBackendGroup(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, lbbg *models.SLoadbalancerBackendGroup, task taskman.ITask) error {
|
||
task.ScheduleRun(nil)
|
||
return nil
|
||
}
|
||
|
||
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)
|
||
}
|
||
lb := lbbg.GetLoadbalancer()
|
||
if lb == nil {
|
||
return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name)
|
||
}
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancerBackendGroup, err := iLoadbalancer.GetILoadBalancerBackendGroupById(lbbg.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
guest := lbb.GetGuest()
|
||
if guest == nil {
|
||
return nil, fmt.Errorf("failed to find guest for lbb %s", lbb.Name)
|
||
}
|
||
iLoadbalancerBackend, err := iLoadbalancerBackendGroup.AddBackendServer(guest.ExternalId, lbb.Weight, lbb.Port)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := db.SetExternalId(lbb, userCred, iLoadbalancerBackend.GetGlobalId()); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, lbb.SyncWithCloudLoadbalancerBackend(ctx, userCred, iLoadbalancerBackend, nil)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerBackend(ctx context.Context, userCred mcclient.TokenCredential, lbb *models.SLoadbalancerBackend, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
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)
|
||
}
|
||
lb := lbbg.GetLoadbalancer()
|
||
if lb == nil {
|
||
return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name)
|
||
}
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancerBackendGroup, err := iLoadbalancer.GetILoadBalancerBackendGroupById(lbbg.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
guest := lbb.GetGuest()
|
||
if guest == nil {
|
||
log.Warningf("failed to find guest for lbb %s", lbb.Name)
|
||
return nil, nil
|
||
}
|
||
_, err = guest.GetIVM()
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
return nil, iLoadbalancerBackendGroup.RemoveBackendServer(guest.ExternalId, lbb.Weight, lbb.Port)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
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)
|
||
}
|
||
lb := lbbg.GetLoadbalancer()
|
||
if lb == nil {
|
||
return nil, fmt.Errorf("failed to find lb for backendgroup %s", lbbg.Name)
|
||
}
|
||
iRegion, err := lb.GetIRegion()
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerBackend.GetIRegion")
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(lb.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerBackend.GetILoadBalancerById")
|
||
}
|
||
iLoadbalancerBackendGroup, err := iLoadbalancer.GetILoadBalancerBackendGroupById(lbbg.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerBackend.GetILoadBalancerBackendGroupById")
|
||
}
|
||
|
||
iBackend, err := iLoadbalancerBackendGroup.GetILoadbalancerBackendById(lbb.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerBackend.GetILoadbalancerBackendById")
|
||
}
|
||
|
||
err = iBackend.SyncConf(lbb.Port, lbb.Weight)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerBackend.SyncConf")
|
||
}
|
||
|
||
iBackend, err = iLoadbalancerBackendGroup.GetILoadbalancerBackendById(lbb.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerBackend.GetILoadbalancerBackendById")
|
||
}
|
||
|
||
return nil, lbb.SyncWithCloudLoadbalancerBackend(ctx, userCred, iBackend, nil)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCreateLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
{
|
||
certId, _ := task.GetParams().GetString("certificate_id")
|
||
if len(certId) > 0 {
|
||
provider := models.CloudproviderManager.FetchCloudproviderById(lblis.ManagerId)
|
||
if provider == nil {
|
||
return nil, fmt.Errorf("failed to find provider for lblis %s", lblis.Name)
|
||
}
|
||
|
||
cert, err := models.LoadbalancerCertificateManager.FetchById(certId)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(err, "LoadbalancerCertificateManager.FetchById(%s)", certId)
|
||
}
|
||
|
||
lbcert, err := models.CachedLoadbalancerCertificateManager.GetOrCreateCachedCertificate(ctx, userCred, provider, lblis, cert.(*models.SLoadbalancerCertificate))
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "CachedLoadbalancerCertificateManager.GetOrCreateCachedCertificate")
|
||
}
|
||
|
||
if len(lbcert.ExternalId) == 0 {
|
||
_, err = self.createLoadbalancerCertificate(ctx, userCred, lbcert)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "createLoadbalancerCertificate")
|
||
}
|
||
}
|
||
|
||
_, err = db.Update(lblis, func() error {
|
||
lblis.CachedCertificateId = lbcert.GetId()
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestCreateLoadbalancerListener.UpdateCachedCertificateId")
|
||
}
|
||
}
|
||
}
|
||
|
||
{
|
||
aclId, _ := task.GetParams().GetString("acl_id")
|
||
if len(aclId) > 0 {
|
||
provider := models.CloudproviderManager.FetchCloudproviderById(lblis.ManagerId)
|
||
if provider == nil {
|
||
return nil, fmt.Errorf("failed to find provider for lblis %s", lblis.Name)
|
||
}
|
||
|
||
acl, err := models.LoadbalancerAclManager.FetchById(aclId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "LoadbalancerAclManager.FetchById")
|
||
}
|
||
|
||
lbacl, err := models.CachedLoadbalancerAclManager.GetOrCreateCachedAcl(ctx, userCred, provider, lblis, acl.(*models.SLoadbalancerAcl))
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "CachedLoadbalancerAclManager.GetOrCreateCachedAcl")
|
||
}
|
||
|
||
if len(lbacl.ExternalId) == 0 {
|
||
_, err = self.createLoadbalancerAcl(ctx, userCred, lbacl)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "createLoadbalancerAcl")
|
||
}
|
||
}
|
||
|
||
_, err = db.Update(lblis, func() error {
|
||
lblis.CachedAclId = lbacl.GetId()
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestCreateLoadbalancerListener.UpdateCachedAclId")
|
||
}
|
||
}
|
||
}
|
||
|
||
params, err := lblis.GetLoadbalancerListenerParams()
|
||
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)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "loadbalancer.GetIRegion")
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(err, "iRegion.GetILoadBalancerById(%s)", loadbalancer.ExternalId)
|
||
}
|
||
iListener, err := iLoadbalancer.CreateILoadBalancerListener(params)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "iLoadbalancer.CreateILoadBalancerListener")
|
||
}
|
||
if err := db.SetExternalId(lblis, userCred, iListener.GetGlobalId()); err != nil {
|
||
return nil, errors.Wrap(err, "db.SetExternalId")
|
||
}
|
||
return nil, lblis.SyncWithCloudLoadbalancerListener(ctx, userCred, loadbalancer, iListener, nil)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
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)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if len(loadbalancer.ExternalId) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(lblis.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
return nil, iListener.Delete()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
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)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(lblis.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, iListener.Start()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestSyncLoadbalancerListener(ctx context.Context, userCred mcclient.TokenCredential, lblis *models.SLoadbalancerListener, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
{
|
||
certId, _ := task.GetParams().GetString("certificate_id")
|
||
if len(certId) > 0 {
|
||
provider := models.CloudproviderManager.FetchCloudproviderById(lblis.ManagerId)
|
||
if provider == nil {
|
||
return nil, fmt.Errorf("failed to find provider for lblis %s", lblis.Name)
|
||
}
|
||
|
||
cert, err := models.LoadbalancerCertificateManager.FetchById(certId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.FetchCert")
|
||
}
|
||
|
||
lbcert, err := models.CachedLoadbalancerCertificateManager.GetOrCreateCachedCertificate(ctx, userCred, provider, lblis, cert.(*models.SLoadbalancerCertificate))
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetCert")
|
||
}
|
||
|
||
if len(lbcert.ExternalId) == 0 {
|
||
_, err = self.createLoadbalancerCertificate(ctx, userCred, lbcert)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.CreateCert")
|
||
}
|
||
}
|
||
|
||
_, err = db.Update(lblis, func() error {
|
||
lblis.CachedCertificateId = lbcert.GetId()
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.UpdateCachedCertificateId")
|
||
}
|
||
}
|
||
}
|
||
|
||
{
|
||
aclId, _ := task.GetParams().GetString("acl_id")
|
||
if len(aclId) > 0 {
|
||
provider := models.CloudproviderManager.FetchCloudproviderById(lblis.ManagerId)
|
||
if provider == nil {
|
||
return nil, fmt.Errorf("failed to find provider for lblis %s", lblis.Name)
|
||
}
|
||
|
||
var lbacl *models.SCachedLoadbalancerAcl
|
||
// 先读取缓存,缓存不存在的情况下,从ACL表中取数据创建缓存
|
||
if _lbacl, err := models.CachedLoadbalancerAclManager.FetchById(aclId); err == nil && _lbacl != nil {
|
||
lbacl = _lbacl.(*models.SCachedLoadbalancerAcl)
|
||
} else {
|
||
acl, err := models.LoadbalancerAclManager.FetchById(aclId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.FetchAcl")
|
||
}
|
||
|
||
lbacl, err = models.CachedLoadbalancerAclManager.GetOrCreateCachedAcl(ctx, userCred, provider, lblis, acl.(*models.SLoadbalancerAcl))
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetAcl")
|
||
}
|
||
}
|
||
|
||
if len(lbacl.ExternalId) == 0 {
|
||
_, err := self.createLoadbalancerAcl(ctx, userCred, lbacl)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.CreateAcl")
|
||
}
|
||
}
|
||
|
||
_, err := db.Update(lblis, func() error {
|
||
lblis.CachedAclId = lbacl.GetId()
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.UpdateCachedAclId")
|
||
}
|
||
}
|
||
}
|
||
|
||
params, err := lblis.GetLoadbalancerListenerParams()
|
||
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)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetIRegion")
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetILoadbalancer")
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(lblis.ExternalId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.GetIListener")
|
||
}
|
||
if err := iListener.Sync(params); err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.SyncListener")
|
||
}
|
||
if err := iListener.Refresh(); err != nil {
|
||
return nil, errors.Wrap(err, "regionDriver.RequestSyncLoadbalancerListener.RefreshListener")
|
||
}
|
||
return nil, lblis.SyncWithCloudLoadbalancerListener(ctx, userCred, loadbalancer, iListener, nil)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
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)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(lblis.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, iListener.Stop()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
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)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(lblis.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
status := iListener.GetStatus()
|
||
if utils.IsInStringArray(status, []string{api.LB_STATUS_ENABLED, api.LB_STATUS_DISABLED}) {
|
||
return nil, lblis.SetStatus(userCred, status, "")
|
||
}
|
||
return nil, fmt.Errorf("Unknown loadbalancer listener status %s", status)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
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)
|
||
}
|
||
loadbalancer := listener.GetLoadbalancer()
|
||
if loadbalancer == nil {
|
||
return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(listener.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
rule := &cloudprovider.SLoadbalancerListenerRule{
|
||
Name: lbr.Name,
|
||
Domain: lbr.Domain,
|
||
Path: lbr.Path,
|
||
}
|
||
if len(lbr.BackendGroupId) > 0 {
|
||
group := lbr.GetLoadbalancerBackendGroup()
|
||
if group == nil {
|
||
return nil, fmt.Errorf("failed to find backend group for listener rule %s", lbr.Name)
|
||
}
|
||
rule.BackendGroupID = group.ExternalId
|
||
rule.BackendGroupType = group.Type
|
||
}
|
||
iListenerRule, err := iListener.CreateILoadBalancerListenerRule(rule)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := db.SetExternalId(lbr, userCred, iListenerRule.GetGlobalId()); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, lbr.SyncWithCloudLoadbalancerListenerRule(ctx, userCred, iListenerRule, nil)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteLoadbalancerListenerRule(ctx context.Context, userCred mcclient.TokenCredential, lbr *models.SLoadbalancerListenerRule, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
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)
|
||
}
|
||
loadbalancer := listener.GetLoadbalancer()
|
||
if loadbalancer == nil {
|
||
return nil, fmt.Errorf("failed to find loadbalancer for listener %s", listener.Name)
|
||
}
|
||
iRegion, err := loadbalancer.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iLoadbalancer, err := iRegion.GetILoadBalancerById(loadbalancer.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
iListener, err := iLoadbalancer.GetILoadBalancerListenerById(listener.ExternalId)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(lbr.ExternalId) == 0 {
|
||
return nil, nil
|
||
}
|
||
iListenerRule, err := iListener.GetILoadBalancerListenerRuleById(lbr.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
return nil, iListenerRule.Delete()
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateVpcData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||
return data, nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestUpdateSnapshotPolicy(ctx context.Context, userCred mcclient.
|
||
TokenCredential, sp *models.SSnapshotPolicy, input cloudprovider.SnapshotPolicyInput, task taskman.ITask) error {
|
||
// it's too cumbersome to pass parameters in taskman, so change a simple way for the moment
|
||
|
||
//spcache, err := models.SnapshotPolicyCacheManager.FetchSnapshotPolicyCache(sp.GetId(), sp.CloudregionId, sp.ManagerId)
|
||
//if err != nil {
|
||
// return errors.Wrapf(err, "Fetch cache ofsnapshotpolicy %s", sp.GetId())
|
||
//}
|
||
//return spcache.UpdateCloudSnapshotPolicy(&input)
|
||
|
||
return nil
|
||
}
|
||
|
||
// RequestApplySnapshotPolicy apply snapshotpolicy for public cloud.
|
||
// In our system, one disk only can hava one snapshot policy attached.
|
||
// Default, some public cloud such as Aliyun is same with us and this function shoule be used for these public cloud.
|
||
// But in Some public cloud such as Qcloud different with us,
|
||
// we should wirte a new function in corressponding regiondriver which detach all snapshotpolicy of disk after
|
||
// attache new one.
|
||
// You can refer to the implementations of function SQcloudRegionDriver.RequestApplySnapshotPolicy().
|
||
func (self *SManagedVirtualizationRegionDriver) RequestApplySnapshotPolicy(ctx context.Context,
|
||
userCred mcclient.TokenCredential, task taskman.ITask, disk *models.SDisk, sp *models.SSnapshotPolicy,
|
||
data jsonutils.JSONObject) error {
|
||
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
|
||
regionId := disk.GetStorage().GetRegion().GetId()
|
||
providerId := disk.GetStorage().ManagerId
|
||
spcache, err := models.SnapshotPolicyCacheManager.Register(ctx, userCred, sp.GetId(), regionId, providerId)
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "registersnapshotpolicy cache failed")
|
||
}
|
||
|
||
iRegion, err := disk.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
err = iRegion.ApplySnapshotPolicyToDisks(spcache.GetExternalId(), disk.GetExternalId())
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
data := jsonutils.NewDict()
|
||
data.Add(jsonutils.NewString(sp.GetId()), "snapshotpolicy_id")
|
||
return data, nil
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCancelSnapshotPolicy(ctx context.Context, userCred mcclient.
|
||
TokenCredential, task taskman.ITask, disk *models.SDisk, sp *models.SSnapshotPolicy, data jsonutils.JSONObject) error {
|
||
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
|
||
regionId := disk.GetStorage().GetRegion().GetId()
|
||
providerId := disk.GetStorage().ManagerId
|
||
spcache, err := models.SnapshotPolicyCacheManager.FetchSnapshotPolicyCache(sp.GetId(), regionId, providerId)
|
||
|
||
if err != nil {
|
||
return nil, errors.Wrap(err, "registersnapshotpolicy cache failed")
|
||
}
|
||
|
||
iRegion, err := spcache.GetIRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
data := jsonutils.NewDict()
|
||
data.Add(jsonutils.NewString(sp.GetId()), "snapshotpolicy_id")
|
||
err = iRegion.CancelSnapshotPolicyToDisks(spcache.GetExternalId(), disk.GetExternalId())
|
||
if err == cloudprovider.ErrNotFound {
|
||
return data, nil
|
||
}
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return data, nil
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateSnapshotDelete(ctx context.Context, snapshot *models.SSnapshot) error {
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestDeleteSnapshot(ctx context.Context, snapshot *models.SSnapshot, task taskman.ITask) error {
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
cloudRegion, err := snapshot.GetISnapshotRegion()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
cloudSnapshot, err := cloudRegion.GetISnapshotById(snapshot.ExternalId)
|
||
if err != nil {
|
||
if err == cloudprovider.ErrNotFound {
|
||
return nil, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
if err := cloudSnapshot.Delete(); err != nil {
|
||
return nil, err
|
||
}
|
||
if err := cloudprovider.WaitDeleted(cloudSnapshot, 10*time.Second, 300*time.Second); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, nil
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateSnapshotCreate(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, data *jsonutils.JSONDict) error {
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestCreateSnapshot(ctx context.Context, snapshot *models.SSnapshot, task taskman.ITask) error {
|
||
disk, err := snapshot.GetDisk()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
iDisk, err := disk.GetIDisk()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
iSnapshot, err := iDisk.CreateISnapshot(ctx, snapshot.Name, "")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
_, err = db.Update(snapshot, func() error {
|
||
snapshot.Size = int(iSnapshot.GetSizeMb())
|
||
snapshot.ExternalId = iSnapshot.GetGlobalId()
|
||
return nil
|
||
})
|
||
return nil, err
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) GetDiskResetParams(snapshot *models.SSnapshot) *jsonutils.JSONDict {
|
||
params := jsonutils.NewDict()
|
||
params.Set("snapshot_id", jsonutils.NewString(snapshot.ExternalId))
|
||
return params
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) OnDiskReset(ctx context.Context, userCred mcclient.TokenCredential,
|
||
disk *models.SDisk, snapshot *models.SSnapshot, data jsonutils.JSONObject) error {
|
||
|
||
externalId, _ := data.GetString("exteranl_disk_id")
|
||
if len(externalId) > 0 {
|
||
_, err := db.Update(disk, func() error {
|
||
disk.ExternalId = externalId
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
iDisk, err := disk.GetIDisk()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
err = iDisk.Refresh()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if disk.DiskSize != iDisk.GetDiskSizeMB() {
|
||
_, err := db.Update(disk, func() error {
|
||
disk.DiskSize = iDisk.GetDiskSizeMB()
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) ValidateCreateSnapshopolicyDiskData(ctx context.Context,
|
||
userCred mcclient.TokenCredential, disk *models.SDisk, snapshotPolicy *models.SSnapshotPolicy) error {
|
||
|
||
err := self.SBaseRegionDriver.ValidateCreateSnapshopolicyDiskData(ctx, userCred, disk, snapshotPolicy)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if snapshotPolicy.RetentionDays < -1 || snapshotPolicy.RetentionDays == 0 || snapshotPolicy.RetentionDays > 65535 {
|
||
return httperrors.NewInputParameterError("Retention days must in 1~65535 or -1")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) OnSnapshotDelete(ctx context.Context, snapshot *models.SSnapshot, task taskman.ITask, data jsonutils.JSONObject) error {
|
||
task.SetStage("OnManagedSnapshotDelete", nil)
|
||
task.ScheduleRun(data)
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) DealNatGatewaySpec(spec string) string {
|
||
return spec
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestBindIPToNatgateway(ctx context.Context, task taskman.ITask,
|
||
natgateway *models.SNatGateway, needBind bool, eipID string) error {
|
||
|
||
task.ScheduleRun(nil)
|
||
return nil
|
||
}
|
||
|
||
func (self *SManagedVirtualizationRegionDriver) RequestPreSnapshotPolicyApply(ctx context.Context, userCred mcclient.
|
||
TokenCredential, task taskman.ITask, disk *models.SDisk, sp *models.SSnapshotPolicy, data jsonutils.JSONObject) error {
|
||
|
||
taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
|
||
|
||
return data, nil
|
||
})
|
||
return nil
|
||
}
|