secgrouprule.go 4.7 KB

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