securitygroup.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 zstack
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  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. ZStackTags
  28. region *SRegion
  29. ZStackBasic
  30. State string `json:"state"`
  31. IPVersion int `json:"ipVersion"`
  32. ZStackTime
  33. InternalID int `json:"internalId"`
  34. Rules []SSecurityGroupRule `json:"rules"`
  35. AttachedL3NetworkUUIDs []string `json:"attachedL3NetworkUuids"`
  36. }
  37. func (region *SRegion) GetSecurityGroup(secgroupId string) (*SSecurityGroup, error) {
  38. secgroup := &SSecurityGroup{region: region}
  39. return secgroup, region.client.getResource("security-groups", secgroupId, secgroup)
  40. }
  41. func (region *SRegion) GetSecurityGroups(secgroupId string, instanceId string, name string) ([]SSecurityGroup, error) {
  42. secgroups := []SSecurityGroup{}
  43. params := url.Values{}
  44. if len(secgroupId) > 0 {
  45. params.Add("q", "uuid="+secgroupId)
  46. }
  47. if len(instanceId) > 0 {
  48. params.Add("q", "vmNic.vmInstanceUuid="+instanceId)
  49. }
  50. if len(name) > 0 {
  51. params.Add("q", "name="+name)
  52. }
  53. err := region.client.listAll("security-groups", params, &secgroups)
  54. if err != nil {
  55. return nil, err
  56. }
  57. for i := 0; i < len(secgroups); i++ {
  58. secgroups[i].region = region
  59. }
  60. return secgroups, nil
  61. }
  62. func (self *SSecurityGroup) GetVpcId() string {
  63. return ""
  64. }
  65. func (self *SSecurityGroup) GetId() string {
  66. return self.UUID
  67. }
  68. func (self *SSecurityGroup) GetGlobalId() string {
  69. return self.UUID
  70. }
  71. func (self *SSecurityGroup) GetDescription() string {
  72. return self.Description
  73. }
  74. func (self *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  75. ret := []cloudprovider.ISecurityGroupRule{}
  76. for i := range self.Rules {
  77. self.Rules[i].region = self.region
  78. ret = append(ret, &self.Rules[i])
  79. }
  80. return ret, nil
  81. }
  82. func (self *SSecurityGroup) GetName() string {
  83. return self.Name
  84. }
  85. func (self *SSecurityGroup) GetStatus() string {
  86. return api.SECGROUP_STATUS_READY
  87. }
  88. func (self *SSecurityGroup) Refresh() error {
  89. group, err := self.region.GetSecurityGroup(self.UUID)
  90. if err != nil {
  91. return err
  92. }
  93. return jsonutils.Update(self, group)
  94. }
  95. func (self *SSecurityGroup) GetProjectId() string {
  96. return ""
  97. }
  98. func (self *SSecurityGroup) CreateRule(opts *cloudprovider.SecurityGroupRuleCreateOptions) (cloudprovider.ISecurityGroupRule, error) {
  99. rule, err := self.region.CreateSecurityGroupRule(self.UUID, opts)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return rule, nil
  104. }
  105. func (region *SRegion) CreateSecurityGroupRule(secgroupId string, opts *cloudprovider.SecurityGroupRuleCreateOptions) (*SSecurityGroupRule, error) {
  106. ruleParam := map[string]interface{}{
  107. "allowedCidr": opts.CIDR,
  108. "type": "Ingress",
  109. "protocol": "ALL",
  110. }
  111. if opts.Direction == secrules.DIR_OUT {
  112. ruleParam["type"] = "Egress"
  113. }
  114. if opts.Protocol != secrules.PROTO_ANY {
  115. ruleParam["protocol"] = strings.ToUpper(opts.Protocol)
  116. }
  117. if opts.Protocol == secrules.PROTO_ICMP || opts.Protocol == secrules.PROTO_ANY {
  118. opts.Ports = ""
  119. }
  120. if opts.Protocol == secrules.PROTO_TCP || opts.Protocol == secrules.PROTO_UDP {
  121. if len(opts.Ports) == 0 {
  122. ruleParam["startPort"] = "0"
  123. ruleParam["endPort"] = "65535"
  124. } else {
  125. if strings.Contains(opts.Ports, "-") {
  126. info := strings.Split(opts.Ports, "-")
  127. if len(info) == 2 {
  128. ruleParam["startPort"] = info[0]
  129. ruleParam["endPort"] = info[1]
  130. }
  131. } else {
  132. ruleParam["startPort"] = opts.Ports
  133. ruleParam["endPort"] = opts.Ports
  134. }
  135. }
  136. }
  137. params := map[string]interface{}{
  138. "params": map[string]interface{}{
  139. "rules": []map[string]interface{}{ruleParam},
  140. },
  141. }
  142. rule := &SSecurityGroupRule{region: region}
  143. err := region.client.create(fmt.Sprintf("security-groups/%s/rules", secgroupId), jsonutils.Marshal(params), rule)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return rule, nil
  148. }
  149. func (region *SRegion) DeleteSecurityGroupRules(ruleIds []string) error {
  150. if len(ruleIds) > 0 {
  151. ids := []string{}
  152. for _, ruleId := range ruleIds {
  153. ids = append(ids, fmt.Sprintf("ruleUuids=%s", ruleId))
  154. }
  155. resource := fmt.Sprintf("security-groups/rules?%s", strings.Join(ids, "&"))
  156. return region.client.delete(resource, "", "")
  157. }
  158. return nil
  159. }
  160. func (region *SRegion) CreateSecurityGroup(opts *cloudprovider.SecurityGroupCreateInput) (*SSecurityGroup, error) {
  161. secgroup := &SSecurityGroup{region: region}
  162. params := map[string]map[string]string{
  163. "params": {
  164. "name": opts.Name,
  165. "description": opts.Desc,
  166. },
  167. }
  168. err := region.client.create("security-groups", jsonutils.Marshal(params), secgroup)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return secgroup, nil
  173. }
  174. func (self *SSecurityGroup) Delete() error {
  175. return self.region.client.delete("security-groups", self.UUID, "Permissive")
  176. }