1.1 创建用户:访问方式:Open API 调用访问
1.2 记得保存AK,一定要复制保存
1.3 权限管理—>新增授权---->只读访问所有阿里云资源的权限
注意:此处因为我初次练习使用,用了只读权限,初学者建议使用只读权限,如果有写权限,OpenAPI可直接调用操作资源
文档中示例代码仅供参考,真正的该产品 SDK 的使用步骤请参见:https://next.api.aliyun.com/api/Ecs/2014-05-26
// This file is auto-generated, don't edit it. Thanks.
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v2/client"
console "github.com/alibabacloud-go/tea-console/client"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
"os"
)
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *ecs20140526.Client, _err error) {
config := &openapi.Config{
// 您的 AccessKey ID
AccessKeyId: accessKeyId,
// 您的 AccessKey Secret
AccessKeySecret: accessKeySecret,
}
// 访问的域名
config.Endpoint = tea.String("ecs-cn-hangzhou.aliyuncs.com")
_result = &ecs20140526.Client{}
_result, _err = ecs20140526.NewClient(config)
return _result, _err
}
func _main(args []*string) (_err error) {
client, _err := CreateClient(tea.String("替换为你的AccessKey ID"), tea.String("替换为你的AccessKey Secret"))
if _err != nil {
return _err
}
describeInstancesRequest := &ecs20140526.DescribeInstancesRequest{
RegionId: tea.String("cn-hangzhou"),
VpcId: tea.String("你的VPCid"),
VSwitchId: tea.String("你的交换机id"),
InstanceNetworkType: tea.String("vpc"),
ZoneId: tea.String("cn-hangzhou-h"),
}
runtime := &util.RuntimeOptions{}
tryErr := func() (_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
all, _err := client.DescribeInstancesWithOptions(describeInstancesRequest, runtime)
if _err != nil {
return _err
}
instances := all.Body.Instances.Instance
RegionId := tea.String("cn-hangzhou")
console.Log(tea.String(tea.StringValue(RegionId) + " ECS 实例列表:"))
for _, instance := range instances {
// 方式1:
console.Log(tea.String("实例名称: " + tea.StringValue(instance.HostName) + " 实例ID: " + tea.StringValue(instance.InstanceId) +
" 内网IP: " + tea.ToString(*instance.VpcAttributes.PrivateIpAddress.IpAddress[0]) + " 公网IP: " + tea.StringValue(instance.EipAddress.IpAddress) + " CPU: " + tea.ToString(tea.Int32Value(instance.Cpu)) + " 内存: " + tea.ToString(tea.Int32Value(instance.Memory)) +
" MB 规格: " + tea.StringValue(instance.InstanceType) + " 地域: " + tea.StringValue(instance.RegionId) + " 网络类型: " + tea.StringValue(instance.InstanceNetworkType) + " 操作系统:" + tea.StringValue(instance.OSType) +
"(" + tea.StringValue(instance.OSName) + " 状态: " + tea.StringValue(instance.Status) + " 创建时间: " + tea.StringValue(instance.CreationTime) + " 付费类型: " + tea.StringValue(instance.InstanceChargeType)))
// 方式2:
fmt.Println("\t ECS实例列表")
fmt.Printf(" 实例名称: %s \n ", *instance.HostName)
fmt.Printf("实例ID: %s \n ", *instance.InstanceId)
fmt.Printf("内网IP: %v \n", *instance.VpcAttributes.PrivateIpAddress.IpAddress[0])
fmt.Printf(" 公网IP: %s \n", *instance.EipAddress.IpAddress)
fmt.Printf(" CPU: %d"+"核 \n", *instance.Cpu)
fmt.Printf(" 内存: %d"+"MB \n", *instance.Memory)
fmt.Printf(" 地域: %s \n", *instance.RegionId)
fmt.Printf(" 网络类型: %s \n", *instance.InstanceNetworkType)
fmt.Printf(" 操作系统: %s \n", *instance.OSName)
fmt.Printf(" 状态: %s \n", *instance.Status)
fmt.Printf(" 创建时间: %s \n", *instance.CreationTime)
fmt.Printf(" 付费类型: %s \n", *instance.InstanceChargeType)
}
fmt.Println(all) // 打印出所有资源
return nil
}()
if tryErr != nil {
var error1 = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error1 = _t
} else {
error1.Message = tea.String(tryErr.Error())
}
// 如有需要,请打印 error
util.AssertAsString(error1.Message)
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}