secgrouprule.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 azure
  15. import (
  16. "strings"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/pkg/util/secrules"
  19. )
  20. type SecurityRulePropertiesFormat struct {
  21. Description string `json:"description,omitempty"`
  22. Protocol string `json:"protocol,omitempty"`
  23. SourcePortRange string `json:"sourcePortRange,omitempty"`
  24. DestinationPortRange string `json:"destinationPortRange,omitempty"`
  25. SourceAddressPrefix string `json:"sourceAddressPrefix,omitempty"`
  26. SourceAddressPrefixes []string `json:"sourceAddressPrefixes,omitempty"`
  27. DestinationAddressPrefix string `json:"destinationAddressPrefix,omitempty"`
  28. DestinationAddressPrefixes []string `json:"destinationAddressPrefixes,omitempty"`
  29. SourcePortRanges []string `json:"sourcePortRanges,omitempty"`
  30. DestinationPortRanges []string `json:"destinationPortRanges,omitempty"`
  31. Access string `json:"access,omitempty"` // Allow or Deny
  32. Priority int `json:"priority,omitempty"`
  33. Direction string `json:"direction,omitempty"` //Inbound or Outbound
  34. ProvisioningState string `json:"-"`
  35. }
  36. type SecurityRules struct {
  37. region *SRegion
  38. Properties SecurityRulePropertiesFormat
  39. Name string
  40. ID string
  41. }
  42. func (self *SecurityRules) GetGlobalId() string {
  43. return strings.ToLower(self.ID)
  44. }
  45. func (self *SecurityRules) GetDescription() string {
  46. return self.Properties.Description
  47. }
  48. func (self *SecurityRules) GetPriority() int {
  49. return self.Properties.Priority
  50. }
  51. func (self *SecurityRules) GetDirection() secrules.TSecurityRuleDirection {
  52. if strings.ToLower(self.Properties.Direction) == "inbound" {
  53. return secrules.DIR_IN
  54. }
  55. return secrules.DIR_OUT
  56. }
  57. func (self *SecurityRules) Delete() error {
  58. return self.region.del(self.ID)
  59. }
  60. func (self *SecurityRules) GetAction() secrules.TSecurityRuleAction {
  61. if strings.ToLower(self.Properties.Access) == "allow" {
  62. return secrules.SecurityRuleAllow
  63. }
  64. return secrules.SecurityRuleDeny
  65. }
  66. func (self *SecurityRules) GetProtocol() string {
  67. if self.Properties.Protocol == "*" {
  68. return secrules.PROTO_ANY
  69. }
  70. return self.Properties.Protocol
  71. }
  72. func (self *SecurityRules) GetCIDRs() []string {
  73. ret := []string{}
  74. if len(self.Properties.DestinationAddressPrefix) > 0 && self.Properties.DestinationAddressPrefix != "*" {
  75. ret = append(ret, self.Properties.DestinationAddressPrefix)
  76. }
  77. for _, ip := range self.Properties.DestinationAddressPrefixes {
  78. if ip != "*" {
  79. ret = append(ret, ip)
  80. }
  81. }
  82. if len(ret) == 0 {
  83. ret = append(ret, "0.0.0.0/0")
  84. }
  85. return ret
  86. }
  87. func (self *SecurityRules) GetPorts() string {
  88. ports := []string{}
  89. if len(self.Properties.DestinationPortRange) > 0 && self.Properties.DestinationPortRange != "*" {
  90. ports = append(ports, self.Properties.DestinationPortRange)
  91. }
  92. for _, port := range self.Properties.DestinationPortRanges {
  93. if port != "*" {
  94. ports = append(ports, port)
  95. }
  96. }
  97. return strings.Join(ports, ",")
  98. }
  99. func (self *SecurityRules) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  100. return cloudprovider.ErrNotImplemented
  101. }