securitygroup.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SSecurityGroup struct {
  24. multicloud.SSecurityGroup
  25. HuaweiTags
  26. region *SRegion
  27. Id string `json:"id"`
  28. Name string `json:"name"`
  29. Description string `json:"description"`
  30. VpcId string `json:"vpc_id"`
  31. EnterpriseProjectId string `json:"enterprise_project_id "`
  32. SecurityGroupRules []SecurityGroupRule `json:"security_group_rules"`
  33. }
  34. func (self *SSecurityGroup) GetId() string {
  35. return self.Id
  36. }
  37. func (self *SSecurityGroup) GetVpcId() string {
  38. return ""
  39. }
  40. func (self *SSecurityGroup) GetName() string {
  41. if len(self.Name) > 0 {
  42. return self.Name
  43. }
  44. return self.Id
  45. }
  46. func (self *SSecurityGroup) GetGlobalId() string {
  47. return self.Id
  48. }
  49. func (self *SSecurityGroup) GetTags() (map[string]string, error) {
  50. return nil, cloudprovider.ErrNotSupported
  51. }
  52. func (self *SSecurityGroup) GetStatus() string {
  53. return api.SECGROUP_STATUS_READY
  54. }
  55. func (self *SSecurityGroup) Refresh() error {
  56. group, err := self.region.GetSecurityGroup(self.GetId())
  57. if err != nil {
  58. return err
  59. }
  60. return jsonutils.Update(self, group)
  61. }
  62. func (self *SSecurityGroup) GetDescription() string {
  63. return self.Description
  64. }
  65. func (self *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  66. ret := make([]cloudprovider.ISecurityGroupRule, 0)
  67. rules, err := self.region.GetSecurityGroupRules(self.Id)
  68. if err != nil {
  69. return nil, err
  70. }
  71. for i := range rules {
  72. rules[i].secgroup = self
  73. ret = append(ret, &rules[i])
  74. }
  75. return ret, nil
  76. }
  77. // https://console.huaweicloud.com/apiexplorer/#/openapi/VPC/doc?version=v3&api=ShowSecurityGroup
  78. func (self *SRegion) GetSecurityGroup(id string) (*SSecurityGroup, error) {
  79. ret := &SSecurityGroup{region: self}
  80. resp, err := self.list(SERVICE_VPC_V3, "vpc/security-groups/"+id, nil)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return ret, resp.Unmarshal(ret, "security_group")
  85. }
  86. // https://console.huaweicloud.com/apiexplorer/#/openapi/VPC/doc?version=v3&api=ListSecurityGroups
  87. func (self *SRegion) GetSecurityGroups(name string) ([]SSecurityGroup, error) {
  88. ret := []SSecurityGroup{}
  89. params := url.Values{}
  90. if len(name) > 0 {
  91. params.Set("name", name)
  92. }
  93. for {
  94. resp, err := self.list(SERVICE_VPC_V3, "vpc/security-groups", params)
  95. if err != nil {
  96. return nil, err
  97. }
  98. part := struct {
  99. SecurityGroups []SSecurityGroup
  100. }{}
  101. err = resp.Unmarshal(&part)
  102. if err != nil {
  103. return nil, errors.Wrapf(err, "Unmarshal")
  104. }
  105. ret = append(ret, part.SecurityGroups...)
  106. if len(part.SecurityGroups) == 0 {
  107. break
  108. }
  109. params.Set("marker", part.SecurityGroups[len(part.SecurityGroups)-1].Id)
  110. }
  111. return ret, nil
  112. }
  113. func (self *SSecurityGroup) CreateRule(opts *cloudprovider.SecurityGroupRuleCreateOptions) (cloudprovider.ISecurityGroupRule, error) {
  114. rule, err := self.region.CreateSecurityGroupRule(self.Id, opts)
  115. if err != nil {
  116. return nil, err
  117. }
  118. rule.secgroup = self
  119. return rule, nil
  120. }
  121. func (self *SSecurityGroup) GetProjectId() string {
  122. return self.EnterpriseProjectId
  123. }
  124. func (self *SSecurityGroup) Delete() error {
  125. return self.region.DeleteSecurityGroup(self.Id)
  126. }