Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions endpoints/openapi_v1/bfe_pool/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ xreq.Handler = CreateAction
// CreateAction action
// AUTO GEN BY ctrl, MODIFY AS U NEED
func CreateAction(req *http.Request) (interface{}, error) {
param, err := product_pool.NewUpsertParam(req)
param, err := product_pool.NewCreateParam(req)
if err != nil {
return nil, err
}
Expand All @@ -54,12 +54,19 @@ func CreateAction(req *http.Request) (interface{}, error) {
}

oneData, err := container.PoolManager.CreateBFEPool(req.Context(), &icluster_conf.PoolParam{
Name: param.Name,
Name: param.Name,
Type: param.Type,
}, &icluster_conf.PoolInstances{
Instances: product_pool.Instancesc2i(param.Instances),
})
if err != nil {
return nil, err
}

return product_pool.NewOneData(oneData), nil
pism, err := container.PoolInstancesManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{oneData})
if err != nil {
return nil, err
}

return product_pool.NewOneData(oneData, pism[oneData.Name]), nil
}
8 changes: 7 additions & 1 deletion endpoints/openapi_v1/bfe_pool/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/bfenetworks/api-server/endpoints/openapi_v1/product_pool"
"github.com/bfenetworks/api-server/lib/xreq"
"github.com/bfenetworks/api-server/model/iauth"
"github.com/bfenetworks/api-server/model/icluster_conf"
"github.com/bfenetworks/api-server/stateful/container"
)

Expand All @@ -46,5 +47,10 @@ func DeleteAction(req *http.Request) (interface{}, error) {
return nil, err
}

return product_pool.NewOneData(oldOne), nil
pism, err := container.PoolInstancesManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{oldOne})
if err != nil {
return nil, err
}

return product_pool.NewOneData(oldOne, pism[oldOne.Name]), nil
}
7 changes: 6 additions & 1 deletion endpoints/openapi_v1/bfe_pool/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/bfenetworks/api-server/lib/xerror"
"github.com/bfenetworks/api-server/lib/xreq"
"github.com/bfenetworks/api-server/model/iauth"
"github.com/bfenetworks/api-server/model/icluster_conf"
"github.com/bfenetworks/api-server/stateful/container"
)

Expand Down Expand Up @@ -51,6 +52,10 @@ func OneAction(req *http.Request) (interface{}, error) {
return nil, xerror.WrapRecordNotExist("Instance Pool")
}

return product_pool.NewOneData(one), nil
pism, err := container.PoolInstancesManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{one})
if err != nil {
return nil, err
}
return product_pool.NewOneData(one, pism[one.Name]), nil

}
15 changes: 9 additions & 6 deletions endpoints/openapi_v1/bfe_pool/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ xreq.Handler = UpdateAction
// UpdateAction action
// AUTO GEN BY ctrl, MODIFY AS U NEED
func UpdateAction(req *http.Request) (interface{}, error) {
param, err := product_pool.NewUpsertParam(req)
param, err := product_pool.NewUpdateParam(req)
if err != nil {
return nil, err
}
Expand All @@ -52,11 +52,14 @@ func UpdateAction(req *http.Request) (interface{}, error) {
return nil, xerror.WrapRecordNotExist("Instance Pool")
}

err = container.PoolManager.UpdateBFEPool(req.Context(), one, &icluster_conf.PoolParam{
pi := &icluster_conf.PoolInstances{
Name: one.Name,
Instances: product_pool.Instancesc2i(param.Instances),
})

one.Instances = product_pool.Instancesc2i(param.Instances)
}
err = container.PoolInstancesManager.UpdateInstances(req.Context(), one, pi)
if err != nil {
return nil, err
}

return product_pool.NewOneData(one), err
return product_pool.NewOneData(one, pi), err
}
37 changes: 24 additions & 13 deletions endpoints/openapi_v1/product_pool/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http"
"strings"

"github.com/bfenetworks/api-server/lib"
"github.com/bfenetworks/api-server/lib/xerror"
"github.com/bfenetworks/api-server/lib/xreq"
"github.com/bfenetworks/api-server/model/iauth"
Expand All @@ -26,13 +27,6 @@ import (
"github.com/bfenetworks/api-server/stateful/container"
)

// UpsertParam Request Param
// AUTO GEN BY ctrl, MODIFY AS U NEED
type UpsertParam struct {
Name *string `json:"name" uri:"instance_pool_name" validate:"required,min=2"`
Instances []*Instance `json:"instances" uri:"instances" validate:"min=1,dive"`
}

// CreateRoute route
// AUTO GEN BY ctrl, MODIFY AS U NEED
var CreateEndpoint = &xreq.Endpoint{
Expand All @@ -42,9 +36,19 @@ var CreateEndpoint = &xreq.Endpoint{
Authorizer: iauth.FAP(iauth.FeatureProductPool, iauth.ActionCreate),
}

// CreateParam Request Param
// AUTO GEN BY ctrl, MODIFY AS U NEED
type CreateParam struct {
Name *string `json:"name" validate:"required,min=2"`
Type *int8 `json:"type" validate:"oneof=1"`
Instances []*Instance `json:"instances" validate:"min=1,dive"`
}

// AUTO GEN BY ctrl, MODIFY AS U NEED
func NewUpsertParam(req *http.Request) (*UpsertParam, error) {
param := &UpsertParam{}
func NewCreateParam(req *http.Request) (*CreateParam, error) {
param := &CreateParam{
Type: lib.PInt8(icluster_conf.PoolInstancesTypeRDB),
}
err := xreq.Bind(req, param)
if err != nil {
return nil, err
Expand All @@ -58,7 +62,7 @@ var _ xreq.Handler = CreateAction
// CreateAction action
// AUTO GEN BY ctrl, MODIFY AS U NEED
func CreateAction(req *http.Request) (interface{}, error) {
param, err := NewUpsertParam(req)
param, err := NewCreateParam(req)
if err != nil {
return nil, err
}
Expand All @@ -80,7 +84,12 @@ func CreateAction(req *http.Request) (interface{}, error) {
return nil, err
}

return NewOneData(oneData), nil
pism, err := container.PoolInstancesManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{oneData})
if err != nil {
return nil, err
}

return NewOneData(oneData, pism[oneData.Name]), nil
}

func Instancesc2i(is []*Instance) []icluster_conf.Instance {
Expand All @@ -103,9 +112,11 @@ func Instancesc2i(is []*Instance) []icluster_conf.Instance {
return rst
}

func CreateProcess(req *http.Request, product *ibasic.Product, param *UpsertParam) (*icluster_conf.Pool, error) {
func CreateProcess(req *http.Request, product *ibasic.Product, param *CreateParam) (*icluster_conf.Pool, error) {
return container.PoolManager.CreateProductPool(req.Context(), product, &icluster_conf.PoolParam{
Name: param.Name,
Name: param.Name,
Type: param.Type,
}, &icluster_conf.PoolInstances{
Instances: Instancesc2i(param.Instances),
})
}
8 changes: 7 additions & 1 deletion endpoints/openapi_v1/product_pool/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/bfenetworks/api-server/lib/xreq"
"github.com/bfenetworks/api-server/model/iauth"
"github.com/bfenetworks/api-server/model/ibasic"
"github.com/bfenetworks/api-server/model/icluster_conf"
"github.com/bfenetworks/api-server/stateful/container"
)

Expand Down Expand Up @@ -52,5 +53,10 @@ func DeleteAction(req *http.Request) (interface{}, error) {
return nil, err
}

return NewOneData(oldOne), nil
pism, err := container.PoolInstancesManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{oldOne})
if err != nil {
return nil, err
}

return NewOneData(oldOne, pism[oldOne.Name]), nil
}
26 changes: 16 additions & 10 deletions endpoints/openapi_v1/product_pool/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ type OneData struct {
Instances []*Instance `json:"instances" uri:"instances"`
}

func NewOneData(pool *icluster_conf.Pool) *OneData {
func NewOneData(pool *icluster_conf.Pool, pis *icluster_conf.PoolInstances) *OneData {
is := []*Instance{}
for _, one := range pool.Instances {
is = append(is, &Instance{
Hostname: one.HostName,
IP: one.IP,
Weight: one.Weight,
Ports: one.Ports,
Tags: one.Tags,
})
if pis != nil {
for _, one := range pis.Instances {
is = append(is, &Instance{
Hostname: one.HostName,
IP: one.IP,
Weight: one.Weight,
Ports: one.Ports,
Tags: one.Tags,
})
}
}

return &OneData{
Expand Down Expand Up @@ -105,5 +107,9 @@ func OneAction(req *http.Request) (interface{}, error) {
return nil, xerror.WrapRecordNotExist("Instance Pool")
}

return NewOneData(one), nil
pism, err := container.PoolInstancesManager.BatchFetchInstances(req.Context(), []*icluster_conf.Pool{one})
if err != nil {
return nil, err
}
return NewOneData(one, pism[one.Name]), nil
}
33 changes: 27 additions & 6 deletions endpoints/openapi_v1/product_pool/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ import (
"github.com/bfenetworks/api-server/stateful/container"
)

// UpdateParam Request Param
// AUTO GEN BY ctrl, MODIFY AS U NEED
type UpdateParam struct {
Name *string `uri:"instance_pool_name" validate:"required,min=2"`
Instances []*Instance `json:"instances" validate:"min=1,dive"`
}

// AUTO GEN BY ctrl, MODIFY AS U NEED
func NewUpdateParam(req *http.Request) (*UpdateParam, error) {
param := &UpdateParam{}
err := xreq.Bind(req, param)
if err != nil {
return nil, err
}

return param, err
}

// UpdateRoute route
// AUTO GEN BY ctrl, MODIFY AS U NEED
var UpdateEndpoint = &xreq.Endpoint{
Expand All @@ -39,7 +57,7 @@ var _ xreq.Handler = UpdateAction
// UpdateAction action
// AUTO GEN BY ctrl, MODIFY AS U NEED
func UpdateAction(req *http.Request) (interface{}, error) {
param, err := NewUpsertParam(req)
param, err := NewCreateParam(req)
if err != nil {
return nil, err
}
Expand All @@ -56,11 +74,14 @@ func UpdateAction(req *http.Request) (interface{}, error) {
return nil, xerror.WrapRecordNotExist("Instance Pool")
}

err = container.PoolManager.UpdateProductPool(req.Context(), product, one, &icluster_conf.PoolParam{
pi := &icluster_conf.PoolInstances{
Name: one.Name,
Instances: Instancesc2i(param.Instances),
})

one.Instances = Instancesc2i(param.Instances)
}
err = container.PoolInstancesManager.UpdateInstances(req.Context(), one, pi)
if err != nil {
return nil, err
}

return NewOneData(one), err
return NewOneData(one, pi), nil
}
3 changes: 3 additions & 0 deletions model/icluster_conf/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func ClusterList2MapByID(list []*Cluster) map[int64]*Cluster {

func NewClusterManager(txn itxn.TxnStorager, storager ClusterStorager,
subClusterStorager SubClusterStorager, bfeClusterStorager ibasic.BFEClusterStorager,
poolInstancesManager *PoolInstancesManager,
versionControlManager *iversion_control.VersionControlManager,
deleteCheckers map[string]func(context.Context, *ibasic.Product, *Cluster) error) *ClusterManager {

Expand All @@ -244,6 +245,7 @@ func NewClusterManager(txn itxn.TxnStorager, storager ClusterStorager,
storager: storager,
subClusterStorager: subClusterStorager,
bfeClusterStorager: bfeClusterStorager,
poolInstancesManager: poolInstancesManager,
versionControlManager: versionControlManager,

deleteCheckers: deleteCheckers,
Expand All @@ -266,6 +268,7 @@ type ClusterManager struct {
subClusterStorager SubClusterStorager
bfeClusterStorager ibasic.BFEClusterStorager

poolInstancesManager *PoolInstancesManager
versionControlManager *iversion_control.VersionControlManager

deleteCheckers map[string]func(context.Context, *ibasic.Product, *Cluster) error
Expand Down
26 changes: 23 additions & 3 deletions model/icluster_conf/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,37 @@ func (rm *ClusterManager) clusterTableConfGenerator(ctx context.Context) (*ivers
return nil, err
}

pools := map[string]*Pool{}
for _, cluster := range clusters {
for _, subcluster := range cluster.SubClusters {
if subcluster.InstancePool != nil {
pools[subcluster.InstancePool.Name] = subcluster.InstancePool
}
}
}

// maybe rpc in db transaction
piMap, err := rm.poolInstancesManager.BatchFetchInstances(ctx, PoolMap2List(pools))
if err != nil {
return nil, err
}

allClusters := cluster_table_conf.AllClusterBackend{}
for _, cluster := range clusters {
clusterBackend := map[string]cluster_table_conf.SubClusterBackend{}

for _, subCluster := range cluster.SubClusters {
if subCluster.InstancePool == nil || len(subCluster.InstancePool.Instances) == 0 {
if subCluster.InstancePool == nil {
continue
}

pi := piMap[subCluster.InstancePool.Name]
if pi == nil || len(pi.Instances) == 0 {
continue
}

subClusterBackend := make(cluster_table_conf.SubClusterBackend, 0, len(subCluster.InstancePool.Instances))
for _, instance := range subCluster.InstancePool.Instances {
subClusterBackend := make(cluster_table_conf.SubClusterBackend, 0, len(pi.Instances))
for _, instance := range pi.Instances {
subClusterBackend = append(subClusterBackend, &cluster_table_conf.BackendConf{
Name: lib.PString(instance.HostName),
Addr: lib.PString(instance.IP),
Expand Down
Loading