Merge pull request #8227 from yousong/feature/yousong-eip-isp

Feature/yousong eip bgp type
This commit is contained in:
yunion-ci-robot
2020-10-20 11:00:16 +08:00
committed by GitHub
8 changed files with 134 additions and 17 deletions
+4
View File
@@ -52,6 +52,7 @@ func init() {
Bandwidth int `help:"Bandwidth in Mbps"`
Ip string `help:"IP address of the EIP"`
Network string `help:"Network of the EIP"`
BgpType string `help:"BgpType of the EIP" positional:"false"`
ChargeType string `help:"bandwidth charge type" choices:"traffic|bandwidth"`
}
R(&EipCreateOptions{}, "eip-create", "Create an EIP", func(s *mcclient.ClientSession, args *EipCreateOptions) error {
@@ -74,6 +75,9 @@ func init() {
if len(args.Network) > 0 {
params.Add(jsonutils.NewString(args.Network), "network")
}
if len(args.BgpType) > 0 {
params.Add(jsonutils.NewString(args.BgpType), "bgp_type")
}
if len(args.Ip) > 0 {
params.Add(jsonutils.NewString(args.Ip), "ip")
+30 -6
View File
@@ -69,6 +69,7 @@ func init() {
AllocPolicy string `help:"Address allocation policy" choices:"none|stepdown|stepup|random"`
ServerType string `help:"Server type" choices:"baremetal|container|eip|guest|ipmi|pxe"`
IsAutoAlloc *bool `help:"Auto allocation IP pool"`
BgpType string `help:"Internet service provider name" positional:"false"`
Desc string `help:"Description" metavar:"DESCRIPTION"`
}
R(&NetworkCreateOptions{}, "network-create", "Create a virtual network", func(s *mcclient.ClientSession, args *NetworkCreateOptions) error {
@@ -95,6 +96,9 @@ func init() {
if len(args.Desc) > 0 {
params.Add(jsonutils.NewString(args.Desc), "description")
}
if len(args.BgpType) > 0 {
params.Add(jsonutils.NewString(args.BgpType), "bgp_type")
}
if args.IsAutoAlloc != nil {
params.Add(jsonutils.NewBool(*args.IsAutoAlloc), "is_auto_alloc")
}
@@ -107,17 +111,21 @@ 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"`
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"`
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")
if len(args.BgpType) > 0 {
params.Add(jsonutils.NewString(args.BgpType), "bgp_type")
}
if len(args.Desc) > 0 {
params.Add(jsonutils.NewString(args.Desc), "description")
}
@@ -253,4 +261,20 @@ func init() {
printObject(net)
return nil
})
type NetworkSetBgpTypeOptions struct {
ID string `help:"Network to set BgpType" json:"-"`
BgpType *string `help:"new BgpType name"`
}
R(&NetworkSetBgpTypeOptions{}, "network-set-bgp-type", "Set BgpType of a network", func(s *mcclient.ClientSession, args *NetworkSetBgpTypeOptions) error {
params, err := options.StructToParams(args)
if err != nil {
return err
}
net, err := modules.Networks.PerformAction(s, args.ID, "set-bgp-type", params)
if err != nil {
return err
}
printObject(net)
return nil
})
}
+3
View File
@@ -67,6 +67,9 @@ type SElasticipCreateInput struct {
// 子网Id
// 私有云创建此参数必传,例如Openstack, ZStack
NetworkId string `json:"network_id"`
// description: BgpType for the new eip
BgpType string `json:"bgp_type"`
}
type ElasticipDetails struct {
+12
View File
@@ -193,6 +193,9 @@ type NetworkCreateInput struct {
// deprecated
Vlan *int `json:"vlan" yunion-deprecated-by:"vlan_id"`
// 线路类型
BgpType string `json:"bgp_type"`
}
type NetworkDetails struct {
@@ -329,3 +332,12 @@ type GetNetworkAddressesOutput struct {
// IP子网地址记录
Addresses []SNetworkAddress `json:"addresses"`
}
type NetworkSetBgpTypeInput struct {
apis.Meta
// description: new BgpType name
// required: true
// example: ChinaTelecom, BGP, etc.
BgpType string `json:"bgp_type"`
}
+2 -2
View File
@@ -98,8 +98,8 @@ type SElasticip struct {
// 计费类型: 流量、带宽
// example: bandwidth
ChargeType string `name:"charge_type" list:"user" create:"required"`
// 目前只有华为云此字段是必需填写的
BgpType string `list:"user" create:"optional"`
// 线路类型
BgpType string `width:"64" charset:"utf8" nullable:"false" get:"user" list:"user" create:"optional"`
// 是否跟随主机删除而自动释放
AutoDellocate tristate.TriState `default:"false" get:"user" create:"optional" update:"user"`
+50
View File
@@ -119,6 +119,9 @@ type SNetwork struct {
// 该网段是否用于自动分配IP地址,如果为false,则用户需要明确选择该网段,才会使用该网段分配IP,
// 如果为true,则用户不指定网段时,则自动从该值为true的网络中选择一个分配地址
IsAutoAlloc tristate.TriState `nullable:"true" list:"user" get:"user" update:"user" create:"optional"`
// 线路类型
BgpType string `width:"64" charset:"utf8" nullable:"false" get:"user" update:"user" create:"optional"`
}
func (manager *SNetworkManager) GetContextManagers() [][]db.IModelManager {
@@ -1456,6 +1459,9 @@ func (manager *SNetworkManager) ValidateCreateData(ctx context.Context, userCred
if input.ServerType == api.NETWORK_TYPE_EIP && vpc.Id != api.DEFAULT_VPC_ID {
return input, httperrors.NewInputParameterError("eip network can only exist in default vpc, got %s(%s)", vpc.Name, vpc.Id)
}
if input.ServerType != api.NETWORK_TYPE_EIP {
input.BgpType = ""
}
var (
ipStart = ipRange.StartIp()
@@ -2871,3 +2877,47 @@ func (manager *SNetworkManager) AllowScope(userCred mcclient.TokenCredential) rb
}
}
}
func (self *SNetwork) AllowPerformSetBgpType(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "set-isp")
}
func (self *SNetwork) PerformSetBgpType(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *api.NetworkSetBgpTypeInput) (jsonutils.JSONObject, error) {
if self.BgpType == input.BgpType {
return nil, nil
}
if self.ServerType != api.NETWORK_TYPE_EIP {
return nil, httperrors.NewInputParameterError("BgpType attribute is only useful for eip network")
}
{
var eips []SElasticip
q := ElasticipManager.Query().
Equals("network_id", self.Id).
NotEquals("bgp_type", input.BgpType)
if err := db.FetchModelObjects(ElasticipManager, q, &eips); err != nil {
return nil, err
}
for i := range eips {
eip := &eips[i]
if diff, err := db.UpdateWithLock(ctx, eip, func() error {
eip.BgpType = input.BgpType
return nil
}); err != nil {
// no need to retry/restore here. return error
// and retry after user resolves the error
return nil, err
} else {
db.OpsLog.LogEvent(eip, db.ACT_UPDATE, diff, userCred)
}
}
}
if diff, err := db.Update(self, func() error {
self.BgpType = input.BgpType
return nil
}); err != nil {
return nil, err
} else {
db.OpsLog.LogEvent(self, db.ACT_UPDATE, diff, userCred)
}
return nil, nil
}
+32 -9
View File
@@ -898,17 +898,40 @@ func (self *SKVMRegionDriver) RequestDeleteVpc(ctx context.Context, userCred mcc
}
func (self *SKVMRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
if len(input.NetworkId) == 0 {
return httperrors.NewMissingParameterError("network_id")
}
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.NetworkId)
if err != nil {
if err == sql.ErrNoRows {
return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
var network *models.SNetwork
if input.NetworkId != "" {
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.NetworkId)
if err != nil {
if err == sql.ErrNoRows {
return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
}
return httperrors.NewGeneralError(err)
}
return httperrors.NewGeneralError(err)
network = _network.(*models.SNetwork)
input.BgpType = network.BgpType
} else if input.BgpType != "" {
q := models.NetworkManager.Query().
Equals("server_type", api.NETWORK_TYPE_EIP).
Equals("bgp_type", input.BgpType)
var nets []models.SNetwork
if err := db.FetchModelObjects(models.NetworkManager, q, &nets); err != nil {
return err
}
for i := range nets {
net := &nets[i]
cnt, _ := net.GetFreeAddressCount()
if cnt > 0 {
network = net
input.NetworkId = net.Id
break
}
}
if network == nil {
return httperrors.NewNotFoundError("no available eip network from BgpType %s", input.BgpType)
}
} else {
return httperrors.NewMissingParameterError("network_id, isp")
}
network := _network.(*models.SNetwork)
if network.ServerType != api.NETWORK_TYPE_EIP {
return httperrors.NewInputParameterError("bad network type %q, want %q", network.ServerType, api.NETWORK_TYPE_EIP)
}
+1
View File
@@ -99,6 +99,7 @@ func (self *EipAllocateTask) OnInit(ctx context.Context, obj db.IStandaloneModel
}
_, err = db.Update(eip, func() error {
eip.IpAddr = ipAddr
eip.BgpType = network.BgpType
return nil
})
if err != nil {