173 lines
5.2 KiB
Go
173 lines
5.2 KiB
Go
|
|
package tkecs
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"fmt"
|
|||
|
|
"net/http"
|
|||
|
|
"sandc/pkg/ali"
|
|||
|
|
|
|||
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|||
|
|
ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v3/client"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type InstanceStatus string
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
InstanceStatusPending InstanceStatus = "Pending"
|
|||
|
|
InstanceStatusRunning InstanceStatus = "Running"
|
|||
|
|
InstanceStatusStarting InstanceStatus = "Starting"
|
|||
|
|
InstanceStatusStopping InstanceStatus = "Stopping"
|
|||
|
|
InstanceStatusStopped InstanceStatus = "Stopped"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type Client struct {
|
|||
|
|
ecs *ecs20140526.Client
|
|||
|
|
regionId string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func NewClient(accessKeyID, accessKeySecret, regionId string) (*Client, error) {
|
|||
|
|
config := &openapi.Config{
|
|||
|
|
// 必填,您的 AccessKey ID
|
|||
|
|
AccessKeyId: ali.String(accessKeyID),
|
|||
|
|
// 必填,您的 AccessKey Secret
|
|||
|
|
AccessKeySecret: ali.String(accessKeySecret),
|
|||
|
|
}
|
|||
|
|
// 访问的域名
|
|||
|
|
config.Endpoint = ali.String(ali.GetEcsEndpointByRegionId(regionId))
|
|||
|
|
ecsClient, err := ecs20140526.NewClient(config)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return &Client{
|
|||
|
|
ecs: ecsClient,
|
|||
|
|
regionId: regionId,
|
|||
|
|
}, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CreateProxyInstance 创建阿里云代理实例
|
|||
|
|
func (c *Client) CreateProxyInstance(name string) (*string, error) {
|
|||
|
|
systemDisk := &ecs20140526.CreateInstanceRequestSystemDisk{
|
|||
|
|
Size: ali.Int32(20), //20G
|
|||
|
|
}
|
|||
|
|
createInstanceRequest := &ecs20140526.CreateInstanceRequest{
|
|||
|
|
RegionId: ali.String(c.regionId),
|
|||
|
|
InstanceType: ali.String("ecs.t5-lc2m1.nano"),
|
|||
|
|
ResourceGroupId: ali.String("rg-aek26d4tdurjdui"),
|
|||
|
|
ImageId: ali.String("m-t4nfwmalijh9zhfflt5i"),
|
|||
|
|
InternetMaxBandwidthOut: ali.Int32(10),
|
|||
|
|
InternetMaxBandwidthIn: ali.Int32(10),
|
|||
|
|
SystemDisk: systemDisk,
|
|||
|
|
InstanceChargeType: ali.String("PrePaid"),
|
|||
|
|
PeriodUnit: ali.String("Month"),
|
|||
|
|
Period: ali.Int32(1),
|
|||
|
|
SecurityGroupId: ali.String("sg-t4nazifybjirx7tepv28"),
|
|||
|
|
InstanceName: ali.String(name),
|
|||
|
|
VSwitchId: ali.String("vsw-t4n1mlcjic25hzki9b6nc"),
|
|||
|
|
InternetChargeType: ali.String("PayByTraffic"),
|
|||
|
|
AutoRenew: ali.Bool(true),
|
|||
|
|
AutoRenewPeriod: ali.Int32(1),
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
res, err := c.ecs.CreateInstance(createInstanceRequest)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.Int32Value(res.StatusCode) != http.StatusOK {
|
|||
|
|
return nil, fmt.Errorf("CreateProxyInstance status failed: %d", http.StatusOK)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fmt.Println("res: ", res, err)
|
|||
|
|
return res.Body.InstanceId, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AllocatePublicIpAddress 为一台ECS实例分配一个固定公网IP地址
|
|||
|
|
func (c *Client) AllocatePublicIpAddress(instanceId string) (*string, error) {
|
|||
|
|
allocatePublicIpAddressRequest := &ecs20140526.AllocatePublicIpAddressRequest{
|
|||
|
|
InstanceId: ali.String(instanceId),
|
|||
|
|
}
|
|||
|
|
res, err := c.ecs.AllocatePublicIpAddress(allocatePublicIpAddressRequest)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.Int32Value(res.StatusCode) != http.StatusOK {
|
|||
|
|
return nil, fmt.Errorf("AllocatePublicIpAddress status failed: %d", http.StatusOK)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return res.Body.IpAddress, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ConvertNatPublicIpToEip
|
|||
|
|
func (c *Client) ConvertNatPublicIpToEip(instanceId string) error {
|
|||
|
|
convertNatPublicIpToEipRequest := &ecs20140526.ConvertNatPublicIpToEipRequest{
|
|||
|
|
InstanceId: ali.String(instanceId),
|
|||
|
|
RegionId: ali.String(c.regionId),
|
|||
|
|
}
|
|||
|
|
res, err := c.ecs.ConvertNatPublicIpToEip(convertNatPublicIpToEipRequest)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.Int32Value(res.StatusCode) != http.StatusOK {
|
|||
|
|
return fmt.Errorf("ConvertNatPublicIpToEip status failed: %d", ali.Int32Value(res.StatusCode))
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// StartInstance 启动一台ECS实例,接口调用成功后实例进入启动中状态
|
|||
|
|
func (c *Client) StartInstance(instanceId string) error {
|
|||
|
|
startInstanceRequest := &ecs20140526.StartInstanceRequest{
|
|||
|
|
InstanceId: ali.String(instanceId),
|
|||
|
|
}
|
|||
|
|
res, err := c.ecs.StartInstance(startInstanceRequest)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.Int32Value(res.StatusCode) != http.StatusOK {
|
|||
|
|
return fmt.Errorf("StartInstance status failed: %d", ali.Int32Value(res.StatusCode))
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CheckInstanceStatusActive 判断实例是否运行中
|
|||
|
|
func (c *Client) CheckInstanceStatusActive(instanceId string) (*bool, error) {
|
|||
|
|
describeInstanceAttributeRequest := &ecs20140526.DescribeInstanceAttributeRequest{
|
|||
|
|
InstanceId: ali.String(instanceId),
|
|||
|
|
}
|
|||
|
|
res, err := c.ecs.DescribeInstanceAttribute(describeInstanceAttributeRequest)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.Int32Value(res.StatusCode) != http.StatusOK {
|
|||
|
|
return nil, fmt.Errorf("CheckInstanceStatusActive status failed: %d", ali.Int32Value(res.StatusCode))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.StringValue(res.Body.Status) == string(InstanceStatusRunning) {
|
|||
|
|
return ali.Bool(true), nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return ali.Bool(false), nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ModifyInstanceAttribute 修改实例的名称
|
|||
|
|
func (c *Client) ModifyInstanceName(instanceId, name string) error {
|
|||
|
|
modifyInstanceAttributeRequest := &ecs20140526.ModifyInstanceAttributeRequest{
|
|||
|
|
InstanceId: ali.String(instanceId),
|
|||
|
|
InstanceName: ali.String(name),
|
|||
|
|
}
|
|||
|
|
res, err := c.ecs.ModifyInstanceAttribute(modifyInstanceAttributeRequest)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ali.Int32Value(res.StatusCode) != http.StatusOK {
|
|||
|
|
return fmt.Errorf("ModifyInstanceName status failed: %d", ali.Int32Value(res.StatusCode))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return nil
|
|||
|
|
|
|||
|
|
}
|