Skip to content

Commit

Permalink
Merge pull request ao-space#13 from big-dust/switch-platform
Browse files Browse the repository at this point in the history
refactor: switch platform by sdk
  • Loading branch information
zhongguang-iscas authored Oct 18, 2023
2 parents 1116597 + 345c4d5 commit 8c953de
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 79 deletions.
17 changes: 9 additions & 8 deletions biz/model/dto/httpbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ const (
AgentCodeCallDiskUsageFailedStr = "AG-563"
AgentCodeMissingMainStorageFailedStr = "AG-590"

AgentCodeSwitchDomainErr = "AG-570" //域名错误
AgentCodeSwitchToNewSSPErr = "AG-571" //临时指向到新的空间平台错误
AgentCodeSwitchNetworkTestErr = "AG-572" //网络测试失败
AgentCodeSwitchRecallGatewayErr = "AG-573" //回写网关失败
AgentCodeSwitchDoingErr = "AG-574" //切换任务正在执行
AgentCodeSwitchGetAccountErr = "AG-575" //从网关获取账号失败
AgentCodeSwitchImigrateErr = "AG-576" //新空间平台迁入失败
AgentCodePrivateSSPRegBoxErr = "AG-577" //私有空间平台注册盒子错误
AgentCodeSwitchDomainErr = "AG-570" //域名错误
AgentCodeSwitchToNewSSPErr = "AG-571" //临时指向到新的空间平台错误
AgentCodeSwitchNetworkTestErr = "AG-572" //网络测试失败
AgentCodeSwitchRecallGatewayErr = "AG-573" //回写网关失败
AgentCodeSwitchDoingErr = "AG-574" //切换任务正在执行
AgentCodeSwitchGetAccountErr = "AG-575" //从网关获取账号失败
AgentCodeSwitchImigrateErr = "AG-576" //新空间平台迁入失败
AgentCodePrivateSSPRegBoxErr = "AG-577" //私有空间平台注册盒子错误
AgentCodeCopyDataToStatusInfoErr = "AG-578" //将迁入请求数据转移到状态信息失败

AgentCodeSwitchTaskNotFoundErr = "AG-580" //切换任务未找到
AgentCodeConnectErr = "AG-581" //连接错误
Expand Down
54 changes: 10 additions & 44 deletions biz/service/switch-platform/emigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,19 @@
package switchplatform

import (
"agent/biz/model/device"
"agent/biz/service/pair"
"agent/config"
"agent/utils"
"fmt"
"net/http"
"strings"
"time"

"agent/utils/logger"
utilshttp "agent/utils/network/http"
"github.com/big-dust/platform-sdk-go/v2"
"time"
)

func emigrate() {
type UserDomainRoute struct {
UserId string `json:"userId"`
UserDomainRedirect string `json:"userDomainRedirect"`
}
type RouteReq struct {
UserDomainRoutes []UserDomainRoute `json:"userDomainRouteInfos"`
}

type RouteRsp struct {
BoxUUID string `json:"boxUUID"`
UserDomainRoutes []UserDomainRoute `json:"userDomainRouteInfos"`
}

var params RouteReq
var params platform.SpacePlatformMigrationOutRequest
for _, oldAC := range si.OldAccount {
for _, newAC := range si.ImigrateResult.UserInfos {
if oldAC.UserId == newAC.UserId {
params.UserDomainRoutes = append(params.UserDomainRoutes, UserDomainRoute{UserId: oldAC.UserId, UserDomainRedirect: newAC.UserDomain})
params.UserDomainRouteInfos = append(params.UserDomainRouteInfos, platform.UserDomainRouteInfo{UserId: oldAC.UserId, UserDomainRedirect: newAC.UserDomain})
}
}
}
Expand All @@ -63,34 +44,19 @@ func emigrate() {

time.Sleep(time.Second * 5)

url, _ := utils.JoinUrl(si.OldApiBaseUrl, strings.ReplaceAll(config.Config.Platform.Route.Path, "{box_uuid}", device.GetDeviceInfo().BoxUuid))

destBRK, err := pair.GetDeviceRegKey(si.OldApiBaseUrl)
client, err := pair.GetSdkClientWithDeviceRegKey(si.NewApiBaseUrl)
if err != nil {
logger.AppLogger().Infof("transId:%v,Failed to get box-reg-key", si.TransId)
continue
logger.AppLogger().Warnf("Failed new SDK Client: err: %v", err.Error())
return
}
resp, err := client.SpacePlatformMigrationOut(&params)

headers := map[string]string{"Box-Reg-Key": destBRK.BoxRegKey, "Request-Id": si.TransId}
rsp := RouteRsp{}

httpReq, httpRsp, body, err := utilshttp.PostJsonWithHeaders(url, params, headers, &rsp)
if err != nil {
logger.AppLogger().Warnf("Failed PostJson, transId:%v, err:%v, @@httpReq:%+v, @@httpRsp:%+v, @@body:%v", si.TransId, err, httpReq, httpRsp, string(body))
logger.AppLogger().Warnf("Space Platform Migrate Out Failed by SDK: transId:%v,emigrate, err:%+v ", si.TransId, err)
continue
}
logger.AppLogger().Infof("transId:%v,emigrate, parms:%+v", si.TransId, params)
logger.AppLogger().Infof("transId:%v,emigrate, rsp:%+v", si.TransId, rsp)
logger.AppLogger().Infof("transId:%v,emigrate, httpReq:%+v", si.TransId, httpReq)
logger.AppLogger().Infof("transId:%v,emigrate, httpRsp:%+v", si.TransId, httpRsp)
logger.AppLogger().Infof("transId:%v,emigrate, body:%v", si.TransId, string(body))

if httpRsp.StatusCode != http.StatusOK {
logger.AppLogger().Infof("transId:%v, Failed to route. httpRsp.StatusCode:%v", si.TransId, httpRsp.StatusCode)
UpdateStatus(StatusAbort, fmt.Sprintf("url:%v, StatusCode:%v", url, httpRsp.StatusCode))
} else {
UpdateStatus(StatusOK, "OK")
}
logger.AppLogger().Infof("transId:%v,emigrate, rsp:%+v", si.TransId, resp)

return
}
Expand Down
46 changes: 21 additions & 25 deletions biz/service/switch-platform/imigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
* @Last Modified by: zhongguang
* @Last Modified time: 2022-11-16 10:39:30
*/

package switchplatform

import (
"agent/biz/model/device"
"agent/biz/service/pair"
"agent/config"
"agent/utils"
"fmt"
"net/http"
"strings"

"agent/utils/logger"
utilshttp "agent/utils/network/http"
"fmt"
"github.com/big-dust/platform-sdk-go/v2"
"github.com/jinzhu/copier"
)

type ImigrateRsp struct {
Expand All @@ -48,36 +43,37 @@ func (r *ImigrateRsp) GetAdminDomain() (string, error) {
return "", fmt.Errorf("not found admin")
}

func imigrate() (*ImigrateRsp, error) {
func imigrate() (*platform.SpacePlatformMigrationResponse, error) {

var mi MigrateInfo
mi.NetworkClinetId = device.GetDeviceInfo().NetworkClient.ClientID

mi.UserInfos = si.OldAccount
url, _ := utils.JoinUrl(si.NewApiBaseUrl, strings.ReplaceAll(config.Config.Platform.Migration.Path, "{box_uuid}", device.GetDeviceInfo().BoxUuid))

destBRK, err := pair.GetDeviceRegKey(si.NewApiBaseUrl)
client, err := pair.GetSdkClientWithDeviceRegKey(si.NewApiBaseUrl)
if err != nil {
logger.AppLogger().Warnf("Failed new SDK Client: err: %v", err.Error())
return nil, err
}

headers := map[string]string{"Box-Reg-Key": destBRK.BoxRegKey, "Request-Id": si.TransId}
rsp := ImigrateRsp{}
input := &platform.SpacePlatformMigrationRequest{
NetworkClientId: mi.NetworkClinetId,
UserInfos: []platform.UserMigrationInfo{},
}

httpReq, httpRsp, body, err1 := utilshttp.PostJsonWithHeaders(url, mi, headers, &rsp)
if err1 != nil {
logger.AppLogger().Warnf("Failed PostJson, transId:%v, err:%v, @@httpReq:%+v, @@httpRsp:%+v, @@body:%v", si.TransId, err1, httpReq, httpRsp, string(body))
return nil, err1
err = copier.Copy(&input.UserInfos, &mi.UserInfos)
if err != nil {
logger.AppLogger().Warnf("Copy UserInfos: err: %v", err.Error())
return nil, err
}
logger.AppLogger().Infof("transId:%v, imigrate, parms:%+v", si.TransId, mi)
logger.AppLogger().Infof("transId:%v, imigrate, rsp:%+v", si.TransId, rsp)
logger.AppLogger().Infof("transId:%v, imigrate, httpReq:%+v", si.TransId, httpReq)
logger.AppLogger().Infof("transId:%v, imigrate, httpRsp:%+v", si.TransId, httpRsp)
logger.AppLogger().Infof("transId:%v, imigrate, body:%v", si.TransId, string(body))
resp, err := client.SpacePlatformMigration(input)

if httpRsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http code:%v", httpRsp.StatusCode)
if err != nil {
logger.AppLogger().Warnf("Imgrate By Sdk Failed: %v", err.Error())
return nil, err
}

return &rsp, nil
logger.AppLogger().Infof("transId:%v, imigrate, rsp:%+v", si.TransId, resp)

return resp, nil
}
14 changes: 12 additions & 2 deletions biz/service/switch-platform/switch_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"agent/utils"
"errors"
"fmt"
"github.com/jinzhu/copier"
"net"
"strings"

Expand Down Expand Up @@ -131,14 +132,23 @@ func doSwitch(transId string, domain string) (dto.BaseRspStr, error) {

//执行迁入
imigrateRsp, err := imigrate()
logger.AppLogger().Debugf("imigrateRsp:%+v, ", imigrateRsp)
if err != nil {
var basersp dto.BaseRspStr
basersp.Code = dto.AgentCodeSwitchImigrateErr
basersp.Message = "failed to imigrate to new SSP."
UpdateStatus(StatusAbort, basersp.Message)
return basersp, err
}
si.ImigrateResult = *imigrateRsp

err = copier.Copy(&si.ImigrateResult, imigrateRsp)
if err != nil {
var basersp dto.BaseRspStr
basersp.Code = dto.AgentCodeCopyDataToStatusInfoErr
basersp.Message = "failed to transport data to StatusInfo."
UpdateStatus(StatusAbort, basersp.Message)
return basersp, err
}

logger.AppLogger().Debugf("transId=%v, imigrateRsp:%+v, ",
transId, imigrateRsp)
Expand All @@ -159,7 +169,7 @@ func doSwitch(transId string, domain string) (dto.BaseRspStr, error) {
transId)

//进行测试
if err := networkDetect(transId, domain, imigrateRsp); err != nil {
if err := networkDetect(transId, domain, &si.ImigrateResult); err != nil {
logger.AppLogger().Debugf("transId=%v, testNetwork:%+v, ",
transId, err)
networkSwitchV2(false)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.3.0
github.com/imdario/mergo v0.3.12
github.com/jinzhu/configor v1.2.1
github.com/jinzhu/copier v0.4.0
github.com/mr-tron/base58 v1.2.0
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975
github.com/paypal/gatt v0.0.0-20151011220935-4ae819d591cf
Expand Down

0 comments on commit 8c953de

Please sign in to comment.