From 81dc7fd91e79f4572f0995ece023c19beacfb758 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Fri, 13 Dec 2019 09:35:29 +0000 Subject: [PATCH] networks: auto-create zone wire --- pkg/compute/models/networks.go | 10 +++++++++- pkg/compute/models/vpcs.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index f9f937072c..de49df4e37 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -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 { diff --git a/pkg/compute/models/vpcs.go b/pkg/compute/models/vpcs.go index a0d5d98652..f0ad6f4eec 100644 --- a/pkg/compute/models/vpcs.go +++ b/pkg/compute/models/vpcs.go @@ -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 +}