waf_rule_create_task.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type WafRuleCreateTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(WafRuleCreateTask{})
  31. }
  32. func (self *WafRuleCreateTask) taskFailed(ctx context.Context, rule *models.SWafRule, err error) {
  33. rule.SetStatus(ctx, self.UserCred, api.WAF_RULE_STATUS_CREATE_FAILED, err.Error())
  34. logclient.AddActionLogWithStartable(self, rule, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  35. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  36. }
  37. func (self *WafRuleCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  38. rule := obj.(*models.SWafRule)
  39. iWaf, err := rule.GetICloudWafInstance(ctx)
  40. if err != nil {
  41. self.taskFailed(ctx, rule, errors.Wrapf(err, "GetICloudWafInstance"))
  42. return
  43. }
  44. opts := cloudprovider.SWafRule{
  45. Name: rule.Name,
  46. Desc: rule.Description,
  47. Action: rule.Action,
  48. Priority: rule.Priority,
  49. Type: rule.Type,
  50. Expression: rule.Expression,
  51. Config: rule.Config,
  52. Enable: rule.Enabled.Bool(),
  53. Statements: []cloudprovider.SWafStatement{},
  54. }
  55. opts.StatementCondition = rule.StatementConditon
  56. statements, err := rule.GetRuleStatements()
  57. if err != nil {
  58. self.taskFailed(ctx, rule, errors.Wrapf(err, "GetRuleStatements"))
  59. return
  60. }
  61. for i := range statements {
  62. opts.Statements = append(opts.Statements, statements[i].SWafStatement)
  63. }
  64. iRule, err := iWaf.AddRule(&opts)
  65. if err != nil {
  66. self.taskFailed(ctx, rule, errors.Wrapf(err, "iWaf.AddRule"))
  67. return
  68. }
  69. rule.SyncWithCloudRule(ctx, self.GetUserCred(), iRule)
  70. self.taskComplete(ctx, rule)
  71. }
  72. func (self *WafRuleCreateTask) taskComplete(ctx context.Context, rule *models.SWafRule) {
  73. self.SetStageComplete(ctx, nil)
  74. }