mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
Merge pull request #6605 from ioito/hotfix/qx-remove-azure-policy-sync
fix: 移除azure policy sync部分
This commit is contained in:
@@ -217,7 +217,6 @@ type ICloudProvider interface {
|
||||
|
||||
GetCapabilities() []string
|
||||
GetICloudQuotas() ([]ICloudQuota, error)
|
||||
GetICloudPolicyDefinitions() ([]ICloudPolicyDefinition, error)
|
||||
}
|
||||
|
||||
func IsSupportProject(prod ICloudProvider) bool {
|
||||
@@ -321,10 +320,6 @@ func (self *SBaseProvider) GetICloudQuotas() ([]ICloudQuota, error) {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SBaseProvider) GetICloudPolicyDefinitions() ([]ICloudPolicyDefinition, error) {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SBaseProvider) GetCloudRegionExternalIdPrefix() string {
|
||||
return self.factory.GetId()
|
||||
}
|
||||
|
||||
@@ -953,11 +953,3 @@ type ICloudQuota interface {
|
||||
GetMaxQuotaCount() int
|
||||
GetCurrentQuotaUsedCount() int
|
||||
}
|
||||
|
||||
type ICloudPolicyDefinition interface {
|
||||
GetGlobalId() string
|
||||
GetName() string
|
||||
GetCategory() string
|
||||
GetCondition() string
|
||||
GetParameters() *jsonutils.JSONDict
|
||||
}
|
||||
|
||||
@@ -16,18 +16,13 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/pkg/utils"
|
||||
"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/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
@@ -36,7 +31,6 @@ import (
|
||||
type SPolicyDefinitionManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
db.SExternalizedResourceBaseManager
|
||||
SManagedResourceBaseManager
|
||||
}
|
||||
|
||||
var PolicyDefinitionManager *SPolicyDefinitionManager
|
||||
@@ -56,7 +50,6 @@ func init() {
|
||||
type SPolicyDefinition struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SExternalizedResourceBase
|
||||
SManagedResourceBase
|
||||
|
||||
// 参数
|
||||
Parameters *jsonutils.JSONDict `get:"domain" list:"domain" create:"admin_optional"`
|
||||
@@ -75,11 +68,6 @@ func (manager *SPolicyDefinitionManager) ListItemFilter(ctx context.Context, q *
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q, err = manager.SManagedResourceBaseManager.ListItemFilter(ctx, q, userCred, query.ManagedResourceListInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return q, nil
|
||||
}
|
||||
|
||||
@@ -103,10 +91,6 @@ func (manager *SPolicyDefinitionManager) QueryDistinctExtraField(q *sqlchemy.SQu
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
q, err = manager.SManagedResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
|
||||
return q, httperrors.ErrNotFound
|
||||
}
|
||||
@@ -149,118 +133,6 @@ func (manager *SPolicyDefinitionManager) GetAvailablePolicyDefinitions(ctx conte
|
||||
return definitions, nil
|
||||
}
|
||||
|
||||
func (manager *SPolicyDefinitionManager) SyncPolicyDefinitions(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, provider *SCloudprovider, iDefinitions []cloudprovider.ICloudPolicyDefinition) compare.SyncResult {
|
||||
lockman.LockClass(ctx, manager, db.GetLockClassKey(manager, userCred))
|
||||
defer lockman.ReleaseClass(ctx, manager, db.GetLockClassKey(manager, userCred))
|
||||
|
||||
syncResult := compare.SyncResult{}
|
||||
|
||||
dbDefinitions, err := manager.getPolicyDefinitionsByManagerId(provider.Id)
|
||||
if err != nil {
|
||||
syncResult.Error(err)
|
||||
return syncResult
|
||||
}
|
||||
|
||||
removed := make([]SPolicyDefinition, 0)
|
||||
commondb := make([]SPolicyDefinition, 0)
|
||||
commonext := make([]cloudprovider.ICloudPolicyDefinition, 0)
|
||||
added := make([]cloudprovider.ICloudPolicyDefinition, 0)
|
||||
|
||||
err = compare.CompareSets(dbDefinitions, iDefinitions, &removed, &commondb, &commonext, &added)
|
||||
if err != nil {
|
||||
syncResult.Error(err)
|
||||
return syncResult
|
||||
}
|
||||
|
||||
for i := 0; i < len(removed); i += 1 {
|
||||
err = removed[i].purge(ctx, userCred)
|
||||
if err != nil {
|
||||
syncResult.DeleteError(err)
|
||||
continue
|
||||
}
|
||||
syncResult.Delete()
|
||||
}
|
||||
for i := 0; i < len(commondb); i += 1 {
|
||||
err = commondb[i].SyncWithCloudPolicyDefinition(ctx, userCred, provider, commonext[i])
|
||||
if err != nil {
|
||||
syncResult.UpdateError(err)
|
||||
continue
|
||||
}
|
||||
syncResult.Update()
|
||||
}
|
||||
for i := 0; i < len(added); i += 1 {
|
||||
err = manager.newFromCloudPolicyDefinition(ctx, userCred, added[i], provider)
|
||||
if err != nil {
|
||||
syncResult.AddError(err)
|
||||
continue
|
||||
}
|
||||
syncResult.Add()
|
||||
}
|
||||
return syncResult
|
||||
}
|
||||
|
||||
func (self *SPolicyDefinition) constructParameters(ctx context.Context, userCred mcclient.TokenCredential, extDefinition cloudprovider.ICloudPolicyDefinition) error {
|
||||
self.Category = extDefinition.GetCategory()
|
||||
self.Condition = extDefinition.GetCondition()
|
||||
switch self.Category {
|
||||
case api.POLICY_DEFINITION_CATEGORY_CLOUDREGION:
|
||||
if !utils.IsInStringArray(self.Condition, []string{api.POLICY_DEFINITION_CONDITION_NOT_IN, api.POLICY_DEFINITION_CONDITION_IN}) {
|
||||
return fmt.Errorf("not support category %s condition %s", self.Category, self.Condition)
|
||||
}
|
||||
parameters := extDefinition.GetParameters()
|
||||
if parameters == nil {
|
||||
return fmt.Errorf("invalid parameters")
|
||||
}
|
||||
cloudregions := []string{}
|
||||
err := parameters.Unmarshal(&cloudregions, "cloudregions")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parameters.Unmarshal")
|
||||
}
|
||||
regions := api.SCloudregionPolicyDefinitions{Cloudregions: []api.SCloudregionPolicyDefinition{}}
|
||||
for _, cloudregion := range cloudregions {
|
||||
region, err := db.FetchByExternalId(CloudregionManager, cloudregion)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "db.FetchByExternalId(%s)", cloudregion)
|
||||
}
|
||||
regionPolicyDefinition := api.SCloudregionPolicyDefinition{
|
||||
Id: region.GetId(),
|
||||
Name: region.GetName(),
|
||||
}
|
||||
regions.Cloudregions = append(regions.Cloudregions, regionPolicyDefinition)
|
||||
}
|
||||
self.Parameters = jsonutils.Marshal(regions).(*jsonutils.JSONDict)
|
||||
case api.POLICY_DEFINITION_CATEGORY_TAG:
|
||||
self.Parameters = extDefinition.GetParameters()
|
||||
default:
|
||||
return fmt.Errorf("not support category %s", self.Category)
|
||||
}
|
||||
self.Status = api.POLICY_DEFINITION_STATUS_READY
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *SPolicyDefinitionManager) newFromCloudPolicyDefinition(ctx context.Context, userCred mcclient.TokenCredential, extDefinition cloudprovider.ICloudPolicyDefinition, provider *SCloudprovider) error {
|
||||
definition := SPolicyDefinition{}
|
||||
definition.SetModelManager(manager, &definition)
|
||||
|
||||
newName, err := db.GenerateName(manager, userCred, extDefinition.GetName())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "db.GenerateName")
|
||||
}
|
||||
|
||||
definition.Name = newName
|
||||
definition.ManagerId = provider.Id
|
||||
definition.Status = api.POLICY_DEFINITION_STATUS_READY
|
||||
definition.ExternalId = extDefinition.GetGlobalId()
|
||||
definition.constructParameters(ctx, userCred, extDefinition)
|
||||
|
||||
err = manager.TableSpec().Insert(&definition)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Insert")
|
||||
}
|
||||
|
||||
return PolicyAssignmentManager.newAssignment(&definition, provider.DomainId)
|
||||
}
|
||||
|
||||
func (self *SPolicyDefinition) GetPolicyAssignments() ([]SPolicyAssignment, error) {
|
||||
assignments := []SPolicyAssignment{}
|
||||
q := PolicyAssignmentManager.Query().Equals("policydefinition_id", self.Id)
|
||||
@@ -270,25 +142,3 @@ func (self *SPolicyDefinition) GetPolicyAssignments() ([]SPolicyAssignment, erro
|
||||
}
|
||||
return assignments, nil
|
||||
}
|
||||
|
||||
func (self *SPolicyDefinition) SyncWithCloudPolicyDefinition(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, extDefinition cloudprovider.ICloudPolicyDefinition) error {
|
||||
_, err := db.UpdateWithLock(ctx, self, func() error {
|
||||
return self.constructParameters(ctx, userCred, extDefinition)
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "db.UpdateWithLock")
|
||||
}
|
||||
return PolicyAssignmentManager.checkAndSetAssignment(self, provider.DomainId)
|
||||
}
|
||||
|
||||
func (self *SPolicyDefinition) AllowPerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return userCred.HasSystemAdminPrivilege()
|
||||
}
|
||||
|
||||
// 同步策略状态
|
||||
func (self *SPolicyDefinition) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.PolicyDefinitionSyncstatusInput) (jsonutils.JSONObject, error) {
|
||||
if len(self.ManagerId) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, StartResourceSyncStatusTask(ctx, userCred, self, "PolicyDefinitionSyncstatusTask", "")
|
||||
}
|
||||
|
||||
@@ -90,14 +90,6 @@ func (self *CloudProviderSyncInfoTask) OnInit(ctx context.Context, obj db.IStand
|
||||
notes := fmt.Sprintf("SyncQuotas for provider %s result: %s", provider.Name, msg)
|
||||
log.Infof(notes)
|
||||
}
|
||||
|
||||
policyDefinitions, err := p.GetICloudPolicyDefinitions()
|
||||
if err == nil {
|
||||
result := models.PolicyDefinitionManager.SyncPolicyDefinitions(ctx, self.GetUserCred(), provider.GetOwnerId(), provider, policyDefinitions)
|
||||
msg := result.Result()
|
||||
notes := fmt.Sprintf("SyncPolicyDefinitions for provider %s result: %s", provider.Name, msg)
|
||||
log.Infof(notes)
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,75 +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"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
|
||||
type PolicyDefinitionSyncstatusTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(PolicyDefinitionSyncstatusTask{})
|
||||
}
|
||||
|
||||
func (self *PolicyDefinitionSyncstatusTask) taskFailed(ctx context.Context, definition *models.SPolicyDefinition, err error) {
|
||||
definition.SetStatus(self.UserCred, api.POLICY_DEFINITION_STATUS_UNKNOWN, err.Error())
|
||||
db.OpsLog.LogEvent(definition, db.ACT_SYNC_STATUS, err.Error(), self.GetUserCred())
|
||||
logclient.AddActionLogWithStartable(self, definition, logclient.ACT_SYNC_STATUS, err.Error(), self.UserCred, false)
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
}
|
||||
|
||||
func (self *PolicyDefinitionSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
definition := obj.(*models.SPolicyDefinition)
|
||||
cloudprovider := definition.GetCloudprovider()
|
||||
if cloudprovider == nil {
|
||||
self.taskFailed(ctx, definition, fmt.Errorf("failed to get cloudprovider for policy definition %s", definition.Name))
|
||||
return
|
||||
}
|
||||
provider, err := cloudprovider.GetProvider()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, definition, errors.Wrap(err, "GetProvider"))
|
||||
return
|
||||
}
|
||||
policyDefinitions, err := provider.GetICloudPolicyDefinitions()
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, definition, errors.Wrap(err, "GetICloudPolicyDefinitions"))
|
||||
return
|
||||
}
|
||||
for i := range policyDefinitions {
|
||||
if policyDefinitions[i].GetGlobalId() == definition.ExternalId {
|
||||
err = definition.SyncWithCloudPolicyDefinition(ctx, self.GetUserCred(), cloudprovider, policyDefinitions[i])
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, definition, errors.Wrap(err, "SyncWithCloudPolicyDefinition"))
|
||||
return
|
||||
}
|
||||
self.SetStageComplete(ctx, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
self.taskFailed(ctx, definition, fmt.Errorf("failed to found policy definition %s from cloud", definition.Name))
|
||||
}
|
||||
@@ -17,15 +17,10 @@ package azure
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SPolicyDefinitonPropertieParameterMetadata struct {
|
||||
@@ -143,108 +138,3 @@ func (client *SAzureClient) GetPolicyAssignments(defineId string) ([]SPolicyAssi
|
||||
}
|
||||
return assignments, nil
|
||||
}
|
||||
|
||||
func (client *SAzureClient) GetICloudDefinitions() ([]cloudprovider.ICloudPolicyDefinition, error) {
|
||||
ret := []cloudprovider.ICloudPolicyDefinition{}
|
||||
definitions, err := client.GetPolicyDefinitions()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetPolicyDefinitions")
|
||||
}
|
||||
for i := range definitions {
|
||||
if definitions[i].Properties.PolicyRule.Then.Effect != "deny" {
|
||||
continue
|
||||
}
|
||||
rule := definitions[i].Properties.PolicyRule.If
|
||||
if rule.Contains("field") {
|
||||
field, _ := rule.GetString("field")
|
||||
if field == "location" {
|
||||
defaultValue := []string{}
|
||||
locationParameter := ""
|
||||
for k, v := range definitions[i].Properties.Parameters {
|
||||
if v.Metadata.StrongType == "location" {
|
||||
defaultValue = v.DefaultValue
|
||||
locationParameter = k
|
||||
break
|
||||
}
|
||||
}
|
||||
assignments, err := client.GetPolicyAssignments(definitions[i].Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetPolicyAssignments(%s)", definitions[i].Id)
|
||||
}
|
||||
for i := range assignments {
|
||||
location, ok := assignments[i].Properties.Parameters[locationParameter]
|
||||
if ok {
|
||||
if len(location.Value) > 0 {
|
||||
assignments[i].values = location.Value
|
||||
} else {
|
||||
assignments[i].values = defaultValue
|
||||
}
|
||||
}
|
||||
regionIds := jsonutils.NewArray()
|
||||
assignments[i].parameters = jsonutils.NewDict()
|
||||
for _, value := range assignments[i].values {
|
||||
region := client.GetRegion(value)
|
||||
if region != nil {
|
||||
regionIds.Add(jsonutils.NewString(region.GetGlobalId()))
|
||||
} else {
|
||||
log.Errorf("failed to found region %s", value)
|
||||
}
|
||||
}
|
||||
assignments[i].category = api.POLICY_DEFINITION_CATEGORY_CLOUDREGION
|
||||
if rule.Contains("in") {
|
||||
assignments[i].condition = api.POLICY_DEFINITION_CONDITION_NOT_IN
|
||||
} else if rule.Contains("notIn") {
|
||||
assignments[i].condition = api.POLICY_DEFINITION_CONDITION_IN
|
||||
}
|
||||
assignments[i].parameters.Add(regionIds, "cloudregions")
|
||||
ret = append(ret, &assignments[i])
|
||||
}
|
||||
} else if strings.Contains(field, "tags") {
|
||||
reg := regexp.MustCompile(`^\[concat\('tags\[', parameters\('\w+'\), '\]'\)\]$`)
|
||||
if !reg.MatchString(field) {
|
||||
continue
|
||||
}
|
||||
if rule.Contains("exists") {
|
||||
exists, _ := rule.Bool("exists")
|
||||
defaultValue := []string{}
|
||||
tagParameter := ""
|
||||
for k, v := range definitions[i].Properties.Parameters {
|
||||
tagParameter = k
|
||||
defaultValue = v.DefaultValue
|
||||
}
|
||||
assignments, err := client.GetPolicyAssignments(definitions[i].Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetPolicyAssignments(%s)", definitions[i].Id)
|
||||
}
|
||||
for i := range assignments {
|
||||
tag, ok := assignments[i].Properties.Parameters[tagParameter]
|
||||
if ok {
|
||||
if len(tag.Value) > 0 {
|
||||
assignments[i].values = tag.Value
|
||||
} else {
|
||||
assignments[i].values = defaultValue
|
||||
}
|
||||
}
|
||||
if len(assignments[i].values) == 0 {
|
||||
continue
|
||||
}
|
||||
tags := jsonutils.NewArray()
|
||||
for _, _tag := range assignments[i].values {
|
||||
tags.Add(jsonutils.NewString(_tag))
|
||||
}
|
||||
assignments[i].parameters = jsonutils.NewDict()
|
||||
assignments[i].category = api.POLICY_DEFINITION_CATEGORY_TAG
|
||||
if exists {
|
||||
assignments[i].condition = api.POLICY_DEFINITION_CONDITION_EXCEPT
|
||||
} else {
|
||||
assignments[i].condition = api.POLICY_DEFINITION_CONDITION_CONTAINS
|
||||
}
|
||||
assignments[i].parameters.Add(tags, "tags")
|
||||
ret = append(ret, &assignments[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@@ -173,10 +173,6 @@ func (self *SAzureProvider) GetIRegions() []cloudprovider.ICloudRegion {
|
||||
return self.client.GetIRegions()
|
||||
}
|
||||
|
||||
func (self *SAzureProvider) GetICloudPolicyDefinitions() ([]cloudprovider.ICloudPolicyDefinition, error) {
|
||||
return self.client.GetICloudDefinitions()
|
||||
}
|
||||
|
||||
func (self *SAzureProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
|
||||
return self.client.GetIRegionById(id)
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/multicloud/azure"
|
||||
"yunion.io/x/onecloud/pkg/util/shellutils"
|
||||
)
|
||||
@@ -59,15 +57,4 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
shellutils.R(&PolicyListOptions{}, "policy-definition-list-onecloud", "List convert policy assignment", func(cli *azure.SRegion, args *PolicyListOptions) error {
|
||||
definitions, err := cli.GetClient().GetICloudDefinitions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, definition := range definitions {
|
||||
fmt.Printf("definition %s category %s condition %s paramters: %s\n", definition.GetName(), definition.GetCategory(), definition.GetCondition(), definition.GetParameters())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user