Skip to content

Commit

Permalink
fix: fix a tencent cvm lh bug
Browse files Browse the repository at this point in the history
  • Loading branch information
teamssix committed Apr 29, 2023
1 parent 4d46a79 commit a491200
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
3 changes: 1 addition & 2 deletions cmd/tencent/lh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ func init() {
lhCmd.AddCommand(lhLsCmd)
lhCmd.AddCommand(lhExecCmd)
lhCmd.PersistentFlags().BoolVar(&lhFlushCache, "flushCache", false, "刷新缓存,不使用缓存数据 (Refresh the cache without using cached data)")
lhCmd.Flags().StringVarP(&lhRegion, "region", "r", "all", "指定区域 ID (Specify Region ID)")
lhCmd.Flags().StringVarP(&lhSpecifiedInstanceID, "instanceID", "i", "all", "指定实例 ID (Specify Instance ID)")

lhLsCmd.Flags().BoolVar(&running, "running", false, "只显示正在运行的实例 (Show only running instances)")

lhLsCmd.Flags().StringVarP(&lhRegion, "region", "r", "all", "指定区域 ID (Specify Region ID)")
lhExecCmd.Flags().StringVarP(&command, "command", "c", "", "设置待执行的命令 (Set the command you want to execute)")
lhExecCmd.Flags().StringVarP(&commandFile, "file", "f", "", "设置待执行的命令文件 (Set the command file you want to execute)")
lhExecCmd.Flags().StringVarP(&scriptType, "scriptType", "s", "auto", "设置执行脚本的类型 (Set the type of script to execute) [sh|bat|ps]")
Expand Down
27 changes: 17 additions & 10 deletions pkg/cloud/tencent/tencentcvm/cvmls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var (
header = []string{"序号 (SN)", "实例 ID (Instance ID)", "实例名称 (Instance Name)", "系统名称 (OS Name)", "系统类型 (OS Type)", "状态 (Status)", "私有 IP (Private IP)", "公网 IP (Public IP)", "区域 ID (Region ID)"}
LinuxSet = []string{"CentOS", "Ubuntu", "Debian", "OpenSUSE", "SUSE", "CoreOS", "FreeBSD", "Kylin", "UnionTech", "TencentOS", "Other Linux"}
header = []string{"序号 (SN)", "实例 ID (Instance ID)", "实例名称 (Instance Name)", "系统名称 (OS Name)", "系统类型 (OS Type)", "状态 (Status)", "私有 IP (Private IP)", "公网 IP (Public IP)", "区域 ID (Region ID)"}
LinuxSet = []string{"CentOS", "Ubuntu", "Debian", "OpenSUSE", "SUSE", "CoreOS", "FreeBSD", "Kylin", "UnionTech", "TencentOS", "Other Linux"}
InstancesOut []Instances
)

type Instances struct {
Expand All @@ -30,9 +31,10 @@ type Instances struct {
RegionId string
}

func DescribeInstances(region string, running bool, SpecifiedInstanceID string) []Instances {
var out []Instances
func DescribeInstances(region string, running bool, specifiedInstanceID string, offSet int64) []Instances {
request := cvm.NewDescribeInstancesRequest()
request.Offset = common.Int64Ptr(offSet)
request.Limit = common.Int64Ptr(100)
request.SetScheme("https")
if running {
request.Filters = []*cvm.Filter{
Expand All @@ -42,13 +44,14 @@ func DescribeInstances(region string, running bool, SpecifiedInstanceID string)
},
}
}
if SpecifiedInstanceID != "all" {
request.InstanceIds = common.StringPtrs([]string{SpecifiedInstanceID})
if specifiedInstanceID != "all" {
request.InstanceIds = common.StringPtrs([]string{specifiedInstanceID})
}
response, err := CVMClient(region).DescribeInstances(request)
errutil.HandleErr(err)
InstancesList := response.Response.InstanceSet
log.Infof("正在 %s 区域中查找实例 (Looking for instances in the %s region)", region, region)
InstancesTotalCount := *response.Response.TotalCount
if len(InstancesList) != 0 {
log.Infof("在 %s 区域下找到 %d 个实例 (Found %d instances in %s region)", region, len(InstancesList), len(InstancesList), region)
var (
Expand Down Expand Up @@ -93,10 +96,13 @@ func DescribeInstances(region string, running bool, SpecifiedInstanceID string)
PublicIpAddress: PublicIpAddress,
RegionId: *v.Placement.Zone,
}
out = append(out, obj)
InstancesOut = append(InstancesOut, obj)
}
}
return out
if InstancesTotalCount > int64(len(InstancesOut)) {
_ = DescribeInstances(region, running, specifiedInstanceID, int64(len(InstancesOut)))
}
return InstancesOut
}

func ReturnInstancesList(region string, running bool, specifiedInstanceID string) []Instances {
Expand All @@ -105,13 +111,14 @@ func ReturnInstancesList(region string, running bool, specifiedInstanceID string
if region == "all" {
for _, j := range GetCVMRegions() {
region := *j.Region
Instance = DescribeInstances(region, running, specifiedInstanceID)
Instance = DescribeInstances(region, running, specifiedInstanceID, 0)
InstancesOut = nil
for _, i := range Instance {
InstancesList = append(InstancesList, i)
}
}
} else {
InstancesList = DescribeInstances(region, running, specifiedInstanceID)
InstancesList = DescribeInstances(region, running, specifiedInstanceID, 0)
}
return InstancesList
}
Expand Down
25 changes: 16 additions & 9 deletions pkg/cloud/tencent/tencentlh/lhls.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

var (
header = []string{"序号 (SN)", "实例ID (Instance ID)", "实例名称 (Instance Name)", "系统名称 (OS Name)", "系统类型 (OS Type)", "状态 (Status)", "私有 IP (Private IP)", "公网 IP (Public IP)", "区域 ID (Region ID)"}
header = []string{"序号 (SN)", "实例ID (Instance ID)", "实例名称 (Instance Name)", "系统名称 (OS Name)", "系统类型 (OS Type)", "状态 (Status)", "私有 IP (Private IP)", "公网 IP (Public IP)", "区域 ID (Region ID)"}
InstancesOut []Instances
)

type Instances struct {
Expand All @@ -30,9 +31,10 @@ type Instances struct {
RegionId string
}

func DescribeInstances(region string, running bool, SpecifiedInstanceID string) []Instances {
var out []Instances
func DescribeInstances(region string, running bool, specifiedInstanceID string, offSet int64) []Instances {
request := lh.NewDescribeInstancesRequest()
request.Offset = common.Int64Ptr(offSet)
request.Limit = common.Int64Ptr(100)
if running {
request.Filters = []*lh.Filter{
{
Expand All @@ -41,13 +43,14 @@ func DescribeInstances(region string, running bool, SpecifiedInstanceID string)
},
}
}
if SpecifiedInstanceID != "all" {
request.InstanceIds = common.StringPtrs([]string{SpecifiedInstanceID})
if specifiedInstanceID != "all" {
request.InstanceIds = common.StringPtrs([]string{specifiedInstanceID})
}
response, err := LHClient(region).DescribeInstances(request)
errutil.HandleErr(err)
InstancesList := response.Response.InstanceSet
log.Infof("正在 %s 区域中查找实例 (Looking for instances in the %s region)", region, region)
InstancesTotalCount := *response.Response.TotalCount
if len(InstancesList) != 0 {
log.Infof("在 %s 区域下找到 %d 个实例 (Found %d instances in %s region)", region, len(InstancesList), len(InstancesList), region)
var (
Expand Down Expand Up @@ -93,10 +96,13 @@ func DescribeInstances(region string, running bool, SpecifiedInstanceID string)
PublicIpAddress: PublicIpAddress,
RegionId: *v.Zone,
}
out = append(out, obj)
InstancesOut = append(InstancesOut, obj)
}
if InstancesTotalCount > int64(len(InstancesOut)) {
_ = DescribeInstances(region, running, specifiedInstanceID, int64(len(InstancesOut)))
}
}
return out
return InstancesOut
}

func ReturnInstancesList(region string, running bool, specifiedInstanceID string) []Instances {
Expand All @@ -105,13 +111,14 @@ func ReturnInstancesList(region string, running bool, specifiedInstanceID string
if region == "all" {
for _, j := range GetLHRegions() {
region := *j.Region
Instance = DescribeInstances(region, running, specifiedInstanceID)
Instance = DescribeInstances(region, running, specifiedInstanceID, 0)
InstancesOut = nil
for _, i := range Instance {
InstancesList = append(InstancesList, i)
}
}
} else {
InstancesList = DescribeInstances(region, running, specifiedInstanceID)
InstancesList = DescribeInstances(region, running, specifiedInstanceID, 0)
}
return InstancesList
}
Expand Down
1 change: 1 addition & 0 deletions pkg/util/errutil/errutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var errorMessages = map[string]string{
"Code=ResourceUnavailable.AgentNotInstalled": "Agent 未安装 (Agent not installed)",
"Incorrect IAM authentication information": "当前 AK 信息无效 (Current AccessKey information is invalid)",
"The API does not exist or has not been published in the environment": "当前用户已存在,请指定其他用户名 (User already exists, please specify another user name)",
"Status=403 Forbidden, Code=AccessDenied": "当前权限不足 (Insufficient permissions)",
}

var errorMessagesNoExit = map[string]string{
Expand Down

0 comments on commit a491200

Please sign in to comment.