secgrouprule.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 compute
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/secrules"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. type SecGroupRulesListOptions struct {
  22. options.BaseListOptions
  23. Secgroup string `help:"Secgroup ID or Name"`
  24. SecgroupName string `help:"Search rules by fuzzy secgroup name"`
  25. Projects []string `help:"Filter rules by project"`
  26. Direction string `help:"filter Direction of rule" choices:"in|out"`
  27. Protocol string `help:"filter Protocol of rule" choices:"any|tcp|udp|icmp"`
  28. Action string `help:"filter Actin of rule" choices:"allow|deny"`
  29. Ports string `help:"filter Ports of rule"`
  30. Ip string `help:"filter cidr of rule"`
  31. }
  32. func (opts *SecGroupRulesListOptions) Params() (jsonutils.JSONObject, error) {
  33. return options.ListStructToParams(opts)
  34. }
  35. type SecGroupRulesCreateOptions struct {
  36. SECGROUP string `help:"Secgroup ID or Name" metavar:"Secgroup"`
  37. RULE string `json:"-"`
  38. Priority int64 `help:"priority of Rule" default:"50"`
  39. Desc string `help:"Description" json:"description"`
  40. }
  41. func (opts *SecGroupRulesCreateOptions) Params() (jsonutils.JSONObject, error) {
  42. rule, err := secrules.ParseSecurityRule(opts.RULE)
  43. if err != nil {
  44. return nil, errors.Wrapf(err, "invalid rule %s", opts.RULE)
  45. }
  46. return jsonutils.Marshal(map[string]interface{}{
  47. "direction": rule.Direction,
  48. "action": rule.Action,
  49. "protocol": rule.Protocol,
  50. "cidr": rule.IPNet.String(),
  51. "ports": rule.GetPortsString(),
  52. "priority": opts.Priority,
  53. "description": opts.Desc,
  54. "secgroup_id": opts.SECGROUP,
  55. }), nil
  56. }
  57. type SecGroupRulesUpdateOptions struct {
  58. options.BaseIdOptions
  59. Name string `help:"New name of rule"`
  60. Priority int64 `help:"priority of Rule"`
  61. Protocol string `help:"Protocol of rule" choices:"any|tcp|udp|icmp"`
  62. Ports string `help:"Ports of rule"`
  63. Cidr string `help:"Cidr of rule"`
  64. Action string `help:"filter Actin of rule" choices:"allow|deny"`
  65. Desc string `help:"Description" metavar:"Description"`
  66. }
  67. func (opts *SecGroupRulesUpdateOptions) Params() (jsonutils.JSONObject, error) {
  68. params := jsonutils.NewDict()
  69. if len(opts.Name) > 0 {
  70. params.Add(jsonutils.NewString(opts.Name), "name")
  71. }
  72. if len(opts.Desc) > 0 {
  73. params.Add(jsonutils.NewString(opts.Desc), "description")
  74. }
  75. if opts.Priority > 0 {
  76. params.Add(jsonutils.NewInt(opts.Priority), "priority")
  77. }
  78. if len(opts.Protocol) > 0 {
  79. params.Add(jsonutils.NewString(opts.Protocol), "protocol")
  80. }
  81. if len(opts.Ports) > 0 {
  82. params.Add(jsonutils.NewString(opts.Ports), "ports")
  83. }
  84. if len(opts.Cidr) > 0 {
  85. params.Add(jsonutils.NewString(opts.Cidr), "cidr")
  86. }
  87. if len(opts.Action) > 0 {
  88. params.Add(jsonutils.NewString(opts.Action), "action")
  89. }
  90. return params, nil
  91. }