secgrouprule.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 zstack
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/pkg/util/secrules"
  20. )
  21. type SSecurityGroupRule struct {
  22. region *SRegion
  23. ZStackBasic
  24. SecurityGroupUUID string `json:"securityGroupUuid"`
  25. Type string `json:"type"`
  26. IPVersion int `json:"ipVersion"`
  27. StartPort int `json:"startPort"`
  28. EndPort int `json:"endPort"`
  29. Protocol string `json:"protocol"`
  30. State string `json:"state"`
  31. AllowedCIDR string `json:"allowedCidr"`
  32. RemoteSecurityGroupUUID string `json:"remoteSecurityGroupUuid"`
  33. ZStackTime
  34. }
  35. func (self *SSecurityGroupRule) GetGlobalId() string {
  36. return self.UUID
  37. }
  38. func (self *SSecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  39. return secrules.SecurityRuleAllow
  40. }
  41. func (self *SSecurityGroupRule) GetDescription() string {
  42. return self.Description
  43. }
  44. func (self *SSecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
  45. if self.Type == "Egress" {
  46. return secrules.DIR_OUT
  47. }
  48. return secrules.DIR_IN
  49. }
  50. func (self *SSecurityGroupRule) GetCIDRs() []string {
  51. ip := self.AllowedCIDR + self.RemoteSecurityGroupUUID
  52. if len(ip) == 0 {
  53. ip = "0.0.0.0/0"
  54. }
  55. ret := []string{ip}
  56. return ret
  57. }
  58. func (self *SSecurityGroupRule) GetProtocol() string {
  59. if self.Protocol == "ALL" {
  60. return secrules.PROTO_ANY
  61. }
  62. return strings.ToLower(self.Protocol)
  63. }
  64. func (self *SSecurityGroupRule) GetPorts() string {
  65. if self.StartPort > 0 && self.EndPort > 0 {
  66. return fmt.Sprintf("%d-%d", self.StartPort, self.EndPort)
  67. }
  68. return ""
  69. }
  70. func (self *SSecurityGroupRule) GetPriority() int {
  71. return 0
  72. }
  73. func (self *SSecurityGroupRule) Delete() error {
  74. return self.region.DeleteSecurityGroupRules([]string{self.UUID})
  75. }
  76. func (self *SSecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  77. return cloudprovider.ErrNotImplemented
  78. }