-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTencentCloudApiCommon.cs
170 lines (139 loc) · 6.71 KB
/
TencentCloudApiCommon.cs
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
using API.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using TencentCloud.Lighthouse.V20200324;
using TencentCloud.Lighthouse.V20200324.Models;
namespace API
{
public class TencentCloudApiCommon
{
public static List<TencentCloudApiUser> l = new List<TencentCloudApiUser>() {
};
public static Dictionary<string, string> Regions = new Dictionary<string, string>() {
{"ap-beijing","北京" },
{"ap-chengdu","成都" },
{"ap-guangzhou","广州" },
{"ap-hongkong","中国香港" },
{"ap-nanjing","南京" },
{"ap-shanghai","上海" },
{"ap-singapore","新加坡" },
{"ap-tokyo","东京" },
{"eu-moscow","莫斯科" },
{"na-siliconvalley","硅谷" },
};
public static TencentCloudApiUser GetSecret(string Email)
{
return l.Where(p => p.Email.Contains(Email.Trim())).FirstOrDefault();
}
public static bool Stop(string Email, string InstanceId, string region)
{
try
{
var item = l.Where(p => p.Email == Email.Trim()).FirstOrDefault();
LighthouseClient client = new LighthouseClient(new TencentCloud.Common.Credential() { SecretId = item.SecretId, SecretKey = item.SecretKey }, region);
var rs = client.StopInstancesSync(new StopInstancesRequest() { InstanceIds = new string[] { InstanceId } });
return true;
}
catch (Exception)
{
}
return false;
}
public static bool Start(string Email, string InstanceId, string region)
{
try
{
var item = l.Where(p => p.Email == Email.Trim()).FirstOrDefault();
LighthouseClient client = new LighthouseClient(new TencentCloud.Common.Credential() { SecretId = item.SecretId, SecretKey = item.SecretKey }, region);
var rs = client.StartInstancesSync(new StartInstancesRequest() { InstanceIds = new string[] { InstanceId } });
return true;
}
catch (Exception)
{
}
return false;
}
public static List<TencentCloudServer> GetAll()
{
List<TencentCloudServer> list = new List<TencentCloudServer>();
foreach (var item in l)
{
foreach (var region in Regions)
{
try
{
LighthouseClient client = new LighthouseClient(new TencentCloud.Common.Credential() { SecretId = item.SecretId, SecretKey = item.SecretKey }, region.Key);
var instances = client.DescribeInstancesSync(new DescribeInstancesRequest());
var rs = client.DescribeInstancesTrafficPackagesSync(new DescribeInstancesTrafficPackagesRequest() { });
foreach (var r in rs.InstanceTrafficPackageSet)
{
try
{
double a = r.TrafficPackageSet[0].TrafficUsed.Value;
double b = r.TrafficPackageSet[0].TrafficPackageTotal.Value;
int 百分比 = int.Parse(Math.Round(a / b * 100, 0).ToString());
var model = instances.InstanceSet.Where(p => p.InstanceId == r.InstanceId).FirstOrDefault();
string InstanceState = "";
switch (model.InstanceState)
{
case "PENDING":
InstanceState = "创建中";
break;
case "LAUNCH_FAILED":
InstanceState = "创建失败";
break;
case "RUNNING":
InstanceState = "运行中";
break;
case "STOPPED":
InstanceState = "关机";
break;
case "STARTING":
InstanceState = "开机中";
break;
case "STOPPING":
InstanceState = "关机中";
break;
case "REBOOTING":
InstanceState = "重启中";
break;
case "SHUTDOWN":
InstanceState = "停止待销毁";
break;
case "TERMINATING":
InstanceState = "销毁中";
break;
default:
break;
}
list.Add(new TencentCloudServer()
{
BaiFenBi = 百分比,
InstanceName = model.InstanceName,
InstanceId = model.InstanceId,
IP = model.PublicAddresses[0],
TrafficPackageRemaining = int.Parse((r.TrafficPackageSet[0].TrafficPackageRemaining.Value / 1024 / 1024 / 1024).ToString()),
TrafficUsed = int.Parse((r.TrafficPackageSet[0].TrafficUsed.Value / 1024 / 1024 / 1024).ToString()),
TrafficPackageTotal = int.Parse((r.TrafficPackageSet[0].TrafficPackageTotal.Value / 1024 / 1024 / 1024).ToString()),
InstanceState = InstanceState,
Email = item.Email,
Region = region,
ExpireTime = DateTime.Parse(model.ExpiredTime)
});
}
catch (Exception)
{
}
}
}
catch (Exception)
{
}
}
}
return list;
}
}
}