waf_rule_set_enable_task.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/apis"
  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. )
  26. type WafRuleSetEnabledTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(WafRuleSetEnabledTask{})
  31. }
  32. func (self *WafRuleSetEnabledTask) taskFailed(ctx context.Context, record *models.SWafRule, err error) {
  33. record.SetStatus(ctx, self.UserCred, apis.STATUS_UNKNOWN, err.Error())
  34. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  35. }
  36. func (self *WafRuleSetEnabledTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  37. record := obj.(*models.SWafRule)
  38. iRule, err := record.GetICloudWafRule(ctx)
  39. if err != nil {
  40. self.taskFailed(ctx, record, errors.Wrapf(err, "GetICloudWafRule"))
  41. return
  42. }
  43. if record.Enabled.Bool() {
  44. err = iRule.Enable()
  45. } else {
  46. err = iRule.Disable()
  47. }
  48. if err != nil {
  49. if errors.Cause(err) == cloudprovider.ErrNotSupported {
  50. self.taskComplete(ctx, record)
  51. return
  52. }
  53. self.taskFailed(ctx, record, errors.Wrapf(err, "SetEnabled"))
  54. return
  55. }
  56. self.taskComplete(ctx, record)
  57. }
  58. func (self *WafRuleSetEnabledTask) taskComplete(ctx context.Context, record *models.SWafRule) {
  59. record.SetStatus(ctx, self.UserCred, api.WAF_RULE_STATUS_AVAILABLE, "")
  60. self.SetStageComplete(ctx, nil)
  61. }