secgrouprules.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 openstack
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/util/secrules"
  21. )
  22. type SSecurityGroupRule struct {
  23. region *SRegion
  24. Direction string
  25. Ethertype string
  26. Id string
  27. PortRangeMax int
  28. PortRangeMin int
  29. Protocol string
  30. RemoteGroupId string
  31. RemoteIpPrefix string
  32. SecurityGroupId string
  33. ProjectId string
  34. RevisionNumber int
  35. Tags []string
  36. TenantId string
  37. CreatedAt time.Time
  38. UpdatedAt time.Time
  39. Description string
  40. }
  41. func (self *SSecurityGroupRule) GetGlobalId() string {
  42. return self.Id
  43. }
  44. func (self *SSecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  45. return secrules.SecurityRuleAllow
  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.RemoteIpPrefix + self.RemoteGroupId
  58. if len(ip) == 0 {
  59. ip = "0.0.0.0/0"
  60. }
  61. ret := []string{ip}
  62. return ret
  63. }
  64. func (self *SSecurityGroupRule) GetProtocol() string {
  65. if len(self.Protocol) == 0 || self.Protocol == "-1" {
  66. return secrules.PROTO_ANY
  67. }
  68. strings.ReplaceAll(self.Protocol, "6", "tcp")
  69. strings.ReplaceAll(self.Protocol, "17", "udp")
  70. strings.ReplaceAll(self.Protocol, "1", "icmp")
  71. return self.Protocol
  72. }
  73. func (self *SSecurityGroupRule) GetPorts() string {
  74. if self.PortRangeMax > 0 && self.PortRangeMin > 0 {
  75. return fmt.Sprintf("%d-%d", self.PortRangeMin, self.PortRangeMax)
  76. }
  77. return ""
  78. }
  79. func (self *SSecurityGroupRule) GetPriority() int {
  80. return 0
  81. }
  82. func (self *SSecurityGroupRule) Delete() error {
  83. return self.region.DeleteSecurityGroupRule(self.Id)
  84. }
  85. func (self *SSecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  86. return cloudprovider.ErrNotImplemented
  87. }