forked from yunionio/cloudmux
/
instancenic.go
234 lines (211 loc) · 6.58 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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 aliyun
import (
"fmt"
"time"
alierr "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/util/netutils"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type SInstanceNic struct {
instance *SInstance
id string
ipAddr string
macAddr string
classic bool
cloudprovider.DummyICloudNic
}
func (self *SInstanceNic) GetId() string {
return self.id
}
func (self *SInstanceNic) mustGetId() string {
if self.id == "" {
panic("empty network interface id")
}
return self.id
}
func (self *SInstanceNic) GetIP() string {
return self.ipAddr
}
func (self *SInstanceNic) GetMAC() string {
if len(self.macAddr) > 0 {
return self.macAddr
}
ip, _ := netutils.NewIPV4Addr(self.GetIP())
return ip.ToMac("00:16:")
}
func (self *SInstanceNic) InClassicNetwork() bool {
return self.classic == true
}
func (self *SInstanceNic) GetDriver() string {
return "virtio"
}
func (self *SInstanceNic) GetINetworkId() string {
return self.instance.VpcAttributes.VSwitchId
}
func (self *SInstanceNic) GetSubAddress() ([]string, error) {
selfId := self.mustGetId()
region := self.instance.host.zone.region
params := map[string]string{
"RegionId": region.GetId(),
"NetworkInterfaceId.1": selfId,
}
body, err := region.ecsRequest("DescribeNetworkInterfaces", params)
if err != nil {
return nil, err
}
type DescribeNetworkInterfacesResponse struct {
TotalCount int `json:"TotalCount"`
RequestID string `json:"RequestId"`
PageSize int `json:"PageSize"`
NextToken string `json:"NextToken"`
PageNumber int `json:"PageNumber"`
NetworkInterfaceSets struct {
NetworkInterfaceSet []struct {
Status string `json:"Status"`
PrivateIPAddress string `json:"PrivateIpAddress"`
ZoneID string `json:"ZoneId"`
ResourceGroupID string `json:"ResourceGroupId"`
InstanceID string `json:"InstanceId"`
VSwitchID string `json:"VSwitchId"`
NetworkInterfaceID string `json:"NetworkInterfaceId"`
MacAddress string `json:"MacAddress"`
SecurityGroupIds struct {
SecurityGroupID []string `json:"SecurityGroupId"`
} `json:"SecurityGroupIds"`
Type string `json:"Type"`
Ipv6Sets struct {
Ipv6Set []struct {
Ipv6Address string `json:"Ipv6Address"`
} `json:"Ipv6Set"`
} `json:"Ipv6Sets"`
VpcID string `json:"VpcId"`
OwnerID string `json:"OwnerId"`
AssociatedPublicIP struct {
} `json:"AssociatedPublicIp"`
CreationTime time.Time `json:"CreationTime"`
Tags struct {
Tag []struct {
TagKey string `json:"TagKey"`
TagValue string `json:"TagValue"`
} `json:"Tag"`
} `json:"Tags"`
PrivateIPSets struct {
PrivateIPSet []struct {
PrivateIPAddress string `json:"PrivateIpAddress"`
AssociatedPublicIP struct {
} `json:"AssociatedPublicIp"`
Primary bool `json:"Primary"`
} `json:"PrivateIpSet"`
} `json:"PrivateIpSets"`
} `json:"NetworkInterfaceSet"`
} `json:"NetworkInterfaceSets"`
}
var resp DescribeNetworkInterfacesResponse
if err := body.Unmarshal(&resp); err != nil {
return nil, errors.Wrapf(err, "unmarshal DescribeNetworkInterfacesResponse: %s", body)
}
if got := len(resp.NetworkInterfaceSets.NetworkInterfaceSet); got != 1 {
return nil, errors.Errorf("got %d element(s) in interface set, expect 1", got)
}
var (
ipAddrs []string
networkInterface = resp.NetworkInterfaceSets.NetworkInterfaceSet[0]
)
if got := networkInterface.NetworkInterfaceID; got != selfId {
return nil, errors.Errorf("got interface data for %s, expect %s", got, selfId)
}
for _, privateIP := range networkInterface.PrivateIPSets.PrivateIPSet {
if !privateIP.Primary {
ipAddrs = append(ipAddrs, privateIP.PrivateIPAddress)
}
}
return ipAddrs, nil
}
func (self *SInstanceNic) ipAddrsParams(ipAddrs []string) map[string]string {
region := self.instance.host.zone.region
params := map[string]string{
"RegionId": region.GetId(),
"NetworkInterfaceId": self.mustGetId(),
}
for i, ipAddr := range ipAddrs {
k := fmt.Sprintf("PrivateIpAddress.%d", i+1)
params[k] = ipAddr
}
return params
}
func (self *SInstanceNic) AssignAddress(ipAddrs []string) error {
var (
selfId = self.mustGetId()
instance = self.instance
zone = instance.host.zone
region = zone.region
)
ecsClient, err := region.getEcsClient()
if err != nil {
return err
}
request := ecs.CreateAssignPrivateIpAddressesRequest()
request.Scheme = "https"
request.NetworkInterfaceId = selfId
request.PrivateIpAddress = &ipAddrs
resp, err := ecsClient.AssignPrivateIpAddresses(request)
if err != nil {
if aerr, ok := err.(*alierr.ServerError); ok {
if aerr.ErrorCode() == "InvalidOperation.Ipv4CountExceeded" {
return errors.Wrap(cloudprovider.ErrAddressCountExceed, aerr.Message())
}
}
return errors.Wrapf(err, "AssignPrivateIpAddresses")
}
allocated := resp.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress
if len(allocated) != len(ipAddrs) {
return errors.Errorf("AssignAddress want %d addresses, got %d", len(ipAddrs), len(allocated))
}
for i := 0; i < len(ipAddrs); i++ {
ip0 := ipAddrs[i]
ip1 := allocated[i]
if ip0 != ip1 {
return errors.Errorf("AssignAddress address %d does not match: want %s, got %s", i, ip0, ip1)
}
}
return nil
}
func (self *SInstanceNic) UnassignAddress(ipAddrs []string) error {
var (
selfId = self.mustGetId()
instance = self.instance
zone = instance.host.zone
region = zone.region
)
ecsClient, err := region.getEcsClient()
if err != nil {
return err
}
request := ecs.CreateUnassignPrivateIpAddressesRequest()
request.Scheme = "https"
request.NetworkInterfaceId = selfId
request.PrivateIpAddress = &ipAddrs
resp, err := ecsClient.UnassignPrivateIpAddresses(request)
if err != nil {
if resp.GetHttpStatus() == 404 {
return nil
}
return err
}
return nil
}