yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/instancenic.go (about)

     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  
    15  package huawei
    16  
    17  import (
    18  	"yunion.io/x/log"
    19  
    20  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    21  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/modules"
    22  )
    23  
    24  // ===========================================
    25  type Interface struct {
    26  	PortState string    `json:"port_state"`
    27  	FixedIPS  []FixedIP `json:"fixed_ips"`
    28  	NetID     string    `json:"net_id"` // 网络ID. 与 SNetwork里的ID对应。统一使用这个ID
    29  	PortID    string    `json:"port_id"`
    30  	MACAddr   string    `json:"mac_addr"`
    31  }
    32  
    33  /*
    34  subnet: {id: "b09877fc-90d4-4fc8-b343-e6e00cb2b233", name: "subnet-149c", cidr: "192.168.0.0/24",…}
    35  availability_zone: "cn-north-1b"
    36  cidr: "192.168.0.0/24"
    37  dhcp_enable: true
    38  dnsList: ["100.125.1.250", "100.125.21.250"]
    39  gateway_ip: "192.168.0.1"
    40  id: "b09877fc-90d4-4fc8-b343-e6e00cb2b233"
    41  ipv6_enable: false
    42  name: "subnet-149c"
    43  neutron_network_id: "b09877fc-90d4-4fc8-b343-e6e00cb2b233"
    44  neutron_subnet_id: "81fcfaa0-8e73-4472-9eba-3b2b7736d3a7"
    45  primary_dns: "100.125.1.250"
    46  secondary_dns: "100.125.21.250"
    47  status: "ACTIVE"
    48  tags: []
    49  vpc_id: "877f1feb-3dc8-4c2d-92e9-0d94fd7d79dd"}
    50  */
    51  type FixedIP struct {
    52  	SubnetID  string `json:"subnet_id"` // 子网ID, 与SNetwork中的 neutron_subnet_id对应. 注意!!! 并不是SNetwork ID。
    53  	IPAddress string `json:"ip_address"`
    54  }
    55  
    56  // ===========================================
    57  
    58  type SInstanceNic struct {
    59  	instance *SInstance
    60  	ipAddr   string
    61  	macAddr  string
    62  
    63  	cloudprovider.DummyICloudNic
    64  }
    65  
    66  func (self *SInstanceNic) GetId() string {
    67  	return ""
    68  }
    69  
    70  func (self *SInstanceNic) GetIP() string {
    71  	return self.ipAddr
    72  }
    73  
    74  func (self *SInstanceNic) GetMAC() string {
    75  	return self.macAddr
    76  }
    77  
    78  func (self *SInstanceNic) GetDriver() string {
    79  	return "virtio"
    80  }
    81  
    82  func (self *SInstanceNic) InClassicNetwork() bool {
    83  	return false
    84  }
    85  
    86  func (self *SInstanceNic) GetINetworkId() string {
    87  	instanceId := self.instance.GetId()
    88  	subnets, err := self.instance.host.zone.region.getSubnetIdsByInstanceId(instanceId)
    89  	if err != nil || len(subnets) == 0 {
    90  		log.Errorf("getSubnetIdsByInstanceId error: %s", err.Error())
    91  		return ""
    92  	}
    93  
    94  	return subnets[0]
    95  }
    96  
    97  func (self *SRegion) getSubnetIdsByInstanceId(instanceId string) ([]string, error) {
    98  	ctx := &modules.SManagerContext{InstanceManager: self.ecsClient.NovaServers, InstanceId: instanceId}
    99  	interfaces := make([]Interface, 0)
   100  	err := DoListInContext(self.ecsClient.Interface.ListInContext, ctx, nil, &interfaces)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	subnets := make([]string, 0)
   106  	for _, i := range interfaces {
   107  		subnets = append(subnets, i.NetID)
   108  	}
   109  
   110  	return subnets, nil
   111  }