secgrouprules.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 hcso
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/secrules"
  22. )
  23. type SecurityGroupRule struct {
  24. secgroup *SSecurityGroup
  25. Direction string
  26. Ethertype string
  27. Id string
  28. Description string
  29. PortRangeMax int64
  30. PortRangeMin int64
  31. Protocol string
  32. RemoteGroupId string
  33. RemoteIPPrefix string
  34. SecurityGroupId string
  35. TenantId string
  36. }
  37. func (self *SecurityGroupRule) GetGlobalId() string {
  38. return self.Id
  39. }
  40. func (self *SecurityGroupRule) GetDescription() string {
  41. return self.Description
  42. }
  43. func (self *SecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
  44. if self.Direction == "egress" {
  45. return secrules.DIR_OUT
  46. }
  47. return secrules.DIR_IN
  48. }
  49. func (self *SecurityGroupRule) GetPriority() int {
  50. return 0
  51. }
  52. func (self *SecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  53. return secrules.SecurityRuleAllow
  54. }
  55. func (self *SecurityGroupRule) GetProtocol() string {
  56. if len(self.Protocol) == 0 {
  57. self.Protocol = secrules.PROTO_ANY
  58. }
  59. return strings.ToLower(self.Protocol)
  60. }
  61. func (self *SecurityGroupRule) GetPorts() string {
  62. if self.PortRangeMax > 0 && self.PortRangeMin > 0 {
  63. if self.PortRangeMax == self.PortRangeMin {
  64. return fmt.Sprintf("%d", self.PortRangeMax)
  65. }
  66. return fmt.Sprintf("%d-%d", self.PortRangeMin, self.PortRangeMax)
  67. }
  68. return ""
  69. }
  70. type SPageInfo struct {
  71. NextMarker string
  72. }
  73. func (self *SecurityGroupRule) GetCIDRs() []string {
  74. ret := []string{self.RemoteIPPrefix + self.RemoteGroupId}
  75. return ret
  76. }
  77. func (self *SecurityGroupRule) Delete() error {
  78. return self.secgroup.region.DeleteSecurityGroupRule(self.Id)
  79. }
  80. func (self *SRegion) DeleteSecurityGroupRule(id string) error {
  81. _, err := self.delete(SERVICE_VPC, "vpc/security-group-rules/"+id)
  82. return err
  83. }
  84. func (self *SRegion) GetSecurityGroupRules(groupId string) ([]SecurityGroupRule, error) {
  85. params := url.Values{}
  86. params.Set("security_group_id", groupId)
  87. ret := []SecurityGroupRule{}
  88. for {
  89. resp, err := self.list(SERVICE_VPC, "vpc/security-group-rules", params)
  90. if err != nil {
  91. return nil, err
  92. }
  93. part := struct {
  94. SecurityGroupRules []SecurityGroupRule
  95. PageInfo SPageInfo
  96. }{}
  97. err = resp.Unmarshal(&part)
  98. if err != nil {
  99. return nil, errors.Wrapf(err, "Unmarshal")
  100. }
  101. ret = append(ret, part.SecurityGroupRules...)
  102. if len(part.PageInfo.NextMarker) == 0 || len(part.SecurityGroupRules) == 0 {
  103. break
  104. }
  105. params.Set("marker", part.PageInfo.NextMarker)
  106. }
  107. return ret, nil
  108. }
  109. func (self *SRegion) CreateSecurityGroupRule(groupId string, opts *cloudprovider.SecurityGroupRuleCreateOptions) (*SecurityGroupRule, error) {
  110. rule := map[string]interface{}{
  111. "security_group_id": groupId,
  112. "description": opts.Desc,
  113. "direction": "ingress",
  114. "ethertype": "IPv4",
  115. "protocol": strings.ToLower(opts.Protocol),
  116. "action": "allow",
  117. "priority": opts.Priority,
  118. }
  119. if len(opts.CIDR) > 0 {
  120. rule["remote_ip_prefix"] = opts.CIDR
  121. }
  122. if opts.Action == secrules.SecurityRuleDeny {
  123. rule["action"] = "deny"
  124. }
  125. if opts.Protocol == secrules.PROTO_ANY {
  126. delete(rule, "protocol")
  127. }
  128. if len(opts.Ports) > 0 {
  129. rule["multiport"] = opts.Ports
  130. }
  131. if opts.Direction == secrules.DIR_OUT {
  132. rule["direction"] = "egress"
  133. }
  134. params := map[string]interface{}{
  135. "security_group_rule": rule,
  136. }
  137. resp, err := self.post(SERVICE_VPC, "vpc/security-group-rules", params)
  138. if err != nil {
  139. return nil, errors.Wrapf(err, "create rule")
  140. }
  141. ret := &SecurityGroupRule{}
  142. return ret, resp.Unmarshal(ret)
  143. }
  144. func (self *SecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  145. return cloudprovider.ErrNotImplemented
  146. }