add subnets

This commit is contained in:
TangBin
2018-10-15 19:34:27 +08:00
parent dede034367
commit 4c2c8c91c4
5 changed files with 87 additions and 13 deletions
-1
View File
@@ -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{
+42 -2
View File
@@ -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
}
+24 -4
View File
@@ -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
}
+20 -5
View File
@@ -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)
}
}
+1 -1
View File
@@ -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())