Files
drawing/tencent.py
2023-03-11 09:48:55 +08:00

97 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
# 从腾讯云自动弹性伸缩
'''
1. 通过腾讯云API创建一个服务器
2. 通过腾讯云API获取服务器的IP地址
3. 通过腾讯云API获取服务器的用户名和密码
4. 通过腾讯云API获取服务器的端口号
5. 通过腾讯云API获取服务器的状态
6. 通过腾讯云API删除一个服务器
1. 如果服务器闲置数大于两台需要通知腾讯云API删除一台闲置服务器
2. 如果服务器负载全满需要通知腾讯云API创建一台新的服务器
3. 如果服务器负载不全满需要通知腾讯云API删除一台闲置服务器
4. 每天的早上9点到下午5点, 至少保留一台空闲服务器
5. 每天的晚上5点到早上9点, 移除所有空闲服务器
'''
try:
# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey此处还需注意密钥对的保密
# 代码泄露可能会导致 SecretId 和 SecretKey 泄露并威胁账号下所有资源的安全性。以下代码示例仅供参考建议采用更安全的方式来使用密钥请参见https://cloud.tencent.com/document/product/1278/85305
# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
cred = credential.Credential("AKIDgO0UrfO2b4mS6io7Yx9Yq1Pjz8AA4qUA", "jVBtV1SnprOviRK5teR7GC82thbTKqrv")
# 实例化一个http选项可选的没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "cvm.ap-guangzhou.tencentcloudapi.com"
# 实例化一个client选项可选的没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = cvm_client.CvmClient(cred, "ap-guangzhou", clientProfile)
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.RunInstancesRequest()
params = {
"InstanceChargeType": "SPOTPAID",
"DisableApiTermination": False,
"Placement": {
"Zone": "ap-guangzhou-6",
"ProjectId": 0
},
"InstanceMarketOptions": {
"SpotOptions": {
"MaxPrice": "1000"
}
},
"VirtualPrivateCloud": {
"AsVpcGateway": False,
"VpcId": "vpc-fyjby1gt",
"SubnetId": "subnet-oqdeakts",
"Ipv6AddressCount": 0
},
"InstanceType": "S6.MEDIUM2",
"ImageId": "img-eb30mz89",
"SystemDisk": {
"DiskSize": 50,
"DiskType": "CLOUD_BSSD"
},
"InternetAccessible": {
"InternetMaxBandwidthOut": 100,
"PublicIpAssigned": True,
"InternetChargeType": "TRAFFIC_POSTPAID_BY_HOUR"
},
"InstanceName": "未命名",
"LoginSettings": {
"KeyIds": [ "skey-672qeot7" ]
},
"SecurityGroupIds": [ "sg-fkkd8qw9" ],
"InstanceCount": 1,
"EnhancedService": {
"SecurityService": {
"Enabled": True
},
"MonitorService": {
"Enabled": True
},
"AutomationService": {
"Enabled": True
}
}
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个RunInstancesResponse的实例与请求对象对应
resp = client.RunInstances(req)
# 输出json格式的字符串回包
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)