mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): remove waf cache (#20283)
This commit is contained in:
@@ -1,29 +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 compute
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/cmd/climc/shell"
|
||||
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options/compute"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cmd := shell.NewResourceCmd(&modules.WafIPSetCaches)
|
||||
cmd.List(&compute.WafIPSetCacheListOptions{})
|
||||
cmd.Show(&options.BaseIdOptions{})
|
||||
cmd.Delete(&options.BaseIdOptions{})
|
||||
}
|
||||
@@ -1,29 +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 compute
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/cmd/climc/shell"
|
||||
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options/compute"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cmd := shell.NewResourceCmd(&modules.WafRegexSetCaches)
|
||||
cmd.List(&compute.WafRegexSetCacheListOptions{})
|
||||
cmd.Show(&options.BaseIdOptions{})
|
||||
cmd.Delete(&options.BaseIdOptions{})
|
||||
}
|
||||
@@ -18,30 +18,15 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
)
|
||||
|
||||
const (
|
||||
WAF_IPSET_STATUS_AVAILABLE = "available"
|
||||
WAF_IPSET_STATUS_DELETING = "deleting"
|
||||
WAF_IPSET_STATUS_DELETE_FAILED = "delete_failed"
|
||||
)
|
||||
|
||||
type WafIPSetDetails struct {
|
||||
apis.StatusInfrasResourceBaseDetails
|
||||
SWafIPSet
|
||||
ManagedResourceInfo
|
||||
CloudregionResourceInfo
|
||||
}
|
||||
|
||||
type WafIPSetListInput struct {
|
||||
apis.StatusInfrasResourceBaseListInput
|
||||
}
|
||||
|
||||
type WafIPSetCacheDetails struct {
|
||||
apis.StatusStandaloneResourceDetails
|
||||
ManagedResourceInfo
|
||||
CloudregionResourceInfo
|
||||
SWafIPSetCache
|
||||
}
|
||||
|
||||
type WafIPSetCacheListInput struct {
|
||||
apis.StatusStandaloneResourceListInput
|
||||
apis.ExternalizedResourceBaseListInput
|
||||
|
||||
ManagedResourceListInput
|
||||
|
||||
@@ -18,30 +18,15 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
)
|
||||
|
||||
const (
|
||||
WAF_REGEX_SET_STATUS_AVAILABLE = "available"
|
||||
WAF_REGEX_SET_STATUS_DELETING = "deleting"
|
||||
WAF_REGEX_SET_STATUS_DELETE_FAILED = "delete_failed"
|
||||
)
|
||||
|
||||
type WafRegexSetDetails struct {
|
||||
apis.StatusInfrasResourceBaseDetails
|
||||
SWafRegexSet
|
||||
ManagedResourceInfo
|
||||
CloudregionResourceInfo
|
||||
}
|
||||
|
||||
type WafRegexSetListInput struct {
|
||||
apis.StatusInfrasResourceBaseListInput
|
||||
}
|
||||
|
||||
type WafRegexSetCacheDetails struct {
|
||||
apis.StatusStandaloneResourceDetails
|
||||
ManagedResourceInfo
|
||||
CloudregionResourceInfo
|
||||
SWafRegexSetCache
|
||||
}
|
||||
|
||||
type WafRegexSetCacheListInput struct {
|
||||
apis.StatusStandaloneResourceListInput
|
||||
apis.ExternalizedResourceBaseListInput
|
||||
|
||||
ManagedResourceListInput
|
||||
|
||||
@@ -16,36 +16,15 @@ package compute
|
||||
|
||||
import "yunion.io/x/onecloud/pkg/apis"
|
||||
|
||||
const (
|
||||
WAF_RULE_GROUP_STATUS_AVAILABLE = "available"
|
||||
WAF_RULE_GROUP_STATUS_DELETING = "deleting"
|
||||
)
|
||||
|
||||
type WafRuleGroupDetails struct {
|
||||
apis.StatusInfrasResourceBaseDetails
|
||||
SWafRuleGroup
|
||||
ManagedResourceInfo
|
||||
CloudregionResourceInfo
|
||||
}
|
||||
|
||||
type WafRuleGroupListInput struct {
|
||||
apis.StatusInfrasResourceBaseListInput
|
||||
|
||||
// 是否是系统RuleGroup
|
||||
IsSystem *bool `json:"is_system"`
|
||||
// 云平台
|
||||
Provider string `json:"provider"`
|
||||
// 云环境
|
||||
CloudEnv string `json:"cloud_env"`
|
||||
}
|
||||
|
||||
type WafRuleGroupCacheDetails struct {
|
||||
apis.StatusStandaloneResourceDetails
|
||||
ManagedResourceInfo
|
||||
CloudregionResourceInfo
|
||||
SWafRuleGroupCache
|
||||
}
|
||||
|
||||
type WafRuleGroupCacheListInput struct {
|
||||
apis.StatusStandaloneResourceListInput
|
||||
apis.ExternalizedResourceBaseListInput
|
||||
ManagedResourceListInput
|
||||
RegionalFilterListInput
|
||||
|
||||
@@ -284,16 +284,16 @@ func (self *SCloudregion) purgeResources(ctx context.Context, managerId string)
|
||||
snapshots := SnapshotManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
tables := TablestoreManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
wafs := WafInstanceManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
ipsetcaches := WafIPSetCacheManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
regsetcaches := WafRegexSetCacheManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
wafgroups := WafRuleGroupCacheManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
ipsets := WafIPSetManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
regsets := WafRegexSetManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
wafgroups := WafRuleGroupManager.Query("id").Equals("manager_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
cprs := CloudproviderRegionManager.Query("row_id").Equals("cloudprovider_id", managerId).Equals("cloudregion_id", self.Id)
|
||||
|
||||
pairs := []purgePair{
|
||||
{manager: CloudproviderRegionManager, key: "row_id", q: cprs},
|
||||
{manager: WafRuleGroupCacheManager, key: "id", q: wafgroups},
|
||||
{manager: WafRegexSetCacheManager, key: "id", q: regsetcaches},
|
||||
{manager: WafIPSetCacheManager, key: "id", q: ipsetcaches},
|
||||
{manager: WafRuleGroupManager, key: "id", q: wafgroups},
|
||||
{manager: WafRegexSetManager, key: "id", q: regsets},
|
||||
{manager: WafIPSetManager, key: "id", q: ipsets},
|
||||
{manager: WafInstanceManager, key: "id", q: wafs},
|
||||
{manager: TablestoreManager, key: "id", q: tables},
|
||||
{manager: SnapshotManager, key: "id", q: snapshots},
|
||||
|
||||
@@ -1,370 +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 models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
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/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
type SWafIPSetCacheManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
SCloudregionResourceBaseManager
|
||||
}
|
||||
|
||||
var WafIPSetCacheManager *SWafIPSetCacheManager
|
||||
|
||||
func init() {
|
||||
WafIPSetCacheManager = &SWafIPSetCacheManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SWafIPSetCache{},
|
||||
"waf_ipset_caches_tbl",
|
||||
"waf_ipset_cache",
|
||||
"waf_ipset_caches",
|
||||
),
|
||||
}
|
||||
WafIPSetCacheManager.SetVirtualObject(WafIPSetCacheManager)
|
||||
}
|
||||
|
||||
type SWafIPSetCache struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
Type cloudprovider.TWafType `width:"20" charset:"utf8" nullable:"false" list:"user"`
|
||||
WafIPSetId string `width:"36" charset:"ascii" nullable:"false" list:"user"`
|
||||
}
|
||||
|
||||
func (manager *SWafIPSetCacheManager) GetContextManagers() [][]db.IModelManager {
|
||||
return [][]db.IModelManager{
|
||||
{CloudregionManager},
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *SWafIPSetCacheManager) FetchCustomizeColumns(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
objs []interface{},
|
||||
fields stringutils2.SSortedStrings,
|
||||
isList bool,
|
||||
) []api.WafIPSetCacheDetails {
|
||||
rows := make([]api.WafIPSetCacheDetails, len(objs))
|
||||
ssRows := manager.SStatusStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
managerRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
regionRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
for i := range rows {
|
||||
rows[i] = api.WafIPSetCacheDetails{
|
||||
StatusStandaloneResourceDetails: ssRows[i],
|
||||
ManagedResourceInfo: managerRows[i],
|
||||
CloudregionResourceInfo: regionRows[i],
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// 列出WAF IPSet缓存
|
||||
func (manager *SWafIPSetCacheManager) ListItemFilter(
|
||||
ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
query api.WafIPSetCacheListInput,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBase.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemFilter(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (manager *SWafIPSetCacheManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
func (manager *SWafIPSetCacheManager) OrderByExtraFields(
|
||||
ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
query api.WafIPSetCacheListInput,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SCloudregionResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (manager *SWafIPSetCacheManager) ListItemExportKeys(ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
keys stringutils2.SSortedStrings,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
if keys.ContainsAny(manager.SCloudregionResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
if keys.ContainsAny(manager.SManagedResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.SStatusStandaloneResourceBase.Delete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
return self.StartDeleteTask(ctx, userCred, "")
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) StartDeleteTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "WafIPSetCacheDeleteTask", self, userCred, nil, parentTaskId, "", nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "NewTask")
|
||||
}
|
||||
self.SetStatus(ctx, userCred, api.WAF_IPSET_STATUS_DELETING, "")
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) GetIRegion(ctx context.Context) (cloudprovider.ICloudRegion, error) {
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
provider, err := self.GetDriver(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetDriver")
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) GetICloudWafIPSet(ctx context.Context) (cloudprovider.ICloudWafIPSet, error) {
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
|
||||
}
|
||||
iRegion, err := self.GetIRegion(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIRegion")
|
||||
}
|
||||
caches, err := iRegion.GetICloudWafIPSets()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetICloudWafIPSets")
|
||||
}
|
||||
for i := range caches {
|
||||
if caches[i].GetGlobalId() == self.ExternalId {
|
||||
return caches[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, self.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafIPSetCache) syncWithCloudIPSet(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudWafIPSet) error {
|
||||
_, err := db.Update(self, func() error {
|
||||
self.Status = api.WAF_IPSET_STATUS_AVAILABLE
|
||||
if options.Options.EnableSyncName {
|
||||
self.Name = ext.GetName()
|
||||
}
|
||||
self.Description = ext.GetDesc()
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SCloudregion) GetIPSets(managerId string) ([]SWafIPSetCache, error) {
|
||||
q := WafIPSetCacheManager.Query().Equals("cloudregion_id", self.Id)
|
||||
if len(managerId) > 0 {
|
||||
q = q.Equals("manager_id", managerId)
|
||||
}
|
||||
caches := []SWafIPSetCache{}
|
||||
err := db.FetchModelObjects(WafIPSetCacheManager, q, &caches)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
return caches, nil
|
||||
}
|
||||
|
||||
func (self *SCloudregion) findOrCreateWafIPSet(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafIPSet) (*SWafIPSet, error) {
|
||||
q := WafIPSetManager.Query().Equals("domain_id", provider.DomainId).Equals("addresses", ext.GetAddresses().String())
|
||||
ipSets := []SWafIPSet{}
|
||||
err := db.FetchModelObjects(WafIPSetManager, q, &ipSets)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
if len(ipSets) > 0 {
|
||||
return &ipSets[0], nil
|
||||
}
|
||||
ipSet := &SWafIPSet{}
|
||||
ipSet.SetModelManager(WafIPSetManager, ipSet)
|
||||
ipSet.Name = ext.GetName()
|
||||
ipSet.Status = api.WAF_IPSET_STATUS_AVAILABLE
|
||||
ipSet.DomainId = provider.DomainId
|
||||
addrs := ext.GetAddresses()
|
||||
ipSet.Addresses = &addrs
|
||||
return ipSet, WafIPSetManager.TableSpec().Insert(ctx, ipSet)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) newFromCloudWafIPSet(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafIPSet, ipSetId string) error {
|
||||
cache := &SWafIPSetCache{}
|
||||
cache.SetModelManager(WafIPSetCacheManager, cache)
|
||||
cache.Name = ext.GetName()
|
||||
cache.WafIPSetId = ipSetId
|
||||
cache.CloudregionId = self.Id
|
||||
cache.ManagerId = provider.Id
|
||||
cache.ExternalId = ext.GetGlobalId()
|
||||
cache.Status = api.WAF_IPSET_STATUS_AVAILABLE
|
||||
cache.Type = ext.GetType()
|
||||
cache.Description = ext.GetDesc()
|
||||
return WafIPSetCacheManager.TableSpec().Insert(ctx, cache)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) SyncWafIPSets(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
provider *SCloudprovider,
|
||||
exts []cloudprovider.ICloudWafIPSet,
|
||||
xor bool,
|
||||
) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, WafIPSetCacheManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
defer lockman.ReleaseRawObject(ctx, WafIPSetCacheManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
|
||||
dbIPSets, err := self.GetIPSets(provider.Id)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
removed := make([]SWafIPSetCache, 0)
|
||||
commondb := make([]SWafIPSetCache, 0)
|
||||
commonext := make([]cloudprovider.ICloudWafIPSet, 0)
|
||||
added := make([]cloudprovider.ICloudWafIPSet, 0)
|
||||
err = compare.CompareSets(dbIPSets, exts, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i++ {
|
||||
err := removed[i].syncRemove(ctx, userCred)
|
||||
if err != nil {
|
||||
result.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
result.Delete()
|
||||
}
|
||||
|
||||
if !xor {
|
||||
for i := 0; i < len(commondb); i++ {
|
||||
err := commondb[i].syncWithCloudIPSet(ctx, userCred, commonext[i])
|
||||
if err != nil {
|
||||
result.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
result.Update()
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < len(added); i++ {
|
||||
ipSet, err := self.findOrCreateWafIPSet(ctx, userCred, provider, added[i])
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
err = self.newFromCloudWafIPSet(ctx, userCred, provider, added[i], ipSet.Id)
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
result.Add()
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -16,15 +16,20 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
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/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
@@ -32,6 +37,9 @@ import (
|
||||
|
||||
type SWafIPSetManager struct {
|
||||
db.SStatusInfrasResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
SCloudregionResourceBaseManager
|
||||
}
|
||||
|
||||
var WafIPSetManager *SWafIPSetManager
|
||||
@@ -50,7 +58,11 @@ func init() {
|
||||
|
||||
type SWafIPSet struct {
|
||||
db.SStatusInfrasResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
Type cloudprovider.TWafType `width:"20" charset:"utf8" nullable:"false" list:"user"`
|
||||
Addresses *cloudprovider.WafAddresses `list:"domain" update:"domain" create:"required"`
|
||||
}
|
||||
|
||||
@@ -64,9 +76,13 @@ func (manager *SWafIPSetManager) FetchCustomizeColumns(
|
||||
) []api.WafIPSetDetails {
|
||||
rows := make([]api.WafIPSetDetails, len(objs))
|
||||
siRows := manager.SStatusInfrasResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
managerRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
regionRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
for i := range rows {
|
||||
rows[i] = api.WafIPSetDetails{
|
||||
StatusInfrasResourceBaseDetails: siRows[i],
|
||||
ManagedResourceInfo: managerRows[i],
|
||||
CloudregionResourceInfo: regionRows[i],
|
||||
}
|
||||
}
|
||||
return rows
|
||||
@@ -85,6 +101,22 @@ func (manager *SWafIPSetManager) ListItemFilter(
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemFilter(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -94,6 +126,17 @@ func (manager *SWafIPSetManager) QueryDistinctExtraField(q *sqlchemy.SQuery, fie
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
@@ -107,6 +150,15 @@ func (manager *SWafIPSetManager) OrderByExtraFields(
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SCloudregionResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -119,6 +171,18 @@ func (manager *SWafIPSetManager) ListItemExportKeys(ctx context.Context,
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
if keys.ContainsAny(manager.SCloudregionResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
if keys.ContainsAny(manager.SManagedResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -139,13 +203,145 @@ func (self *SWafIPSet) StartDeleteTask(ctx context.Context, userCred mcclient.To
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "NewTask")
|
||||
}
|
||||
self.SetStatus(ctx, userCred, api.WAF_IPSET_STATUS_DELETING, "")
|
||||
self.SetStatus(ctx, userCred, apis.STATUS_DELETING, "")
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (self *SWafIPSet) GetCaches() ([]SWafIPSetCache, error) {
|
||||
q := WafIPSetCacheManager.Query().Equals("waf_ipset_id", self.Id)
|
||||
caches := []SWafIPSetCache{}
|
||||
err := db.FetchModelObjects(WafIPSetCacheManager, q, &caches)
|
||||
return caches, err
|
||||
func (self *SCloudregion) GetIPSets(managerId string) ([]SWafIPSet, error) {
|
||||
q := WafIPSetManager.Query().Equals("cloudregion_id", self.Id)
|
||||
if len(managerId) > 0 {
|
||||
q = q.Equals("manager_id", managerId)
|
||||
}
|
||||
ret := []SWafIPSet{}
|
||||
err := db.FetchModelObjects(WafIPSetManager, q, &ret)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SCloudregion) SyncWafIPSets(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
provider *SCloudprovider,
|
||||
exts []cloudprovider.ICloudWafIPSet,
|
||||
xor bool,
|
||||
) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, WafIPSetManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
defer lockman.ReleaseRawObject(ctx, WafIPSetManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
|
||||
dbIPSets, err := self.GetIPSets(provider.Id)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
removed := make([]SWafIPSet, 0)
|
||||
commondb := make([]SWafIPSet, 0)
|
||||
commonext := make([]cloudprovider.ICloudWafIPSet, 0)
|
||||
added := make([]cloudprovider.ICloudWafIPSet, 0)
|
||||
err = compare.CompareSets(dbIPSets, exts, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i++ {
|
||||
err := removed[i].syncRemove(ctx, userCred)
|
||||
if err != nil {
|
||||
result.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
result.Delete()
|
||||
}
|
||||
|
||||
if !xor {
|
||||
for i := 0; i < len(commondb); i++ {
|
||||
err := commondb[i].syncWithCloudIPSet(ctx, userCred, commonext[i])
|
||||
if err != nil {
|
||||
result.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
result.Update()
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < len(added); i++ {
|
||||
err = self.newFromCloudWafIPSet(ctx, userCred, provider, added[i])
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
result.Add()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (self *SWafIPSet) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafIPSet) syncWithCloudIPSet(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudWafIPSet) error {
|
||||
_, err := db.Update(self, func() error {
|
||||
self.Status = apis.STATUS_AVAILABLE
|
||||
if options.Options.EnableSyncName {
|
||||
self.Name = ext.GetName()
|
||||
}
|
||||
address := ext.GetAddresses()
|
||||
self.Addresses = &address
|
||||
if desc := ext.GetDesc(); len(desc) > 0 {
|
||||
self.Description = desc
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SCloudregion) newFromCloudWafIPSet(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafIPSet) error {
|
||||
ret := &SWafIPSet{}
|
||||
ret.SetModelManager(WafIPSetManager, ret)
|
||||
ret.Name = ext.GetName()
|
||||
ret.CloudregionId = self.Id
|
||||
ret.ManagerId = provider.Id
|
||||
ret.ExternalId = ext.GetGlobalId()
|
||||
ret.Status = apis.STATUS_AVAILABLE
|
||||
ret.Type = ext.GetType()
|
||||
ret.Description = ext.GetDesc()
|
||||
address := ext.GetAddresses()
|
||||
ret.Addresses = &address
|
||||
return WafIPSetManager.TableSpec().Insert(ctx, ret)
|
||||
}
|
||||
|
||||
func (self *SWafIPSet) GetIRegion(ctx context.Context) (cloudprovider.ICloudRegion, error) {
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
provider, err := self.GetDriver(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetDriver")
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafIPSet) GetICloudWafIPSet(ctx context.Context) (cloudprovider.ICloudWafIPSet, error) {
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
|
||||
}
|
||||
iRegion, err := self.GetIRegion(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIRegion")
|
||||
}
|
||||
caches, err := iRegion.GetICloudWafIPSets()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetICloudWafIPSets")
|
||||
}
|
||||
for i := range caches {
|
||||
if caches[i].GetGlobalId() == self.ExternalId {
|
||||
return caches[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, self.ExternalId)
|
||||
}
|
||||
|
||||
@@ -1,370 +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 models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
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/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
type SWafRegexSetCacheManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
SCloudregionResourceBaseManager
|
||||
}
|
||||
|
||||
var WafRegexSetCacheManager *SWafRegexSetCacheManager
|
||||
|
||||
func init() {
|
||||
WafRegexSetCacheManager = &SWafRegexSetCacheManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SWafRegexSetCache{},
|
||||
"waf_regexset_caches_tbl",
|
||||
"waf_regexset_cache",
|
||||
"waf_regexset_caches",
|
||||
),
|
||||
}
|
||||
WafRegexSetCacheManager.SetVirtualObject(WafRegexSetCacheManager)
|
||||
}
|
||||
|
||||
type SWafRegexSetCache struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
Type cloudprovider.TWafType `width:"20" charset:"utf8" nullable:"false" list:"user"`
|
||||
WafRegexSetId string `width:"36" charset:"ascii" nullable:"false" list:"user"`
|
||||
}
|
||||
|
||||
func (manager *SWafRegexSetCacheManager) GetContextManagers() [][]db.IModelManager {
|
||||
return [][]db.IModelManager{
|
||||
{CloudregionManager},
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *SWafRegexSetCacheManager) FetchCustomizeColumns(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
objs []interface{},
|
||||
fields stringutils2.SSortedStrings,
|
||||
isList bool,
|
||||
) []api.WafRegexSetCacheDetails {
|
||||
rows := make([]api.WafRegexSetCacheDetails, len(objs))
|
||||
ssRows := manager.SStatusStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
managerRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
regionRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
for i := range rows {
|
||||
rows[i] = api.WafRegexSetCacheDetails{
|
||||
StatusStandaloneResourceDetails: ssRows[i],
|
||||
ManagedResourceInfo: managerRows[i],
|
||||
CloudregionResourceInfo: regionRows[i],
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// 列出WAF RegexSet缓存
|
||||
func (manager *SWafRegexSetCacheManager) ListItemFilter(
|
||||
ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
query api.WafRegexSetCacheListInput,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBase.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemFilter(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (manager *SWafRegexSetCacheManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
func (manager *SWafRegexSetCacheManager) OrderByExtraFields(
|
||||
ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
query api.WafRegexSetCacheListInput,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SCloudregionResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (manager *SWafRegexSetCacheManager) ListItemExportKeys(ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
keys stringutils2.SSortedStrings,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
if keys.ContainsAny(manager.SCloudregionResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
if keys.ContainsAny(manager.SManagedResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.SStatusStandaloneResourceBase.Delete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
return self.StartDeleteTask(ctx, userCred, "")
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) StartDeleteTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "WafRegexSetCacheDeleteTask", self, userCred, nil, parentTaskId, "", nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "NewTask")
|
||||
}
|
||||
self.SetStatus(ctx, userCred, api.WAF_REGEX_SET_STATUS_DELETING, "")
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) GetIRegion(ctx context.Context) (cloudprovider.ICloudRegion, error) {
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
provider, err := self.GetDriver(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetDriver")
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) GetICloudWafRegexSet(ctx context.Context) (cloudprovider.ICloudWafRegexSet, error) {
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
|
||||
}
|
||||
iRegion, err := self.GetIRegion(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIRegion")
|
||||
}
|
||||
caches, err := iRegion.GetICloudWafRegexSets()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetICloudWafRegexSets")
|
||||
}
|
||||
for i := range caches {
|
||||
if caches[i].GetGlobalId() == self.ExternalId {
|
||||
return caches[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, self.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSetCache) syncWithCloudRegexSet(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudWafRegexSet) error {
|
||||
_, err := db.Update(self, func() error {
|
||||
self.Status = api.WAF_IPSET_STATUS_AVAILABLE
|
||||
if options.Options.EnableSyncName {
|
||||
self.Name = ext.GetName()
|
||||
}
|
||||
self.Description = ext.GetDesc()
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SCloudregion) GetRegexSets(managerId string) ([]SWafRegexSetCache, error) {
|
||||
q := WafRegexSetCacheManager.Query().Equals("cloudregion_id", self.Id)
|
||||
if len(managerId) > 0 {
|
||||
q = q.Equals("manager_id", managerId)
|
||||
}
|
||||
caches := []SWafRegexSetCache{}
|
||||
err := db.FetchModelObjects(WafRegexSetCacheManager, q, &caches)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
return caches, nil
|
||||
}
|
||||
|
||||
func (self *SCloudregion) findOrCreateWafRegexSet(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRegexSet) (*SWafRegexSet, error) {
|
||||
q := WafRegexSetManager.Query().Equals("domain_id", provider.DomainId).Equals("regex_patterns", ext.GetRegexPatterns().String())
|
||||
patternSets := []SWafRegexSet{}
|
||||
err := db.FetchModelObjects(WafRegexSetManager, q, &patternSets)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
if len(patternSets) > 0 {
|
||||
return &patternSets[0], nil
|
||||
}
|
||||
ps := &SWafRegexSet{}
|
||||
ps.SetModelManager(WafRegexSetManager, ps)
|
||||
ps.Name = ext.GetName()
|
||||
ps.Status = api.WAF_IPSET_STATUS_AVAILABLE
|
||||
ps.DomainId = provider.DomainId
|
||||
patterns := ext.GetRegexPatterns()
|
||||
ps.RegexPatterns = &patterns
|
||||
return ps, WafRegexSetManager.TableSpec().Insert(ctx, ps)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) newFromCloudWafRegexSet(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRegexSet, ipSetId string) error {
|
||||
cache := &SWafRegexSetCache{}
|
||||
cache.SetModelManager(WafRegexSetCacheManager, cache)
|
||||
cache.Name = ext.GetName()
|
||||
cache.WafRegexSetId = ipSetId
|
||||
cache.CloudregionId = self.Id
|
||||
cache.ManagerId = provider.Id
|
||||
cache.ExternalId = ext.GetGlobalId()
|
||||
cache.Status = api.WAF_IPSET_STATUS_AVAILABLE
|
||||
cache.Type = ext.GetType()
|
||||
cache.Description = ext.GetDesc()
|
||||
return WafRegexSetCacheManager.TableSpec().Insert(ctx, cache)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) SyncWafRegexSets(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
provider *SCloudprovider,
|
||||
exts []cloudprovider.ICloudWafRegexSet,
|
||||
xor bool,
|
||||
) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, WafRegexSetCacheManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
defer lockman.ReleaseRawObject(ctx, WafRegexSetCacheManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
|
||||
dbRegexSets, err := self.GetRegexSets(provider.Id)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
removed := make([]SWafRegexSetCache, 0)
|
||||
commondb := make([]SWafRegexSetCache, 0)
|
||||
commonext := make([]cloudprovider.ICloudWafRegexSet, 0)
|
||||
added := make([]cloudprovider.ICloudWafRegexSet, 0)
|
||||
err = compare.CompareSets(dbRegexSets, exts, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i++ {
|
||||
err := removed[i].syncRemove(ctx, userCred)
|
||||
if err != nil {
|
||||
result.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
result.Delete()
|
||||
}
|
||||
|
||||
if !xor {
|
||||
for i := 0; i < len(commondb); i++ {
|
||||
err := commondb[i].syncWithCloudRegexSet(ctx, userCred, commonext[i])
|
||||
if err != nil {
|
||||
result.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
result.Update()
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < len(added); i++ {
|
||||
ipSet, err := self.findOrCreateWafRegexSet(ctx, userCred, provider, added[i])
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
err = self.newFromCloudWafRegexSet(ctx, userCred, provider, added[i], ipSet.Id)
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
result.Add()
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -16,15 +16,20 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
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/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
@@ -32,6 +37,9 @@ import (
|
||||
|
||||
type SWafRegexSetManager struct {
|
||||
db.SStatusInfrasResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
SCloudregionResourceBaseManager
|
||||
}
|
||||
|
||||
var WafRegexSetManager *SWafRegexSetManager
|
||||
@@ -50,8 +58,13 @@ func init() {
|
||||
|
||||
type SWafRegexSet struct {
|
||||
db.SStatusInfrasResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
RegexPatterns *cloudprovider.WafRegexPatterns `list:"domain" update:"domain" create:"required"`
|
||||
Type cloudprovider.TWafType `width:"20" charset:"utf8" nullable:"false" list:"user"`
|
||||
}
|
||||
|
||||
func (manager *SWafRegexSetManager) FetchCustomizeColumns(
|
||||
@@ -64,9 +77,13 @@ func (manager *SWafRegexSetManager) FetchCustomizeColumns(
|
||||
) []api.WafRegexSetDetails {
|
||||
rows := make([]api.WafRegexSetDetails, len(objs))
|
||||
siRows := manager.SStatusInfrasResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
managerRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
regionRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
for i := range rows {
|
||||
rows[i] = api.WafRegexSetDetails{
|
||||
StatusInfrasResourceBaseDetails: siRows[i],
|
||||
ManagedResourceInfo: managerRows[i],
|
||||
CloudregionResourceInfo: regionRows[i],
|
||||
}
|
||||
}
|
||||
return rows
|
||||
@@ -85,6 +102,21 @@ func (manager *SWafRegexSetManager) ListItemFilter(
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemFilter(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -94,6 +126,16 @@ func (manager *SWafRegexSetManager) QueryDistinctExtraField(q *sqlchemy.SQuery,
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
@@ -107,6 +149,14 @@ func (manager *SWafRegexSetManager) OrderByExtraFields(
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SCloudregionResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -119,6 +169,18 @@ func (manager *SWafRegexSetManager) ListItemExportKeys(ctx context.Context,
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
if keys.ContainsAny(manager.SCloudregionResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
if keys.ContainsAny(manager.SManagedResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -130,6 +192,10 @@ func (self *SWafRegexSet) RealDelete(ctx context.Context, userCred mcclient.Toke
|
||||
return self.SStatusInfrasResourceBase.Delete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSet) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSet) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
return self.StartDeleteTask(ctx, userCred, "")
|
||||
}
|
||||
@@ -139,13 +205,141 @@ func (self *SWafRegexSet) StartDeleteTask(ctx context.Context, userCred mcclient
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "NewTask")
|
||||
}
|
||||
self.SetStatus(ctx, userCred, api.WAF_REGEX_SET_STATUS_DELETING, "")
|
||||
self.SetStatus(ctx, userCred, apis.STATUS_DELETING, "")
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSet) GetCaches() ([]SWafRegexSetCache, error) {
|
||||
q := WafRegexSetCacheManager.Query().Equals("waf_regexset_id", self.Id)
|
||||
caches := []SWafRegexSetCache{}
|
||||
err := db.FetchModelObjects(WafRegexSetCacheManager, q, &caches)
|
||||
return caches, err
|
||||
func (self *SWafRegexSet) GetIRegion(ctx context.Context) (cloudprovider.ICloudRegion, error) {
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
provider, err := self.GetDriver(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetDriver")
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSet) GetICloudWafRegexSet(ctx context.Context) (cloudprovider.ICloudWafRegexSet, error) {
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
|
||||
}
|
||||
iRegion, err := self.GetIRegion(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIRegion")
|
||||
}
|
||||
caches, err := iRegion.GetICloudWafRegexSets()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetICloudWafRegexSets")
|
||||
}
|
||||
for i := range caches {
|
||||
if caches[i].GetGlobalId() == self.ExternalId {
|
||||
return caches[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, self.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRegexSet) syncWithCloudRegexSet(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudWafRegexSet) error {
|
||||
_, err := db.Update(self, func() error {
|
||||
self.Status = apis.STATUS_AVAILABLE
|
||||
if options.Options.EnableSyncName {
|
||||
self.Name = ext.GetName()
|
||||
}
|
||||
if desc := ext.GetDesc(); len(desc) > 0 {
|
||||
self.Description = desc
|
||||
}
|
||||
patterns := ext.GetRegexPatterns()
|
||||
self.RegexPatterns = &patterns
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SCloudregion) GetRegexSets(managerId string) ([]SWafRegexSet, error) {
|
||||
q := WafRegexSetManager.Query().Equals("cloudregion_id", self.Id)
|
||||
if len(managerId) > 0 {
|
||||
q = q.Equals("manager_id", managerId)
|
||||
}
|
||||
ret := []SWafRegexSet{}
|
||||
err := db.FetchModelObjects(WafIPSetManager, q, &ret)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SCloudregion) newFromCloudWafRegexSet(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRegexSet) error {
|
||||
ret := &SWafRegexSet{}
|
||||
ret.SetModelManager(WafRegexSetManager, ret)
|
||||
ret.Name = ext.GetName()
|
||||
ret.CloudregionId = self.Id
|
||||
ret.ManagerId = provider.Id
|
||||
ret.ExternalId = ext.GetGlobalId()
|
||||
ret.Status = apis.STATUS_AVAILABLE
|
||||
ret.Type = ext.GetType()
|
||||
patterns := ext.GetRegexPatterns()
|
||||
ret.RegexPatterns = &patterns
|
||||
ret.Description = ext.GetDesc()
|
||||
return WafRegexSetManager.TableSpec().Insert(ctx, ret)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) SyncWafRegexSets(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
provider *SCloudprovider,
|
||||
exts []cloudprovider.ICloudWafRegexSet,
|
||||
xor bool,
|
||||
) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, WafRegexSetManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
defer lockman.ReleaseRawObject(ctx, WafRegexSetManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
|
||||
dbRegexSets, err := self.GetRegexSets(provider.Id)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
removed := make([]SWafRegexSet, 0)
|
||||
commondb := make([]SWafRegexSet, 0)
|
||||
commonext := make([]cloudprovider.ICloudWafRegexSet, 0)
|
||||
added := make([]cloudprovider.ICloudWafRegexSet, 0)
|
||||
err = compare.CompareSets(dbRegexSets, exts, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i++ {
|
||||
err := removed[i].syncRemove(ctx, userCred)
|
||||
if err != nil {
|
||||
result.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
result.Delete()
|
||||
}
|
||||
|
||||
if !xor {
|
||||
for i := 0; i < len(commondb); i++ {
|
||||
err := commondb[i].syncWithCloudRegexSet(ctx, userCred, commonext[i])
|
||||
if err != nil {
|
||||
result.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
result.Update()
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < len(added); i++ {
|
||||
err = self.newFromCloudWafRegexSet(ctx, userCred, provider, added[i])
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
result.Add()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,359 +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 models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
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/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
type SWafRuleGroupCacheManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
SCloudregionResourceBaseManager
|
||||
}
|
||||
|
||||
var WafRuleGroupCacheManager *SWafRuleGroupCacheManager
|
||||
|
||||
func init() {
|
||||
WafRuleGroupCacheManager = &SWafRuleGroupCacheManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SWafRuleGroupCache{},
|
||||
"waf_rule_group_caches_tbl",
|
||||
"waf_rule_group_cache",
|
||||
"waf_rule_group_caches",
|
||||
),
|
||||
}
|
||||
WafRuleGroupCacheManager.SetVirtualObject(WafRuleGroupCacheManager)
|
||||
}
|
||||
|
||||
type SWafRuleGroupCache struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
Type cloudprovider.TWafType `width:"20" charset:"utf8" nullable:"false" list:"user"`
|
||||
WafRuleGroupId string `width:"36" charset:"ascii" nullable:"false" list:"user"`
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupCacheManager) GetContextManagers() [][]db.IModelManager {
|
||||
return [][]db.IModelManager{
|
||||
{CloudregionManager},
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupCacheManager) FetchCustomizeColumns(
|
||||
ctx context.Context,
|
||||
userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
objs []interface{},
|
||||
fields stringutils2.SSortedStrings,
|
||||
isList bool,
|
||||
) []api.WafRuleGroupCacheDetails {
|
||||
rows := make([]api.WafRuleGroupCacheDetails, len(objs))
|
||||
ssRows := manager.SStatusStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
managerRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
regionRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
for i := range rows {
|
||||
rows[i] = api.WafRuleGroupCacheDetails{
|
||||
StatusStandaloneResourceDetails: ssRows[i],
|
||||
ManagedResourceInfo: managerRows[i],
|
||||
CloudregionResourceInfo: regionRows[i],
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// 列出WAF RuleGroup缓存
|
||||
func (manager *SWafRuleGroupCacheManager) ListItemFilter(
|
||||
ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
query api.WafRuleGroupCacheListInput,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBase.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemFilter(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupCacheManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
q, err = manager.SStatusStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupCacheManager) OrderByExtraFields(
|
||||
ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
query api.WafRuleGroupCacheListInput,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SCloudregionResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupCacheManager) ListItemExportKeys(ctx context.Context,
|
||||
q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential,
|
||||
keys stringutils2.SSortedStrings,
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
q, err := manager.SStatusStandaloneResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
if keys.ContainsAny(manager.SCloudregionResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
if keys.ContainsAny(manager.SManagedResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.SStatusStandaloneResourceBase.Delete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
|
||||
return self.StartDeleteTask(ctx, userCred, "")
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) StartDeleteTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "WafRuleGroupCacheDeleteTask", self, userCred, nil, parentTaskId, "", nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "NewTask")
|
||||
}
|
||||
self.SetStatus(ctx, userCred, api.WAF_RULE_GROUP_STATUS_DELETING, "")
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) GetIRegion(ctx context.Context) (cloudprovider.ICloudRegion, error) {
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
provider, err := self.GetDriver(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetDriver")
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) GetICloudWafRuleGroup(ctx context.Context) (cloudprovider.ICloudWafRuleGroup, error) {
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
|
||||
}
|
||||
iRegion, err := self.GetIRegion(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIRegion")
|
||||
}
|
||||
caches, err := iRegion.GetICloudWafRuleGroups()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetICloudWafRuleGroups")
|
||||
}
|
||||
for i := range caches {
|
||||
if caches[i].GetGlobalId() == self.ExternalId {
|
||||
return caches[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, self.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroupCache) syncWithCloudRuleGroup(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudWafRuleGroup) error {
|
||||
_, err := db.Update(self, func() error {
|
||||
self.Status = api.WAF_RULE_GROUP_STATUS_AVAILABLE
|
||||
self.Name = ext.GetName()
|
||||
self.Type = ext.GetWafType()
|
||||
self.Description = ext.GetDesc()
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SCloudregion) GetRuleGroups(managerId string) ([]SWafRuleGroupCache, error) {
|
||||
q := WafRuleGroupCacheManager.Query().Equals("cloudregion_id", self.Id)
|
||||
if len(managerId) > 0 {
|
||||
q = q.Equals("manager_id", managerId)
|
||||
}
|
||||
caches := []SWafRuleGroupCache{}
|
||||
err := db.FetchModelObjects(WafRuleGroupCacheManager, q, &caches)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
return caches, nil
|
||||
}
|
||||
|
||||
func (self *SCloudregion) createWafRuleGroup(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRuleGroup) (*SWafRuleGroup, error) {
|
||||
rg := &SWafRuleGroup{}
|
||||
rg.SetModelManager(WafRuleGroupManager, rg)
|
||||
rg.Name = ext.GetName()
|
||||
rg.Status = api.WAF_RULE_GROUP_STATUS_AVAILABLE
|
||||
rg.Description = ext.GetDesc()
|
||||
rg.DomainId = provider.DomainId
|
||||
return rg, WafRuleGroupManager.TableSpec().Insert(ctx, rg)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) createRuleGroup(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRuleGroup) (*SWafRuleGroup, error) {
|
||||
rg := &SWafRuleGroup{}
|
||||
rg.SetModelManager(WafRuleGroupManager, rg)
|
||||
rg.Name = ext.GetName()
|
||||
rg.Status = api.WAF_RULE_GROUP_STATUS_AVAILABLE
|
||||
rg.Description = ext.GetDesc()
|
||||
rg.DomainId = provider.DomainId
|
||||
return rg, WafRuleGroupManager.TableSpec().Insert(ctx, rg)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) newFromCloudWafRuleGroup(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRuleGroup) error {
|
||||
rg, err := self.createRuleGroup(ctx, userCred, provider, ext)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "createRuleGroup")
|
||||
}
|
||||
cache := &SWafRuleGroupCache{}
|
||||
cache.SetModelManager(WafRuleGroupCacheManager, cache)
|
||||
cache.Name = ext.GetName()
|
||||
cache.WafRuleGroupId = rg.Id
|
||||
cache.CloudregionId = self.Id
|
||||
cache.ManagerId = provider.Id
|
||||
cache.ExternalId = ext.GetGlobalId()
|
||||
cache.Status = api.WAF_RULE_GROUP_STATUS_AVAILABLE
|
||||
cache.Type = ext.GetWafType()
|
||||
cache.Description = ext.GetDesc()
|
||||
return WafRuleGroupCacheManager.TableSpec().Insert(ctx, cache)
|
||||
}
|
||||
|
||||
func (self *SCloudregion) SyncWafRuleGroups(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, exts []cloudprovider.ICloudWafRuleGroup) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, WafRuleGroupCacheManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
defer lockman.ReleaseRawObject(ctx, WafRuleGroupCacheManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
|
||||
dbRuleGroups, err := self.GetRuleGroups(provider.Id)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
removed := make([]SWafRuleGroupCache, 0)
|
||||
commondb := make([]SWafRuleGroupCache, 0)
|
||||
commonext := make([]cloudprovider.ICloudWafRuleGroup, 0)
|
||||
added := make([]cloudprovider.ICloudWafRuleGroup, 0)
|
||||
err = compare.CompareSets(dbRuleGroups, exts, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i++ {
|
||||
err := removed[i].syncRemove(ctx, userCred)
|
||||
if err != nil {
|
||||
result.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
result.Delete()
|
||||
}
|
||||
|
||||
for i := 0; i < len(commondb); i++ {
|
||||
err := commondb[i].syncWithCloudRuleGroup(ctx, userCred, commonext[i])
|
||||
if err != nil {
|
||||
result.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
result.Update()
|
||||
}
|
||||
|
||||
for i := 0; i < len(added); i++ {
|
||||
err = self.newFromCloudWafRuleGroup(ctx, userCred, provider, added[i])
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
result.Add()
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -16,27 +16,28 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
identity_api "yunion.io/x/onecloud/pkg/apis/identity"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
"yunion.io/x/onecloud/pkg/util/yunionmeta"
|
||||
)
|
||||
|
||||
type SWafRuleGroupManager struct {
|
||||
db.SStatusInfrasResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
SCloudregionResourceBaseManager
|
||||
}
|
||||
|
||||
var WafRuleGroupManager *SWafRuleGroupManager
|
||||
@@ -56,12 +57,10 @@ func init() {
|
||||
type SWafRuleGroup struct {
|
||||
db.SStatusInfrasResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
SManagedResourceBase
|
||||
SCloudregionResourceBase
|
||||
|
||||
// 支持的WAF类型,仅is_system=true时有效
|
||||
WafType cloudprovider.TWafType `width:"40" charset:"ascii" list:"domain" nullable:"false"`
|
||||
Provider string `width:"20" charset:"ascii" list:"domain" nullable:"false"`
|
||||
CloudEnv string `width:"20" charset:"ascii" list:"domain" nullable:"false"`
|
||||
IsSystem bool `nullable:"false" default:"false" list:"domain" update:"domain" create:"optional"`
|
||||
WafType cloudprovider.TWafType `width:"40" charset:"ascii" list:"domain" nullable:"false"`
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupManager) FetchCustomizeColumns(
|
||||
@@ -74,9 +73,13 @@ func (manager *SWafRuleGroupManager) FetchCustomizeColumns(
|
||||
) []api.WafRuleGroupDetails {
|
||||
rows := make([]api.WafRuleGroupDetails, len(objs))
|
||||
siRows := manager.SStatusInfrasResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
managerRows := manager.SManagedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
regionRows := manager.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
for i := range rows {
|
||||
rows[i] = api.WafRuleGroupDetails{
|
||||
StatusInfrasResourceBaseDetails: siRows[i],
|
||||
ManagedResourceInfo: managerRows[i],
|
||||
CloudregionResourceInfo: regionRows[i],
|
||||
}
|
||||
}
|
||||
return rows
|
||||
@@ -96,16 +99,19 @@ func (manager *SWafRuleGroupManager) ListItemFilter(
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
if query.IsSystem != nil {
|
||||
q = q.Equals("is_system", *query.IsSystem)
|
||||
q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ExternalizedResourceBaseListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
if len(query.Provider) > 0 {
|
||||
q = q.Equals("provider", query.Provider)
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
if len(query.CloudEnv) > 0 {
|
||||
q = q.Equals("cloud_env", query.CloudEnv)
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemFilter(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemFilter")
|
||||
}
|
||||
|
||||
return q, nil
|
||||
@@ -117,6 +123,16 @@ func (manager *SWafRuleGroupManager) QueryDistinctExtraField(q *sqlchemy.SQuery,
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
q, err = manager.SCloudregionResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
|
||||
@@ -130,6 +146,15 @@ func (manager *SWafRuleGroupManager) OrderByExtraFields(
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
q, err = manager.SCloudregionResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.RegionalFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.OrderByExtraFields")
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -142,9 +167,26 @@ func (manager *SWafRuleGroupManager) ListItemExportKeys(ctx context.Context,
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SStatusInfrasResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
if keys.ContainsAny(manager.SCloudregionResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SCloudregionResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SCloudregionResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
if keys.ContainsAny(manager.SManagedResourceBaseManager.GetExportKeys()...) {
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SManagedResourceBaseManager.ListItemExportKeys")
|
||||
}
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroup) syncRemove(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return self.RealDelete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroup) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return nil
|
||||
}
|
||||
@@ -163,185 +205,122 @@ func (self *SWafRuleGroup) RealDelete(ctx context.Context, userCred mcclient.Tok
|
||||
return self.SStatusInfrasResourceBase.Delete(ctx, userCred)
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupManager) GetWafGroups(cloudEnv string) ([]SWafRuleGroup, error) {
|
||||
q := manager.Query().Equals("cloud_env", cloudEnv).IsTrue("is_system")
|
||||
groups := []SWafRuleGroup{}
|
||||
err := db.FetchModelObjects(WafRuleGroupManager, q, &groups)
|
||||
return groups, err
|
||||
func (self *SWafRuleGroup) GetIRegion(ctx context.Context) (cloudprovider.ICloudRegion, error) {
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
provider, err := self.GetDriver(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetDriver")
|
||||
}
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroup) syncWithCloudSku(ctx context.Context, userCred mcclient.TokenCredential, ext sWafGroup) error {
|
||||
func (self *SWafRuleGroup) GetICloudWafRuleGroup(ctx context.Context) (cloudprovider.ICloudWafRuleGroup, error) {
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty external id")
|
||||
}
|
||||
iRegion, err := self.GetIRegion(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIRegion")
|
||||
}
|
||||
caches, err := iRegion.GetICloudWafRuleGroups()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetICloudWafRuleGroups")
|
||||
}
|
||||
for i := range caches {
|
||||
if caches[i].GetGlobalId() == self.ExternalId {
|
||||
return caches[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, self.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SWafRuleGroup) syncWithCloudRuleGroup(ctx context.Context, userCred mcclient.TokenCredential, ext cloudprovider.ICloudWafRuleGroup) error {
|
||||
_, err := db.Update(self, func() error {
|
||||
self.Name = ext.Name
|
||||
self.Description = ext.Description
|
||||
self.IsPublic = true
|
||||
self.Status = api.WAF_RULE_GROUP_STATUS_AVAILABLE
|
||||
self.Status = apis.STATUS_AVAILABLE
|
||||
self.Name = ext.GetName()
|
||||
self.WafType = ext.GetWafType()
|
||||
self.Description = ext.GetDesc()
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "db.Update")
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SCloudregion) GetRuleGroups(managerId string) ([]SWafRuleGroup, error) {
|
||||
q := WafRuleGroupManager.Query().Equals("cloudregion_id", self.Id)
|
||||
if len(managerId) > 0 {
|
||||
q = q.Equals("manager_id", managerId)
|
||||
}
|
||||
result, err := self.SyncManagedWafRules(ctx, userCred, ext.Rules)
|
||||
ret := []SWafRuleGroup{}
|
||||
err := db.FetchModelObjects(WafRuleGroupManager, q, &ret)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "SyncManagedWafRules")
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
log.Debugf("Sync waf group %s rule result: %s", self.Name, result.Result())
|
||||
return nil
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupManager) newFromCloudWafGroup(ctx context.Context, userCred mcclient.TokenCredential, ext sWafGroup) error {
|
||||
group := &ext.SWafRuleGroup
|
||||
group.SetModelManager(manager, group)
|
||||
group.Status = api.WAF_RULE_GROUP_STATUS_AVAILABLE
|
||||
group.IsPublic = true
|
||||
err := WafRuleGroupManager.TableSpec().Insert(ctx, group)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Insert")
|
||||
}
|
||||
result, err := group.SyncManagedWafRules(ctx, userCred, ext.Rules)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "SyncManagedWafRules")
|
||||
}
|
||||
log.Debugf("Sync waf group %s rule result: %s", group.Name, result.Result())
|
||||
return nil
|
||||
}
|
||||
|
||||
type sWafGroup struct {
|
||||
SWafRuleGroup
|
||||
Rules []SWafRule
|
||||
}
|
||||
|
||||
func (self sWafGroup) GetGlobalId() string {
|
||||
return self.ExternalId
|
||||
}
|
||||
|
||||
func (self SWafRule) GetGlobalId() string {
|
||||
return self.ExternalId
|
||||
}
|
||||
|
||||
func (manager *SWafRuleGroupManager) SyncWafGroups(ctx context.Context, userCred mcclient.TokenCredential, cloudEnv string, isStart bool) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, cloudEnv, manager.Keyword())
|
||||
defer lockman.ReleaseRawObject(ctx, cloudEnv, manager.Keyword())
|
||||
func (self *SCloudregion) SyncWafRuleGroups(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, exts []cloudprovider.ICloudWafRuleGroup) compare.SyncResult {
|
||||
lockman.LockRawObject(ctx, WafRuleGroupManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
defer lockman.ReleaseRawObject(ctx, WafRuleGroupManager.Keyword(), fmt.Sprintf("%s-%s", self.Id, provider.Id))
|
||||
|
||||
result := compare.SyncResult{}
|
||||
|
||||
meta, err := yunionmeta.FetchYunionmeta(ctx)
|
||||
if err != nil {
|
||||
result.Error(errors.Wrapf(err, "FetchYunionmeta"))
|
||||
return result
|
||||
}
|
||||
|
||||
exts := []sWafGroup{}
|
||||
err = meta.List(WafRuleManager.Keyword(), cloudEnv, &exts)
|
||||
if err != nil {
|
||||
result.Error(errors.Wrapf(err, "List(%s)", cloudEnv))
|
||||
return result
|
||||
}
|
||||
dbGroup, err := manager.GetWafGroups(cloudEnv)
|
||||
if err != nil {
|
||||
result.Error(errors.Wrapf(err, "GetWafGroups"))
|
||||
return result
|
||||
}
|
||||
|
||||
if isStart && len(dbGroup) > 0 {
|
||||
log.Infof("%s waf group already synced, skip...", cloudEnv)
|
||||
return result
|
||||
}
|
||||
|
||||
removed := make([]SWafRuleGroup, 0)
|
||||
commondb := make([]SWafRuleGroup, 0)
|
||||
commonext := make([]sWafGroup, 0)
|
||||
added := make([]sWafGroup, 0)
|
||||
|
||||
err = compare.CompareSets(dbGroup, exts, &removed, &commondb, &commonext, &added)
|
||||
dbRuleGroups, err := self.GetRuleGroups(provider.Id)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i += 1 {
|
||||
err = removed[i].RealDelete(ctx, userCred)
|
||||
removed := make([]SWafRuleGroup, 0)
|
||||
commondb := make([]SWafRuleGroup, 0)
|
||||
commonext := make([]cloudprovider.ICloudWafRuleGroup, 0)
|
||||
added := make([]cloudprovider.ICloudWafRuleGroup, 0)
|
||||
err = compare.CompareSets(dbRuleGroups, exts, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
result.Error(err)
|
||||
return result
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i++ {
|
||||
err := removed[i].syncRemove(ctx, userCred)
|
||||
if err != nil {
|
||||
result.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
result.Delete()
|
||||
}
|
||||
for i := 0; i < len(commondb); i += 1 {
|
||||
err = commondb[i].syncWithCloudSku(ctx, userCred, commonext[i])
|
||||
|
||||
for i := 0; i < len(commondb); i++ {
|
||||
err := commondb[i].syncWithCloudRuleGroup(ctx, userCred, commonext[i])
|
||||
if err != nil {
|
||||
result.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
result.Update()
|
||||
}
|
||||
for i := 0; i < len(added); i += 1 {
|
||||
err = manager.newFromCloudWafGroup(ctx, userCred, added[i])
|
||||
|
||||
for i := 0; i < len(added); i++ {
|
||||
err = self.newFromCloudWafRuleGroup(ctx, userCred, provider, added[i])
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
result.Add()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func fetchCloudEnvs() ([]string, error) {
|
||||
accounts := []SCloudaccount{}
|
||||
q := CloudaccountManager.Query("provider", "access_url").In("provider", CloudproviderManager.GetPublicProviderProvidersQuery()).Distinct()
|
||||
err := q.All(&accounts)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "q.All")
|
||||
}
|
||||
ret := []string{}
|
||||
for i := range accounts {
|
||||
ret = append(ret, api.GetCloudEnv(accounts[i].Provider, accounts[i].AccessUrl))
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func SyncWafGroups(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
|
||||
err := func() error {
|
||||
cloudEnvs, err := fetchCloudEnvs()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "fetchCloudEnvs")
|
||||
}
|
||||
|
||||
if len(cloudEnvs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
meta, err := yunionmeta.FetchYunionmeta(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "FetchYunionmeta")
|
||||
}
|
||||
|
||||
index, err := meta.Index(WafRuleManager.Keyword())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "getWafIndex")
|
||||
}
|
||||
|
||||
for _, cloudEnv := range cloudEnvs {
|
||||
skuMeta := &SWafRuleGroup{}
|
||||
skuMeta.SetModelManager(WafRuleGroupManager, skuMeta)
|
||||
skuMeta.DomainId = identity_api.DEFAULT_DOMAIN_ID
|
||||
skuMeta.Id = cloudEnv
|
||||
|
||||
oldMd5 := db.Metadata.GetStringValue(ctx, skuMeta, db.SKU_METADAT_KEY, userCred)
|
||||
newMd5, ok := index[cloudEnv]
|
||||
if !ok || newMd5 == yunionmeta.EMPTY_MD5 || len(oldMd5) > 0 && newMd5 == oldMd5 {
|
||||
continue
|
||||
}
|
||||
|
||||
db.Metadata.SetValue(ctx, skuMeta, db.SKU_METADAT_KEY, newMd5, userCred)
|
||||
|
||||
result := WafRuleGroupManager.SyncWafGroups(ctx, userCred, cloudEnv, isStart)
|
||||
log.Debugf("sync %s waf group result: %s", cloudEnv, result.Result())
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
log.Errorf("SyncWafGroups: error: %v", err)
|
||||
}
|
||||
func (self *SCloudregion) newFromCloudWafRuleGroup(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudWafRuleGroup) error {
|
||||
ret := &SWafRuleGroup{}
|
||||
ret.SetModelManager(WafRuleGroupManager, ret)
|
||||
ret.Name = ext.GetName()
|
||||
ret.CloudregionId = self.Id
|
||||
ret.ManagerId = provider.Id
|
||||
ret.ExternalId = ext.GetGlobalId()
|
||||
ret.Status = apis.STATUS_AVAILABLE
|
||||
ret.WafType = ext.GetWafType()
|
||||
ret.Description = ext.GetDesc()
|
||||
return WafRuleGroupManager.TableSpec().Insert(ctx, ret)
|
||||
}
|
||||
|
||||
@@ -81,22 +81,20 @@ func (self *SWafRuleStatement) syncWithStatement(ctx context.Context, userCred m
|
||||
switch self.Type {
|
||||
case cloudprovider.WafStatementTypeIPSet:
|
||||
if len(self.IPSetId) > 0 {
|
||||
_cache, err := db.FetchByExternalId(WafIPSetCacheManager, self.IPSetId)
|
||||
ipSet, err := db.FetchByExternalId(WafIPSetManager, self.IPSetId)
|
||||
if err != nil {
|
||||
log.Errorf("WafIPSetCacheManager(%s) error: %v", self.IPSetId, err)
|
||||
log.Errorf("WafIPSetManager(%s) error: %v", self.IPSetId, err)
|
||||
} else {
|
||||
cache := _cache.(*SWafIPSetCache)
|
||||
self.IPSetId = cache.WafIPSetId
|
||||
self.IPSetId = ipSet.GetId()
|
||||
}
|
||||
}
|
||||
case cloudprovider.WafStatementTypeRegexSet:
|
||||
if len(self.RegexSetId) > 0 {
|
||||
_cache, err := db.FetchByExternalId(WafRegexSetCacheManager, self.RegexSetId)
|
||||
regexSet, err := db.FetchByExternalId(WafRegexSetManager, self.RegexSetId)
|
||||
if err != nil {
|
||||
log.Errorf("WafRegexSetCacheManager(%s) error: %v", self.RegexSetId, err)
|
||||
log.Errorf("WafRegexSetManager(%s) error: %v", self.RegexSetId, err)
|
||||
} else {
|
||||
cache := _cache.(*SWafRegexSetCache)
|
||||
self.RegexSetId = cache.WafRegexSetId
|
||||
self.RegexSetId = regexSet.GetId()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,22 +112,20 @@ func (self *SWafRule) newFromCloudStatement(ctx context.Context, userCred mcclie
|
||||
switch statement.Type {
|
||||
case cloudprovider.WafStatementTypeIPSet:
|
||||
if len(statement.IPSetId) > 0 {
|
||||
_cache, err := db.FetchByExternalId(WafIPSetCacheManager, statement.IPSetId)
|
||||
ipSet, err := db.FetchByExternalId(WafIPSetManager, statement.IPSetId)
|
||||
if err != nil {
|
||||
log.Errorf("WafIPSetCacheManager(%s) error: %v", statement.IPSetId, err)
|
||||
log.Errorf("WafIPSetManager(%s) error: %v", statement.IPSetId, err)
|
||||
} else {
|
||||
cache := _cache.(*SWafIPSetCache)
|
||||
statement.IPSetId = cache.WafIPSetId
|
||||
statement.IPSetId = ipSet.GetId()
|
||||
}
|
||||
}
|
||||
case cloudprovider.WafStatementTypeRegexSet:
|
||||
if len(statement.RegexSetId) > 0 {
|
||||
_cache, err := db.FetchByExternalId(WafRegexSetCacheManager, statement.RegexSetId)
|
||||
regexSet, err := db.FetchByExternalId(WafRegexSetManager, statement.RegexSetId)
|
||||
if err != nil {
|
||||
log.Errorf("WafRegexSetCacheManager(%s) error: %v", statement.RegexSetId, err)
|
||||
log.Errorf("WafRegexSetManager(%s) error: %v", statement.RegexSetId, err)
|
||||
} else {
|
||||
cache := _cache.(*SWafRegexSetCache)
|
||||
statement.RegexSetId = cache.WafRegexSetId
|
||||
statement.RegexSetId = regexSet.GetId()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,11 +206,8 @@ func InitHandlers(app *appsrv.Application) {
|
||||
models.ProjectMappingManager,
|
||||
|
||||
models.WafRuleGroupManager,
|
||||
models.WafRuleGroupCacheManager,
|
||||
models.WafIPSetManager,
|
||||
models.WafIPSetCacheManager,
|
||||
models.WafRegexSetManager,
|
||||
models.WafRegexSetCacheManager,
|
||||
models.WafInstanceManager,
|
||||
models.WafRuleManager,
|
||||
|
||||
|
||||
@@ -181,7 +181,6 @@ func StartServiceWithJobs(jobs func(cron *cronman.SCronJobManager)) {
|
||||
cron.AddJobEveryFewHour("AutoCleanImageCache", 1, 5, 0, models.CachedimageManager.AutoCleanImageCaches, false)
|
||||
|
||||
cron.AddJobAtIntervalsWithStartRun("SyncSkus", time.Duration(opts.ServerSkuSyncIntervalMinutes)*time.Minute, models.SyncServerSkus, true)
|
||||
cron.AddJobAtIntervalsWithStartRun("SyncManagedWafGroups", time.Duration(opts.ServerSkuSyncIntervalMinutes)*time.Minute, models.SyncWafGroups, true)
|
||||
|
||||
cron.AddJobEveryFewDays("SyncDBInstanceSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncDBInstanceSkus, true)
|
||||
cron.AddJobEveryFewDays("SyncNatSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncNatSkus, true)
|
||||
|
||||
@@ -1,67 +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 tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type WafIPSetCacheDeleteTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(WafIPSetCacheDeleteTask{})
|
||||
}
|
||||
|
||||
func (self *WafIPSetCacheDeleteTask) taskFailed(ctx context.Context, cache *models.SWafIPSetCache, err error) {
|
||||
cache.SetStatus(ctx, self.UserCred, api.WAF_IPSET_STATUS_DELETE_FAILED, err.Error())
|
||||
logclient.AddActionLogWithStartable(self, cache, logclient.ACT_DELETE, err, self.UserCred, false)
|
||||
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
|
||||
}
|
||||
|
||||
func (self *WafIPSetCacheDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
cache := obj.(*models.SWafIPSetCache)
|
||||
iCache, err := cache.GetICloudWafIPSet(ctx)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
self.taskComplete(ctx, cache)
|
||||
return
|
||||
}
|
||||
self.taskFailed(ctx, cache, errors.Wrapf(err, "GetICloudWafIPSet"))
|
||||
return
|
||||
}
|
||||
err = iCache.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, cache, errors.Wrapf(err, "iCache.Delete"))
|
||||
return
|
||||
}
|
||||
self.taskComplete(ctx, cache)
|
||||
}
|
||||
|
||||
func (self *WafIPSetCacheDeleteTask) taskComplete(ctx context.Context, cache *models.SWafIPSetCache) {
|
||||
cache.RealDelete(ctx, self.GetUserCred())
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
@@ -37,34 +37,26 @@ func init() {
|
||||
}
|
||||
|
||||
func (self *WafIPSetDeleteTask) taskFailed(ctx context.Context, ipset *models.SWafIPSet, err error) {
|
||||
ipset.SetStatus(ctx, self.UserCred, api.WAF_IPSET_STATUS_DELETE_FAILED, err.Error())
|
||||
ipset.SetStatus(ctx, self.UserCred, apis.STATUS_DELETE_FAILED, err.Error())
|
||||
logclient.AddActionLogWithStartable(self, ipset, logclient.ACT_DELETE, err, self.UserCred, false)
|
||||
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
|
||||
}
|
||||
|
||||
func (self *WafIPSetDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
ipset := obj.(*models.SWafIPSet)
|
||||
caches, err := ipset.GetCaches()
|
||||
iIpSet, err := ipset.GetICloudWafIPSet(ctx)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, ipset, errors.Wrapf(err, "GetCaches"))
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
self.taskComplete(ctx, ipset)
|
||||
return
|
||||
}
|
||||
self.taskFailed(ctx, ipset, errors.Wrapf(err, "GetICloudWafIPSet"))
|
||||
return
|
||||
}
|
||||
for i := range caches {
|
||||
iCache, err := caches[i].GetICloudWafIPSet(ctx)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
caches[i].RealDelete(ctx, self.GetUserCred())
|
||||
continue
|
||||
}
|
||||
self.taskFailed(ctx, ipset, errors.Wrapf(err, "GetICloudWafIPSet"))
|
||||
return
|
||||
}
|
||||
err = iCache.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, ipset, errors.Wrapf(err, "iCache.Delete"))
|
||||
return
|
||||
}
|
||||
caches[i].RealDelete(ctx, self.GetUserCred())
|
||||
err = iIpSet.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, ipset, errors.Wrapf(err, "iCache.Delete"))
|
||||
return
|
||||
}
|
||||
self.taskComplete(ctx, ipset)
|
||||
}
|
||||
|
||||
@@ -1,67 +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 tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type WafRegexSetCacheDeleteTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(WafRegexSetCacheDeleteTask{})
|
||||
}
|
||||
|
||||
func (self *WafRegexSetCacheDeleteTask) taskFailed(ctx context.Context, cache *models.SWafRegexSetCache, err error) {
|
||||
cache.SetStatus(ctx, self.UserCred, api.WAF_REGEX_SET_STATUS_DELETE_FAILED, err.Error())
|
||||
logclient.AddActionLogWithStartable(self, cache, logclient.ACT_DELETE, err, self.UserCred, false)
|
||||
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
|
||||
}
|
||||
|
||||
func (self *WafRegexSetCacheDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
cache := obj.(*models.SWafRegexSetCache)
|
||||
iCache, err := cache.GetICloudWafRegexSet(ctx)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
self.taskComplete(ctx, cache)
|
||||
return
|
||||
}
|
||||
self.taskFailed(ctx, cache, errors.Wrapf(err, "GetICloudWafRegexSet"))
|
||||
return
|
||||
}
|
||||
err = iCache.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, cache, errors.Wrapf(err, "iCache.Delete"))
|
||||
return
|
||||
}
|
||||
self.taskComplete(ctx, cache)
|
||||
}
|
||||
|
||||
func (self *WafRegexSetCacheDeleteTask) taskComplete(ctx context.Context, cache *models.SWafRegexSetCache) {
|
||||
cache.RealDelete(ctx, self.GetUserCred())
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
@@ -37,34 +37,26 @@ func init() {
|
||||
}
|
||||
|
||||
func (self *WafRegexSetDeleteTask) taskFailed(ctx context.Context, regexset *models.SWafRegexSet, err error) {
|
||||
regexset.SetStatus(ctx, self.UserCred, api.WAF_REGEX_SET_STATUS_DELETE_FAILED, err.Error())
|
||||
regexset.SetStatus(ctx, self.UserCred, apis.STATUS_DELETE_FAILED, err.Error())
|
||||
logclient.AddActionLogWithStartable(self, regexset, logclient.ACT_DELETE, err, self.UserCred, false)
|
||||
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
|
||||
}
|
||||
|
||||
func (self *WafRegexSetDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
regexset := obj.(*models.SWafRegexSet)
|
||||
caches, err := regexset.GetCaches()
|
||||
iSet, err := regexset.GetICloudWafRegexSet(ctx)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, regexset, errors.Wrapf(err, "GetCaches"))
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
self.taskComplete(ctx, regexset)
|
||||
return
|
||||
}
|
||||
self.taskFailed(ctx, regexset, errors.Wrapf(err, "GetICloudWafRegexSet"))
|
||||
return
|
||||
}
|
||||
for i := range caches {
|
||||
iCache, err := caches[i].GetICloudWafRegexSet(ctx)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
caches[i].RealDelete(ctx, self.GetUserCred())
|
||||
continue
|
||||
}
|
||||
self.taskFailed(ctx, regexset, errors.Wrapf(err, "GetICloudWafRegexSet"))
|
||||
return
|
||||
}
|
||||
err = iCache.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, regexset, errors.Wrapf(err, "iCache.Delete"))
|
||||
return
|
||||
}
|
||||
caches[i].RealDelete(ctx, self.GetUserCred())
|
||||
err = iSet.Delete()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, regexset, errors.Wrapf(err, "iCache.Delete"))
|
||||
return
|
||||
}
|
||||
self.taskComplete(ctx, regexset)
|
||||
}
|
||||
|
||||
@@ -1,32 +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 compute
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
WafIPSetCaches modulebase.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
WafIPSetCaches = modules.NewComputeManager("waf_ipset_cache", "waf_ipset_caches",
|
||||
[]string{"ID", "Name", "Status", "Cloudregion", "Provider", "Account", "Type", "Domain_Id", "Domain", "Metadata"},
|
||||
[]string{})
|
||||
|
||||
modules.RegisterCompute(&WafIPSetCaches)
|
||||
}
|
||||
@@ -1,32 +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 compute
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
WafRegexSetCaches modulebase.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
WafRegexSetCaches = modules.NewComputeManager("waf_regexset_cache", "waf_regexset_caches",
|
||||
[]string{"ID", "Name", "Status", "Cloudregion", "Provider", "Account", "Type", "Domain_Id", "Domain", "Metadata"},
|
||||
[]string{})
|
||||
|
||||
modules.RegisterCompute(&WafRegexSetCaches)
|
||||
}
|
||||
Reference in New Issue
Block a user