mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
loadbalancer cache create api
This commit is contained in:
@@ -91,6 +91,29 @@ func init() {
|
||||
printLbAclList(result, modules.LoadbalancerCachedAcls.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type LoadbalancerCachedAclCreateOptions struct {
|
||||
CLOUDPROVIDER string `help:"cloud provider id"`
|
||||
CLOUDREGION string `help:"cloud region id"`
|
||||
ACL string `help:"acl id"`
|
||||
Listener string `help:"cloud listener id, required by huawei"`
|
||||
}
|
||||
|
||||
R(&LoadbalancerCachedAclCreateOptions{}, "lbacl-cache-create", "Create cached lbacl", func(s *mcclient.ClientSession, opts *LoadbalancerCachedAclCreateOptions) error {
|
||||
params, err := options.StructToParams(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lbacl, err := modules.LoadbalancerCachedAcls.Create(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
printObject(lbacl)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&options.LoadbalancerAclDeleteOptions{}, "lbacl-cache-purge", "Purge cached lbacl", func(s *mcclient.ClientSession, opts *options.LoadbalancerAclDeleteOptions) error {
|
||||
lbacl, err := modules.LoadbalancerCachedAcls.PerformAction(s, opts.ID, "purge", nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -47,6 +47,28 @@ func init() {
|
||||
printList(result, modules.LoadbalancerCachedCertificates.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type LoadbalancerCachedCertificateCreateOptions struct {
|
||||
CLOUDPROVIDER string `help:"cloud provider id"`
|
||||
CLOUDREGION string `help:"cloud region id"`
|
||||
CERTIFICATE string `help:"certificate id"`
|
||||
}
|
||||
|
||||
R(&LoadbalancerCachedCertificateCreateOptions{}, "lbcert-cache-create", "Create cached lbcert", func(s *mcclient.ClientSession, opts *LoadbalancerCachedCertificateCreateOptions) error {
|
||||
params, err := options.StructToParams(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lbcert, err := modules.LoadbalancerCachedCertificates.Create(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
printObject(lbcert)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&options.LoadbalancerCertificateDeleteOptions{}, "lbcert-cache-delete", "Delete cached lbcert", func(s *mcclient.ClientSession, opts *options.LoadbalancerCertificateDeleteOptions) error {
|
||||
lbcert, err := modules.LoadbalancerCachedCertificates.Delete(s, opts.ID, nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -60,35 +60,11 @@ type SCachedLoadbalancerAcl struct {
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
AclId string `width:"128" charset:"ascii" nullable:"false" index:"true" list:"user"` // 本地ACL ID
|
||||
AclEntries *SLoadbalancerAclEntries `list:"user" update:"user" create:"required"`
|
||||
AclId string `width:"128" charset:"ascii" nullable:"false" index:"true" list:"user" create:"required"` // 本地ACL ID
|
||||
AclEntries *SLoadbalancerAclEntries `list:"user" update:"user" nullable:"true" create:"optional"`
|
||||
ListenerId string `width:"36" charset:"ascii" nullable:"true" list:"user" create:"optional"` // huawei only
|
||||
}
|
||||
|
||||
func (man *SCachedLoadbalancerAclManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerProjId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
data, err := loadbalancerAclsValidateAclEntries(data, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := man.SVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerProjId, query, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
managerIdV := validators.NewModelIdOrNameValidator("manager", "cloudprovider", ownerProjId)
|
||||
managerIdV.Optional(true)
|
||||
if err := managerIdV.Validate(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
regionV := validators.NewModelIdOrNameValidator("cloudregion", "cloudregion", ownerProjId)
|
||||
regionV.Default("default")
|
||||
if err := regionV.Validate(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
region := regionV.Model.(*SCloudregion)
|
||||
return region.GetDriver().ValidateCreateLoadbalancerAclData(ctx, userCred, data)
|
||||
}
|
||||
|
||||
func (lbacl *SCachedLoadbalancerAcl) AllowPerformStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return false
|
||||
}
|
||||
@@ -116,6 +92,56 @@ func (lbacl *SCachedLoadbalancerAcl) StartLoadBalancerAclSyncTask(ctx context.Co
|
||||
return nil
|
||||
}
|
||||
|
||||
func (man *SCachedLoadbalancerAclManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
aclV := validators.NewModelIdOrNameValidator("acl", "loadbalanceracl", ownerId)
|
||||
regionV := validators.NewModelIdOrNameValidator("cloudregion", "cloudregion", ownerId)
|
||||
providerV := validators.NewModelIdOrNameValidator("cloudprovider", "cloudprovider", ownerId)
|
||||
keyV := map[string]validators.IValidator{
|
||||
"acl": aclV,
|
||||
"cloudregion": regionV,
|
||||
"cloudprovider": providerV,
|
||||
}
|
||||
|
||||
for _, v := range keyV {
|
||||
if err := v.Validate(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if providerV.Model.(*SCloudprovider).Provider == api.CLOUD_PROVIDER_HUAWEI {
|
||||
listenerV := validators.NewModelIdOrNameValidator("listener", "loadbalancerlistener", ownerId)
|
||||
if err := listenerV.Validate(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
data.Remove("listener_id")
|
||||
}
|
||||
|
||||
q := man.Query().Equals("acl_id", aclV.Model.GetId()).Equals("cloudregion_id", regionV.Model.GetId()).IsFalse("deleted")
|
||||
if listener, _ := data.GetString("listener_id"); len(listener) > 0 {
|
||||
q.Equals("listener_id", listener)
|
||||
}
|
||||
|
||||
count, err := q.CountWithError()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return nil, httperrors.NewDuplicateResourceError("the acl cache in region %s aready exists.", regionV.Model.GetId())
|
||||
}
|
||||
|
||||
provider := providerV.Model.(*SCloudprovider)
|
||||
data.Set("manager_id", jsonutils.NewString(provider.Id))
|
||||
name, _ := db.GenerateName(man, ownerId, aclV.Model.GetName())
|
||||
data.Set("name", jsonutils.NewString(name))
|
||||
if _, err := man.SVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (lbacl *SCachedLoadbalancerAcl) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerProjId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
lbacl.SSharableVirtualResourceBase.PostCreate(ctx, userCred, ownerProjId, query, data)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/validators"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -60,7 +61,7 @@ type SCachedLoadbalancerCertificate struct {
|
||||
SManagedResourceBase // 云账号ID
|
||||
SCloudregionResourceBase // Region ID
|
||||
|
||||
CertificateId string `width:"128" charset:"ascii" nullable:"false" index:"true" list:"user"` // 本地证书ID
|
||||
CertificateId string `width:"128" charset:"ascii" nullable:"false" create:"required" index:"true" list:"user"` // 本地证书ID
|
||||
Certificate string `create:"required" list:"user" update:"user"`
|
||||
PrivateKey string `create:"required" list:"admin" update:"user"`
|
||||
|
||||
@@ -136,7 +137,58 @@ func (lbcert *SCachedLoadbalancerCertificate) StartLoadBalancerCertificateDelete
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lbcert *SCachedLoadbalancerCertificate) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerProjId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
func (man *SCachedLoadbalancerCertificateManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
certificateV := validators.NewModelIdOrNameValidator("certificate", "loadbalancercertificate", ownerId)
|
||||
regionV := validators.NewModelIdOrNameValidator("cloudregion", "cloudregion", ownerId)
|
||||
providerV := validators.NewModelIdOrNameValidator("cloudprovider", "cloudprovider", ownerId)
|
||||
keyV := map[string]validators.IValidator{
|
||||
"certificate": certificateV,
|
||||
"cloudregion": regionV,
|
||||
"cloudprovider": providerV,
|
||||
}
|
||||
|
||||
for _, v := range keyV {
|
||||
if err := v.Validate(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// validate local cert
|
||||
cert := certificateV.Model.(*SLoadbalancerCertificate)
|
||||
if len(cert.PrivateKey) == 0 {
|
||||
return nil, httperrors.NewResourceNotReadyError("invalid local certificate, private key is empty.")
|
||||
} else if len(cert.Certificate) == 0 {
|
||||
return nil, httperrors.NewResourceNotReadyError("invalid local certificate, certificate is empty.")
|
||||
} else {
|
||||
data.Set("certificate", jsonutils.NewString(cert.Certificate))
|
||||
data.Set("private_key", jsonutils.NewString(cert.PrivateKey))
|
||||
}
|
||||
|
||||
count, err := man.Query().Equals("certificate_id", certificateV.Model.GetId()).Equals("cloudregion_id", regionV.Model.GetId()).IsFalse("deleted").CountWithError()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return nil, httperrors.NewDuplicateResourceError("the certificate cache in region %s aready exists.", regionV.Model.GetId())
|
||||
}
|
||||
|
||||
provider := providerV.Model.(*SCloudprovider)
|
||||
data.Set("manager_id", jsonutils.NewString(provider.Id))
|
||||
name, _ := db.GenerateName(man, ownerId, certificateV.Model.GetName())
|
||||
data.Set("name", jsonutils.NewString(name))
|
||||
if _, err := man.SVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (self *SCachedLoadbalancerCertificate) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerProjId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
self.SetStatus(userCred, api.LB_CREATING, "")
|
||||
if err := self.StartLoadbalancerCertificateCreateTask(ctx, userCred, ""); err != nil {
|
||||
log.Errorf("CachedLoadbalancerCertificate.PostCreate %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -691,8 +691,15 @@ func (self *SHuaWeiRegionDriver) createLoadbalancerAcl(ctx context.Context, user
|
||||
Entrys: []cloudprovider.SLoadbalancerAccessControlListEntry{},
|
||||
AccessControlEnable: (listener.AclStatus == api.LB_BOOL_ON),
|
||||
}
|
||||
if lbacl.AclEntries != nil {
|
||||
for _, entry := range *lbacl.AclEntries {
|
||||
|
||||
_originAcl, err := db.FetchById(models.LoadbalancerAclManager, lbacl.AclId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "huaWeiRegionDriver.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})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,8 +298,14 @@ func (self *SManagedVirtualizationRegionDriver) createLoadbalancerAcl(ctx contex
|
||||
return nil, fmt.Errorf("regionDriver.createLoadbalancerAcl %s", err)
|
||||
}
|
||||
|
||||
if lbacl.AclEntries != nil {
|
||||
for _, entry := range *lbacl.AclEntries {
|
||||
_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})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user