waf_syncstatus_task.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package waf
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type WafSyncstatusTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(WafSyncstatusTask{})
  32. }
  33. func (self *WafSyncstatusTask) taskFailed(ctx context.Context, waf *models.SWafInstance, err error) {
  34. waf.SetStatus(ctx, self.UserCred, api.WAF_STATUS_UNKNOWN, err.Error())
  35. logclient.AddActionLogWithStartable(self, waf, logclient.ACT_SYNC_STATUS, err, self.UserCred, false)
  36. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  37. }
  38. func (self *WafSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  39. waf := obj.(*models.SWafInstance)
  40. iWaf, err := waf.GetICloudWafInstance(ctx)
  41. if err != nil {
  42. self.taskFailed(ctx, waf, errors.Wrapf(err, "GetICloudWafInstance"))
  43. return
  44. }
  45. waf.SyncWithCloudWafInstance(ctx, self.GetUserCred(), iWaf)
  46. rules, err := iWaf.GetRules()
  47. if err != nil {
  48. if errors.Cause(err) == cloudprovider.ErrNotImplemented || errors.Cause(err) == cloudprovider.ErrNotSupported {
  49. self.SetStageComplete(ctx, nil)
  50. return
  51. }
  52. self.taskFailed(ctx, waf, errors.Wrapf(err, "GetRules"))
  53. return
  54. }
  55. result := waf.SyncWafRules(ctx, self.GetUserCred(), rules)
  56. log.Infof("Sync waf %s rules result: %s", waf.Name, result.Result())
  57. self.SetStageComplete(ctx, nil)
  58. }