@@ -3,8 +3,10 @@ package cvm
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 "github.com/404tk/cloudtoolkit/pkg/schema"
9+ "github.com/404tk/cloudtoolkit/utils"
810 "github.com/404tk/cloudtoolkit/utils/logger"
911 "github.com/404tk/cloudtoolkit/utils/processbar"
1012 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
@@ -17,14 +19,24 @@ type Driver struct {
1719 Region string
1820}
1921
22+ func (d * Driver ) NewClient () (* cvm.Client , error ) {
23+ cpf := profile .NewClientProfile ()
24+ region := d .Region
25+ if region == "all" || region == "" {
26+ region = "ap-guangzhou"
27+ }
28+ return cvm .NewClient (d .Credential , region , cpf )
29+ }
30+
31+ var linuxSet = []string {"CentOS" , "Ubuntu" , "Debian" , "OpenSUSE" , "SUSE" , "CoreOS" , "FreeBSD" , "Kylin" , "UnionTech" , "TencentOS" , "Other Linux" }
32+
2033// GetResource returns all the resources in the store for a provider.
2134func (d * Driver ) GetResource (ctx context.Context ) ([]schema.Host , error ) {
2235 list := schema .NewResources ().Hosts
2336 logger .Info ("Start enumerating CVM ..." )
24- cpf := profile .NewClientProfile ()
2537 var regions []string
2638 if d .Region == "all" {
27- client , _ := cvm .NewClient (d . Credential , "ap-guangzhou" , cpf )
39+ client , _ := d .NewClient ()
2840 req := cvm .NewDescribeRegionsRequest ()
2941 resp , err := client .DescribeRegions (req )
3042 if err != nil {
@@ -40,7 +52,8 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
4052 flag := false
4153 prevLength := 0
4254 for _ , r := range regions {
43- client , _ := cvm .NewClient (d .Credential , r , cpf )
55+ d .Region = r
56+ client , _ := d .NewClient ()
4457 request := cvm .NewDescribeInstancesRequest ()
4558 response , err := client .DescribeInstances (request )
4659 if err != nil {
@@ -58,11 +71,18 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
5871 }
5972 host := schema.Host {
6073 HostName : * instance .InstanceName ,
74+ ID : * instance .InstanceId ,
6175 PublicIPv4 : ipv4 ,
6276 PrivateIpv4 : privateIPv4 ,
6377 Public : ipv4 != "" ,
6478 Region : r ,
6579 }
80+ os_name := strings .Split (* instance .OsName , " " )[0 ]
81+ if utils .IsContain (linuxSet , os_name ) {
82+ host .OSType = "LINUX_UNIX"
83+ } else {
84+ host .OSType = "WINDOWS"
85+ }
6686 list = append (list , host )
6787 }
6888 select {
0 commit comments