securitygroup.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 hcso
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/utils"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  22. )
  23. // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090615.html
  24. type SSecurityGroup struct {
  25. multicloud.SSecurityGroup
  26. huawei.HuaweiTags
  27. region *SRegion
  28. ID string `json:"id"`
  29. Name string `json:"name"`
  30. Description string `json:"description"`
  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) IsEmulated() bool {
  63. return false
  64. }
  65. func (self *SSecurityGroup) GetDescription() string {
  66. return self.Description
  67. }
  68. func (self *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  69. ret := make([]cloudprovider.ISecurityGroupRule, 0)
  70. rules, err := self.region.GetSecurityGroupRules(self.ID)
  71. if err != nil {
  72. return nil, err
  73. }
  74. for i := range rules {
  75. rules[i].secgroup = self
  76. ret = append(ret, &rules[i])
  77. }
  78. return ret, nil
  79. }
  80. func (self *SRegion) GetSecurityGroup(id string) (*SSecurityGroup, error) {
  81. ret := &SSecurityGroup{region: self}
  82. resp, err := self.list(SERVICE_VPC, "vpc/security-groups/"+id, nil)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return ret, resp.Unmarshal(ret, "security_group")
  87. }
  88. // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090617.html
  89. func (self *SRegion) GetSecurityGroups(vpcId string, name string) ([]SSecurityGroup, error) {
  90. querys := map[string]string{}
  91. if len(vpcId) > 0 && !utils.IsInStringArray(vpcId, []string{"default", api.NORMAL_VPC_ID}) { // vpc_id = default or normal 时报错 '{"code":"VPC.0601","message":"Query security groups error vpcId is invalid."}'
  92. querys["vpc_id"] = vpcId
  93. }
  94. securitygroups := make([]SSecurityGroup, 0)
  95. err := doListAllWithMarker(self.ecsClient.SecurityGroups.List, querys, &securitygroups)
  96. if err != nil {
  97. return nil, err
  98. }
  99. // security 中的vpc字段只是一个标识,实际可以跨vpc使用
  100. for i := range securitygroups {
  101. securitygroup := &securitygroups[i]
  102. securitygroup.region = self
  103. }
  104. result := []SSecurityGroup{}
  105. for _, secgroup := range securitygroups {
  106. if len(name) == 0 || secgroup.Name == name {
  107. result = append(result, secgroup)
  108. }
  109. }
  110. return result, nil
  111. }
  112. func (self *SSecurityGroup) GetProjectId() string {
  113. return ""
  114. }
  115. func (self *SSecurityGroup) Delete() error {
  116. return self.region.DeleteSecurityGroup(self.ID)
  117. }