/
invoker.go
133 lines (115 loc) · 3.41 KB
/
invoker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
*/
package openapi
import (
"fmt"
"os"
"strings"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/config"
"github.com/aliyun/aliyun-cli/i18n"
"github.com/aliyun/aliyun-cli/meta"
)
//
// implementations:
// - RpcInvoker,
// - RpcForceInvoker
// - RestfulInvoker
type Invoker interface {
getClient() *sdk.Client
getRequest() *requests.CommonRequest
Prepare(ctx *cli.Context) error
Call() (*responses.CommonResponse, error)
}
//
// implementations
// - Waiter
// - Pager
type InvokeHelper interface {
CallWith(invoker Invoker) (string, error)
}
//
// basic invoker to init common object and headers
type BasicInvoker struct {
profile *config.Profile
client *sdk.Client
request *requests.CommonRequest
product *meta.Product
}
func NewBasicInvoker(cp *config.Profile) *BasicInvoker {
return &BasicInvoker{profile: cp}
}
func (a *BasicInvoker) getClient() *sdk.Client {
return a.client
}
func (a *BasicInvoker) getRequest() *requests.CommonRequest {
return a.request
}
func (a *BasicInvoker) Init(ctx *cli.Context, product *meta.Product) error {
var err error
a.product = product
a.client, err = a.profile.GetClient(ctx)
if err != nil {
return fmt.Errorf("init client failed %s", err)
}
if vendorEnv, ok := os.LookupEnv("ALIBABA_CLOUD_VENDOR"); ok {
a.client.AppendUserAgent("vendor", vendorEnv)
}
a.client.AppendUserAgent("Aliyun-CLI", cli.GetVersion())
a.request = requests.NewCommonRequest()
a.request.Product = product.Code
a.request.RegionId = a.profile.RegionId
if v, ok := config.RegionFlag(ctx.Flags()).GetValue(); ok {
a.request.RegionId = v
}
a.request.Version = product.Version
if v, ok := VersionFlag(ctx.Flags()).GetValue(); ok {
a.request.Version = v
}
if v, ok := EndpointFlag(ctx.Flags()).GetValue(); ok {
a.request.Domain = v
}
for _, s := range HeaderFlag(ctx.Flags()).GetValues() {
if k, v, ok := cli.SplitStringWithPrefix(s, "="); ok {
a.request.Headers[k] = v
if k == "Accept" {
if strings.Contains(v, "xml") {
a.request.AcceptFormat = "XML"
} else if strings.Contains(v, "json") {
a.request.AcceptFormat = "JSON"
}
}
if k == "Content-Type" {
a.request.SetContentType(v)
}
} else {
return fmt.Errorf("invaild flag --header `%s` use `--header HeaderName=Value`", s)
}
}
hint := "you can find it on https://help.aliyun.com"
if product.Version != "" {
hint = fmt.Sprintf("see '%s' or `aliyun help %s` get more information.",
product.GetDocumentLink(i18n.GetLanguage()), product.GetLowerCode())
}
if a.request.Version == "" {
return cli.NewErrorWithTip(fmt.Errorf("missing version for product %s", product.Code),
"Use flag `--version <YYYY-MM-DD>` to assign version, "+hint)
}
if a.request.RegionId == "" {
return cli.NewErrorWithTip(fmt.Errorf("missing region for product %s", product.Code),
"Use flag --region <regionId> to assign region, "+hint)
}
if a.request.Domain == "" {
a.request.Domain, err = product.GetEndpoint(a.request.RegionId, a.client)
if err != nil {
return cli.NewErrorWithTip(
fmt.Errorf("unknown endpoint for %s/%s! failed %s", product.GetLowerCode(), a.request.RegionId, err),
"Use flag --endpoint xxx.aliyuncs.com to assign endpoint, "+hint)
}
}
return nil
}