securitygroup.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 openstack
  15. import (
  16. "net/url"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/secrules"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. )
  26. const (
  27. SECGROUP_NOT_SUPPORT = "openstack_skip_security_group"
  28. )
  29. type SSecurityGroup struct {
  30. multicloud.SSecurityGroup
  31. OpenStackTags
  32. region *SRegion
  33. Description string
  34. Id string
  35. Name string
  36. SecurityGroupRules []SSecurityGroupRule
  37. ProjectId string
  38. RevisionNumber int
  39. CreatedAt time.Time
  40. UpdatedAt time.Time
  41. Tags []string
  42. TenantId string
  43. }
  44. func (region *SRegion) GetSecurityGroup(secgroupId string) (*SSecurityGroup, error) {
  45. resource := "/v2.0/security-groups/" + secgroupId
  46. resp, err := region.vpcGet(resource)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "vpcGet")
  49. }
  50. secgroup := &SSecurityGroup{region: region}
  51. err = resp.Unmarshal(secgroup, "security_group")
  52. if err != nil {
  53. return nil, errors.Wrap(err, "resp.Unmarshal")
  54. }
  55. return secgroup, nil
  56. }
  57. func (region *SRegion) GetSecurityGroups(projectId, name string) ([]SSecurityGroup, error) {
  58. secgroups := []SSecurityGroup{}
  59. resource := "/v2.0/security-groups"
  60. query := url.Values{}
  61. if len(name) > 0 {
  62. query.Set("name", name)
  63. }
  64. if len(projectId) > 0 {
  65. query.Set("project_id", projectId)
  66. }
  67. for {
  68. resp, err := region.vpcList(resource, query)
  69. if err != nil {
  70. return nil, errors.Wrap(err, "vpcList")
  71. }
  72. part := struct {
  73. SecurityGroups []SSecurityGroup
  74. SecurityGroupsLinks SNextLinks
  75. }{}
  76. err = resp.Unmarshal(&part)
  77. if err != nil {
  78. return nil, errors.Wrap(err, "resp.Unmarshal")
  79. }
  80. secgroups = append(secgroups, part.SecurityGroups...)
  81. marker := part.SecurityGroupsLinks.GetNextMark()
  82. if len(marker) == 0 {
  83. break
  84. }
  85. query.Set("marker", marker)
  86. }
  87. return secgroups, nil
  88. }
  89. func (secgroup *SSecurityGroup) GetVpcId() string {
  90. return ""
  91. }
  92. func (secgroup *SSecurityGroup) GetId() string {
  93. return secgroup.Id
  94. }
  95. func (secgroup *SSecurityGroup) GetGlobalId() string {
  96. return secgroup.Id
  97. }
  98. func (secgroup *SSecurityGroup) GetDescription() string {
  99. return secgroup.Description
  100. }
  101. func (secgroup *SSecurityGroup) GetName() string {
  102. if len(secgroup.Name) > 0 {
  103. return secgroup.Name
  104. }
  105. return secgroup.Id
  106. }
  107. func (secgroup *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  108. ret := []cloudprovider.ISecurityGroupRule{}
  109. for i := range secgroup.SecurityGroupRules {
  110. secgroup.SecurityGroupRules[i].region = secgroup.region
  111. ret = append(ret, &secgroup.SecurityGroupRules[i])
  112. }
  113. return ret, nil
  114. }
  115. func (secgroup *SSecurityGroup) GetStatus() string {
  116. return api.SECGROUP_STATUS_READY
  117. }
  118. func (secgroup *SSecurityGroup) Refresh() error {
  119. new, err := secgroup.region.GetSecurityGroup(secgroup.Id)
  120. if err != nil {
  121. return err
  122. }
  123. return jsonutils.Update(secgroup, new)
  124. }
  125. func (region *SRegion) DeleteSecurityGroupRule(ruleId string) error {
  126. resource := "/v2.0/security-group-rules/" + ruleId
  127. _, err := region.vpcDelete(resource)
  128. return err
  129. }
  130. func (self *SSecurityGroup) CreateRule(opts *cloudprovider.SecurityGroupRuleCreateOptions) (cloudprovider.ISecurityGroupRule, error) {
  131. rule, err := self.region.CreateSecurityGroupRule(self.Id, opts)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return rule, nil
  136. }
  137. func (region *SRegion) CreateSecurityGroupRule(secgroupId string, opts *cloudprovider.SecurityGroupRuleCreateOptions) (*SSecurityGroupRule, error) {
  138. direction := "ingress"
  139. if opts.Direction == secrules.SecurityRuleEgress {
  140. direction = "egress"
  141. }
  142. if opts.Protocol == secrules.PROTO_ANY {
  143. opts.Protocol = ""
  144. }
  145. ruleInfo := map[string]interface{}{
  146. "direction": direction,
  147. "security_group_id": secgroupId,
  148. "remote_ip_prefix": opts.CIDR,
  149. }
  150. if len(opts.Protocol) > 0 {
  151. ruleInfo["protocol"] = opts.Protocol
  152. }
  153. params := map[string]map[string]interface{}{
  154. "security_group_rule": ruleInfo,
  155. }
  156. if len(opts.Ports) > 0 {
  157. if !strings.Contains(opts.Ports, "-") {
  158. params["security_group_rule"]["port_range_max"] = opts.Ports
  159. params["security_group_rule"]["port_range_min"] = opts.Ports
  160. } else {
  161. info := strings.Split(opts.Ports, "-")
  162. if len(info) == 2 {
  163. params["security_group_rule"]["port_range_min"] = info[0]
  164. params["security_group_rule"]["port_range_max"] = info[1]
  165. }
  166. }
  167. }
  168. resp, err := region.vpcPost("/v2.0/security-group-rules", params)
  169. if err != nil {
  170. return nil, err
  171. }
  172. rule := &SSecurityGroupRule{region: region}
  173. return rule, resp.Unmarshal(rule, "security_group_rule")
  174. }
  175. func (region *SRegion) DeleteSecurityGroup(secGroupId string) error {
  176. resource := "/v2.0/security-groups/" + secGroupId
  177. _, err := region.vpcDelete(resource)
  178. return err
  179. }
  180. func (secgroup *SSecurityGroup) Delete() error {
  181. return secgroup.region.DeleteSecurityGroup(secgroup.Id)
  182. }
  183. func (region *SRegion) CreateSecurityGroup(opts *cloudprovider.SecurityGroupCreateInput) (*SSecurityGroup, error) {
  184. params := map[string]map[string]interface{}{
  185. "security_group": {
  186. "name": opts.Name,
  187. "description": opts.Desc,
  188. },
  189. }
  190. if len(opts.ProjectId) > 0 {
  191. params["security_group"]["project_id"] = opts.ProjectId
  192. }
  193. resp, err := region.vpcPost("/v2.0/security-groups", params)
  194. if err != nil {
  195. return nil, errors.Wrap(err, "vpcPost")
  196. }
  197. secgroup := &SSecurityGroup{region: region}
  198. err = resp.Unmarshal(secgroup, "security_group")
  199. if err != nil {
  200. return nil, errors.Wrap(err, "resp.Unmarshal")
  201. }
  202. return secgroup, nil
  203. }
  204. func (secgroup *SSecurityGroup) GetProjectId() string {
  205. return secgroup.TenantId
  206. }