mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix(region): support aws assign public ip with network
This commit is contained in:
@@ -114,18 +114,20 @@ func init() {
|
||||
})
|
||||
|
||||
type NetworkCreateOptions2 struct {
|
||||
Wire string `help:"ID or Name of wire in which the network is created"`
|
||||
Vpc string `help:"ID or Name of vpc in which the network is created"`
|
||||
Zone string `help:"ID or Name of zone in which the network is created"`
|
||||
NAME string `help:"Name of new network"`
|
||||
PREFIX string `help:"Start of IPv4 address range"`
|
||||
BgpType string `help:"Internet service provider name" positional:"false"`
|
||||
Desc string `help:"Description" metavar:"DESCRIPTION"`
|
||||
Wire string `help:"ID or Name of wire in which the network is created"`
|
||||
Vpc string `help:"ID or Name of vpc in which the network is created"`
|
||||
Zone string `help:"ID or Name of zone in which the network is created"`
|
||||
NAME string `help:"Name of new network"`
|
||||
PREFIX string `help:"Start of IPv4 address range"`
|
||||
BgpType string `help:"Internet service provider name" positional:"false"`
|
||||
AssignPublicIp bool
|
||||
Desc string `help:"Description" metavar:"DESCRIPTION"`
|
||||
}
|
||||
R(&NetworkCreateOptions2{}, "network-create2", "Create a virtual network", func(s *mcclient.ClientSession, args *NetworkCreateOptions2) error {
|
||||
params := jsonutils.NewDict()
|
||||
params.Add(jsonutils.NewString(args.NAME), "name")
|
||||
params.Add(jsonutils.NewString(args.PREFIX), "guest_ip_prefix")
|
||||
params.Set("assign_public_ip", jsonutils.NewBool(args.AssignPublicIp))
|
||||
if len(args.BgpType) > 0 {
|
||||
params.Add(jsonutils.NewString(args.BgpType), "bgp_type")
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ require (
|
||||
k8s.io/client-go v0.19.3
|
||||
k8s.io/cluster-bootstrap v0.19.3
|
||||
moul.io/http2curl/v2 v2.3.0
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523113658-0669df73bac9
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523152809-6d3849b81667
|
||||
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656
|
||||
yunion.io/x/jsonutils v1.0.1-0.20230428104347-7c2fdff8e8e7
|
||||
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361
|
||||
|
||||
@@ -1185,8 +1185,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
|
||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
|
||||
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523113658-0669df73bac9 h1:3BB/EC6dSg+xP7VFTEZNAODp/oWZ7oX4DtSU4JRQDLk=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523113658-0669df73bac9/go.mod h1:crMeQeaNaZefTXfXbQkoj5SStggqkSNVABHtYBFjM3Y=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523152809-6d3849b81667 h1:/B8QXgM33YcSnG/ag2w+eo4q9W8kfR3rCRR5Bl7FxM4=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523152809-6d3849b81667/go.mod h1:crMeQeaNaZefTXfXbQkoj5SStggqkSNVABHtYBFjM3Y=
|
||||
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 h1:0zlZD5uhZoIHgLVAWCz2aHaYk2ZrNsACCYD7R6EIBII=
|
||||
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
|
||||
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
|
||||
|
||||
@@ -241,6 +241,9 @@ type NetworkCreateInput struct {
|
||||
|
||||
// 线路类型
|
||||
BgpType string `json:"bgp_type"`
|
||||
|
||||
// 是否申请ip时自动挂载公网ip,仅对aws生效
|
||||
AssignPublicIp bool `json:"assign_public_ip"`
|
||||
}
|
||||
|
||||
type SNetworkNics struct {
|
||||
|
||||
@@ -117,9 +117,6 @@ type SNetwork struct {
|
||||
|
||||
VlanId int `nullable:"false" default:"1" list:"user" update:"user" create:"optional"`
|
||||
|
||||
// 二层网络Id
|
||||
// WireId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
|
||||
|
||||
// 服务器类型
|
||||
// example: server
|
||||
ServerType string `width:"16" charset:"ascii" default:"guest" nullable:"true" list:"user" create:"optional"`
|
||||
@@ -1896,7 +1893,7 @@ func (self *SNetwork) PostCreate(ctx context.Context, userCred mcclient.TokenCre
|
||||
self.SSharableVirtualResourceBase.PostCreate(ctx, userCred, ownerId, query, data)
|
||||
vpc, _ := self.GetVpc()
|
||||
if vpc != nil && vpc.IsManaged() {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "NetworkCreateTask", self, userCred, nil, "", "", nil)
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "NetworkCreateTask", self, userCred, data.(*jsonutils.JSONDict), "", "", nil)
|
||||
if err != nil {
|
||||
log.Errorf("networkcreateTask create fail: %s", err)
|
||||
} else {
|
||||
|
||||
@@ -146,7 +146,7 @@ type IRegionDriver interface {
|
||||
RequestSyncBucketStatus(ctx context.Context, userCred mcclient.TokenCredential, bucket *SBucket, task taskman.ITask) error
|
||||
RequestSyncDBInstanceBackupStatus(ctx context.Context, userCred mcclient.TokenCredential, backup *SDBInstanceBackup, task taskman.ITask) error
|
||||
|
||||
RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, network *SNetwork) error
|
||||
RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, network *SNetwork, task taskman.ITask) error
|
||||
|
||||
ValidateCreateCdnData(ctx context.Context, userCred mcclient.TokenCredential, input api.CDNDomainCreateInput) (api.CDNDomainCreateInput, error)
|
||||
}
|
||||
|
||||
@@ -446,7 +446,7 @@ func (self *SBaseRegionDriver) ValidateCreateWafRuleData(ctx context.Context, us
|
||||
return input, errors.Wrapf(cloudprovider.ErrNotImplemented, "ValidateCreateWafRuleData")
|
||||
}
|
||||
|
||||
func (self *SBaseRegionDriver) RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, net *models.SNetwork) error {
|
||||
func (self *SBaseRegionDriver) RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, net *models.SNetwork, task taskman.ITask) error {
|
||||
return errors.Wrapf(cloudprovider.ErrNotImplemented, "RequestCreateNetwork")
|
||||
}
|
||||
|
||||
|
||||
@@ -3033,7 +3033,7 @@ func (self *SManagedVirtualizationRegionDriver) RequestAssociateEip(ctx context.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, net *models.SNetwork) error {
|
||||
func (self *SManagedVirtualizationRegionDriver) RequestCreateNetwork(ctx context.Context, userCred mcclient.TokenCredential, net *models.SNetwork, task taskman.ITask) error {
|
||||
wire, err := net.GetWire()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetWire")
|
||||
@@ -3054,6 +3054,7 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateNetwork(ctx context
|
||||
Cidr: prefix.String(),
|
||||
Desc: net.Description,
|
||||
}
|
||||
opts.AssignPublicIp, _ = task.GetParams().Bool("assign_public_ip")
|
||||
|
||||
provider := wire.GetCloudprovider()
|
||||
opts.ProjectId, err = provider.SyncProject(ctx, userCred, net.ProjectId)
|
||||
|
||||
@@ -60,7 +60,7 @@ func (self *NetworkCreateTask) OnInit(ctx context.Context, obj db.IStandaloneMod
|
||||
return
|
||||
}
|
||||
|
||||
err = driver.RequestCreateNetwork(ctx, self.GetUserCred(), net)
|
||||
err = driver.RequestCreateNetwork(ctx, self.GetUserCred(), net, self)
|
||||
if err != nil {
|
||||
self.taskFailed(ctx, net, errors.Wrapf(err, "RequestCreateNetwork"))
|
||||
return
|
||||
|
||||
Vendored
+1
-1
@@ -1457,7 +1457,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
|
||||
# sigs.k8s.io/yaml v1.2.0
|
||||
## explicit; go 1.12
|
||||
sigs.k8s.io/yaml
|
||||
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523113658-0669df73bac9
|
||||
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230523152809-6d3849b81667
|
||||
## explicit; go 1.18
|
||||
yunion.io/x/cloudmux/pkg/apis
|
||||
yunion.io/x/cloudmux/pkg/apis/billing
|
||||
|
||||
+5
-4
@@ -15,10 +15,11 @@
|
||||
package cloudprovider
|
||||
|
||||
type SNetworkCreateOptions struct {
|
||||
Name string
|
||||
Desc string
|
||||
ProjectId string
|
||||
Cidr string
|
||||
Name string
|
||||
Desc string
|
||||
ProjectId string
|
||||
Cidr string
|
||||
AssignPublicIp bool
|
||||
}
|
||||
|
||||
type SWireCreateOptions struct {
|
||||
|
||||
+2
-2
@@ -462,8 +462,8 @@ func (self *SRegion) CreateLoadbalancer(opts *cloudprovider.SLoadbalancerCreateO
|
||||
return nil, errors.Wrapf(err, "GetNetworks(%s)", opts.VpcId)
|
||||
}
|
||||
for i := range nets {
|
||||
if !utils.IsInStringArray(nets[i].NetworkId, opts.NetworkIds) && nets[i].ZoneId != opts.ZoneId {
|
||||
opts.NetworkIds = append(opts.NetworkIds, nets[i].NetworkId)
|
||||
if !utils.IsInStringArray(nets[i].SubnetId, opts.NetworkIds) && nets[i].ZoneId != opts.ZoneId {
|
||||
opts.NetworkIds = append(opts.NetworkIds, nets[i].SubnetId)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
+45
-40
@@ -23,7 +23,6 @@ import (
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/netutils"
|
||||
"yunion.io/x/pkg/util/rbacscope"
|
||||
@@ -38,32 +37,31 @@ type SNetwork struct {
|
||||
AwsTags
|
||||
wire *SWire
|
||||
|
||||
AvailableIpAddressCount int
|
||||
CidrBlock string
|
||||
AvailableIpAddressCount int `xml:"availableIpAddressCount"`
|
||||
CidrBlock string `xml:"cidrBlock"`
|
||||
CreationTime time.Time
|
||||
Description string
|
||||
IsDefault bool
|
||||
Status string
|
||||
NetworkId string
|
||||
SubnetId string `xml:"subnetId"`
|
||||
NetworkName string
|
||||
VpcId string
|
||||
VpcId string `xml:"vpcId"`
|
||||
ZoneId string
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetId() string {
|
||||
return self.NetworkId
|
||||
return self.SubnetId
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetName() string {
|
||||
if len(self.NetworkName) == 0 {
|
||||
return self.NetworkId
|
||||
return self.SubnetId
|
||||
}
|
||||
|
||||
return self.NetworkName
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetGlobalId() string {
|
||||
return self.NetworkId
|
||||
return self.SubnetId
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetStatus() string {
|
||||
@@ -75,17 +73,13 @@ func (self *SNetwork) GetStatus() string {
|
||||
}
|
||||
|
||||
func (self *SNetwork) Refresh() error {
|
||||
new, err := self.wire.zone.region.getNetwork(self.NetworkId)
|
||||
new, err := self.wire.zone.region.getNetwork(self.SubnetId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return jsonutils.Update(self, new)
|
||||
}
|
||||
|
||||
func (self *SNetwork) IsEmulated() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetSysTags() map[string]string {
|
||||
data := map[string]string{}
|
||||
routes, _ := self.wire.vpc.region.GetRouteTablesByNetworkId(self.GetId())
|
||||
@@ -153,40 +147,51 @@ func (self *SNetwork) GetPublicScope() rbacscope.TRbacScope {
|
||||
}
|
||||
|
||||
func (self *SNetwork) Delete() error {
|
||||
return self.wire.zone.region.deleteNetwork(self.NetworkId)
|
||||
return self.wire.zone.region.deleteNetwork(self.SubnetId)
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetAllocTimeoutSeconds() int {
|
||||
return 120 // 2 minutes
|
||||
}
|
||||
|
||||
func (self *SRegion) createNetwork(zoneId string, vpcId string, name string, cidr string, desc string) (string, error) {
|
||||
params := &ec2.CreateSubnetInput{}
|
||||
params.SetAvailabilityZone(zoneId)
|
||||
params.SetVpcId(vpcId)
|
||||
params.SetCidrBlock(cidr)
|
||||
func (self *SRegion) ModifySubnetAttribute(subnetId string, assignPublicIp bool) error {
|
||||
params := map[string]string{
|
||||
"SubnetId": subnetId,
|
||||
"MapPublicIpOnLaunch.Value": "false",
|
||||
}
|
||||
if assignPublicIp {
|
||||
params["MapPublicIpOnLaunch.Value"] = "true"
|
||||
}
|
||||
ret := struct {
|
||||
Return bool `xml:"return"`
|
||||
}{}
|
||||
|
||||
ec2Client, err := self.getEc2Client()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "getEc2Client")
|
||||
return self.ec2Request("ModifySubnetAttribute", params, &ret)
|
||||
}
|
||||
|
||||
func (self *SRegion) createNetwork(zoneId string, vpcId string, name string, cidr, desc string) (string, error) {
|
||||
params := map[string]string{
|
||||
"AvailabilityZone": zoneId,
|
||||
"VpcId": vpcId,
|
||||
"CidrBlock": cidr,
|
||||
"TagSpecification.1.ResourceType": "subnet",
|
||||
"TagSpecification.1.Tag.1.Key": "Name",
|
||||
"TagSpecification.1.Tag.1.Value": name,
|
||||
}
|
||||
ret, err := ec2Client.CreateSubnet(params)
|
||||
if err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
paramsTags := &ec2.CreateTagsInput{}
|
||||
tagspec := TagSpec{ResourceType: "subnet"}
|
||||
tagspec.SetNameTag(name)
|
||||
tagspec.SetDescTag(desc)
|
||||
ec2Tag, _ := tagspec.GetTagSpecifications()
|
||||
paramsTags.SetResources([]*string{ret.Subnet.SubnetId})
|
||||
paramsTags.SetTags(ec2Tag.Tags)
|
||||
_, err := ec2Client.CreateTags(paramsTags)
|
||||
if err != nil {
|
||||
log.Infof("createNetwork write tags failed:%s", err)
|
||||
}
|
||||
return *ret.Subnet.SubnetId, nil
|
||||
if len(desc) > 0 {
|
||||
params["TagSpecification.1.Tag.2.Key"] = "Description"
|
||||
params["TagSpecification.1.Tag.2.Value"] = desc
|
||||
}
|
||||
|
||||
ret := struct {
|
||||
Subnet SNetwork `xml:"subnet"`
|
||||
}{}
|
||||
|
||||
err := self.ec2Request("CreateSubnet", params, &ret)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "CreateSubnet")
|
||||
}
|
||||
return ret.Subnet.SubnetId, nil
|
||||
}
|
||||
|
||||
func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) {
|
||||
@@ -260,7 +265,7 @@ func (self *SRegion) GetNetwroks(ids []string, vpcId string) ([]SNetwork, error)
|
||||
subnet.Status = *item.State
|
||||
subnet.ZoneId = *item.AvailabilityZone
|
||||
subnet.IsDefault = *item.DefaultForAz
|
||||
subnet.NetworkId = *item.SubnetId
|
||||
subnet.SubnetId = *item.SubnetId
|
||||
subnet.NetworkName = tagspec.GetNameTag()
|
||||
jsonutils.Update(&subnet.AwsTags.TagSet, item.Tags)
|
||||
subnets = append(subnets, subnet)
|
||||
|
||||
+5
-4
@@ -96,14 +96,15 @@ func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, e
|
||||
func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
|
||||
networkId, err := self.zone.region.createNetwork(self.zone.ZoneId, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc)
|
||||
if err != nil {
|
||||
log.Errorf("createNetwork error %s", err)
|
||||
return nil, errors.Wrap(err, "createNetwork")
|
||||
}
|
||||
if opts.AssignPublicIp {
|
||||
self.zone.region.ModifySubnetAttribute(networkId, opts.AssignPublicIp)
|
||||
}
|
||||
self.inetworks = nil
|
||||
network := self.getNetworkById(networkId)
|
||||
if network == nil {
|
||||
log.Errorf("cannot find network after create????")
|
||||
return nil, errors.Wrap(cloudprovider.ErrNotFound, "getNetworkById")
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "getNetworkById(%s)", networkId)
|
||||
}
|
||||
return network, nil
|
||||
}
|
||||
@@ -130,7 +131,7 @@ func (self *SWire) addNetwork(network *SNetwork) {
|
||||
}
|
||||
find := false
|
||||
for i := 0; i < len(self.inetworks); i += 1 {
|
||||
if self.inetworks[i].GetId() == network.NetworkId {
|
||||
if self.inetworks[i].GetId() == network.SubnetId {
|
||||
find = true
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user