/
tencent.go
174 lines (159 loc) · 5.36 KB
/
tencent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package tencent
import (
"context"
"encoding/base64"
"fmt"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/billing"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/cam"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/cdb"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/cos"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/cvm"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/dns"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/lighthouse"
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/tat"
"github.com/404tk/cloudtoolkit/pkg/schema"
"github.com/404tk/cloudtoolkit/utils"
"github.com/404tk/cloudtoolkit/utils/cache"
"github.com/404tk/cloudtoolkit/utils/logger"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
sts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813"
)
// Provider is a data provider for tencent API
type Provider struct {
vendor string
credential *common.Credential
region string
}
// New creates a new provider client for tencent API
func New(options schema.Options) (*Provider, error) {
accessKey, ok := options.GetMetadata(utils.AccessKey)
if !ok {
return nil, &schema.ErrNoSuchKey{Name: utils.AccessKey}
}
secretKey, ok := options.GetMetadata(utils.SecretKey)
if !ok {
return nil, &schema.ErrNoSuchKey{Name: utils.SecretKey}
}
token, _ := options.GetMetadata(utils.SecurityToken)
region, _ := options.GetMetadata(utils.Region)
credential := common.NewTokenCredential(accessKey, secretKey, token)
cpf := profile.NewClientProfile()
payload, _ := options.GetMetadata(utils.Payload)
if payload == "cloudlist" {
request := sts.NewGetCallerIdentityRequest()
// cpf.HttpProfile.Endpoint = "sts.tencentcloudapi.com"
stsclient, _ := sts.NewClient(credential, "ap-guangzhou", cpf)
response, err := stsclient.GetCallerIdentity(request)
if err != nil {
return nil, err
}
msg := "Current account ARN: " + *response.Response.Arn
// accountId, _ := strconv.Atoi(*response.Response.UserId)
cache.Cfg.CredInsert(*response.Response.Type, options)
logger.Warning(msg)
}
return &Provider{
vendor: "tencent",
credential: credential,
region: region,
}, nil
}
// Name returns the name of the provider
func (p *Provider) Name() string {
return p.vendor
}
// Resources returns the provider for a resource deployment source.
func (p *Provider) Resources(ctx context.Context) (schema.Resources, error) {
list := schema.NewResources()
list.Provider = p.vendor
var err error
for _, product := range utils.Cloudlist {
switch product {
case "balance":
d := &billing.Driver{Cred: p.credential, Region: p.region}
d.QueryAccountBalance(ctx)
case "host":
var cvms, lights []schema.Host
cvmprovider := &cvm.Driver{Credential: p.credential, Region: p.region}
cvms, err = cvmprovider.GetResource(ctx)
list.Hosts = append(list.Hosts, cvms...)
light := &lighthouse.Driver{Credential: p.credential, Region: p.region}
lights, err = light.GetResource(ctx)
list.Hosts = append(list.Hosts, lights...)
tat.CacheHostList = list.Hosts
case "domain":
dnsprovider := &dns.Driver{Credential: p.credential}
list.Domains, err = dnsprovider.GetDomains(ctx)
case "account":
camprovider := &cam.Driver{Credential: p.credential}
list.Users, err = camprovider.GetCamUser(ctx)
case "database":
var mysqls, mariadbs, postgres, mssqls []schema.Database
cdbprovider := cdb.Driver{Credential: p.credential, Region: p.region}
mysqls, err = cdbprovider.ListMySQL(ctx)
list.Databases = append(list.Databases, mysqls...)
mariadbs, err = cdbprovider.ListMariaDB(ctx)
list.Databases = append(list.Databases, mariadbs...)
postgres, err = cdbprovider.ListPostgreSQL(ctx)
list.Databases = append(list.Databases, postgres...)
mssqls, err = cdbprovider.ListSQLServer(ctx)
list.Databases = append(list.Databases, mssqls...)
case "bucket":
cosprovider := &cos.Driver{Credential: p.credential}
list.Storages, err = cosprovider.GetBuckets(ctx)
default:
}
}
return list, err
}
func (p *Provider) UserManagement(action, args_1, args_2 string) {
c := &cam.Driver{Credential: p.credential}
switch action {
case "add":
c.UserName = args_1
c.Password = args_2
c.AddUser()
case "del":
c.UserName = args_1
c.DelUser()
case "shadow":
c.RoleName = args_1
c.Uin = args_2
c.AddRole()
case "delrole":
c.RoleName = args_1
c.DelRole()
default:
logger.Error("Please set metadata like \"add username password\" or \"del username\"")
}
}
func (p *Provider) BucketDump(ctx context.Context, action, bucketname string) {
logger.Info("Recommended use https://cosbrowser.cloud.tencent.com/web")
}
func (p *Provider) EventDump(action, sourceIp string) {}
func (p *Provider) ExecuteCloudVMCommand(instanceId, cmd string) {
var region, ostype string
for _, host := range tat.CacheHostList {
if host.ID == instanceId {
region = host.Region
ostype = host.OSType
break
}
}
if region == "" {
logger.Error("Run cloudlist first")
return
}
d := tat.Driver{Credential: p.credential, Region: region}
command, err := base64.StdEncoding.DecodeString(cmd)
if err != nil {
logger.Error(err.Error())
return
}
output := d.RunCommand(instanceId, ostype, string(command))
if output != "" {
fmt.Println(output)
}
}
func (p *Provider) DBManagement(action, args string) {}