diff --git a/pkg/util/aws/eip.go b/pkg/util/aws/eip.go index 4eaeee3b96..0d85b8f4ab 100644 --- a/pkg/util/aws/eip.go +++ b/pkg/util/aws/eip.go @@ -144,12 +144,16 @@ func (self *SEipAddress) ChangeBandwidth(bw int) error { return self.region.UpdateEipBandwidth(self.AllocationId, bw) } -func (self *SRegion) GetEips(eipId string, offset int, limit int) ([]SEipAddress, int, error) { +func (self *SRegion) GetEips(eipId string, eipAddress string, offset int, limit int) ([]SEipAddress, int, error) { params := ec2.DescribeAddressesInput{} if len(eipId) > 0 { params.SetAllocationIds([]*string{&eipId}) } + if len(eipAddress) > 0 { + params.SetPublicIps([]*string{&eipAddress}) + } + res, err := self.ec2Client.DescribeAddresses(¶ms) if err != nil { log.Errorf("DescribeEipAddresses fail %s", err) @@ -191,7 +195,18 @@ func (self *SRegion) GetEips(eipId string, offset int, limit int) ([]SEipAddress } func (self *SRegion) GetEip(eipId string) (*SEipAddress, error) { - eips, total, err := self.GetEips(eipId, 0, 0) + eips, total, err := self.GetEips(eipId, "", 0, 0) + if err != nil { + return nil, err + } + if total != 1 { + return nil, cloudprovider.ErrNotFound + } + return &eips[0], nil +} + +func (self *SRegion) GetEipByIpAddress(eipAddress string) (*SEipAddress, error) { + eips, total, err := self.GetEips("", eipAddress, 0, 0) if err != nil { return nil, err } diff --git a/pkg/util/aws/instance.go b/pkg/util/aws/instance.go index 91212aa624..4d76ca5733 100644 --- a/pkg/util/aws/instance.go +++ b/pkg/util/aws/instance.go @@ -228,17 +228,16 @@ func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) { } func (self *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) { - // todo: implement me - if len(self.PublicIpAddress.IpAddress) > 0 { + if len(self.EipAddress.IpAddress) > 0 { + return self.host.zone.region.GetEipByIpAddress(self.EipAddress.IpAddress) + } else if len(self.PublicIpAddress.IpAddress) > 0 { eip := SEipAddress{} eip.region = self.host.zone.region eip.IpAddress = self.PublicIpAddress.IpAddress[0] eip.InstanceId = self.InstanceId - eip.AllocationId = self.InstanceId // fixed + eip.AllocationId = self.InstanceId // fixed. AllocationId等于InstanceId即表示为 仿真EIP。 eip.Bandwidth = 10000 return &eip, nil - } else if len(self.EipAddress.IpAddress) > 0 { - return self.host.zone.region.GetEip(self.EipAddress.AllocationId) } else { return nil, nil } @@ -451,6 +450,7 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit } var networkInterfaces SNetworkInterfaces + var eipAddress SEipAddress for _, n := range instance.NetworkInterfaces { i := SNetworkInterface{ MacAddress: *n.MacAddress, @@ -458,6 +458,11 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit PrimaryIpAddress: *n.PrivateIpAddress, } networkInterfaces.NetworkInterface = append(networkInterfaces.NetworkInterface, i) + + // todo: 可能有多个EIP的情况。目前只支持一个EIP + if eipAddress.IpAddress == "" && *n.Association.IpOwnerId != "amazon" && len(*n.Association.PublicIp) > 0 { + eipAddress.IpAddress = *n.Association.PublicIp + } } var vpcattr SVpcAttributes @@ -470,6 +475,16 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit productCodes = append(productCodes, *p.ProductCodeId) } + publicIpAddress := SIpAddress{} + if len(*instance.PublicIpAddress) > 0 { + publicIpAddress.IpAddress = []string{*instance.PublicIpAddress} + } + + innerIpAddress := SIpAddress{} + if len(*instance.PrivateIpAddress) > 0 { + innerIpAddress.IpAddress = []string{*instance.PrivateIpAddress} + } + szone, err := self.getZoneById(*instance.Placement.AvailabilityZone) if err != nil { return nil, 0, err @@ -496,8 +511,9 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit PublicDNSName: *instance.PublicDnsName, RootDeviceName: *instance.RootDeviceName, Status: *instance.State.Name, - InnerIpAddress: SIpAddress{[]string{*instance.PrivateIpAddress}}, - PublicIpAddress: SIpAddress{[]string{*instance.PublicIpAddress}}, + InnerIpAddress: innerIpAddress, + PublicIpAddress: publicIpAddress, + EipAddress: eipAddress, InstanceName: tagspec.GetNameTag(), Description: tagspec.GetDescTag(), Disks: disks, @@ -509,7 +525,6 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit OSName: image.OSName, // todo: 这里在model层回写OSName信息 OSType: image.OSType, // ExpiredTime: - // EipAddress: // VlanId: // OSType: } diff --git a/pkg/util/aws/region.go b/pkg/util/aws/region.go index 4103613793..13f81d6457 100644 --- a/pkg/util/aws/region.go +++ b/pkg/util/aws/region.go @@ -246,7 +246,7 @@ func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) { return nil, err } - eips, total, err := self.GetEips("", 0, 0) + eips, total, err := self.GetEips("", "", 0, 0) if err != nil { return nil, err } @@ -414,7 +414,7 @@ func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudpro } func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) { - eips, total, err := self.GetEips(eipId, 0, 0) + eips, total, err := self.GetEips(eipId, "", 0, 0) if err != nil { return nil, err } diff --git a/pkg/util/aws/shell/eip.go b/pkg/util/aws/shell/eip.go index ecdc12b0e0..1b74e7e41a 100644 --- a/pkg/util/aws/shell/eip.go +++ b/pkg/util/aws/shell/eip.go @@ -11,7 +11,7 @@ func init() { Limit int `help:"List limit"` } shellutils.R(&EipListOptions{}, "eip-list", "List eips", func(cli *aws.SRegion, args *EipListOptions) error { - eips, total, e := cli.GetEips("", args.Offset, args.Limit) + eips, total, e := cli.GetEips("", "", args.Offset, args.Limit) if e != nil { return e }