networks: auto-create zone wire

This commit is contained in:
Yousong Zhou
2020-01-17 17:41:06 +08:00
parent 0a3c8acbd4
commit 81dc7fd91e
2 changed files with 26 additions and 1 deletions
+9 -1
View File
@@ -1374,7 +1374,15 @@ func (manager *SNetworkManager) ValidateCreateData(ctx context.Context, userCred
return input, httperrors.NewInternalServerError("query wire for zone %s and vpc %s: %v", input.Zone, input.Vpc, err)
}
if len(wires) == 0 {
return input, httperrors.NewNotFoundError("wire not found for zone %s and vpc %s", input.Zone, input.Vpc)
if region.Provider == api.CLOUD_PROVIDER_ONECLOUD {
wire, err := vpc.initWire(ctx, zone)
if err != nil {
return input, httperrors.NewNotFoundError("init wire for zone %s and vpc %s: %v", input.Zone, input.Vpc, err)
}
input.WireId = wire.Id
} else {
return input, httperrors.NewNotFoundError("wire not found for zone %s and vpc %s", input.Zone, input.Vpc)
}
} else if len(wires) > 1 {
return input, httperrors.NewConflictError("found %d wires for zone %s and vpc %s", len(wires), input.Zone, input.Vpc)
} else {
+17
View File
@@ -927,3 +927,20 @@ func (vpc *SVpc) GetGlobalVpc() (*SGlobalVpc, error) {
}
return gv.(*SGlobalVpc), nil
}
func (self *SVpc) initWire(ctx context.Context, zone *SZone) (*SWire, error) {
wire := &SWire{
VpcId: self.Id,
ZoneId: zone.Id,
Bandwidth: 10000,
Mtu: 1500,
}
wire.IsEmulated = true
wire.Name = fmt.Sprintf("vpc-%s", self.Name)
wire.SetModelManager(WireManager, wire)
err := WireManager.TableSpec().Insert(wire)
if err != nil {
return nil, err
}
return wire, nil
}