部分分页拉取
This commit is contained in:
parent
fc0efc490e
commit
f13c890358
15
devcloud-mini/.vscode/launch.json
vendored
Normal file
15
devcloud-mini/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${fileDirname}"
|
||||
}
|
||||
]
|
||||
}
|
@ -58,3 +58,11 @@ func NewSearchRequest() *SearchRequest {
|
||||
Tag: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
func NewResource() *Resource {
|
||||
return &Resource{
|
||||
Meta: &Meta{},
|
||||
Spec: &Spec{},
|
||||
Status: &Status{},
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func (s *SecretServiceImpl) DescribeSecret(ctx context.Context, in *secret.Descr
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ins, nil
|
||||
return ins.SetDefault(), nil
|
||||
|
||||
}
|
||||
|
||||
@ -83,6 +83,7 @@ func (s *SecretServiceImpl) QuerySecret(ctx context.Context, in *secret.QuerySec
|
||||
if err := cursor.Decode(e); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.SetDefault()
|
||||
set.Add(e)
|
||||
}
|
||||
|
||||
@ -91,6 +92,11 @@ func (s *SecretServiceImpl) QuerySecret(ctx context.Context, in *secret.QuerySec
|
||||
}
|
||||
|
||||
// SyncResource implements secret.Service.
|
||||
func (s *SecretServiceImpl) SyncResource(context.Context, *secret.SyncResourceRequest, secret.SyncResourceHandleFunc) error {
|
||||
panic("unimplemented")
|
||||
func (s *SecretServiceImpl) SyncResource(ctx context.Context, in *secret.SyncResourceRequest, cb secret.SyncResourceHandleFunc) error {
|
||||
secret, err := s.DescribeSecret(ctx, secret.NewDescribeSecretRequeset(in.Id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return secret.Sync(cb)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package impl_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
|
||||
@ -32,9 +33,13 @@ func TestQuerySecret(t *testing.T) {
|
||||
|
||||
func TestDescribeSecret(t *testing.T) {
|
||||
req := secret.NewDescribeSecretRequeset("0f6836e0-a894-3f87-b031-216478a8093b")
|
||||
set, err := svc.DescribeSecret(ctx, req)
|
||||
ins, err := svc.DescribeSecret(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(set)
|
||||
t.Log(ins)
|
||||
|
||||
ins.Sync(func(rr secret.ResourceResponse) {
|
||||
fmt.Println(rr)
|
||||
})
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/infraboard/mcube/v2/ioc"
|
||||
"github.com/infraboard/mcube/v2/tools/pretty"
|
||||
"github.com/infraboard/mcube/v2/types"
|
||||
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -43,6 +44,7 @@ type SyncResourceHandleFunc func(ResourceResponse)
|
||||
type ResourceResponse struct {
|
||||
Success bool
|
||||
InstanceId string `json:"instance_id"`
|
||||
Resource *resource.Resource `json:"resource"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,13 @@ type Secret struct {
|
||||
CreateSecretRequest `bson:"inline"`
|
||||
}
|
||||
|
||||
func (s *Secret) SetDefault() *Secret {
|
||||
if s.SyncLimit == 0 {
|
||||
s.SyncLimit = 10
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Secret) String() string {
|
||||
return pretty.ToJSON(s)
|
||||
}
|
||||
@ -40,6 +47,7 @@ func (s *Secret) String() string {
|
||||
func NewCreateSecretRequest() *CreateSecretRequest {
|
||||
return &CreateSecretRequest{
|
||||
Regions: []string{},
|
||||
SyncLimit: 10,
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,12 +68,21 @@ type CreateSecretRequest struct {
|
||||
|
||||
// 资源所在区域
|
||||
Regions []string `json:"regions"`
|
||||
// 通过分页大小
|
||||
SyncLimit int64 `json:"sync_limit"`
|
||||
}
|
||||
|
||||
func (r *CreateSecretRequest) SetIsEncrypted(v bool) {
|
||||
r.isEncrypted = v
|
||||
}
|
||||
|
||||
func (r *CreateSecretRequest) GetSyncLimit() int64 {
|
||||
if r.SyncLimit == 0 {
|
||||
return 10
|
||||
}
|
||||
return r.SyncLimit
|
||||
}
|
||||
|
||||
func (r *CreateSecretRequest) EncryptedApiSecret() error {
|
||||
if r.isEncrypted {
|
||||
return nil
|
||||
|
94
devcloud-mini/cmdb/apps/secret/sync.go
Normal file
94
devcloud-mini/cmdb/apps/secret/sync.go
Normal file
@ -0,0 +1,94 @@
|
||||
package secret
|
||||
|
||||
import (
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
|
||||
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
|
||||
)
|
||||
|
||||
func (s *Secret) Sync(cb SyncResourceHandleFunc) error {
|
||||
switch s.Vendor {
|
||||
case resource.VENDOR_TENCENT:
|
||||
// 腾讯云的API来进行同步, 云资源(API: https://console.cloud.tencent.com/api/explorer?Product=cvm&Version=2017-03-12&Action=DescribeRegions)
|
||||
|
||||
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||||
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
||||
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
||||
credential := common.NewCredential(
|
||||
s.ApiKey,
|
||||
s.ApiSecret,
|
||||
)
|
||||
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "lighthouse.tencentcloudapi.com"
|
||||
|
||||
for i := range s.Regions {
|
||||
region := s.Regions[i]
|
||||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||||
client, _ := lighthouse.NewClient(credential, region, cpf)
|
||||
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
request := lighthouse.NewDescribeInstancesRequest()
|
||||
SetLimit(request, s.SyncLimit)
|
||||
SetOffset(request, 0)
|
||||
|
||||
hasNext := true
|
||||
for hasNext {
|
||||
// 返回的resp是一个DescribeInstancesResponse的实例,与请求对象对应
|
||||
response, err := client.DescribeInstances(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 处理拉取到的一页的数据
|
||||
for _, ins := range response.Response.InstanceSet {
|
||||
cb(ResourceResponse{
|
||||
Resource: TransferLighthouseToResource(ins),
|
||||
})
|
||||
}
|
||||
|
||||
// 当前数据,都没有填满一页,说明后面没有数据
|
||||
if *response.Response.TotalCount < *request.Limit {
|
||||
hasNext = false
|
||||
} else {
|
||||
SetOffset(request, GetValue(request.Offset)+GetValue(request.Limit))
|
||||
}
|
||||
}
|
||||
}
|
||||
case resource.VENDOR_ALIYUN:
|
||||
// 阿里云API
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TransferLighthouseToResource(ins *lighthouse.Instance) *resource.Resource {
|
||||
res := resource.NewResource()
|
||||
// 具体的转化逻辑
|
||||
res.Meta.Id = GetValue(ins.InstanceId)
|
||||
res.Spec.Name = GetValue(ins.InstanceName)
|
||||
res.Spec.Cpu = int32(GetValue(ins.CPU))
|
||||
res.Spec.Memory = GetValue(ins.Memory)
|
||||
res.Spec.Storage = GetValue(ins.SystemDisk.DiskSize)
|
||||
res.Status.PrivateAddress = common.StringValues(ins.PrivateAddresses)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func SetOffset(req *lighthouse.DescribeInstancesRequest, v int64) {
|
||||
req.Offset = &v
|
||||
}
|
||||
|
||||
func SetLimit(req *lighthouse.DescribeInstancesRequest, v int64) {
|
||||
req.Limit = &v
|
||||
}
|
||||
|
||||
func GetValue[T any](ptr *T) T {
|
||||
if ptr == nil {
|
||||
var zero T
|
||||
return zero
|
||||
}
|
||||
|
||||
return *ptr
|
||||
}
|
0
devcloud-mini/etc/unit_test.env
Normal file
0
devcloud-mini/etc/unit_test.env
Normal file
2
go.mod
2
go.mod
@ -10,6 +10,8 @@ require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/infraboard/mcube/v2 v2.0.51
|
||||
github.com/rs/zerolog v1.32.0
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1116
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1115
|
||||
go.mongodb.org/mongo-driver v1.14.0
|
||||
golang.org/x/crypto v0.31.0
|
||||
google.golang.org/grpc v1.70.0
|
||||
|
5
go.sum
5
go.sum
@ -188,6 +188,11 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1115/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1116 h1:hd0TUuEj077JDFJfCVj9WR4P14FYWIROTh13Mq0s0tM=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1116/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1115 h1:x1QmcmWiHgvyADvnNeUMtWZbvrBgkOT1bddlAyFBlgA=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1115/go.mod h1:/+1yyz1i2y7y0hZsmuDskH6JAFeGUrAxJhai2TqTC98=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
|
Loading…
x
Reference in New Issue
Block a user