补充资源同步
This commit is contained in:
parent
ba04b02cf8
commit
87bda92fac
@ -58,7 +58,6 @@ func (i *consumer) Priority() int {
|
||||
return event.PRIORITY - 1
|
||||
}
|
||||
|
||||
func (i *consumer) Close(ctx context.Context) error {
|
||||
func (i *consumer) Close(ctx context.Context) {
|
||||
i.ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
@ -1 +1,31 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"122.51.31.227/go-course/go18/devcloud/cmdb/apps/resource"
|
||||
"github.com/infraboard/mcube/v2/ioc"
|
||||
"github.com/infraboard/mcube/v2/ioc/config/datasource"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ioc.Controller().Registry(&ResourceServiceImpl{})
|
||||
}
|
||||
|
||||
var _ resource.Service = (*ResourceServiceImpl)(nil)
|
||||
|
||||
type ResourceServiceImpl struct {
|
||||
ioc.ObjectImpl
|
||||
}
|
||||
|
||||
func (s *ResourceServiceImpl) Name() string {
|
||||
return resource.APP_NAME
|
||||
}
|
||||
|
||||
func (s *ResourceServiceImpl) Init() error {
|
||||
if datasource.Get().AutoMigrate {
|
||||
err := datasource.DB().AutoMigrate(&resource.Resource{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1 +1,28 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"122.51.31.227/go-course/go18/devcloud/cmdb/apps/resource"
|
||||
"github.com/infraboard/mcube/v2/ioc/config/datasource"
|
||||
"github.com/infraboard/mcube/v2/types"
|
||||
)
|
||||
|
||||
// Add implements resource.Service.
|
||||
func (s *ResourceServiceImpl) Add(ctx context.Context, in *resource.Resource) (*resource.Resource, error) {
|
||||
if err := datasource.DBFromCtx(ctx).Save(in).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return in, nil
|
||||
}
|
||||
|
||||
// DeleteResource implements resource.Service.
|
||||
func (s *ResourceServiceImpl) DeleteResource(ctx context.Context, in *resource.DeleteResourceRequest) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// Search implements resource.Service.
|
||||
func (s *ResourceServiceImpl) Search(ctx context.Context, in *resource.SearchRequest) (*types.Set[*resource.Resource], error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
@ -91,8 +91,10 @@ func (s *SecretServiceImpl) SyncResource(ctx context.Context, in *secret.SyncRes
|
||||
// 获取syncer, 执行同步
|
||||
for _, rs := range ins.ResourceType {
|
||||
syncer := secret.GetSyncer(rs)
|
||||
taskInfo := syncer.Sync(ctx, ins, rs)
|
||||
taskSet.Add(taskInfo)
|
||||
for _, region := range ins.Regions {
|
||||
taskInfo := syncer.Sync(ctx, ins, region, rs)
|
||||
taskSet.Add(taskInfo)
|
||||
}
|
||||
}
|
||||
return taskSet, nil
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package impl_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -12,8 +13,8 @@ func TestCreateSecret(t *testing.T) {
|
||||
req := secret.NewCreateSecretRequest()
|
||||
req.Name = "腾讯云只读账号"
|
||||
req.Vendor = resource.VENDOR_TENCENT
|
||||
req.ApiKey = "xxx"
|
||||
req.ApiSecret = "xx"
|
||||
req.ApiKey = os.Getenv("TX_API_KEY")
|
||||
req.ApiSecret = os.Getenv("TX_API_SECRET")
|
||||
req.SetEnabled(true)
|
||||
req.ResourceType = append(req.ResourceType, resource.TYPE_VM)
|
||||
req.Regions = []string{"ap-shanghai", "ap-guangzhou"}
|
||||
@ -34,7 +35,7 @@ func TestQuerySecret(t *testing.T) {
|
||||
}
|
||||
|
||||
const (
|
||||
SECRET_ID = "ed02f7cd-ee5b-33f0-bdf5-e305ecb3efb4"
|
||||
SECRET_ID = "e5aa1ad4-6069-397e-a7ed-a78b9cd2a93b"
|
||||
)
|
||||
|
||||
func TestDescribeSecret(t *testing.T) {
|
||||
|
@ -30,5 +30,5 @@ func RegistrySyncer(t resource.TYPE, s Syncer) {
|
||||
|
||||
type Syncer interface {
|
||||
// 资源同步
|
||||
Sync(context.Context, *Secret, resource.TYPE) *task.Task
|
||||
Sync(ctx context.Context, ins *Secret, region string, rt resource.TYPE) *task.Task
|
||||
}
|
||||
|
59
devcloud/cmdb/apps/secret/sync/data/vm.json
Normal file
59
devcloud/cmdb/apps/secret/sync/data/vm.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"Response": {
|
||||
"InstanceSet": [
|
||||
{
|
||||
"BlueprintId": "lhbp-b46k6f98",
|
||||
"BundleId": "bundle_starter_mc_promo_med2_02",
|
||||
"CPU": 2,
|
||||
"CreatedTime": "2025-03-02T09:54:10Z",
|
||||
"ExpiredTime": "2026-03-02T09:54:10Z",
|
||||
"InitInvocationId": "",
|
||||
"InstanceChargeType": "PREPAID",
|
||||
"InstanceId": "lhins-dxvcse1s",
|
||||
"InstanceName": "Docker CE-MyRb",
|
||||
"InstanceRestrictState": "NORMAL",
|
||||
"InstanceState": "RUNNING",
|
||||
"InternetAccessible": {
|
||||
"InternetChargeType": "TRAFFIC_POSTPAID_BY_HOUR",
|
||||
"InternetMaxBandwidthOut": 4,
|
||||
"PublicIpAssigned": true
|
||||
},
|
||||
"IsolatedTime": null,
|
||||
"LatestOperation": "",
|
||||
"LatestOperationRequestId": "",
|
||||
"LatestOperationStartedTime": "2025-03-02T09:53:37Z",
|
||||
"LatestOperationState": "",
|
||||
"LoginSettings": {
|
||||
"KeyIds": []
|
||||
},
|
||||
"Memory": 2,
|
||||
"OsName": "Ubuntu Server 24.04 LTS 64bit",
|
||||
"Platform": "UBUNTU",
|
||||
"PlatformType": "LINUX_UNIX",
|
||||
"PrivateAddresses": [
|
||||
"10.0.4.9"
|
||||
],
|
||||
"PublicAddresses": [
|
||||
"122.51.31.227"
|
||||
],
|
||||
"PublicIpv6Addresses": [],
|
||||
"RenewFlag": "NOTIFY_AND_MANUAL_RENEW",
|
||||
"SupportIpv6Detail": {
|
||||
"Detail": "EFFECTIVE_IMMEDIATELY",
|
||||
"IsSupport": true,
|
||||
"Message": ""
|
||||
},
|
||||
"SystemDisk": {
|
||||
"DiskId": "lhdisk-adyw1bgo",
|
||||
"DiskSize": 50,
|
||||
"DiskType": "CLOUD_SSD"
|
||||
},
|
||||
"Tags": [],
|
||||
"Uuid": "dd452294-6c06-4d4e-ab5f-b5d74404b863",
|
||||
"Zone": "ap-shanghai-2"
|
||||
}
|
||||
],
|
||||
"RequestId": "9863afd3-47f4-4aa0-9e95-9b61ea3e5573",
|
||||
"TotalCount": 1
|
||||
}
|
||||
}
|
@ -2,14 +2,20 @@ package sync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"122.51.31.227/go-course/go18/devcloud/cmdb/apps/resource"
|
||||
"122.51.31.227/go-course/go18/devcloud/cmdb/apps/secret"
|
||||
"github.com/infraboard/mcube/v2/ioc"
|
||||
"github.com/infraboard/mcube/v2/ioc/config/log"
|
||||
"github.com/infraboard/mcube/v2/tools/ptr"
|
||||
"github.com/infraboard/modules/task/apps/event"
|
||||
"github.com/infraboard/modules/task/apps/task"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -35,13 +41,76 @@ func (s *VmSyncerServiceImpl) Init() error {
|
||||
}
|
||||
|
||||
// Sync implements secret.Syncer.
|
||||
func (s *VmSyncerServiceImpl) Sync(ctx context.Context, secretIns *secret.Secret, rs resource.TYPE) *task.Task {
|
||||
func (s *VmSyncerServiceImpl) Sync(ctx context.Context, ins *secret.Secret, region string, rs resource.TYPE) *task.Task {
|
||||
// 怎么使用对应secret 来完成, 用的腾讯的API
|
||||
// https://console.cloud.tencent.com/api/explorer?Product=cvm&Version=2017-03-12&Action=DescribeRegions
|
||||
// https://console.cloud.tencent.com/api/explorer?Product=lighthouse&Version=2020-03-24&Action=DescribeInstances
|
||||
return task.GetService().Run(ctx, task.NewFnTask(func(ctx context.Context, req any) error {
|
||||
taskInfo := task.GetTaskFromCtx(ctx)
|
||||
_, err := event.GetService().AddEvent(ctx, task.NewInfoEvent("同步开始", taskInfo.Id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer event.GetService().AddEvent(ctx, task.NewInfoEvent("同步结束", taskInfo.Id))
|
||||
|
||||
// taskInfo := task.GetTaskFromCtx(ctx)
|
||||
s.log.Debug().Msg("test for vm sync wait")
|
||||
time.Sleep(1 * time.Second)
|
||||
// secrt同步
|
||||
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||||
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性
|
||||
// 以下代码示例仅供参考,建议采用更安全的方式来使用密钥
|
||||
// 请参见:https://cloud.tencent.com/document/product/1278/85305
|
||||
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
||||
credential := common.NewCredential(
|
||||
ins.ApiKey,
|
||||
ins.ApiSecret,
|
||||
)
|
||||
// 使用临时密钥示例
|
||||
// credential := common.NewTokenCredential("SecretId", "SecretKey", "Token")
|
||||
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "lighthouse.tencentcloudapi.com"
|
||||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||||
// "ap-shanghai"
|
||||
client, _ := lighthouse.NewClient(credential, region, cpf)
|
||||
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
request := lighthouse.NewDescribeInstancesRequest()
|
||||
|
||||
// 返回的resp是一个DescribeInstancesResponse的实例,与请求对象对应
|
||||
response, err := client.DescribeInstances(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = event.GetService().AddEvent(ctx, task.NewInfoEvent("接口调用成功", taskInfo.Id).SetDetail(response.ToJsonString()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 获取出了实例的列表
|
||||
for _, vm := range response.Response.InstanceSet {
|
||||
// 转化为一个Resource对象
|
||||
res := resource.NewResource()
|
||||
res.Vendor = resource.VENDOR_TENCENT
|
||||
res.CredentialId = ins.Id
|
||||
res.Region = region
|
||||
res.ResourceType = resource.TYPE_VM
|
||||
res.Id = ptr.GetValue(vm.InstanceId)
|
||||
res.Name = ptr.GetValue(vm.InstanceName)
|
||||
res.Cpu = ptr.GetValue(vm.CPU)
|
||||
res.Memory = ptr.GetValue(vm.Memory)
|
||||
res.SystemStorage = ptr.GetValue(vm.SystemDisk.DiskSize)
|
||||
res.Extra["os_name"] = ptr.GetValue(vm.OsName)
|
||||
res.PrivateAddress = ptr.GetArrayValue(vm.PrivateAddresses)
|
||||
res.PublicAddress = ptr.GetArrayValue(vm.PublicAddresses)
|
||||
res.Phase = ptr.GetValue(vm.InstanceState)
|
||||
res, err := resource.GetService().Add(ctx, res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = event.GetService().AddEvent(ctx, task.NewInfoEvent(fmt.Sprintf("资源%s同步成功", res.Name), taskInfo.Id).SetDetail(res.String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, secretIns).SetAsync(true).SetLabel(secret.TASK_LABLE_SECRET_ID, secretIns.Id))
|
||||
}, ins).SetAsync(true).SetLabel(secret.TASK_LABLE_SECRET_ID, ins.Id))
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
database = "devcloud_go18"
|
||||
username = "root"
|
||||
password = "123456"
|
||||
auto_migrate = false
|
||||
auto_migrate = true
|
||||
debug = false
|
||||
|
||||
[mongo]
|
||||
|
@ -0,0 +1,3 @@
|
||||
|
||||
TX_API_KEY=
|
||||
TX_API_SECRET=
|
7
go.mod
7
go.mod
@ -9,12 +9,14 @@ require (
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/infraboard/devops v0.0.6
|
||||
github.com/infraboard/mcube/v2 v2.0.61
|
||||
github.com/infraboard/modules v0.0.13
|
||||
github.com/infraboard/mcube/v2 v2.0.63
|
||||
github.com/infraboard/modules v0.0.20
|
||||
github.com/rs/xid v1.6.0
|
||||
github.com/rs/zerolog v1.34.0
|
||||
github.com/segmentio/kafka-go v0.4.47
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1138
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1134
|
||||
go.mongodb.org/mongo-driver v1.17.3
|
||||
golang.org/x/crypto v0.38.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
@ -80,6 +82,7 @@ require (
|
||||
github.com/prometheus/common v0.63.0 // indirect
|
||||
github.com/prometheus/procfs v0.16.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/spf13/cobra v1.9.1 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
|
15
go.sum
15
go.sum
@ -97,10 +97,10 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/infraboard/devops v0.0.6 h1:oo7RfRBxu9hbI/+bYzXuX40BfG1hRyVtxC0fBhoFGBU=
|
||||
github.com/infraboard/devops v0.0.6/go.mod h1:Ac+W3wFy5pG9EH7f1W8DxiAZRhnTDFHrp27WKkYnNoU=
|
||||
github.com/infraboard/mcube/v2 v2.0.61 h1:al8Z+poXXOjfTIAY48ujFzV1uYzH/N7/xmve/ZXArbo=
|
||||
github.com/infraboard/mcube/v2 v2.0.61/go.mod h1:TbYs8cnD8Cg19sTdU0D+vqWAN+LzoxhMYWmAC2pfJkQ=
|
||||
github.com/infraboard/modules v0.0.13 h1:wykjQswgVnqv3mL2qtjqAui/AWuZWPaG/dkzITmDkrc=
|
||||
github.com/infraboard/modules v0.0.13/go.mod h1:B+wkbV8oGOmSwribb3y/+F9PAp8H8UzhkrK7YySuYIk=
|
||||
github.com/infraboard/mcube/v2 v2.0.63 h1:OUmDajfLAQ1LUVEutCSRbDIQItyLSPHYWXUJLO6IbaE=
|
||||
github.com/infraboard/mcube/v2 v2.0.63/go.mod h1:TbYs8cnD8Cg19sTdU0D+vqWAN+LzoxhMYWmAC2pfJkQ=
|
||||
github.com/infraboard/modules v0.0.20 h1:5tNT9eSkTBCx4OA+7sZr1Bf+miOGf9Zft2X/sESrlpE=
|
||||
github.com/infraboard/modules v0.0.20/go.mod h1:H2kRn3Ksp+3OMt9yYB2imNR2RJgSytTVt9WFP8XpDRQ=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
|
||||
@ -181,6 +181,8 @@ github.com/prometheus/procfs v0.16.0/go.mod h1:8veyXUu3nGP7oaCxhX6yeaM5u4stL2FeM
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
@ -207,6 +209,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.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1134/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1138 h1:eMVp9kzjBptP3K0xaUUS68dF5nNTFLbom3uQREaqftM=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1138/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1134 h1:+pjecViStkJrHVbZs+OtCFULfAoD0UOxAZJVDwBjKC8=
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.1134/go.mod h1:vp2EZhERa7VxNFUlntLv7JDu3OD/pbpUpduFf5BGSfQ=
|
||||
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