創建騰訊雲服務器
This commit is contained in:
		@@ -3,8 +3,6 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -13,8 +11,6 @@ import (
 | 
			
		||||
	"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 {
 | 
			
		||||
@@ -38,46 +34,100 @@ type Config struct {
 | 
			
		||||
	} `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)
 | 
			
		||||
// 通過腾讯云API創建服務器
 | 
			
		||||
func CreateServerByTencentCloud(SecretId, SecretKey string) error {
 | 
			
		||||
	credential := common.NewCredential(SecretId, SecretKey)
 | 
			
		||||
	cpf := profile.NewClientProfile()
 | 
			
		||||
	cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
 | 
			
		||||
	client, _ := cvm.NewClient(credential, "", cpf)
 | 
			
		||||
	client, err := cvm.NewClient(credential, "ap-shanghai", cpf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("初始化騰訊雲SDK客戶端失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 实例化一个请求对象,每个接口都会对应一个request对象
 | 
			
		||||
	request := cvm.NewRunInstancesRequest()
 | 
			
		||||
 | 
			
		||||
	// 指定啓動模板, 以創建指定規格的服務器
 | 
			
		||||
	request.LaunchTemplate = &cvm.LaunchTemplate{
 | 
			
		||||
		LaunchTemplateId: common.StringPtr("lt-ks6y5evh"),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 返回的resp是一个RunInstancesResponse的实例,与请求对象对应
 | 
			
		||||
	response, err := client.RunInstances(request)
 | 
			
		||||
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
 | 
			
		||||
		fmt.Printf("An API error has returned: %s", err)
 | 
			
		||||
		return
 | 
			
		||||
		return fmt.Errorf("已返回 API 错误: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
		return fmt.Errorf("运行实例失败: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	// 输出json格式的字符串回包
 | 
			
		||||
	fmt.Printf("%s", response.ToJsonString())
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 通過腾讯云API註銷指定的服務器
 | 
			
		||||
func TerminateServerByTencentCloud(SecretId, SecretKey string) error {
 | 
			
		||||
	credential := common.NewCredential(SecretId, SecretKey)
 | 
			
		||||
	cpf := profile.NewClientProfile()
 | 
			
		||||
	cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
 | 
			
		||||
	client, err := cvm.NewClient(credential, "ap-shanghai", cpf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("初始化騰訊雲SDK客戶端失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 实例化一个请求对象,每个接口都会对应一个request对象
 | 
			
		||||
	request := cvm.NewTerminateInstancesRequest()
 | 
			
		||||
 | 
			
		||||
	// 指定要註銷的服務器ID
 | 
			
		||||
	request.InstanceIds = []*string{common.StringPtr("ins-q0z8ev39")}
 | 
			
		||||
 | 
			
		||||
	// 返回的resp是一个TerminateInstancesResponse的实例,与请求对象对应
 | 
			
		||||
	response, err := client.TerminateInstances(request)
 | 
			
		||||
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
 | 
			
		||||
		return fmt.Errorf("已返回 API 错误: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("註銷實例失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	// 输出json格式的字符串回包
 | 
			
		||||
	fmt.Printf("%s", response.ToJsonString())
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 檢查騰訊雲中的服務器狀態
 | 
			
		||||
func CheckServerStatusByTencentCloud(SecretId, SecretKey string) error {
 | 
			
		||||
	credential := common.NewCredential(SecretId, SecretKey)
 | 
			
		||||
	cpf := profile.NewClientProfile()
 | 
			
		||||
	cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
 | 
			
		||||
	client, err := cvm.NewClient(credential, "ap-shanghai", cpf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("初始化騰訊雲SDK客戶端失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 实例化一个请求对象,每个接口都会对应一个request对象
 | 
			
		||||
	request := cvm.NewDescribeInstancesRequest()
 | 
			
		||||
 | 
			
		||||
	// 返回的resp是一个DescribeInstancesResponse的实例,与请求对象对应
 | 
			
		||||
	response, err := client.DescribeInstances(request)
 | 
			
		||||
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
 | 
			
		||||
		return fmt.Errorf("已返回 API 错误: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("运行实例失败: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	// 输出json格式的字符串回包
 | 
			
		||||
	fmt.Printf("%s", response.ToJsonString())
 | 
			
		||||
 | 
			
		||||
	// 解析JSON
 | 
			
		||||
	var data map[string]interface{}
 | 
			
		||||
	if err := json.Unmarshal([]byte(response.ToJsonString()), &data); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(data["TotalCount"])
 | 
			
		||||
	fmt.Println(data["InstanceSet"])
 | 
			
		||||
	// TODO: 判斷每臺服務器狀態
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (server *Server) CheckStatus() error {
 | 
			
		||||
							
								
								
									
										57
									
								
								models/server_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								models/server_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"gopkg.in/yaml.v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 單元測試:創建服務器
 | 
			
		||||
func TestCreateServerByTencentCloud(t *testing.T) {
 | 
			
		||||
	absPath, _ := filepath.Abs("../data/config.yaml")
 | 
			
		||||
	configFile, err := ioutil.ReadFile(absPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("讀取配置文件失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	var config Config
 | 
			
		||||
	if err := yaml.Unmarshal(configFile, &config); err != nil {
 | 
			
		||||
		t.Errorf("格式化配置文件失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err := CreateServerByTencentCloud(config.TencentCloud.SecretId, config.TencentCloud.SecretKey); err != nil {
 | 
			
		||||
		t.Errorf("創建服務器失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 單元測試:獲取服務器列表
 | 
			
		||||
func TestGetServerListByTencentCloud(t *testing.T) {
 | 
			
		||||
	absPath, _ := filepath.Abs("../data/config.yaml")
 | 
			
		||||
	configFile, err := ioutil.ReadFile(absPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("讀取配置文件失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	var config Config
 | 
			
		||||
	if err := yaml.Unmarshal(configFile, &config); err != nil {
 | 
			
		||||
		t.Errorf("格式化配置文件失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err := CheckServerStatusByTencentCloud(config.TencentCloud.SecretId, config.TencentCloud.SecretKey); err != nil {
 | 
			
		||||
		t.Errorf("獲取服務器列表失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 單元測試:註銷服務器
 | 
			
		||||
func TestTerminateServerByTencentCloud(t *testing.T) {
 | 
			
		||||
	absPath, _ := filepath.Abs("../data/config.yaml")
 | 
			
		||||
	configFile, err := ioutil.ReadFile(absPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("讀取配置文件失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	var config Config
 | 
			
		||||
	if err := yaml.Unmarshal(configFile, &config); err != nil {
 | 
			
		||||
		t.Errorf("格式化配置文件失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if err := TerminateServerByTencentCloud(config.TencentCloud.SecretId, config.TencentCloud.SecretKey); err != nil {
 | 
			
		||||
		t.Errorf("註銷服務器失敗: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user