mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 06:09:39 +08:00
fix: support azure policy
This commit is contained in:
@@ -26,7 +26,6 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/appsrv"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
)
|
||||
@@ -77,7 +76,7 @@ func (self *CloudProviderSyncInfoTask) GetSyncRange() models.SSyncRange {
|
||||
func (self *CloudProviderSyncInfoTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
provider := obj.(*models.SCloudprovider)
|
||||
|
||||
self.SetStage("OnSyncCloudProviderQuotaInfoComplete", nil)
|
||||
self.SetStage("OnSyncCloudProviderPreInfoComplete", nil)
|
||||
|
||||
taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
|
||||
p, err := provider.GetProvider()
|
||||
@@ -85,22 +84,25 @@ func (self *CloudProviderSyncInfoTask) OnInit(ctx context.Context, obj db.IStand
|
||||
return nil, errors.Wrap(err, "GetProvider")
|
||||
}
|
||||
quotas, err := p.GetICloudQuotas()
|
||||
if err != nil {
|
||||
if errors.Cause(err) != cloudprovider.ErrNotImplemented {
|
||||
return nil, errors.Wrap(err, "GetICloudQuotas")
|
||||
}
|
||||
return nil, nil
|
||||
if err == nil {
|
||||
result := models.CloudproviderQuotaManager.SyncQuotas(ctx, self.GetUserCred(), provider.GetOwnerId(), provider, nil, api.CLOUD_PROVIDER_QUOTA_RANGE_CLOUDPROVIDER, quotas)
|
||||
msg := result.Result()
|
||||
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)
|
||||
}
|
||||
result := models.CloudproviderQuotaManager.SyncQuotas(ctx, self.GetUserCred(), provider.GetOwnerId(), provider, nil, api.CLOUD_PROVIDER_QUOTA_RANGE_CLOUDPROVIDER, quotas)
|
||||
msg := result.Result()
|
||||
notes := fmt.Sprintf("SyncQuotas for provider %s result: %s", provider.Name, msg)
|
||||
log.Infof(notes)
|
||||
return nil, nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderQuotaInfoComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderPreInfoComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
provider := obj.(*models.SCloudprovider)
|
||||
syncRange := self.GetSyncRange()
|
||||
|
||||
@@ -113,9 +115,9 @@ func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderQuotaInfoComplete(ctx
|
||||
})
|
||||
}
|
||||
|
||||
func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderQuotaInfoCompleteFailed(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderPreInfoCompleteFailed(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
log.Errorf("faild to sync provider quotas %s", body.String())
|
||||
self.OnSyncCloudProviderQuotaInfoComplete(ctx, obj, body)
|
||||
self.OnSyncCloudProviderPreInfoComplete(ctx, obj, body)
|
||||
}
|
||||
|
||||
func (self *CloudProviderSyncInfoTask) OnSyncCloudProviderInfoComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// 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))
|
||||
}
|
||||
Reference in New Issue
Block a user