代理批量关联租户接口、关于页运行时与资源指标
This commit is contained in:
@@ -229,6 +229,63 @@ func (s *ProxyService) Delete(ctx context.Context, id uint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetTenants 设置关联此代理的租户全集:列表内的租户改挂到本代理(含从
|
||||
// 其他代理改挂),列表外已关联的解除;整体事务生效,返回新的关联计数。
|
||||
func (s *ProxyService) SetTenants(ctx context.Context, id uint, cfgIDs []uint) (int64, error) {
|
||||
if err := s.db.WithContext(ctx).First(&model.Proxy{}, id).Error; err != nil {
|
||||
return 0, fmt.Errorf("find proxy %d: %w", id, err)
|
||||
}
|
||||
ids, err := s.validTenantIDs(ctx, cfgIDs)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
unbind := tx.Model(&model.OciConfig{}).Where("proxy_id = ?", id)
|
||||
if len(ids) > 0 {
|
||||
unbind = unbind.Where("id NOT IN ?", ids)
|
||||
}
|
||||
if err := unbind.Update("proxy_id", nil).Error; err != nil {
|
||||
return fmt.Errorf("unbind tenants: %w", err)
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
err := tx.Model(&model.OciConfig{}).Where("id IN ?", ids).Update("proxy_id", id).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("bind tenants: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int64(len(ids)), nil
|
||||
}
|
||||
|
||||
// validTenantIDs 去重并校验租户 ID 均存在;含无效 ID 时报 ErrProxyInvalid。
|
||||
func (s *ProxyService) validTenantIDs(ctx context.Context, cfgIDs []uint) ([]uint, error) {
|
||||
seen := map[uint]struct{}{}
|
||||
ids := make([]uint, 0, len(cfgIDs))
|
||||
for _, v := range cfgIDs {
|
||||
if _, ok := seen[v]; !ok {
|
||||
seen[v] = struct{}{}
|
||||
ids = append(ids, v)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return ids, nil
|
||||
}
|
||||
var n int64
|
||||
err := s.db.WithContext(ctx).Model(&model.OciConfig{}).Where("id IN ?", ids).Count(&n).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count tenants: %w", err)
|
||||
}
|
||||
if n != int64(len(ids)) {
|
||||
return nil, fmt.Errorf("存在无效的租户 ID: %w", ErrProxyInvalid)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// SpecOf 解密并组装 SDK 用的代理参数;id 为 nil 返回 nil(直连)。
|
||||
func (s *ProxyService) SpecOf(ctx context.Context, id *uint) (*oci.ProxySpec, error) {
|
||||
if id == nil {
|
||||
|
||||
Reference in New Issue
Block a user