代码
产品sdk封装函数:
package apsara
import (
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/nas"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
)
type Client struct {
Vpc *vpc.Client
Ecs *ecs.Client
Slb *slb.Client
Rds *rds.Client
Nas *nas.Client
}
func NewClient() (*Client, error) {
zone := "zone"
ak := "ak"
sk := "sk"
vpcClient, err := vpc.NewClientWithAccessKey(zone, ak, sk)
if err != nil {
return nil, err
}
vpcClient.Domain = ""
vpcClient.SetHTTPSInsecure(true)
ecsClient, err := ecs.NewClientWithAccessKey(zone, ak, sk)
if err != nil {
return nil, err
}
ecsClient.Domain = ""
ecsClient.SetHTTPSInsecure(true)
slbClient, err := slb.NewClientWithAccessKey(zone, ak, sk)
if err != nil {
return nil, err
}
slbClient.Domain = ""
slbClient.SetHTTPSInsecure(true)
rdsClient, err := rds.NewClientWithAccessKey(zone, ak, sk)
if err != nil {
return nil, err
}
rdsClient.Domain = ""
rdsClient.SetHTTPSInsecure(true)
nasClient, err := nas.NewClientWithAccessKey(zone, ak, sk)
if err != nil {
return nil, err
}
nasClient.Domain = ""
nasClient.SetHTTPSInsecure(true)
c := &Client{
Vpc: vpcClient,
Ecs: ecsClient,
Slb: slbClient,
Rds: rdsClient,
Nas: nasClient,
}
return c, nil
}
main函数,以调DescribeLoadBalancersRequest接口为例:
func main() {
client, err := apsara.NewClient()
if err != nil {
fmt.Println("err:", err)
}
// slb
request := slb.CreateDescribeLoadBalancersRequest()
request.Scheme = "HTTPS"
request.PageNumber = requests.NewInteger(1)
request.PageSize = requests.NewInteger(50)
headers := map[string]string{
"x-acs-resourcegroupid": "5", // 指定资源集
}
request.Headers = headers
response, err := client.Slb.DescribeLoadBalancers(request)
if err != nil {
fmt.Println("err1:", err)
}
fmt.Println("res:---", response)
}
Reference
阿里云官方文档:https://help.aliyun.com/