securitygroup.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 bingocloud
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/secrules"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. type SSecurityGroup struct {
  26. multicloud.SSecurityGroup
  27. BingoTags
  28. region *SRegion
  29. ComplexIPPermissions string `json:"complexIpPermissions"`
  30. ComplexIPPermissionsEgress string `json:"complexIpPermissionsEgress"`
  31. DisplayName string `json:"displayName"`
  32. GroupDescription string `json:"groupDescription"`
  33. GroupId string `json:"groupId"`
  34. GroupName string `json:"groupName"`
  35. IPPermissionType string `json:"ipPermissionType"`
  36. IPPermissions []IPPermissions `json:"ipPermissions"`
  37. IPPermissionsEgress []IPPermissions `json:"ipPermissionsEgress"`
  38. OwnerId string `json:"ownerId"`
  39. }
  40. func (self *SSecurityGroup) GetId() string {
  41. return self.GroupId
  42. }
  43. func (self *SSecurityGroup) GetGlobalId() string {
  44. return self.GetId()
  45. }
  46. func (self *SSecurityGroup) GetName() string {
  47. return self.GroupName
  48. }
  49. func (self *SSecurityGroup) GetDescription() string {
  50. return self.GroupDescription
  51. }
  52. func (self *SSecurityGroup) GetProjectId() string {
  53. return ""
  54. }
  55. func (self *SSecurityGroup) GetReferences() ([]cloudprovider.SecurityGroupReference, error) {
  56. return []cloudprovider.SecurityGroupReference{}, nil
  57. }
  58. func (self *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  59. ret := []cloudprovider.ISecurityGroupRule{}
  60. for i := range self.IPPermissionsEgress {
  61. self.IPPermissionsEgress[i].direction = secrules.DIR_OUT
  62. ret = append(ret, &self.IPPermissionsEgress[i])
  63. }
  64. for i := range self.IPPermissions {
  65. self.IPPermissions[i].direction = secrules.DIR_IN
  66. ret = append(ret, &self.IPPermissions[i])
  67. }
  68. return ret, nil
  69. }
  70. func (self *SSecurityGroup) GetVpcId() string {
  71. return ""
  72. }
  73. func (self *SSecurityGroup) GetStatus() string {
  74. return api.SECGROUP_STATUS_READY
  75. }
  76. func (self *SSecurityGroup) Delete() error {
  77. return self.region.deleteSecurityGroup(self.GroupId)
  78. }
  79. func (self *SRegion) CreateSecurityGroupRules(secGrpId string, opts *cloudprovider.SecurityGroupRuleCreateOptions) error {
  80. params := map[string]string{
  81. "GroupId": secGrpId,
  82. "IpProtocol": "all",
  83. "BoundType": "In",
  84. "Policy": "DROP",
  85. "FromPort": "0",
  86. "ToPort": "65535",
  87. }
  88. if opts.Protocol != secrules.PROTO_ANY {
  89. params["IpProtocol"] = opts.Protocol
  90. }
  91. if opts.Direction == secrules.DIR_OUT {
  92. params["BoundType"] = "Out"
  93. }
  94. if opts.Action == secrules.SecurityRuleAllow {
  95. params["Policy"] = "ACCEPT"
  96. }
  97. start, end := 0, 0
  98. if len(opts.Ports) > 0 {
  99. if strings.Contains(opts.Ports, "-") {
  100. ports := strings.Split(opts.Ports, "-")
  101. if len(ports) != 2 {
  102. return errors.Errorf("invalid ports %s", opts.Ports)
  103. }
  104. var err error
  105. _start, _end := ports[0], ports[1]
  106. start, err = strconv.Atoi(_start)
  107. if err != nil {
  108. return errors.Errorf("invalid start port %s", _start)
  109. }
  110. end, err = strconv.Atoi(_end)
  111. if err != nil {
  112. return errors.Errorf("invalid end port %s", _end)
  113. }
  114. } else {
  115. port, err := strconv.Atoi(opts.Ports)
  116. if err != nil {
  117. return errors.Errorf("invalid ports %s", opts.Ports)
  118. }
  119. start, end = port, port
  120. }
  121. }
  122. if start > 0 && end > 0 {
  123. params["FromPort"] = fmt.Sprintf("%d", start)
  124. params["ToPort"] = fmt.Sprintf("%d", end)
  125. }
  126. _, err := self.invoke("AuthorizeSecurityGroupIngress", params)
  127. if err == nil {
  128. return errors.Wrapf(err, "AuthorizeSecurityGroupIngress")
  129. }
  130. return nil
  131. }
  132. func (self *SRegion) GetSecurityGroups(id, name, nextToken string) ([]SSecurityGroup, string, error) {
  133. params := map[string]string{}
  134. params["Filter.1.Name"] = "owner-id"
  135. params["Filter.1.Value.1"] = self.getAccountUser()
  136. if len(id) > 0 {
  137. params["GroupId.1"] = id
  138. }
  139. if len(name) > 0 {
  140. params["Filter.2.Name"] = "group-name"
  141. params["Filter.2.Value.1"] = name
  142. }
  143. if len(nextToken) > 0 {
  144. params["NextToken"] = nextToken
  145. }
  146. resp, err := self.invoke("DescribeSecurityGroups", params)
  147. if err != nil {
  148. return nil, "", err
  149. }
  150. ret := struct {
  151. SecurityGroupInfo []SSecurityGroup
  152. NextToken string
  153. }{}
  154. _ = resp.Unmarshal(&ret)
  155. return ret.SecurityGroupInfo, ret.NextToken, nil
  156. }
  157. func (self *SRegion) GetISecurityGroupById(id string) (cloudprovider.ICloudSecurityGroup, error) {
  158. groups, _, err := self.GetSecurityGroups(id, "", "")
  159. if err != nil {
  160. return nil, err
  161. }
  162. for i := range groups {
  163. if groups[i].GetGlobalId() == id {
  164. groups[i].region = self
  165. return &groups[i], nil
  166. }
  167. }
  168. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  169. }
  170. func (self *SRegion) deleteSecurityGroup(id string) error {
  171. params := map[string]string{}
  172. params["GroupId"] = id
  173. _, err := self.invoke("DeleteSecurityGroup", params)
  174. return err
  175. }
  176. type SecurityGroupCreateOutput struct {
  177. Return bool
  178. GroupId string
  179. }
  180. func (self *SRegion) CreateISecurityGroup(opts *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
  181. params := map[string]string{}
  182. if len(opts.Name) > 0 {
  183. params["GroupName"] = opts.Name
  184. }
  185. resp, err := self.invoke("CreateSecurityGroup", params)
  186. if err != nil {
  187. return nil, err
  188. }
  189. ret := &SecurityGroupCreateOutput{}
  190. _ = resp.Unmarshal(&ret)
  191. if ret.Return {
  192. return self.GetISecurityGroupById(ret.GroupId)
  193. }
  194. return nil, errors.Wrap(cloudprovider.ErrUnknown, "CreateSecurityGroup")
  195. }