| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package hcso
- import (
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/utils"
- api "yunion.io/x/cloudmux/pkg/apis/compute"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- "yunion.io/x/cloudmux/pkg/multicloud/huawei"
- )
- // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090615.html
- type SSecurityGroup struct {
- multicloud.SSecurityGroup
- huawei.HuaweiTags
- region *SRegion
- ID string `json:"id"`
- Name string `json:"name"`
- Description string `json:"description"`
- EnterpriseProjectID string `json:"enterprise_project_id "`
- SecurityGroupRules []SecurityGroupRule `json:"security_group_rules"`
- }
- func (self *SSecurityGroup) GetId() string {
- return self.ID
- }
- func (self *SSecurityGroup) GetVpcId() string {
- return ""
- }
- func (self *SSecurityGroup) GetName() string {
- if len(self.Name) > 0 {
- return self.Name
- }
- return self.ID
- }
- func (self *SSecurityGroup) GetGlobalId() string {
- return self.ID
- }
- func (self *SSecurityGroup) GetTags() (map[string]string, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (self *SSecurityGroup) GetStatus() string {
- return api.SECGROUP_STATUS_READY
- }
- func (self *SSecurityGroup) Refresh() error {
- group, err := self.region.GetSecurityGroup(self.GetId())
- if err != nil {
- return err
- }
- return jsonutils.Update(self, group)
- }
- func (self *SSecurityGroup) IsEmulated() bool {
- return false
- }
- func (self *SSecurityGroup) GetDescription() string {
- return self.Description
- }
- func (self *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
- ret := make([]cloudprovider.ISecurityGroupRule, 0)
- rules, err := self.region.GetSecurityGroupRules(self.ID)
- if err != nil {
- return nil, err
- }
- for i := range rules {
- rules[i].secgroup = self
- ret = append(ret, &rules[i])
- }
- return ret, nil
- }
- func (self *SRegion) GetSecurityGroup(id string) (*SSecurityGroup, error) {
- ret := &SSecurityGroup{region: self}
- resp, err := self.list(SERVICE_VPC, "vpc/security-groups/"+id, nil)
- if err != nil {
- return nil, err
- }
- return ret, resp.Unmarshal(ret, "security_group")
- }
- // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090617.html
- func (self *SRegion) GetSecurityGroups(vpcId string, name string) ([]SSecurityGroup, error) {
- querys := map[string]string{}
- 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."}'
- querys["vpc_id"] = vpcId
- }
- securitygroups := make([]SSecurityGroup, 0)
- err := doListAllWithMarker(self.ecsClient.SecurityGroups.List, querys, &securitygroups)
- if err != nil {
- return nil, err
- }
- // security 中的vpc字段只是一个标识,实际可以跨vpc使用
- for i := range securitygroups {
- securitygroup := &securitygroups[i]
- securitygroup.region = self
- }
- result := []SSecurityGroup{}
- for _, secgroup := range securitygroups {
- if len(name) == 0 || secgroup.Name == name {
- result = append(result, secgroup)
- }
- }
- return result, nil
- }
- func (self *SSecurityGroup) GetProjectId() string {
- return ""
- }
- func (self *SSecurityGroup) Delete() error {
- return self.region.DeleteSecurityGroup(self.ID)
- }
|