secgrouprules.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ucloud
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/pkg/util/secrules"
  20. )
  21. type SecurityGroupRule struct {
  22. secgroup *SSecurityGroup
  23. DstPort string `json:"DstPort"`
  24. Priority string `json:"Priority"`
  25. ProtocolType string `json:"ProtocolType"`
  26. RuleAction string `json:"RuleAction"`
  27. SrcIP string `json:"SrcIP"`
  28. Remark string
  29. }
  30. func (self SecurityGroupRule) String() string {
  31. return fmt.Sprintf("%s|%s|%s|%s|%s|%s", self.ProtocolType, self.DstPort, self.SrcIP, self.RuleAction, self.Priority, self.Remark)
  32. }
  33. func (self *SecurityGroupRule) GetGlobalId() string {
  34. return fmt.Sprintf("%s|%s|%s|%s|%s", self.Priority, self.RuleAction, self.SrcIP, self.DstPort, self.ProtocolType)
  35. }
  36. func (self *SecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  37. if self.RuleAction == "ACCEPT" {
  38. return secrules.SecurityRuleAllow
  39. }
  40. return secrules.SecurityRuleDeny
  41. }
  42. func (self *SecurityGroupRule) GetDescription() string {
  43. return self.Remark
  44. }
  45. func (self *SecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
  46. return secrules.DIR_IN
  47. }
  48. func (self *SecurityGroupRule) GetCIDRs() []string {
  49. return []string{self.SrcIP}
  50. }
  51. func (self *SecurityGroupRule) GetProtocol() string {
  52. return strings.ToLower(self.ProtocolType)
  53. }
  54. func (self *SecurityGroupRule) GetPorts() string {
  55. return self.DstPort
  56. }
  57. func (self *SecurityGroupRule) GetPriority() int {
  58. switch self.Priority {
  59. case "HIGH":
  60. return 1
  61. case "MEDIUM":
  62. return 2
  63. case "LOW":
  64. return 3
  65. }
  66. return 1
  67. }
  68. func (self *SecurityGroupRule) Delete() error {
  69. params := NewUcloudParams()
  70. params.Set("FWId", self.secgroup.FWID)
  71. idx := 0
  72. for _, rule := range self.secgroup.Rule {
  73. if rule.GetGlobalId() == self.GetGlobalId() {
  74. continue
  75. }
  76. params.Set(fmt.Sprintf("Rule.%d", idx), rule.String())
  77. idx++
  78. }
  79. return self.secgroup.region.DoAction("UpdateFirewall", params, nil)
  80. }
  81. func (self *SecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  82. return cloudprovider.ErrNotImplemented
  83. }