secgrouprule.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2023 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 volcengine
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/pkg/util/secrules"
  20. )
  21. type SCidrList []string
  22. type SSecurityGroupRule struct {
  23. secgroup *SSecurityGroup
  24. CreationTime time.Time
  25. UpdateTime time.Time
  26. Description string
  27. Direction string
  28. Protocol string
  29. Policy string
  30. PortStart int
  31. PortEnd int
  32. CidrIp string
  33. PrefixListId string
  34. PrefixListCidrs SCidrList
  35. Priority int
  36. SourceGroupId string
  37. }
  38. func (self *SSecurityGroupRule) GetGlobalId() string {
  39. return fmt.Sprintf("%d|%s|%s|%s|%s|%s|%s|%d|%d", self.Priority, self.Direction, self.Policy, self.Protocol, self.CidrIp, self.PrefixListId, self.SourceGroupId, self.PortStart, self.PortEnd)
  40. }
  41. func (self *SSecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  42. if self.Policy == "accept" {
  43. return secrules.SecurityRuleAllow
  44. }
  45. return secrules.SecurityRuleDeny
  46. }
  47. func (self *SSecurityGroupRule) GetDescription() string {
  48. return self.Description
  49. }
  50. func (self *SSecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
  51. if self.Direction == "egress" {
  52. return secrules.DIR_OUT
  53. }
  54. return secrules.DIR_IN
  55. }
  56. func (self *SSecurityGroupRule) GetCIDRs() []string {
  57. ip := self.CidrIp + self.PrefixListId + self.SourceGroupId
  58. ret := []string{ip}
  59. if len(self.PrefixListCidrs) > 0 {
  60. ret = append(ret, self.PrefixListCidrs...)
  61. }
  62. return ret
  63. }
  64. func (self *SSecurityGroupRule) GetProtocol() string {
  65. if len(self.Protocol) == 0 || self.Protocol == "all" {
  66. return secrules.PROTO_ANY
  67. }
  68. return self.Protocol
  69. }
  70. func (self *SSecurityGroupRule) GetPorts() string {
  71. if self.PortStart > 0 && self.PortEnd > 0 {
  72. if self.PortStart == self.PortEnd {
  73. return fmt.Sprintf("%d", self.PortStart)
  74. }
  75. return fmt.Sprintf("%d-%d", self.PortStart, self.PortEnd)
  76. }
  77. return ""
  78. }
  79. func (self *SSecurityGroupRule) GetPriority() int {
  80. return self.Priority
  81. }
  82. func (self *SSecurityGroupRule) Delete() error {
  83. params := map[string]string{
  84. "SecurityGroupId": self.secgroup.SecurityGroupId,
  85. "Protocol": self.Protocol,
  86. "PortStart": fmt.Sprintf("%d", self.PortStart),
  87. "PortEnd": fmt.Sprintf("%d", self.PortEnd),
  88. "Policy": self.Policy,
  89. "Priority": fmt.Sprintf("%d", self.Priority),
  90. }
  91. if len(self.CidrIp) > 0 {
  92. params["CidrIp"] = self.CidrIp
  93. }
  94. if len(self.PrefixListId) > 0 {
  95. params["PrefixListId"] = self.PrefixListId
  96. }
  97. if len(self.SourceGroupId) > 0 {
  98. params["SourceGroupId"] = self.SourceGroupId
  99. }
  100. action := "RevokeSecurityGroupIngress"
  101. if self.Direction == "egress" {
  102. action = "RevokeSecurityGroupEgress"
  103. }
  104. _, err := self.secgroup.region.vpcRequest(action, params)
  105. return err
  106. }
  107. func (self *SSecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  108. return cloudprovider.ErrNotImplemented
  109. }