Skip to content

Commit

Permalink
refactor: switch platform by sdk
Browse files Browse the repository at this point in the history
Signed-off-by: yang <[email protected]>
  • Loading branch information
big-dust committed Sep 27, 2023
1 parent 387aa2d commit bbb4a41
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ build
debug
#/*.sh
res/loadbin.go
.idea
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
}
6 changes: 4 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,15 @@ 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
copier.Copy(&si.ImigrateResult, imigrateRsp)

logger.AppLogger().Debugf("transId=%v, imigrateRsp:%+v, ",
transId, imigrateRsp)
Expand All @@ -159,7 +161,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
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ go 1.20

require (
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/big-dust/platform-sdk-go v0.0.6
github.com/docker/docker v20.10.8+incompatible
github.com/docker/go-connections v0.4.0
github.com/dungeonsnd/gocom v1.0.45
github.com/gibson042/canonicaljson-go v1.0.3
github.com/gin-gonic/gin v1.7.2
github.com/go-redis/redis/v8 v8.11.5
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.2.0
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 Expand Up @@ -50,8 +52,9 @@ require (
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator v9.31.0+incompatible // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.0 // indirect
Expand All @@ -61,7 +64,7 @@ require (
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
github.com/lestrrat-go/strftime v1.0.5 // indirect
github.com/magiconair/properties v1.8.0 // indirect
Expand All @@ -77,7 +80,7 @@ require (
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
Expand All @@ -86,8 +89,8 @@ require (
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect
google.golang.org/grpc v1.39.1 // indirect
Expand Down
Loading

0 comments on commit bbb4a41

Please sign in to comment.