1 Star 0 Fork 0

李童/cmdb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
operator.go 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
李童 提交于 2023-02-27 16:27 . tasksecret
package cvm
import (
"time"
"gitee.com/vak80/cmdb/apps/machine"
resouce "gitee.com/vak80/cmdb/apps/resource"
"gitee.com/vak80/cmdb/utils"
"github.com/infraboard/mcube/logger"
"github.com/infraboard/mcube/logger/zap"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)
func NewCVMOperator(conn *cvm.Client) *CVMOperator {
return &CVMOperator{
client: conn,
log: zap.L().Named("CVM"),
}
}
type CVMOperator struct {
client *cvm.Client
log logger.Logger
}
func (o *CVMOperator) transferSet(items []*cvm.Instance) *machine.HostSet {
set := machine.NewHostSet()
for i := range items {
set.Add(o.transferOne(items[i]))
}
return set
}
func (o *CVMOperator) transferOne(ins *cvm.Instance) *machine.Host {
h := machine.NewDefaultHost()
h.Base.Vendor = resouce.Vendor_TENCENT
h.Base.Region = o.client.GetRegion()
h.Base.Zone = utils.PtrStrV(ins.Placement.Zone)
h.Base.CreateAt = o.parseTime(utils.PtrStrV(ins.CreatedTime))
h.Base.Id = utils.PtrStrV(ins.InstanceId)
h.Information.ExpireAt = o.parseTime(utils.PtrStrV(ins.ExpiredTime))
h.Information.Type = utils.PtrStrV(ins.InstanceType)
h.Information.Name = utils.PtrStrV(ins.InstanceName)
h.Information.Status = utils.PtrStrV(ins.InstanceState)
h.Information.Tags = transferTags(ins.Tags)
h.Information.PublicIp = utils.SlicePtrStrv(ins.PublicIpAddresses)
h.Information.PrivateIp = utils.SlicePtrStrv(ins.PrivateIpAddresses)
h.Information.PayType = utils.PtrStrV(ins.InstanceChargeType)
h.Describe.Cpu = utils.PtrInt64(ins.CPU)
h.Describe.Memory = utils.PtrInt64(ins.Memory)
h.Describe.OsName = utils.PtrStrV(ins.OsName)
h.Describe.ImageId = utils.PtrStrV(ins.ImageId)
if ins.InternetAccessible != nil {
h.Describe.InternetMaxBandwidthOut = utils.PtrInt64(ins.InternetAccessible.InternetMaxBandwidthOut)
}
h.Describe.KeyPairName = utils.SlicePtrStrv(ins.LoginSettings.KeyIds)
h.Describe.SecurityGroups = utils.SlicePtrStrv(ins.SecurityGroupIds)
return h
}
func (o *CVMOperator) parseTime(t string) int64 {
if t == "" {
return 0
}
ts, err := time.Parse("2006-01-02T15:04:05Z", t)
if err != nil {
o.log.Errorf("parse time error %s", t, err)
return 0
}
return ts.UnixNano()
}
func transferTags(tags []*cvm.Tag) (ret []*resouce.Tag) {
for _, i := range tags {
for _, j := range ret {
j.Key = utils.PtrStrV(i.Key)
j.Value = utils.PtrStrV(i.Value)
}
}
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/vak80/cmdb.git
git@gitee.com:vak80/cmdb.git
vak80
cmdb
cmdb
v0.0.4

搜索帮助