Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Environ NetworkInterfaces for EC2 #1502

Merged
merged 7 commits into from Feb 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 48 additions & 4 deletions provider/ec2/environ.go
Expand Up @@ -777,10 +777,54 @@ func (e *environ) ReleaseAddress(instId instance.Id, _ network.Id, addr network.
return nil
}

// NetworkInterfaces implements Environ.NetworkInterfaces, but it's
// not implemented on this provider yet.
func (*environ) NetworkInterfaces(_ instance.Id) ([]network.InterfaceInfo, error) {
return nil, errors.NotImplementedf("NetworkInterfaces")
// NetworkInterfaces implements Environ.NetworkInterfaces.
func (e *environ) NetworkInterfaces(instId instance.Id) ([]network.InterfaceInfo, error) {
ec2Client := e.ec2()
var err error
var networkInterfacesResp *ec2.NetworkInterfacesResp
for a := shortAttempt.Start(); a.Next(); {
filter := ec2.NewFilter()
filter.Add("attachment.instance-id", string(instId))
networkInterfacesResp, err = ec2Client.NetworkInterfaces(nil, filter)
if err == nil {
break
}
}
if err != nil {
// either the instance doesn't exist or we couldn't get through to
// the ec2 api
return nil, errors.Annotatef(err, "cannot get instance %v network interfaces", instId)
}
ec2Interfaces := networkInterfacesResp.Interfaces
result := make([]network.InterfaceInfo, len(ec2Interfaces))
for i, iface := range ec2Interfaces {
resp, err := ec2Client.Subnets([]string{iface.SubnetId}, nil)
if err != nil {
return nil, errors.Annotatef(err, "failed to retrieve subnet %v info", iface.SubnetId)
}
if len(resp.Subnets) != 1 {
return nil, errors.Errorf("expected 1 subnet, got %d", len(resp.Subnets))
}
subnet := resp.Subnets[0]
cidr := subnet.CIDRBlock

result[i] = network.InterfaceInfo{
DeviceIndex: iface.Attachment.DeviceIndex,
MACAddress: iface.MACAddress,
CIDR: cidr,
NetworkName: "", // Not needed for now.
ProviderId: network.Id(iface.Id),
ProviderSubnetId: network.Id(iface.SubnetId),
VLANTag: 0, // Not supported on EC2.
// Not supported on EC2, so fake it.
InterfaceName: fmt.Sprintf("eth%d", iface.Attachment.DeviceIndex),
Disabled: false,
NoAutoStart: false,
ConfigType: network.ConfigUnknown,
Address: network.NewAddress(iface.PrivateIPAddress, network.ScopeCloudLocal),
}
}
return result, nil
}

// Subnets returns basic information about the specified subnets known
Expand Down
20 changes: 20 additions & 0 deletions provider/ec2/local_test.go
Expand Up @@ -799,6 +799,26 @@ func (t *localServerSuite) TestReleaseAddress(c *gc.C) {
c.Assert(err, gc.ErrorMatches, msg)
}

func (t *localServerSuite) TestNetworkInterfaces(c *gc.C) {
env, instId := t.setUpInstanceWithDefaultVpc(c)
interfaces, err := env.NetworkInterfaces(instId)
c.Assert(err, jc.ErrorIsNil)
expectedInterfaces := []network.InterfaceInfo{{
DeviceIndex: 0,
MACAddress: "20:01:60:cb:27:37",
CIDR: "10.10.0.0/20",
ProviderId: "eni-0",
ProviderSubnetId: "subnet-0",
VLANTag: 0,
InterfaceName: "eth0",
Disabled: false,
NoAutoStart: false,
ConfigType: "",
Address: network.NewAddress("10.10.0.5", network.ScopeCloudLocal),
}}
c.Assert(interfaces, jc.DeepEquals, expectedInterfaces)
}

func (t *localServerSuite) TestSubnets(c *gc.C) {
env, _ := t.setUpInstanceWithDefaultVpc(c)
subnets, err := env.Subnets("", []network.Id{"subnet-0"})
Expand Down