From 4c2c8c91c4ffe3cab6505da4ab81c199fce98606 Mon Sep 17 00:00:00 2001 From: TangBin Date: Mon, 15 Oct 2018 19:34:27 +0800 Subject: [PATCH] add subnets --- pkg/util/aws/instance.go | 1 - pkg/util/aws/network.go | 44 ++++++++++++++++++++++++++++++++++++++-- pkg/util/aws/vpc.go | 28 +++++++++++++++++++++---- pkg/util/aws/wire.go | 25 ++++++++++++++++++----- pkg/util/aws/zone.go | 2 +- 5 files changed, 87 insertions(+), 13 deletions(-) diff --git a/pkg/util/aws/instance.go b/pkg/util/aws/instance.go index 71d14046fe..523236f298 100644 --- a/pkg/util/aws/instance.go +++ b/pkg/util/aws/instance.go @@ -313,7 +313,6 @@ func (self *SRegion) CreateInstance(name string, imageId string, instanceType st VolumeType: &disk.Type, } - // todo: 这里是镜像绑定的deviceName divceName := fmt.Sprintf("/dev/sda1") blockDevice := &ec2.BlockDeviceMapping{ diff --git a/pkg/util/aws/network.go b/pkg/util/aws/network.go index 8ba5fa6c4a..df2bd940de 100644 --- a/pkg/util/aws/network.go +++ b/pkg/util/aws/network.go @@ -4,6 +4,7 @@ import ( "time" "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/cloudprovider" + "github.com/aws/aws-sdk-go/service/ec2" ) type SNetwork struct { @@ -15,8 +16,8 @@ type SNetwork struct { Description string IsDefault bool Status string - VSwitchId string - VSwitchName string + NetworkId string + NetworkName string VpcId string ZoneId string } @@ -97,3 +98,42 @@ func (self *SRegion) deleteNetwork(vswitchId string) error { return nil } +func (self *SRegion) GetNetwroks(ids []string, vpcId string) ([]SNetwork, int, error) { + params := &ec2.DescribeSubnetsInput{} + if len(ids) > 0 { + _ids := make([]*string, len(ids)) + for _, id := range ids{ + _ids = append(_ids, &id) + } + params.SetSubnetIds(_ids) + } + + if len(vpcId) > 0 { + filters := make([]*ec2.Filter, 1) + vpcFilter := &ec2.Filter{} + vpcFilter.SetName("vpc-id") + vpcFilter.SetValues([]*string{&vpcId}) + filters = append(filters, vpcFilter) + params.SetFilters(filters) + } + + items, err := self.ec2Client.DescribeSubnets(params) + if err != nil { + return nil, 0, err + } + + subnets := make([]SNetwork, len(items.Subnets)) + for _, item := range items.Subnets { + subnet := SNetwork{} + subnet.CidrBlock = *item.CidrBlock + subnet.VpcId = *item.VpcId + subnet.Status = *item.State + subnet.ZoneId = *item.AvailabilityZone + subnet.IsDefault = *item.DefaultForAz + subnet.NetworkId = *item.SubnetId + subnet.NetworkName = *item.SubnetId + subnets = append(subnets, subnet) + } + + return subnets, len(subnets), nil +} diff --git a/pkg/util/aws/vpc.go b/pkg/util/aws/vpc.go index 2178c91d32..a6b82d9b4f 100644 --- a/pkg/util/aws/vpc.go +++ b/pkg/util/aws/vpc.go @@ -10,10 +10,6 @@ type SUserCIDRs struct { UserCidr []string } -type SVSwitchIds struct { - VSwitchId []string -} - type SVpc struct { region *SRegion @@ -98,3 +94,27 @@ func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) func (self *SVpc) SyncSecurityGroup(secgroupId string, name string, rules []secrules.SecurityRule) (string, error) { panic("implement me") } + +func (self *SVpc) getWireByZoneId(zoneId string) *SWire { + for i := 0; i <= len(self.iwires); i += 1 { + wire := self.iwires[i].(*SWire) + if wire.zone.ZoneId == zoneId { + return wire + } + } + return nil +} + +func (self *SVpc) fetchNetworks() error { + networks, _, err := self.region.GetNetwroks(nil, self.VpcId) + if err != nil { + return err + } + + for i := 0; i < len(networks); i += 1 { + wire := self.getWireByZoneId(networks[i].ZoneId) + networks[i].wire = wire + wire.addNetwork(&networks[i]) + } + return nil +} \ No newline at end of file diff --git a/pkg/util/aws/wire.go b/pkg/util/aws/wire.go index 330760f87e..d89ee24653 100644 --- a/pkg/util/aws/wire.go +++ b/pkg/util/aws/wire.go @@ -52,11 +52,10 @@ func (self *SWire) GetIZone() cloudprovider.ICloudZone { func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) { if self.inetworks == nil { - // todo: vpc.fetchVSwitches() - // err := self.vpc.fetchVSwitches() - // if err != nil { - return nil, nil - // } + err := self.vpc.fetchNetworks() + if err != nil { + return nil, err + } } return self.inetworks, nil } @@ -88,3 +87,19 @@ func (self *SWire) getNetworkById(networkId string) *SNetwork { } return nil } + +func (self *SWire) addNetwork(network *SNetwork) { + if self.inetworks == nil { + self.inetworks = make([]cloudprovider.ICloudNetwork, 0) + } + find := false + for i := 0; i < len(self.inetworks); i += 1 { + if self.inetworks[i].GetId() == network.NetworkId { + find = true + break + } + } + if !find { + self.inetworks = append(self.inetworks, network) + } +} \ No newline at end of file diff --git a/pkg/util/aws/zone.go b/pkg/util/aws/zone.go index 6e48f7fb74..0faf16fc5d 100644 --- a/pkg/util/aws/zone.go +++ b/pkg/util/aws/zone.go @@ -43,7 +43,7 @@ func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) { return self.iwires, nil } -func (self *SZone) getNetworkById(networkId string) error { +func (self *SZone) getNetworkById(networkId string) *SNetwork { log.Debugf("Search in wires %d", len(self.iwires)) for i := 0; i < len(self.iwires); i += 1 { log.Debugf("Search in wire %s", self.iwires[i].GetName())