This commit is contained in:
2023-06-02 17:18:52 +08:00
parent 792b7d24a3
commit 0b1b4f6d4d
3 changed files with 68 additions and 0 deletions

View File

@@ -3,9 +3,18 @@ package models
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"main/configs"
"net/http"
"time"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
"gopkg.in/yaml.v2"
)
type Server struct {
@@ -22,6 +31,55 @@ type Server struct {
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
type Config struct {
TencentCloud struct {
SecretId string `yaml:"SecretId"`
SecretKey string `yaml:"SecretKey"`
} `yaml:"TencentCloud"`
}
func CreateServerByTencentCloud() {
// 從 data/config.yaml 中獲取配置
configFile, err := ioutil.ReadFile("data/config.yaml")
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
var config Config
err = yaml.Unmarshal(configFile, &config)
if err != nil {
log.Fatalf("Failed to unmarshal config file: %v", err)
}
fmt.Println(config.TencentCloud.SecretId)
fmt.Println(config.TencentCloud.SecretKey)
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露并威胁账号下所有资源的安全性。以下代码示例仅供参考建议采用更安全的方式来使用密钥请参见https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
// 实例化一个client选项可选的没有特殊需求可以跳过
// 实例化要请求产品的client对象,clientProfile是可选的
credential := common.NewCredential(config.TencentCloud.SecretId, config.TencentCloud.SecretKey)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
client, _ := cvm.NewClient(credential, "", cpf)
// 实例化一个请求对象,每个接口都会对应一个request对象
request := cvm.NewRunInstancesRequest()
// 返回的resp是一个RunInstancesResponse的实例与请求对象对应
response, err := client.RunInstances(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
// 输出json格式的字符串回包
fmt.Printf("%s", response.ToJsonString())
}
func (server *Server) CheckStatus() error {
switch server.Type {
case "訓練":