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