secgrouprule.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 apsara
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/util/secrules"
  21. "yunion.io/x/pkg/utils"
  22. )
  23. type SPermission struct {
  24. region *SRegion
  25. CreateTime time.Time
  26. Description string
  27. DestCidrIp string
  28. DestGroupId string
  29. DestGroupName string
  30. DestGroupOwnerAccount string
  31. Direction string
  32. IpProtocol string
  33. NicType SecurityGroupPermissionNicType
  34. Policy string
  35. PortRange string
  36. Priority int
  37. SourceCidrIp string
  38. SourceGroupId string
  39. SourceGroupName string
  40. SourceGroupOwnerAccount string
  41. SecurityGroupRuleId string
  42. SecurityGroupId string
  43. }
  44. func (self *SPermission) GetGlobalId() string {
  45. return self.SecurityGroupRuleId
  46. }
  47. func (self *SPermission) GetAction() secrules.TSecurityRuleAction {
  48. if self.Policy == "Drop" {
  49. return secrules.SecurityRuleDeny
  50. }
  51. return secrules.SecurityRuleAllow
  52. }
  53. func (self *SPermission) GetDescription() string {
  54. return self.Description
  55. }
  56. func (self *SPermission) GetDirection() secrules.TSecurityRuleDirection {
  57. if self.Direction == "ingress" {
  58. return secrules.DIR_IN
  59. }
  60. return secrules.DIR_OUT
  61. }
  62. func (self *SPermission) GetCIDRs() []string {
  63. ret := []string{}
  64. if len(self.SourceCidrIp) > 0 {
  65. ret = append(ret, self.SourceCidrIp)
  66. }
  67. if len(self.SourceGroupId) > 0 {
  68. ret = append(ret, self.SourceGroupId)
  69. }
  70. if len(self.DestGroupId) > 0 {
  71. ret = append(ret, self.SourceGroupId)
  72. }
  73. if len(self.DestCidrIp) > 0 {
  74. ret = append(ret, self.DestCidrIp)
  75. }
  76. return ret
  77. }
  78. func (self *SPermission) GetProtocol() string {
  79. if strings.ToLower(self.IpProtocol) == "all" {
  80. return secrules.PROTO_ANY
  81. }
  82. return strings.ToLower(self.IpProtocol)
  83. }
  84. func (self *SPermission) GetPorts() string {
  85. if self.PortRange == "-1/-1" || self.PortRange == "1/65535" || self.PortRange == "" {
  86. return ""
  87. }
  88. info := strings.Split(self.PortRange, "/")
  89. if len(info) != 2 {
  90. return ""
  91. }
  92. if info[0] == info[1] {
  93. if info[0] == "-1" {
  94. return ""
  95. }
  96. return info[0]
  97. }
  98. return fmt.Sprintf("%s-%s", info[0], info[1])
  99. }
  100. func (self *SPermission) GetPriority() int {
  101. return self.Priority
  102. }
  103. func (self *SRegion) DeleteSecurityGroupRule(groupId string, direction secrules.TSecurityRuleDirection, ruleId string) error {
  104. action := "RevokeSecurityGroup"
  105. if direction == secrules.DIR_OUT {
  106. action = "RevokeSecurityGroupEgress"
  107. }
  108. params := map[string]string{
  109. "RegionId": self.RegionId,
  110. "ClientToken": utils.GenRequestId(20),
  111. "SecurityGroupId": groupId,
  112. "SecurityGroupRuleId.1": ruleId,
  113. }
  114. _, err := self.ecsRequest(action, params)
  115. return err
  116. }
  117. func (self *SPermission) Delete() error {
  118. return self.region.DeleteSecurityGroupRule(self.SecurityGroupId, self.GetDirection(), self.SecurityGroupRuleId)
  119. }
  120. func (self *SPermission) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  121. return cloudprovider.ErrNotImplemented
  122. }