Codebase list golang-github-denverdino-aliyungo / 5ed2bf44-94a5-4446-ae82-9d1e805ab744/upstream
Import upstream version 0.0~git20210817.bed6921 Debian Janitor 2 years ago
115 changed file(s) with 4833 addition(s) and 426 deletion(s). Raw diff Collapse all Expand all
11 jobs:
22 build:
33 docker:
4 - image: circleci/golang:1.8
4 - image: circleci/golang:1.10
55 working_directory: /go/src/github.com/denverdino/aliyungo
66 steps:
77 - checkout
0 name: Go
1 on: [push]
2 jobs:
3
4 build:
5 name: Build
6 runs-on: ubuntu-latest
7 steps:
8
9 - name: Set up Go 1.13
10 uses: actions/setup-go@v1
11 with:
12 go-version: 1.13
13 id: go
14
15 - name: Check out code into the Go module directory
16 uses: actions/checkout@v1
17
18 - name: Get dependencies
19 run: |
20 go get -v -t -d ./...
21 if [ -f Gopkg.toml ]; then
22 curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
23 dep ensure
24 fi
25 - name: Run golangci-lint with reviewdog
26 uses: reviewdog/action-golangci-lint@v1.1.3
27 with:
28 github_token: ${{ secrets.github_token }}
29
30
00 language: go
1
1 arch:
2 - AMD64
3 - ppc64le
24 go:
3 - 1.7.4
5 - 1.10.8
46
57 # let us have speedy Docker-based Travis workers
68 sudo: false
00 package acm
11
22 import (
3 "crypto/hmac"
4 "crypto/sha1"
5 "encoding/base64"
6 "errors"
7 "fmt"
8 "io/ioutil"
39 "net/http"
10 "net/url"
11 "strconv"
12 "strings"
413 "time"
5 "fmt"
6 "errors"
7 "io/ioutil"
8 "strings"
9 "encoding/base64"
10 "crypto/sha1"
11 "crypto/hmac"
12 "strconv"
13 "net/url"
1414 )
1515
1616 type Client struct {
6565 servers := strings.Split(body, "\n")
6666
6767 for k, v := range servers {
68 if strings.Index(v, ":") == -1 {
68 if !strings.Contains(v, ":") {
6969 c.servers[k] = v + ":8080"
7070 } else {
7171 c.servers[k] = v
100100 timeStamp = timeStamp[:13]
101101
102102 spec := "?"
103 if strings.Index(api, "?") != -1 {
103 if strings.Contains(api, "?") {
104104 spec = "&"
105105 }
106106
147147 return "", err
148148 }
149149
150 byt, err = GbkToUtf8(byt)
151 if err != nil {
152 return "", err
153 }
154
155150 body := string(byt)
156151
157152 if resp.StatusCode != 200 {
180175 }
181176
182177 func (c *Client) Publish(dataId, group, content string) (string, error) {
183 bt, err := Utf8ToGbk([]byte(content))
184 if err != nil {
185 return "", err
186 }
187178
188179 return c.callApi("diamond-server/basestone.do?method=syncUpdateAll", map[string]string{
189180 "tenant": c.NameSpace,
190181 "dataId": dataId,
191182 "group": group,
192 "content": string(bt),
183 "content": content,
193184 }, "POST")
194185 }
195186
00 package acm
11
22 import (
3 "testing"
3 "fmt"
44 "log"
55 "os"
6 "fmt"
6 "testing"
77 )
88
99 func getClient() *Client {
2525 client := getClient()
2626 defer client.Delete("test", "test")
2727
28 _, err := client.Publish("test", "test", "test")
28 _, err := client.Publish("test", "test", "test测试")
2929
3030 if err != nil {
3131 t.Fatalf("pulish error:%s", err)
4949 if err != nil {
5050 t.Error(err)
5151 }
52 if ret != "test测试" {
53 t.Error("wrong respond content")
54 }
5255 fmt.Println(ret)
5356 })
5457 }
5558
5659 func TestClient_Subscribe(t *testing.T) {
5760 RunWithTest(t, func(client *Client, t *testing.T) {
58 _, err := client.Subscribe("test", "test","")
61 _, err := client.Subscribe("test", "test", "")
5962 if err != nil {
6063 t.Error(err)
6164 }
0 package auth
1
2 import (
3 "crypto/rand"
4 "fmt"
5 "io"
6 "os"
7 "syscall"
8 "time"
9 )
10
11 const (
12 // Bits is the number of bits in a UUID
13 Bits = 128
14
15 // Size is the number of bytes in a UUID
16 Size = Bits / 8
17
18 format = "%08x%04x%04x%04x%012x"
19 )
20
21 var (
22 // Loggerf can be used to override the default logging destination. Such
23 // log messages in this library should be logged at warning or higher.
24 Loggerf = func(format string, args ...interface{}) {}
25 )
26
27 // UUID represents a UUID value. UUIDs can be compared and set to other values
28 // and accessed by byte.
29 type UUID [Size]byte
30
31 // GenerateUUID creates a new, version 4 uuid.
32 func GenerateUUID() (u UUID) {
33 const (
34 // ensures we backoff for less than 450ms total. Use the following to
35 // select new value, in units of 10ms:
36 // n*(n+1)/2 = d -> n^2 + n - 2d -> n = (sqrt(8d + 1) - 1)/2
37 maxretries = 9
38 backoff = time.Millisecond * 10
39 )
40
41 var (
42 totalBackoff time.Duration
43 count int
44 retries int
45 )
46
47 for {
48 // This should never block but the read may fail. Because of this,
49 // we just try to read the random number generator until we get
50 // something. This is a very rare condition but may happen.
51 b := time.Duration(retries) * backoff
52 time.Sleep(b)
53 totalBackoff += b
54
55 n, err := io.ReadFull(rand.Reader, u[count:])
56 if err != nil {
57 if retryOnError(err) && retries < maxretries {
58 count += n
59 retries++
60 Loggerf("error generating version 4 uuid, retrying: %v", err)
61 continue
62 }
63
64 // Any other errors represent a system problem. What did someone
65 // do to /dev/urandom?
66 panic(fmt.Errorf("error reading random number generator, retried for %v: %v", totalBackoff.String(), err))
67 }
68
69 break
70 }
71
72 u[6] = (u[6] & 0x0f) | 0x40 // set version byte
73 u[8] = (u[8] & 0x3f) | 0x80 // set high order byte 0b10{8,9,a,b}
74
75 return u
76 }
77
78 func (u UUID) String() string {
79 return fmt.Sprintf(format, u[:4], u[4:6], u[6:8], u[8:10], u[10:])
80 }
81
82 // retryOnError tries to detect whether or not retrying would be fruitful.
83 func retryOnError(err error) bool {
84 switch err := err.(type) {
85 case *os.PathError:
86 return retryOnError(err.Err) // unpack the target error
87 case syscall.Errno:
88 if err == syscall.EPERM {
89 // EPERM represents an entropy pool exhaustion, a condition under
90 // which we backoff and retry.
91 return true
92 }
93 }
94
95 return false
96 }
0 package auth
1
2 import (
3 "testing"
4 )
5
6 const iterations = 1000
7
8 func TestUUID4Generation(t *testing.T) {
9 for i := 0; i < iterations; i++ {
10 u := GenerateUUID()
11
12 if u[6]&0xf0 != 0x40 {
13 t.Fatalf("version byte not correctly set: %v, %08b %08b", u, u[6], u[6]&0xf0)
14 }
15
16 if u[8]&0xc0 != 0x80 {
17 t.Fatalf("top order 8th byte not correctly set: %v, %b", u, u[8])
18 }
19 }
20 }
0 package auth
1
2 import (
3 "crypto/md5"
4 "fmt"
5 "net/url"
6 "time"
7 )
8
9 // An URLSigner provides URL signing utilities to sign URLs for Aliyun CDN
10 // resources.
11 // authentication document: https://help.aliyun.com/document_detail/85117.html
12 type URLSigner struct {
13 authType string
14 privKey string
15 }
16
17 // NewURLSigner returns a new signer object.
18 func NewURLSigner(authType string, privKey string) *URLSigner {
19 return &URLSigner{
20 authType: authType,
21 privKey: privKey,
22 }
23 }
24
25 // Sign returns a signed aliyuncdn url based on authentication type
26 func (s URLSigner) Sign(uri string, expires time.Time) (string, error) {
27 r, err := url.Parse(uri)
28 if err != nil {
29 return "", fmt.Errorf("unable to parse url: %s", uri)
30 }
31
32 switch s.authType {
33 case "a":
34 return aTypeSign(r, s.privKey, expires), nil
35 case "b":
36 return bTypeSign(r, s.privKey, expires), nil
37 case "c":
38 return cTypeSign(r, s.privKey, expires), nil
39 default:
40 return "", fmt.Errorf("invalid authentication type")
41 }
42 }
43
44 // sign by A type authentication method.
45 // authentication document: https://help.aliyun.com/document_detail/85113.html
46 func aTypeSign(r *url.URL, privateKey string, expires time.Time) string {
47 //rand is a random uuid without "-"
48 rand := GenerateUUID().String()
49 // not use, "0" by default
50 uid := "0"
51 secret := fmt.Sprintf("%s-%d-%s-%s-%s", r.Path, expires.Unix(), rand, uid, privateKey)
52 hashValue := md5.Sum([]byte(secret))
53 authKey := fmt.Sprintf("%d-%s-%s-%x", expires.Unix(), rand, uid, hashValue)
54 if r.RawQuery == "" {
55 return fmt.Sprintf("%s?auth_key=%s", r.String(), authKey)
56 }
57 return fmt.Sprintf("%s&auth_key=%s", r.String(), authKey)
58
59 }
60
61 // sign by B type authentication method.
62 // authentication document: https://help.aliyun.com/document_detail/85114.html
63 func bTypeSign(r *url.URL, privateKey string, expires time.Time) string {
64 formatExp := expires.Format("200601021504")
65 secret := privateKey + formatExp + r.Path
66 hashValue := md5.Sum([]byte(secret))
67 signURL := fmt.Sprintf("%s://%s/%s/%x%s?%s", r.Scheme, r.Host, formatExp, hashValue, r.Path, r.RawQuery)
68 return signURL
69 }
70
71 // sign by C type authentication method.
72 // authentication document: https://help.aliyun.com/document_detail/85115.html
73 func cTypeSign(r *url.URL, privateKey string, expires time.Time) string {
74 hexExp := fmt.Sprintf("%x", expires.Unix())
75 secret := privateKey + r.Path + hexExp
76 hashValue := md5.Sum([]byte(secret))
77 signURL := fmt.Sprintf("%s://%s/%x/%s%s?%s", r.Scheme, r.Host, hashValue, hexExp, r.Path, r.RawQuery)
78 return signURL
79 }
0 package auth
1
2 import (
3 "crypto/md5"
4 "fmt"
5 "net/url"
6 "reflect"
7 "testing"
8 "time"
9 )
10
11 var (
12 testSignTime = time.Unix(1541064730, 0)
13 testPrivKey = "12345678"
14 )
15
16 func assertEqual(t *testing.T, name string, x, y interface{}) {
17 if !reflect.DeepEqual(x, y) {
18 t.Errorf("%s: Not equal! Expected='%v', Actual='%v'\n", name, x, y)
19 t.FailNow()
20 }
21 }
22
23 func TestAtypeAuth(t *testing.T) {
24 r, _ := url.Parse("https://example.com/a?foo=bar")
25 url := aTypeTest(r, testPrivKey, testSignTime)
26 assertEqual(t, "testTypeA", "https://example.com/a?foo=bar&auth_key=1541064730-0-0-f9dd5ed1e274ab4b1d5f5745344bf28b", url)
27 }
28
29 func TestBtypeAuth(t *testing.T) {
30 signer := NewURLSigner("b", testPrivKey)
31 url, _ := signer.Sign("https://example.com/a?foo=bar", testSignTime)
32 assertEqual(t, "testTypeB", "https://example.com/201811011732/3a19d83a89ccb00a73212420791b0123/a?foo=bar", url)
33 }
34
35 func TestCtypeAuth(t *testing.T) {
36 signer := NewURLSigner("c", testPrivKey)
37 url, _ := signer.Sign("https://example.com/a?foo=bar", testSignTime)
38 assertEqual(t, "testTypeC", "https://example.com/7d6b308ce87beb16d9dba32d741220f6/5bdac81a/a?foo=bar", url)
39 }
40
41 func aTypeTest(r *url.URL, privateKey string, expires time.Time) string {
42 //rand equals "0" in test case
43 rand := "0"
44 uid := "0"
45 secret := fmt.Sprintf("%s-%d-%s-%s-%s", r.Path, expires.Unix(), rand, uid, privateKey)
46 hashValue := md5.Sum([]byte(secret))
47 authKey := fmt.Sprintf("%d-%s-%s-%x", expires.Unix(), rand, uid, hashValue)
48 if r.RawQuery == "" {
49 return fmt.Sprintf("%s?auth_key=%s", r.String(), authKey)
50 }
51 return fmt.Sprintf("%s&auth_key=%s", r.String(), authKey)
52 }
88 const (
99 Web = "web"
1010 Download = "download"
11 video = "video"
11 Video = "video"
1212 LiveStream = "liveStream"
1313 Ipaddr = "ipaddr"
1414 Domain = "domain"
2626 AccessControlMaxAge = "Access-Control-Max-Age"
2727 )
2828
29 var CdnTypes = []string{Web, Download, video, LiveStream}
29 var CdnTypes = []string{Web, Download, Video, LiveStream}
3030 var SourceTypes = []string{Ipaddr, Domain, OSS}
3131 var Scopes = []string{Domestic, Overseas, Global}
3232 var HeaderKeys = []string{ContentType, CacheControl, ContentDisposition, ContentLanguage, Expires, AccessControlAllowMethods, AccessControlAllowOrigin, AccessControlMaxAge}
0 package cen
1
2 import (
3 "os"
4
5 "github.com/denverdino/aliyungo/common"
6 )
7
8 // Interval for checking status in WaitForXXX method
9 const DefaultWaitForInterval = 5
10
11 // Default timeout value for WaitForXXX method
12 const DefaultTimeout = 60
13
14 type Client struct {
15 common.Client
16 }
17
18 const (
19 // CENDefaultEndpoint is the default API endpoint of CEN services
20 CENDefaultEndpoint = "https://cbn.aliyuncs.com"
21 CENAPIVersion = "2017-09-12"
22 CENServiceCode = "cen"
23 )
24
25 // ---------------------------------------
26 // NewCENClient creates a new instance of CEN client
27 // ---------------------------------------
28 func NewCENClient(accessKeyId, accessKeySecret string, regionID common.Region) *Client {
29 return NewCENClientWithSecurityToken(accessKeyId, accessKeySecret, "", regionID)
30 }
31
32 func NewCENClientWithSecurityToken(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
33 endpoint := os.Getenv("CEN_ENDPOINT")
34 if endpoint == "" {
35 endpoint = CENDefaultEndpoint
36 }
37
38 return NewCENClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
39 }
40
41 //only for Hangzhou Regional Domain
42 func NewCENClientWithSecurityToken4RegionalDomain(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
43 endpoint := os.Getenv("CEN_ENDPOINT")
44 if endpoint == "" {
45 endpoint = CENDefaultEndpoint
46 }
47
48 return NewCENClientWithEndpointAndSecurityToken4RegionalDomain(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
49 }
50
51 func NewCENClientWithEndpointAndSecurityToken(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
52 client := &Client{}
53 client.WithEndpoint(endpoint).
54 WithVersion(CENAPIVersion).
55 WithAccessKeyId(accessKeyId).
56 WithAccessKeySecret(accessKeySecret).
57 WithSecurityToken(securityToken).
58 WithServiceCode(CENServiceCode).
59 WithRegionID(regionID).
60 InitClient()
61 return client
62 }
63
64 func NewCENClientWithEndpointAndSecurityToken4RegionalDomain(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
65 client := &Client{}
66 client.WithEndpoint(endpoint).
67 WithVersion(CENAPIVersion).
68 WithAccessKeyId(accessKeyId).
69 WithAccessKeySecret(accessKeySecret).
70 WithSecurityToken(securityToken).
71 WithServiceCode(CENServiceCode).
72 WithRegionID(regionID).
73 InitClient4RegionalDomain()
74 return client
75 }
0 package cen
1
2 import (
3 "log"
4
5 "github.com/denverdino/aliyungo/common"
6 )
7
8 type PublishRouteEntriesArgs struct {
9 CenId string
10 ChildInstanceId string
11 ChildInstanceRegionId string
12 ChildInstanceRouteTableId string
13 ChildInstanceType string
14 DestinationCidrBlock string
15 }
16
17 type DescribePublishedRouteEntriesArgs struct {
18 common.Pagination
19 CenId string
20 ChildInstanceId string
21 ChildInstanceRegionId string
22 ChildInstanceType string
23 ChildInstanceRouteTableId string
24 DestinationCidrBlock string
25 }
26
27 type DescribePublishedRouteEntriesResponse struct {
28 common.Response
29 common.PaginationResult
30 PublishedRouteEntries struct {
31 PublishedRouteEntry []PublishedRouteEntry
32 }
33 }
34
35 type ConflictStatus string
36 type NextHopType string
37
38 const (
39 ConflictStatusConflict = ConflictStatus("conflict")
40 ConflictStatusOverflow = ConflictStatus("overflow")
41 ConflictStatusProhibited = ConflictStatus("prohibited")
42 )
43
44 const (
45 NextHopTypeInstance = NextHopType("Instance")
46 NextHopTypeHaVip = NextHopType("HaVip")
47 NextHopTypeRouterInterface = NextHopType("RouterInterface")
48 )
49
50 type PublishStatus string
51
52 const (
53 PublishStatusPublished = PublishStatus("Published")
54 PublishStatusNotPublished = PublishStatus("NonPublished")
55 )
56
57 type RouteType string
58
59 const (
60 RouteTypeSystem = RouteType("System")
61 RouteTypeCustom = RouteType("Custom")
62 RouteTypeBGP = RouteType("BGP")
63 )
64
65 type PublishedRouteEntry struct {
66 ChildInstanceRouteTableId string
67 Conflicts struct {
68 Conflict []Conflict
69 }
70 DestinationCidrBlock string
71 NextHopId string
72
73 NextHopType string
74 OperationalMode bool
75 PublishStatus string
76 RouteType string
77 }
78
79 type Conflict struct {
80 DestinationCidrBlock string
81 InstanceId string
82 InstanceType string
83 RegionId string
84 Status string
85 }
86
87 // PublishRouteEntries publish route
88 //
89 // You can read doc at https://help.aliyun.com/document_detail/85470.html
90 func (client *Client) PublishRouteEntries(args *PublishRouteEntriesArgs) error {
91 response := &common.Response{}
92 err := client.Invoke("PublishRouteEntries", args, response)
93 if err != nil {
94 log.Printf("PublishRouteEntries: %s, %s\n", response.RequestId, err.Error())
95 }
96 return err
97 }
98
99 // DescribePublishedRouteEntries describe published route
100 //
101 // You can read doc at https://help.aliyun.com/document_detail/85472.html
102 func (client *Client) DescribePublishedRouteEntries(
103 args *DescribePublishedRouteEntriesArgs,
104 ) (response *DescribePublishedRouteEntriesResponse, err error) {
105
106 response = &DescribePublishedRouteEntriesResponse{}
107
108 err = client.Invoke("DescribePublishedRouteEntries", args, response)
109
110 if err != nil {
111 log.Printf("DescribePublishedRouteEntries: %v, %v\n", args, response)
112 }
113
114 return response, err
115 }
0 package cen
1
2 import (
3 "encoding/json"
4 "fmt"
5 "testing"
6 )
7
8 var (
9 ak = ""
10 sec = ""
11 )
12
13 func TestDescribePublishedRoute(t *testing.T) {
14 client := NewCENClient(ak, sec, "cn-shanghai")
15 res, err := client.DescribePublishedRouteEntries(
16 &DescribePublishedRouteEntriesArgs{
17 CenId: "cen-qhu4rn3cknrg5o4qhl",
18 ChildInstanceType: "VPC",
19 ChildInstanceRegionId: "cn-shanghai",
20 ChildInstanceRouteTableId: "vtb-uf699blmsutb4wkbzqcmt",
21 ChildInstanceId: "vpc-uf6ch2jfder4r0z51vtox",
22 },
23 )
24 if err != nil {
25 t.Errorf("describe: %s", err.Error())
26 t.FailNow()
27 }
28 fmt.Printf("Result: %+v", res)
29 b, err := json.MarshalIndent(res, "", " ")
30 fmt.Printf("%s", b)
31 }
32
33 func TestPublishedRoute(t *testing.T) {
34 client := NewCENClient(ak, sec, "cn-shanghai")
35 err := client.PublishRouteEntries(
36 &PublishRouteEntriesArgs{
37 CenId: "cen-qhu4rn3cknrg5o4qhl",
38 ChildInstanceType: "VPC",
39 ChildInstanceRegionId: "cn-shanghai",
40 ChildInstanceRouteTableId: "vtb-uf6nco4vj87ly556c589f",
41 ChildInstanceId: "vpc-uf6ch2jfder4r0z51vtox",
42 DestinationCidrBlock: "192.168.0.0/26",
43 },
44 )
45 if err != nil {
46 t.Errorf("publish: %s", err.Error())
47 t.FailNow()
48 }
49 }
3030
3131 func (client *Client) SetDebug(debug bool) {
3232 client.debug = debug
33 }
34
35 // SetTransport sets transport to the http client
36 func (client *Client) SetTransport(transport http.RoundTripper) {
37 if client.httpClient == nil {
38 client.httpClient = &http.Client{}
39 }
40 client.httpClient.Transport = transport
3341 }
3442
3543 const (
66 "fmt"
77 "io/ioutil"
88 "log"
9 "net"
910 "net/http"
1011 "net/url"
1112 "os"
4748 ak += "&"
4849 }
4950 client.AccessKeySecret = ak
50 client.debug = false
51 handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
52 if err != nil {
53 handshakeTimeout = 0
54 }
55 if handshakeTimeout == 0 {
56 client.httpClient = &http.Client{}
57 } else {
58 t := &http.Transport{
59 TLSHandshakeTimeout: time.Duration(handshakeTimeout) * time.Second}
60 client.httpClient = &http.Client{Transport: t}
61 }
51 client.InitClient()
6252 client.endpoint = endpoint
6353 client.version = version
6454 }
7060 client.regionID = regionID
7161 }
7262
63 // Initialize properties of a client instance including regionID
64 //only for hz regional Domain
65 func (client *Client) NewInit4RegionalDomain(endpoint, version, accessKeyId, accessKeySecret, serviceCode string, regionID Region) {
66 client.Init(endpoint, version, accessKeyId, accessKeySecret)
67 client.serviceCode = serviceCode
68 client.regionID = regionID
69
70 client.setEndpoint4RegionalDomain(client.regionID, client.serviceCode, client.AccessKeyId, client.AccessKeySecret, client.securityToken)
71 }
72
7373 // Intialize client object when all properties are ready
7474 func (client *Client) InitClient() *Client {
7575 client.debug = false
76 handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
77 if err != nil {
78 handshakeTimeout = 0
79 }
80 if handshakeTimeout == 0 {
81 client.httpClient = &http.Client{}
82 } else {
83 t := &http.Transport{
84 TLSHandshakeTimeout: time.Duration(handshakeTimeout) * time.Second}
85 client.httpClient = &http.Client{Transport: t}
86 }
76
77 // create DefaultTransport manully, because transport doesn't has clone method in go 1.10
78 t :=&http.Transport{
79 Proxy: http.ProxyFromEnvironment,
80 DialContext: (&net.Dialer{
81 Timeout: 30 * time.Second,
82 KeepAlive: 30 * time.Second,
83 DualStack: true,
84 }).DialContext,
85 MaxIdleConns: 100,
86 IdleConnTimeout: 90 * time.Second,
87 TLSHandshakeTimeout: 10 * time.Second,
88 ExpectContinueTimeout: 1 * time.Second,
89 }
90
91 handshakeTimeoutStr, ok := os.LookupEnv("TLSHandshakeTimeout")
92 if ok {
93 handshakeTimeout, err := strconv.Atoi(handshakeTimeoutStr)
94 if err != nil {
95 log.Printf("Get TLSHandshakeTimeout from env error: %v.", err)
96 } else {
97 t.TLSHandshakeTimeout = time.Duration(handshakeTimeout) * time.Second
98 }
99 }
100
101 responseHeaderTimeoutStr, ok := os.LookupEnv("ResponseHeaderTimeout")
102 if ok {
103 responseHeaderTimeout, err := strconv.Atoi(responseHeaderTimeoutStr)
104 if err != nil {
105 log.Printf("Get ResponseHeaderTimeout from env error: %v.", err)
106 } else {
107 t.ResponseHeaderTimeout = time.Duration(responseHeaderTimeout) * time.Second
108 }
109 }
110
111 expectContinueTimeoutStr, ok := os.LookupEnv("ExpectContinueTimeout")
112 if ok {
113 expectContinueTimeout, err := strconv.Atoi(expectContinueTimeoutStr)
114 if err != nil {
115 log.Printf("Get ExpectContinueTimeout from env error: %v.", err)
116 } else {
117 t.ExpectContinueTimeout = time.Duration(expectContinueTimeout) * time.Second
118 }
119 }
120
121 idleConnTimeoutStr, ok := os.LookupEnv("IdleConnTimeout")
122 if ok {
123 idleConnTimeout, err := strconv.Atoi(idleConnTimeoutStr)
124 if err != nil {
125 log.Printf("Get IdleConnTimeout from env error: %v.", err)
126 } else {
127 t.IdleConnTimeout = time.Duration(idleConnTimeout) * time.Second
128 }
129 }
130
131 client.httpClient = &http.Client{
132 Transport: t,
133 }
134
135 httpTimeoutStr, ok := os.LookupEnv("HttpTimeout")
136 if ok {
137 httpTimeout, err := strconv.Atoi(httpTimeoutStr)
138 if err != nil {
139 log.Printf("Get HttpTimeout from env error: %v.", err)
140 } else {
141 client.httpClient.Timeout = time.Duration(httpTimeout) * time.Second
142 }
143 }
144
145 return client
146 }
147
148 // Intialize client object when all properties are ready
149 //only for regional domain hz
150 func (client *Client) InitClient4RegionalDomain() *Client {
151 client.InitClient()
152 //set endpoint
153 client.setEndpoint4RegionalDomain(client.regionID, client.serviceCode, client.AccessKeyId, client.AccessKeySecret, client.securityToken)
87154 return client
88155 }
89156
104171 locationClient := NewLocationClient(accessKeyId, accessKeySecret, securityToken)
105172 locationClient.SetDebug(true)
106173 ep := locationClient.DescribeOpenAPIEndpoint(region, serviceCode)
107 if ep == "" {
108 ep = loadEndpointFromFile(region, serviceCode)
109 }
174
175 if ep != "" {
176 client.endpoint = ep
177 }
178 }
179
180 // Get openapi endpoint accessed by ecs instance.
181 // For some UnitRegions, the endpoint pattern is https://[product].[regionid].aliyuncs.com
182 // For some CentralRegions, the endpoint pattern is https://[product].vpc-proxy.aliyuncs.com
183 // The other region, the endpoint pattern is https://[product]-vpc.[regionid].aliyuncs.com
184 func (client *Client) setEndpoint4RegionalDomain(region Region, serviceCode, accessKeyId, accessKeySecret, securityToken string) {
185 if endpoint, ok := CentralDomainServices[serviceCode]; ok {
186 client.endpoint = fmt.Sprintf("https://%s", endpoint)
187 return
188 }
189 for _, service := range RegionalDomainServices {
190 if service == serviceCode {
191 if ep, ok := UnitRegions[region]; ok {
192 client.endpoint = fmt.Sprintf("https://%s.%s.aliyuncs.com", serviceCode, ep)
193 return
194 }
195
196 client.endpoint = fmt.Sprintf("https://%s%s.%s.aliyuncs.com", serviceCode, "-vpc", region)
197 return
198 }
199 }
200 locationClient := NewLocationClient(accessKeyId, accessKeySecret, securityToken)
201 locationClient.SetDebug(true)
202 ep := locationClient.DescribeOpenAPIEndpoint(region, serviceCode)
110203
111204 if ep != "" {
112205 client.endpoint = ep
207300 client.endpoint = endpoint
208301 }
209302
303 func (client *Client) GetEndpoint() string {
304 return client.endpoint
305 }
306
210307 // SetEndpoint sets custom version
211308 func (client *Client) SetVersion(version string) {
212309 client.version = version
256353 client.securityToken = securityToken
257354 }
258355
356 // SetTransport sets transport to the http client
357 func (client *Client) SetTransport(transport http.RoundTripper) {
358 if client.httpClient == nil {
359 client.httpClient = &http.Client{}
360 }
361 client.httpClient.Transport = transport
362 }
363
259364 func (client *Client) initEndpoint() error {
260365 // if set any value to "CUSTOMIZED_ENDPOINT" could skip location service.
261366 // example: export CUSTOMIZED_ENDPOINT=true
263368 return nil
264369 }
265370
371 if client.endpoint != "" {
372 return nil
373 }
374
266375 if client.serviceCode != "" && client.regionID != "" {
267376 endpoint := client.getEndpointByLocation()
268377 if endpoint == "" {
280389 }
281390
282391 //init endpoint
283 if err := client.initEndpoint(); err != nil {
284 return err
285 }
392 //if err := client.initEndpoint(); err != nil {
393 // return err
394 //}
286395
287396 request := Request{}
288397 request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
329438 if client.debug {
330439 var prettyJSON bytes.Buffer
331440 err = json.Indent(&prettyJSON, body, "", " ")
332 log.Println(string(prettyJSON.Bytes()))
441 if err != nil {
442 log.Printf("Failed in json.Indent: %v\n", err)
443 } else {
444 log.Printf("JSON body: %s\n", prettyJSON.String())
445 }
333446 }
334447
335448 if statusCode >= 400 && statusCode <= 599 {
336449 errorResponse := ErrorResponse{}
337450 err = json.Unmarshal(body, &errorResponse)
451 if err != nil {
452 log.Printf("Failed in json.Unmarshal: %v\n", err)
453 }
338454 ecsError := &Error{
339455 ErrorResponse: errorResponse,
340456 StatusCode: statusCode,
408524 if client.debug {
409525 var prettyJSON bytes.Buffer
410526 err = json.Indent(&prettyJSON, body, "", " ")
411 log.Println(string(prettyJSON.Bytes()))
527 if err != nil {
528 log.Printf("Failed in json.Indent: %v\n", err)
529 }
530 log.Println(prettyJSON.String())
412531 }
413532
414533 if statusCode >= 400 && statusCode <= 599 {
415534 errorResponse := ErrorResponse{}
416535 err = json.Unmarshal(body, &errorResponse)
536 if err != nil {
537 log.Printf("Failed in json.Unmarshal: %v\n", err)
538 }
417539 ecsError := &Error{
418540 ErrorResponse: errorResponse,
419541 StatusCode: statusCode,
439561 }
440562
441563 //init endpoint
442 if err := client.initEndpoint(); err != nil {
443 return err
444 }
564 //if err := client.initEndpoint(); err != nil {
565 // return err
566 //}
445567
446568 request := Request{}
447569 request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
497619 if client.debug {
498620 var prettyJSON bytes.Buffer
499621 err = json.Indent(&prettyJSON, body, "", " ")
500 log.Println(string(prettyJSON.Bytes()))
622 log.Println(prettyJSON.String())
501623 }
502624
503625 if statusCode >= 400 && statusCode <= 599 {
00 package common
11
2 import "os"
2 import (
3 "fmt"
4 "net/http"
5 "os"
6 "testing"
7 )
38
49 var (
510 TestAccessKeyId = os.Getenv("AccessKeyId")
1722 }
1823 return testDebugClient
1924 }
25
26 func TestClient_SetTransport(t *testing.T) {
27 client := NewTestClientForDebug()
28 transport := &myTransport{}
29 client.SetTransport(transport)
30 if client.httpClient.Transport.(*myTransport) != transport {
31 t.Fail()
32 }
33 }
34
35 type myTransport struct{}
36
37 func (m *myTransport) RoundTrip(req *http.Request) (*http.Response, error) {
38 return http.DefaultTransport.RoundTrip(req)
39 }
40
41 func Test_InitClient4RegionalDomain(t *testing.T) {
42
43 var tests = []struct {
44 service string
45 version string
46 endpoint string
47 }{
48 {"ecs", "2014-05-26", "https://ecs-cn-hangzhou.aliyuncs.com"},
49 {"pvtz", "2018-01-01", "https://pvtz.aliyuncs.com"},
50 {"slb", "2014-05-15", "https://slb.aliyuncs.com"},
51 {"vpc", "2016-04-28", "https://vpc.aliyuncs.com"},
52 }
53
54 for _, test := range tests {
55 for _, region := range ValidRegions {
56 if region == Qingdao || region == HangZhouFinance {
57 continue
58 }
59
60 client := &Client{}
61 client.SetDebug(true)
62 client.WithEndpoint(test.endpoint).
63 WithVersion(test.version).
64 WithAccessKeyId(TestAccessKeyId).
65 WithAccessKeySecret(TestAccessKeySecret).
66 WithSecurityToken(TestSecurityToken).
67 WithServiceCode(test.service).
68 WithRegionID(region).
69 InitClient4RegionalDomain()
70
71 if endpoint, ok := CentralDomainServices[test.service]; ok {
72 domain := fmt.Sprintf("https://%s", endpoint)
73 if client.endpoint != domain {
74 t.Fail()
75 }
76 continue
77 }
78
79 if ep, ok := UnitRegions[region]; ok {
80 domain := fmt.Sprintf("https://%s.%s.aliyuncs.com", test.service, ep)
81 if client.endpoint != domain {
82 t.Fail()
83 }
84 continue
85 }
86
87 domain := fmt.Sprintf("https://%s%s.%s.aliyuncs.com", test.service, "-vpc", region)
88 if client.endpoint != domain {
89 t.Fail()
90 }
91 }
92
93 }
94 }
33 "encoding/xml"
44 "fmt"
55 "io/ioutil"
6 "log"
67 "os"
78 "strings"
9 "sync"
810 "time"
9 "log"
1011 )
1112
1213 const (
1819 )
1920
2021 var (
21 endpoints = make(map[Region]map[string]string)
22 //endpoints = make(map[Region]map[string]string)
23
24 endpoints = sync.Map{}
2225
2326 SpecailEnpoints = map[Region]map[string]string{
2427 APNorthEast1: {
5053 "slb": "https://slb.eu-central-1.aliyuncs.com",
5154 "rds": "https://rds.eu-central-1.aliyuncs.com",
5255 "vpc": "https://vpc.eu-central-1.aliyuncs.com",
56 },
57 EUWest1: {
58 "ecs": "https://ecs.eu-west-1.aliyuncs.com",
59 "slb": "https://slb.eu-west-1.aliyuncs.com",
60 "rds": "https://rds.eu-west-1.aliyuncs.com",
61 "vpc": "https://vpc.eu-west-1.aliyuncs.com",
5362 },
5463 Zhangjiakou: {
5564 "ecs": "https://ecs.cn-zhangjiakou.aliyuncs.com",
122131 }
123132
124133 func getProductRegionEndpoint(region Region, serviceCode string) string {
125 if sp, ok := endpoints[region]; ok {
126 if endpoint, ok := sp[serviceCode]; ok {
127 return endpoint
128 }
129 }
130
134
135 if sp, ok := endpoints.Load(region); ok {
136 spt, ok := sp.(*sync.Map)
137 if ok {
138 if endp, ok := spt.Load(serviceCode); ok {
139 return endp.(string)
140 }
141 }
142 }
131143 return ""
132144 }
133145
134146 func setProductRegionEndpoint(region Region, serviceCode string, endpoint string) {
135 endpoints[region] = map[string]string{
136 serviceCode: endpoint,
137 }
147 m := sync.Map{}
148 m.Store(serviceCode, endpoint)
149 endpoints.Store(region, &m)
138150 }
139151
140152 func (client *LocationClient) DescribeOpenAPIEndpoint(region Region, serviceCode string) string {
160172 }
161173
162174 if err != nil || endpoint == nil || len(endpoint.Endpoints.Endpoint) <= 0 {
163 log.Printf("aliyungo: can not get endpoint from service, use default. endpoint=[%v], error=[%v]\n",endpoint, err)
175 log.Printf("aliyungo: can not get endpoint from service, use default. endpoint=[%v], error=[%v]\n", endpoint, err)
164176 return ""
165177 }
166178
13351335 <Product><ProductName>Slb</ProductName><DomainName>slb.eu-central-1.aliyuncs.com</DomainName></Product>
13361336 </Products>
13371337 </Endpoint>
1338 <Endpoint name="eu-west-1">
1339 <RegionIds><RegionId>eu-west-1</RegionId></RegionIds>
1340 <Products>
1341 <Product><ProductName>Rds</ProductName><DomainName>rds.eu-west-1.aliyuncs.com</DomainName></Product>
1342 <Product><ProductName>Ecs</ProductName><DomainName>ecs.eu-west-1.aliyuncs.com</DomainName></Product>
1343 <Product><ProductName>Vpc</ProductName><DomainName>vpc.eu-west-1.aliyuncs.com</DomainName></Product>
1344 <Product><ProductName>Kms</ProductName><DomainName>kms.eu-west-1.aliyuncs.com</DomainName></Product>
1345 <Product><ProductName>Cms</ProductName><DomainName>metrics.cn-hangzhou.aliyuncs.com</DomainName></Product>
1346 <Product><ProductName>Slb</ProductName><DomainName>slb.eu-west-1.aliyuncs.com</DomainName></Product>
1347 </Products>
1348 </Endpoint>
13381349 <Endpoint name="cn-zhangjiakou">
13391350 <RegionIds><RegionId>cn-zhangjiakou</RegionId></RegionIds>
13401351 <Products>
1313 Zhangjiakou = Region("cn-zhangjiakou")
1414 Huhehaote = Region("cn-huhehaote")
1515
16 Chengdu = Region("cn-chengdu")
17
1618 APSouthEast1 = Region("ap-southeast-1")
1719 APNorthEast1 = Region("ap-northeast-1")
1820 APSouthEast2 = Region("ap-southeast-2")
2729 MEEast1 = Region("me-east-1")
2830
2931 EUCentral1 = Region("eu-central-1")
32 EUWest1 = Region("eu-west-1")
3033
3134 ShenZhenFinance = Region("cn-shenzhen-finance-1")
3235 ShanghaiFinance = Region("cn-shanghai-finance-1")
36 HangZhouFinance = Region("cn-hangzhou-finance-1")
37
38 CNNorth2Gov1 = Region("cn-north-2-gov-1")
39 RUSWest1 = Region("rus-west-1")
3340 )
3441
3542 var ValidRegions = []Region{
3845 APNorthEast1, APSouthEast1, APSouthEast2, APSouthEast3, APSouthEast5,
3946 APSouth1,
4047 MEEast1,
41 EUCentral1,
42 ShenZhenFinance, ShanghaiFinance,
48 EUCentral1, EUWest1,
49 ShenZhenFinance, ShanghaiFinance, HangZhouFinance, CNNorth2Gov1,
4350 }
4451
4552 // IsValidRegion checks if r is an Ali supported region.
1212 PrePaid = InstanceChargeType("PrePaid")
1313 PostPaid = InstanceChargeType("PostPaid")
1414 )
15
16 var SpecialDeployedProducts = map[string]map[Region]interface{}{
17 "vpc": {
18 Hangzhou: Hangzhou,
19 Shenzhen: Shenzhen,
20 APSouthEast1: APSouthEast1,
21 USWest1: USWest1,
22 USEast1: USEast1,
23 Chengdu: Chengdu,
24 Zhangjiakou: Zhangjiakou,
25 Huhehaote: Huhehaote,
26 APSouthEast3: APSouthEast3,
27 EUCentral1: EUCentral1,
28 EUWest1: EUWest1,
29 APSouth1: APSouth1,
30 APNorthEast1: APNorthEast1,
31 APSouthEast5: APSouthEast5,
32 APSouthEast2: APSouthEast2,
33 MEEast1: MEEast1,
34 CNNorth2Gov1: CNNorth2Gov1,
35 },
36 }
37
38 var CentralDomainServices = map[string]string{
39 "pvtz": "pvtz.vpc-proxy.aliyuncs.com",
40 }
41
42 var RegionalDomainServices = []string{
43 "ecs",
44 "vpc",
45 "slb",
46 }
47
48 // Unit-Domain of central product
49 var UnitRegions = map[Region]interface{}{
50 Hangzhou: Hangzhou,
51 Shenzhen: Shenzhen,
52 APSouthEast1: APSouthEast1,
53 USWest1: USWest1,
54 USEast1: USEast1,
55 Chengdu: Chengdu,
56 Zhangjiakou: Zhangjiakou,
57 Huhehaote: Huhehaote,
58 APSouthEast3: APSouthEast3,
59 EUCentral1: EUCentral1,
60 EUWest1: EUWest1,
61 APSouth1: APSouth1,
62 APNorthEast1: APNorthEast1,
63 APSouthEast5: APSouthEast5,
64 APSouthEast2: APSouthEast2,
65 CNNorth2Gov1: CNNorth2Gov1,
66 //MEEast1: MEEast1,
67 //RUSWest1: RUSWest1,
68 //Beijing: Beijing,
69 //Shanghai: Shanghai,
70 //Hongkong: Hongkong,
71 //ShanghaiFinance: ShanghaiFinance,
72 //ShenZhenFinance: ShenZhenFinance,
73 HangZhouFinance: Hangzhou,
74 }
1575
1676 type DescribeEndpointArgs struct {
1777 Id Region
7575 client.userAgent = userAgent
7676 }
7777
78 // SetEndpoint sets customer endpoint
79 func (client *Client) SetEndpoint(endpoint string) {
80 client.endpoint = endpoint
81 }
82
83 // SetTransport sets transport to the http client
84 func (client *Client) SetTransport(transport http.RoundTripper) {
85 if client.httpClient == nil {
86 client.httpClient = &http.Client{}
87 }
88 client.httpClient.Transport = transport
89 }
90
7891 type Request struct {
7992 Method string
8093 URL string
172185
173186 if client.debug {
174187 var prettyJSON bytes.Buffer
175 err = json.Indent(&prettyJSON, body, "", " ")
176 log.Println(string(prettyJSON.Bytes()))
188 _ = json.Indent(&prettyJSON, body, "", " ")
189 log.Println(prettyJSON.String())
177190 }
178191
179192 if statusCode >= 400 && statusCode <= 599 {
180193 errorResponse := common.ErrorResponse{}
181 err = json.Unmarshal(body, &errorResponse)
194 _ = json.Unmarshal(body, &errorResponse)
182195 ecsError := &common.Error{
183196 ErrorResponse: errorResponse,
184197 StatusCode: statusCode,
186199 return ecsError
187200 }
188201
189 if response != nil {
202 if response != nil && len(body) > 0 {
190203 err = json.Unmarshal(body, response)
191204 //log.Printf("%++v", response)
192205 if err != nil {
1010 "fmt"
1111
1212 "encoding/json"
13
1314 "github.com/denverdino/aliyungo/common"
1415 "github.com/denverdino/aliyungo/ecs"
1516 )
2627 DeleteFailed = ClusterState("deleteFailed")
2728 Deleted = ClusterState("deleted")
2829 InActive = ClusterState("inactive")
30
31 ClusterTypeKubernetes = "Kubernetes"
32 ClusterTypeManagedKubernetes = "ManagedKubernetes"
33
34 ClusterTypeServerlessKubernetes = "Ask"
2935 )
36
37 var NodeStableClusterState = []ClusterState{Running, Updating, Failed, DeleteFailed, Deleted, InActive}
38
39 var NodeUnstableClusterState = []ClusterState{Initial, Scaling, Deleting}
3040
3141 type NodeStatus struct {
3242 Health int64 `json:"health"`
5969 NodeStatus string `json:"node_status"`
6070 DockerVersion string `json:"docker_version"`
6171 ClusterType string `json:"cluster_type"`
72 Profile string `json:"profile"`
6273 }
6374
6475 func (client *Client) DescribeClusters(nameFilter string) (clusters []ClusterType, err error) {
91102 ECSImageID string `json:"ecs_image_id,omitempty"`
92103 IOOptimized ecs.IoOptimized `json:"io_optimized"`
93104 ReleaseEipFlag bool `json:"release_eip_flag"`
94 }
95
96 type ClusterCreationResponse struct {
105 NeedSLB bool `json:"need_slb"`
106 }
107
108 type ClusterCommonResponse struct {
97109 Response
98 ClusterID string `json:"cluster_id"`
99 }
100
101 func (client *Client) CreateCluster(region common.Region, args *ClusterCreationArgs) (cluster ClusterCreationResponse, err error) {
110 ClusterID string `json:"cluster_id"`
111 Token string `json:"token,omitempty"`
112 TaskId string `json:"task_id,omitempty"`
113 InstanceId string `json:"instanceId"`
114 }
115
116 //Deprecated
117 func (client *Client) CreateCluster(region common.Region, args *ClusterCreationArgs) (cluster ClusterCommonResponse, err error) {
102118 err = client.Invoke(region, http.MethodPost, "/clusters", nil, args, &cluster)
103119 return
104120 }
126142 SNatEntry bool `json:"SNatEntry,omitempty"`
127143 }
128144
145 // Deprecated
129146 type KubernetesCreationArgs struct {
130 DisableRollback bool `json:"disable_rollback"`
131 Name string `json:"name"`
132 TimeoutMins int64 `json:"timeout_mins"`
133 ZoneId string `json:"zoneid,omitempty"`
134 VPCID string `json:"vpcid,omitempty"`
135 VSwitchId string `json:"vswitchid,omitempty"`
136 ContainerCIDR string `json:"container_cidr,omitempty"`
137 ServiceCIDR string `json:"service_cidr,omitempty"`
147 DisableRollback bool `json:"disable_rollback"`
148 Name string `json:"name"`
149 TimeoutMins int64 `json:"timeout_mins"`
150 ZoneId string `json:"zoneid,omitempty"`
151 VPCID string `json:"vpcid,omitempty"`
152 RegionId string `json:"region_id,omitempty"`
153 VSwitchId string `json:"vswitchid,omitempty"`
154 VSwitchIds []string `json:"vswitch_ids,omitempty"`
155 ImageId string `json:"image_id"`
156 ContainerCIDR string `json:"container_cidr,omitempty"`
157 ServiceCIDR string `json:"service_cidr,omitempty"`
158
138159 MasterInstanceType string `json:"master_instance_type,omitempty"`
139160 MasterSystemDiskSize int64 `json:"master_system_disk_size,omitempty"`
140161 MasterSystemDiskCategory ecs.DiskCategory `json:"master_system_disk_category,omitempty"`
162
163 MasterInstanceChargeType string `json:"master_instance_charge_type"`
164 MasterPeriodUnit string `json:"master_period_unit"`
165 MasterPeriod int `json:"master_period"`
166 MasterAutoRenew bool `json:"master_auto_renew"`
167 MasterAutoRenewPeriod int `json:"master_auto_renew_period"`
168
141169 WorkerInstanceType string `json:"worker_instance_type,omitempty"`
170 WorkerInstanceTypes []string `json:"worker_instance_types,omitempty"`
142171 WorkerSystemDiskSize int64 `json:"worker_system_disk_size,omitempty"`
143172 WorkerSystemDiskCategory ecs.DiskCategory `json:"worker_system_disk_category,omitempty"`
144 WorkerDataDisk bool `json:"worker_data_disk,omitempty"`
145 WorkerDataDiskCategory string `json:"worker_data_disk_category,omitempty"`
173 WorkerDataDisk bool `json:"worker_data_disk"`
174 WorkerDataDiskCategory ecs.DiskCategory `json:"worker_data_disk_category,omitempty"`
146175 WorkerDataDiskSize int64 `json:"worker_data_disk_size,omitempty"`
147 LoginPassword string `json:"login_password,omitempty"`
148 KeyPair string `json:"key_pair,omitempty"`
149 NumOfNodes int64 `json:"num_of_nodes,omitempty"`
150 SNatEntry bool `json:"snat_entry,omitempty"`
151 SSHFlags bool `json:"ssh_flags,omitempty"`
152 CloudMonitorFlags bool `json:"cloud_monitor_flags,omitempty"`
176
177 WorkerInstanceChargeType string `json:"worker_instance_charge_type"`
178 WorkerPeriodUnit string `json:"worker_period_unit"`
179 WorkerPeriod int `json:"worker_period"`
180 WorkerAutoRenew bool `json:"worker_auto_renew"`
181 WorkerAutoRenewPeriod int `json:"worker_auto_renew_period"`
182
183 LoginPassword string `json:"login_password,omitempty"`
184 KeyPair string `json:"key_pair,omitempty"`
185 UserCA string `json:"user_ca,omitempty"`
186 NumOfNodes int64 `json:"num_of_nodes,omitempty"`
187 SNatEntry bool `json:"snat_entry"`
188 SSHFlags bool `json:"ssh_flags"`
189 CloudMonitorFlags bool `json:"cloud_monitor_flags"`
190 NodeCIDRMask string `json:"node_cidr_mask,omitempty"`
191 LoggingType string `json:"logging_type,omitempty"`
192 SLSProjectName string `json:"sls_project_name,omitempty"`
193 PublicSLB bool `json:"public_slb"`
153194
154195 ClusterType string `json:"cluster_type"`
155196 Network string `json:"network,omitempty"`
158199 StackParams KubernetesStackArgs `json:"stack_params,omitempty"`
159200 }
160201
202 // Deprecated
161203 type KubernetesMultiAZCreationArgs struct {
162 DisableRollback bool `json:"disable_rollback"`
163 Name string `json:"name"`
164 TimeoutMins int64 `json:"timeout_mins"`
165 ClusterType string `json:"cluster_type"`
166 MultiAZ bool `json:"multi_az,omitempty"`
167 VPCID string `json:"vpcid,omitempty"`
168 ContainerCIDR string `json:"container_cidr"`
169 ServiceCIDR string `json:"service_cidr"`
170 VSwitchIdA string `json:"vswitch_id_a,omitempty"`
171 VSwitchIdB string `json:"vswitch_id_b,omitempty"`
172 VSwitchIdC string `json:"vswitch_id_c,omitempty"`
204 DisableRollback bool `json:"disable_rollback"`
205 Name string `json:"name"`
206 TimeoutMins int64 `json:"timeout_mins"`
207 ClusterType string `json:"cluster_type"`
208 MultiAZ bool `json:"multi_az"`
209 VPCID string `json:"vpcid,omitempty"`
210 ImageId string `json:"image_id"`
211 ContainerCIDR string `json:"container_cidr"`
212 ServiceCIDR string `json:"service_cidr"`
213 VSwitchIdA string `json:"vswitch_id_a,omitempty"`
214 VSwitchIdB string `json:"vswitch_id_b,omitempty"`
215 VSwitchIdC string `json:"vswitch_id_c,omitempty"`
216
173217 MasterInstanceTypeA string `json:"master_instance_type_a,omitempty"`
174218 MasterInstanceTypeB string `json:"master_instance_type_b,omitempty"`
175219 MasterInstanceTypeC string `json:"master_instance_type_c,omitempty"`
176220 MasterSystemDiskCategory ecs.DiskCategory `json:"master_system_disk_category"`
177221 MasterSystemDiskSize int64 `json:"master_system_disk_size"`
222
223 MasterInstanceChargeType string `json:"master_instance_charge_type"`
224 MasterPeriodUnit string `json:"master_period_unit"`
225 MasterPeriod int `json:"master_period"`
226 MasterAutoRenew bool `json:"master_auto_renew"`
227 MasterAutoRenewPeriod int `json:"master_auto_renew_period"`
228
178229 WorkerInstanceTypeA string `json:"worker_instance_type_a,omitempty"`
179230 WorkerInstanceTypeB string `json:"worker_instance_type_b,omitempty"`
180231 WorkerInstanceTypeC string `json:"worker_instance_type_c,omitempty"`
181232 WorkerSystemDiskCategory ecs.DiskCategory `json:"worker_system_disk_category"`
182233 WorkerSystemDiskSize int64 `json:"worker_system_disk_size"`
183234 WorkerDataDisk bool `json:"worker_data_disk"`
184 WorkerDataDiskCategory string `json:"worker_data_disk_category"`
235 WorkerDataDiskCategory ecs.DiskCategory `json:"worker_data_disk_category"`
185236 WorkerDataDiskSize int64 `json:"worker_data_disk_size"`
186 NumOfNodesA int64 `json:"num_of_nodes_a"`
187 NumOfNodesB int64 `json:"num_of_nodes_b"`
188 NumOfNodesC int64 `json:"num_of_nodes_c"`
189 LoginPassword string `json:"login_password,omitempty"`
190 KeyPair string `json:"key_pair,omitempty"`
191 SSHFlags bool `json:"ssh_flags"`
192 CloudMonitorFlags bool `json:"cloud_monitor_flags"`
237
238 WorkerInstanceChargeType string `json:"worker_instance_charge_type"`
239 WorkerPeriodUnit string `json:"worker_period_unit"`
240 WorkerPeriod int `json:"worker_period"`
241 WorkerAutoRenew bool `json:"worker_auto_renew"`
242 WorkerAutoRenewPeriod int `json:"worker_auto_renew_period"`
243
244 NumOfNodesA int64 `json:"num_of_nodes_a"`
245 NumOfNodesB int64 `json:"num_of_nodes_b"`
246 NumOfNodesC int64 `json:"num_of_nodes_c"`
247 LoginPassword string `json:"login_password,omitempty"`
248 KeyPair string `json:"key_pair,omitempty"`
249 UserCA string `json:"user_ca,omitempty"`
250 SSHFlags bool `json:"ssh_flags"`
251 CloudMonitorFlags bool `json:"cloud_monitor_flags"`
252 NodeCIDRMask string `json:"node_cidr_mask,omitempty"`
253 LoggingType string `json:"logging_type,omitempty"`
254 SLSProjectName string `json:"sls_project_name,omitempty"`
255 PublicSLB bool `json:"public_slb"`
193256
194257 KubernetesVersion string `json:"kubernetes_version,omitempty"`
195258 Network string `json:"network,omitempty"`
196259 }
197260
198 func (client *Client) CreateKubernetesMultiAZCluster(region common.Region, args *KubernetesMultiAZCreationArgs) (cluster ClusterCreationResponse, err error) {
261 // Deprecated
262 func (client *Client) CreateKubernetesMultiAZCluster(region common.Region, args *KubernetesMultiAZCreationArgs) (cluster ClusterCommonResponse, err error) {
199263 err = client.Invoke(region, http.MethodPost, "/clusters", nil, args, &cluster)
200264 return
201265 }
202266
203 func (client *Client) CreateKubernetesCluster(region common.Region, args *KubernetesCreationArgs) (cluster ClusterCreationResponse, err error) {
267 // Deprecated
268 func (client *Client) CreateKubernetesCluster(region common.Region, args *KubernetesCreationArgs) (cluster ClusterCommonResponse, err error) {
204269 err = client.Invoke(region, http.MethodPost, "/clusters", nil, args, &cluster)
205270 return
206271 }
213278 SubClass string `json:"SubClass"`
214279 }
215280
281 // deprecated
216282 type KubernetesClusterParameter struct {
217 ServiceCidr string `json:"ServiceCIDR"`
218 ContainerCidr string `json:"ContainerCIDR"`
219 DockerVersion string `json:"DockerVersion"`
220 EtcdVersion string `json:"EtcdVersion"`
221 KubernetesVersion string `json:"KubernetesVersion"`
222 VPCID string `json:"VpcId"`
223 KeyPair string `json:"KeyPair"`
224 MasterSystemDiskCategory string `json:"MasterSystemDiskCategory"`
225 MasterSystemDiskSize string `json:"MasterSystemDiskSize"`
226 WorkerSystemDiskCategory string `json:"WorkerSystemDiskCategory"`
227 WorkerSystemDiskSize string `json:"WorkerSystemDiskSize"`
228 ZoneId string `json:"ZoneId"`
283 ServiceCidr string `json:"ServiceCIDR"`
284 ContainerCidr string `json:"ContainerCIDR"`
285 DockerVersion string `json:"DockerVersion"`
286 EtcdVersion string `json:"EtcdVersion"`
287 KubernetesVersion string `json:"KubernetesVersion"`
288 VPCID string `json:"VpcId"`
289 ImageId string `json:"ImageId"`
290 KeyPair string `json:"KeyPair"`
291
292 MasterSystemDiskCategory ecs.DiskCategory `json:"MasterSystemDiskCategory"`
293 MasterSystemDiskSize string `json:"MasterSystemDiskSize"`
294 MasterImageId string `json:"MasterImageId"`
295
296 MasterInstanceChargeType string `json:"MasterInstanceChargeType"`
297 MasterPeriodUnit string `json:"MasterPeriodUnit"`
298 MasterPeriod string `json:"MasterPeriod"`
299 MasterAutoRenew *bool
300 RawMasterAutoRenew string `json:"MasterAutoRenew"`
301 MasterAutoRenewPeriod string `json:"MasterAutoRenewPeriod"`
302
303 WorkerSystemDiskCategory ecs.DiskCategory `json:"WorkerSystemDiskCategory"`
304 WorkerSystemDiskSize string `json:"WorkerSystemDiskSize"`
305 WorkerImageId string `json:"WorkerImageId"`
306 WorkerDataDisk *bool
307 RawWorkerDataDisk string `json:"WorkerDataDisk"`
308 WorkerDataDiskCategory ecs.DiskCategory `json:"WorkerDataDiskCategory"`
309 WorkerDataDiskSize string `json:"WorkerDataDiskSize"`
310
311 WorkerInstanceChargeType string `json:"WorkerInstanceChargeType"`
312 WorkerPeriodUnit string `json:"WorkerPeriodUnit"`
313 WorkerPeriod string `json:"WorkerPeriod"`
314 WorkerAutoRenew *bool
315 RawWorkerAutoRenew string `json:"WorkerAutoRenew"`
316 WorkerAutoRenewPeriod string `json:"WorkerAutoRenewPeriod"`
317
318 ZoneId string `json:"ZoneId"`
319 NodeCIDRMask string `json:"NodeCIDRMask"`
320 LoggingType string `json:"LoggingType"`
321 SLSProjectName string `json:"SLSProjectName"`
322 PublicSLB *bool
323 RawPublicSLB string `json:"PublicSLB"`
229324
230325 // Single AZ
231326 MasterInstanceType string `json:"MasterInstanceType"`
248343 VSwitchIdC string `json:"VSwitchIdC"`
249344 }
250345
346 // Deprecated
251347 type KubernetesCluster struct {
252348 ClusterType
253349
277373 Parameters KubernetesClusterParameter `json:"parameters"`
278374 }
279375
376 // Deprecated
280377 func (client *Client) DescribeKubernetesCluster(id string) (cluster KubernetesCluster, err error) {
281378 err = client.Invoke("", http.MethodGet, "/clusters/"+id, nil, nil, &cluster)
282379 if err != nil {
283380 return cluster, err
284381 }
382
285383 var metaData KubernetesClusterMetaData
286384 err = json.Unmarshal([]byte(cluster.RawMetaData), &metaData)
385 if err != nil {
386 return cluster, err
387 }
287388 cluster.MetaData = metaData
288389 cluster.RawMetaData = ""
289 return
290 }
291
390
391 return
392 }
393
394 // Deprecated
292395 type ClusterResizeArgs struct {
293396 Size int64 `json:"size"`
294397 InstanceType string `json:"instance_type"`
303406 Name string `json:"name"`
304407 }
305408
409 // Deprecated
306410 func (client *Client) ResizeCluster(clusterID string, args *ClusterResizeArgs) error {
307411 return client.Invoke("", http.MethodPut, "/clusters/"+clusterID, nil, args, nil)
308412 }
309413
310414 // deprecated
311 // use ResizeKubernetesCluster instead
415 // use ScaleKubernetesCluster instead
312416 func (client *Client) ResizeKubernetes(clusterID string, args *KubernetesCreationArgs) error {
313417 return client.Invoke("", http.MethodPut, "/clusters/"+clusterID, nil, args, nil)
314418 }
315419
420 // Deprecated
316421 type KubernetesClusterResizeArgs struct {
317422 DisableRollback bool `json:"disable_rollback"`
318423 TimeoutMins int64 `json:"timeout_mins"`
319424 LoginPassword string `json:"login_password,omitempty"`
320425
321426 // Single AZ
322 WorkerInstanceType string `json:"worker_instance_type"`
323 NumOfNodes int64 `json:"num_of_nodes"`
427 WorkerInstanceType string `json:"worker_instance_type"`
428 WorkerInstanceTypes []string `json:"worker_instance_types"`
429 NumOfNodes int64 `json:"num_of_nodes"`
324430
325431 // Multi AZ
326432 WorkerInstanceTypeA string `json:"worker_instance_type_a"`
331437 NumOfNodesC int64 `json:"num_of_nodes_c"`
332438 }
333439
440 // deprecated
441 // use ScaleKubernetesCluster instead
334442 func (client *Client) ResizeKubernetesCluster(clusterID string, args *KubernetesClusterResizeArgs) error {
335443 return client.Invoke("", http.MethodPut, "/clusters/"+clusterID, nil, args, nil)
336444 }
337445
446 // Deprecated
447 type KubernetesClusterScaleArgs struct {
448 LoginPassword string `json:"login_password,omitempty"`
449 KeyPair string `json:"key_pair,omitempty"`
450 WorkerInstanceTypes []string `json:"worker_instance_types"`
451 WorkerSystemDiskSize int64 `json:"worker_system_disk_size"`
452 WorkerSystemDiskCategory ecs.DiskCategory `json:"worker_system_disk_category"`
453 WorkerDataDisk bool `json:"worker_data_disk"`
454 Count int `json:"count"`
455
456 // Edge worker related args
457 IsEdgeWorker bool `json:"is_edge_worker"`
458 EnsRegionId string `json:"ens_region_id"`
459 EnsInternetChargeType string `json:"ens_internet_charge_type"`
460
461 //data disk
462 WorkerDataDiskCategory ecs.DiskCategory `json:"worker_data_disk_category"`
463 WorkerDataDiskSize int64 `json:"worker_data_disk_size"`
464 WorkerDataDiskEncrypted string `json:"worker_data_disk_encrypted"`
465 WorkerDataDiskKMSKeyId string `json:"worker_data_disk_kms_key_id"`
466 }
467
468 // Deprecated
469 func (client *Client) ScaleKubernetesCluster(clusterID string, args *KubernetesClusterScaleArgs) error {
470 return client.Invoke("", http.MethodPost, "/api/v2/clusters/"+clusterID, nil, args, nil)
471 }
472
473 // Deprecated
338474 func (client *Client) ModifyClusterName(clusterID, clusterName string) error {
339475 return client.Invoke("", http.MethodPost, "/clusters/"+clusterID+"/name/"+clusterName, nil, nil, nil)
340476 }
341477
478 // Deprecated
342479 func (client *Client) DeleteCluster(clusterID string) error {
343480 return client.Invoke("", http.MethodDelete, "/clusters/"+clusterID, nil, nil, nil)
344481 }
354491 return
355492 }
356493
494 type ClusterEndpoints struct {
495 ApiServerEndpoint string `json:"api_server_endpoint"`
496 DashboardEndpoint string `json:"dashboard_endpoint"`
497 MiranaEndpoint string `json:"mirana_endpoint"`
498 ReverseTunnelEndpoint string `json:"reverse_tunnel_endpoint"`
499 IntranetApiServerEndpoint string `json:"intranet_api_server_endpoint"`
500 }
501
502 func (client *Client) GetClusterEndpoints(id string) (clusterEndpoints ClusterEndpoints, err error) {
503 err = client.Invoke("", http.MethodGet, "/clusters/"+id+"/endpoints", nil, nil, &clusterEndpoints)
504 return
505 }
506
357507 type ClusterConfig struct {
358508 Config string `json:"config"`
359509 }
360510
511 // deprecated
512 // Please use new api DescribeClusterUserConfig
361513 func (client *Client) GetClusterConfig(id string) (config ClusterConfig, err error) {
362514 err = client.Invoke("", http.MethodGet, "/k8s/"+id+"/user_config", nil, nil, &config)
363515 return
374526 HostName string `json:"host_name"`
375527 ImageId string `json:"image_id"`
376528 InstanceId string `json:"instance_id"`
529 NodeName string `json:"node_name"`
377530 }
378531
379532 type GetKubernetesClusterNodesResponse struct {
382535 Nodes []KubernetesNodeType `json:"nodes"`
383536 }
384537
385 func (client *Client) GetKubernetesClusterNodes(id string, pagination common.Pagination) (nodes []KubernetesNodeType, paginationResult *PaginationResult, err error) {
538 // GetKubernetesClusterNodes is used to get cluster nodes or node pool nodes
539 func (client *Client) GetKubernetesClusterNodes(id string, pagination common.Pagination, nodepoolId string) (nodes []KubernetesNodeType, paginationResult *PaginationResult, err error) {
386540 response := &GetKubernetesClusterNodesResponse{}
387 err = client.Invoke("", http.MethodGet, "/clusters/"+id+"/nodes?pageNumber="+strconv.Itoa(pagination.PageNumber)+"&pageSize="+strconv.Itoa(pagination.PageSize), nil, nil, &response)
541 if nodepoolId != "" {
542 err = client.Invoke("", http.MethodGet, "/clusters/"+id+"/nodes?nodepool_id="+nodepoolId+"&pageNumber="+strconv.Itoa(pagination.PageNumber)+"&pageSize="+strconv.Itoa(pagination.PageSize), nil, nil, &response)
543 } else {
544 err = client.Invoke("", http.MethodGet, "/clusters/"+id+"/nodes?pageNumber="+strconv.Itoa(pagination.PageNumber)+"&pageSize="+strconv.Itoa(pagination.PageSize), nil, nil, &response)
545 }
388546 if err != nil {
389547 return nil, nil, err
390548 }
394552
395553 const ClusterDefaultTimeout = 300
396554 const DefaultWaitForInterval = 10
397 const DefaultPreSleepTime = 240
555 const DefaultPreCheckSleepTime = 20
556 const DefaultPreSleepTime = 220
398557
399558 // WaitForCluster waits for instance to given status
400559 // when instance.NotFound wait until timeout
402561 if timeout <= 0 {
403562 timeout = ClusterDefaultTimeout
404563 }
564
565 // Sleep 20 second to check cluster creating or failed
566 sleep := math.Min(float64(timeout), float64(DefaultPreCheckSleepTime))
567 time.Sleep(time.Duration(sleep) * time.Second)
568
405569 cluster, err := client.DescribeCluster(clusterId)
406570 if err != nil {
407571 return err
572 } else if cluster.State == Failed {
573 return fmt.Errorf("Waitting for cluster %s %s failed. Looking the specified reason in the web console.", clusterId, status)
408574 } else if cluster.State == status {
409575 //TODO
410576 return nil
411577 }
578
412579 // Create or Reset cluster usually cost at least 4 min, so there will sleep a long time before polling
413 sleep := math.Min(float64(timeout), float64(DefaultPreSleepTime))
580 sleep = math.Min(float64(timeout), float64(DefaultPreSleepTime))
414581 time.Sleep(time.Duration(sleep) * time.Second)
415582
416583 for {
4141 }
4242
4343 for _, cluster := range clusters {
44 if cluster.ClusterType != "Kubernetes" {
44 if cluster.ClusterType != "Kubernetes" && cluster.ClusterType != "ManagedKubernetes" {
4545 continue
4646 }
4747 t.Logf("Cluster: %++v", cluster)
5050 t.Errorf("Failed to DescribeCluster: %v", err)
5151 }
5252 t.Logf("Cluster Describe: %++v", c)
53 t.Logf("Cluster KeyPair %v", c.Parameters.KeyPair)
5453
5554 if c.MetaData.MultiAZ || c.MetaData.SubClass == "3az" {
5655 t.Logf("%v is a MultiAZ kubernetes cluster", c.ClusterID)
57 t.Logf("Cluster VSWA ID %v", c.Parameters.VSwitchIdA)
58 t.Logf("Cluster VSWB ID %v", c.Parameters.VSwitchIdB)
59 t.Logf("Cluster VSWC ID %v", c.Parameters.VSwitchIdC)
60 t.Logf("Cluster MasterInstanceTypeA %v", c.Parameters.MasterInstanceTypeA)
61 t.Logf("Cluster MasterInstanceTypeB %v", c.Parameters.MasterInstanceTypeB)
62 t.Logf("Cluster MasterInstanceTypeC %v", c.Parameters.MasterInstanceTypeC)
63 t.Logf("Cluster NumOfNodeA %v", c.Parameters.NumOfNodesA)
64 t.Logf("Cluster NumOfNodeB %v", c.Parameters.NumOfNodesB)
65 t.Logf("Cluster NumOfNodeC %v", c.Parameters.NumOfNodesC)
6656 } else {
67 t.Logf("%v is a single kubernetes cluster", c.ClusterID)
68 t.Logf("Cluster VSW ID %v", c.Parameters.VSwitchID)
69 t.Logf("Cluster MasterInstanceType %v", c.Parameters.MasterInstanceType)
70 t.Logf("Cluster NumOfNode %v", c.Parameters.NumOfNodes)
57 if cluster.ClusterType == "ManagedKubernetes" {
58 t.Logf("%v is a Managed kubernetes cluster", c.ClusterID)
59 } else {
60 t.Logf("%v is a SingleAZ kubernetes cluster", c.ClusterID)
61 }
7162 }
7263 }
7364 }
9586 t.Logf("Cluster certs: %++v", certs)
9687
9788 }
89 }
90
91 func _TestGetClusterEndpoints(t *testing.T) {
92 client := NewTestClientForDebug()
93 clusterId := "c213c31b97430433c87afe4852b6a08ef"
94 clusterEndpoints, err := client.GetClusterEndpoints(clusterId)
95 if err != nil {
96 t.Fatalf("Failed to GetClusterEndpoints: %v", err)
97 }
98 t.Logf("Succeed getting clusterEndpoints %v", clusterEndpoints)
9899 }
99100
100101 func _TestCreateClusters(t *testing.T) {
154155 WorkerDataDisk: true,
155156 WorkerDataDiskCategory: "cloud_efficiency",
156157 WorkerDataDiskSize: 100,
158 PublicSLB: true,
159 NodeCIDRMask: "25",
160 LoggingType: "SLS",
161 SLSProjectName: "k8s-test-my-terraform-singleaz",
157162 }
158163 cluster, err := client.CreateKubernetesCluster(common.Hangzhou, &args)
159164 if err != nil {
163168 t.Logf("Cluster: %++v", cluster)
164169 }
165170
166 func _TestCreateKubernetesMultiAZCluster(t *testing.T) {
167
168 client := NewTestClientForDebug()
169
170 args := KubernetesMultiAZCreationArgs{
171 Name: "multiaz-test1",
172 ClusterType: "Kubernetes",
173 DisableRollback: true,
174 MultiAZ: true,
175 VPCID: "vpc-id",
176 VSwitchIdA: "vsw-id1",
177 VSwitchIdB: "vsw-id2",
178 VSwitchIdC: "vsw-id3",
179 NumOfNodesA: 1,
180 NumOfNodesB: 1,
181 NumOfNodesC: 1,
182 MasterInstanceTypeA: "ecs.sn1ne.large",
183 MasterInstanceTypeB: "ecs.sn1ne.large",
184 MasterInstanceTypeC: "ecs.sn1ne.large",
171 func _TestCreateManagedKubernetesCluster(t *testing.T) {
172
173 client := NewTestClientForDebug()
174
175 args := KubernetesCreationArgs{
176 Name: "single-managed-az-k8s",
177 ClusterType: "ManagedKubernetes",
178 DisableRollback: true,
179 //VPCID: "vpc-id",
180 //VSwitchId: "vsw-id",
181 ZoneId: "cn-hangzhou-g",
182 SNatEntry: true,
183 NumOfNodes: 2,
184 MasterInstanceType: "ecs.sn1ne.large",
185185 MasterSystemDiskCategory: "cloud_efficiency",
186186 MasterSystemDiskSize: 40,
187 WorkerInstanceTypeA: "ecs.sn1ne.large",
188 WorkerInstanceTypeB: "ecs.sn1ne.large",
189 WorkerInstanceTypeC: "ecs.sn1ne.large",
187 WorkerInstanceType: "ecs.sn1ne.large",
190188 WorkerSystemDiskCategory: "cloud_efficiency",
191189 WorkerSystemDiskSize: 40,
192190 SSHFlags: true,
193 ContainerCIDR: "172.17.0.0/16",
194 ServiceCIDR: "172.20.0.0/20",
191 ContainerCIDR: "172.16.0.0/16",
192 ServiceCIDR: "172.19.0.0/20",
195193 LoginPassword: "test-password123",
196194 WorkerDataDisk: true,
197195 WorkerDataDiskCategory: "cloud_efficiency",
198196 WorkerDataDiskSize: 100,
197 PublicSLB: true,
198 NodeCIDRMask: "25",
199 LoggingType: "SLS",
200 SLSProjectName: "k8s-test-my-terraform-singleaz",
201 }
202 cluster, err := client.CreateKubernetesCluster(common.Hangzhou, &args)
203 if err != nil {
204 t.Fatalf("Failed to CreateKubernetesCluster: %v", err)
205 }
206
207 t.Logf("Cluster: %++v", cluster)
208 }
209
210 func _TestCreateKubernetesMultiAZCluster(t *testing.T) {
211
212 client := NewTestClientForDebug()
213
214 args := KubernetesMultiAZCreationArgs{
215 Name: "multiaz-test",
216 ClusterType: "Kubernetes",
217 DisableRollback: true,
218 MultiAZ: true,
219 VPCID: "vpc-id",
220 VSwitchIdA: "vsw-id1",
221 VSwitchIdB: "vsw-id2",
222 VSwitchIdC: "vsw-id3",
223 NumOfNodesA: 1,
224 NumOfNodesB: 1,
225 NumOfNodesC: 1,
226
227 MasterInstanceTypeA: "ecs.ic5.xlarge",
228 MasterInstanceTypeB: "ecs.ic5.xlarge",
229 MasterInstanceTypeC: "ecs.ic5.xlarge",
230 MasterSystemDiskCategory: "cloud_efficiency",
231 MasterSystemDiskSize: 40,
232
233 MasterInstanceChargeType: "PrePaid",
234 MasterPeriodUnit: "Week",
235 MasterPeriod: 1,
236 MasterAutoRenew: true,
237 MasterAutoRenewPeriod: 1,
238
239 WorkerInstanceTypeA: "ecs.ic5.xlarge",
240 WorkerInstanceTypeB: "ecs.ic5.xlarge",
241 WorkerInstanceTypeC: "ecs.ic5.xlarge",
242 WorkerSystemDiskCategory: "cloud_efficiency",
243 WorkerSystemDiskSize: 40,
244
245 WorkerInstanceChargeType: "PrePaid",
246 WorkerPeriodUnit: "Week",
247 WorkerPeriod: 1,
248 WorkerAutoRenew: true,
249 WorkerAutoRenewPeriod: 1,
250
251 SSHFlags: true,
252 ContainerCIDR: "172.20.0.0/16",
253 ServiceCIDR: "172.21.0.0/20",
254 LoginPassword: "test-password123",
255 ImageId: "centos_7_03_64_20G_alibase_20170818.vhd",
256 KubernetesVersion: "1.11.2",
257 WorkerDataDisk: true,
258 WorkerDataDiskCategory: "cloud_efficiency",
259 WorkerDataDiskSize: 100,
260 NodeCIDRMask: "25",
261 LoggingType: "SLS",
262 SLSProjectName: "k8s-test-my-terraform",
199263 }
200264 cluster, err := client.CreateKubernetesMultiAZCluster(common.Hangzhou, &args)
201265 if err != nil {
205269 t.Logf("Cluster: %++v", cluster)
206270 }
207271
208 func _TestResizeKubernetesCluster(t *testing.T) {
209 client := NewTestClientForDebug()
210
211 args := KubernetesClusterResizeArgs{
212 DisableRollback: true,
213 TimeoutMins: 60,
214 LoginPassword: "test-password123",
215 WorkerInstanceType: "ecs.sn1ne.large",
216 NumOfNodes: 2,
217 }
218
219 err := client.ResizeKubernetesCluster("c3419330390a94906bcacc55bfa9dd21f", &args)
220 if err != nil {
221 t.Fatalf("Failed to TestResizeKubernetesCluster: %v", err)
222 }
223 }
272 func TestScaleKubernetesCluster(t *testing.T) {
273 // Comment below to test
274 //t.SkipNow()
275
276 client := NewTestClientForDebug()
277
278 args := KubernetesClusterScaleArgs{
279 LoginPassword: "test-password123",
280 WorkerInstanceTypes: []string{"ecs.sn1ne.large"},
281 WorkerSystemDiskCategory: "cloud_ssd",
282 WorkerSystemDiskSize: int64(40),
283 Count: 2,
284 WorkerDataDisk: true,
285 WorkerDataDiskCategory: "cloud_ssd",
286 WorkerDataDiskSize: int64(200),
287 }
288
289 err := client.ScaleKubernetesCluster(TestClusterId, &args)
290 if err != nil {
291 t.Fatalf("Failed to TestScaleKubernetesCluster: %v", err)
292 }
293 }
294
295 func Test_GetKubernetesClusterNodes(t *testing.T) {
296 client := NewTestClientForDebug()
297
298 resp, _, err := client.GetKubernetesClusterNodes(TestClusterId, common.Pagination{PageNumber: 1, PageSize: 1000}, NodePoolId)
299 if err != nil {
300 t.Errorf("Error %++v", err)
301 } else {
302 t.Logf("response: %++v", resp)
303 }
304 }
11
22 import (
33 "os"
4 "strconv"
45
56 "github.com/denverdino/aliyungo/common"
67 )
89 //Modify with your Access Key Id and Access Key Secret
910
1011 var (
11 TestAccessKeyId = os.Getenv("AccessKeyId")
12 TestAccessKeySecret = os.Getenv("AccessKeySecret")
13 TestSecurityToken = os.Getenv("SecurityToken")
14 TestRegionID = common.Region(os.Getenv("RegionId"))
12 TestAccessKeyId = os.Getenv("AccessKeyId")
13 TestAccessKeySecret = os.Getenv("AccessKeySecret")
14 TestSecurityToken = os.Getenv("SecurityToken")
15 TestRegionID = common.Region(os.Getenv("RegionId"))
16 TestVpcId = os.Getenv("VpcId")
17 TestVSwitchId = os.Getenv("VSwitchId")
18 TestClusterId = os.Getenv("ClusterId")
19 TestPrivateIpAddress, _ = strconv.ParseBool(os.Getenv("PrivateIpAddress"))
20 TestToken = os.Getenv("Token")
1521 )
1622
1723 var testClient *Client
0 package cs
1
2 import (
3 "testing"
4 )
5
6 func Test_ModifyCluster(t *testing.T) {
7 client := NewTestClientForDebug()
8
9 args := &ModifyClusterArgs{
10 DeletionProtection: false,
11 MaintenanceWindow: MaintenanceWindow{
12 Enable: true,
13 MaintenanceTime: "2020-12-02T13:06:50.224Z",
14 Duration: "3h",
15 WeeklyPeriod: "Wednesday",
16 },
17 }
18
19 err := client.ModifyCluster(TestClusterId, args)
20 if err != nil {
21 t.Errorf("Error %++v", err)
22 } else {
23 t.Logf("OK")
24 }
25 }
26
27 func Test_UpgradeCluster(t *testing.T) {
28 client := NewTestClientForDebug()
29
30 args := &UpgradeClusterArgs{
31 Version: "1.14.8-aliyun.1",
32 }
33
34 err := client.UpgradeCluster(TestClusterId, args)
35 if err != nil {
36 t.Errorf("Error %++v", err)
37 } else {
38 t.Logf("OK")
39 }
40 }
41
42 func Test_CancelUpgradeCluster(t *testing.T) {
43 client := NewTestClientForDebug()
44
45 err := client.CancelUpgradeCluster(TestClusterId)
46 if err != nil {
47 t.Errorf("Error %++v", err)
48 } else {
49 t.Logf("OK")
50 }
51 }
52
53 func Test_QueryUpgradeClusterResult(t *testing.T) {
54 client := NewTestClientForDebug()
55
56 result, err := client.QueryUpgradeClusterResult(TestClusterId)
57 if err != nil {
58 t.Errorf("Error %++v", err)
59 } else {
60 t.Logf("OK, result: %++v", result)
61 }
62 }
63
64 func Test_CreateDelicatedKubernetesCluster(t *testing.T) {
65 t.SkipNow()
66 client := NewTestClientForDebug()
67
68 request := &DelicatedKubernetesClusterCreationRequest{}
69 response, err := client.CreateDelicatedKubernetesCluster(request)
70 if err != nil {
71 t.Fatalf("Error %++v", err)
72 } else {
73 t.Logf("Response %++v", response)
74 }
75 }
76
77 func Test_CreateManagedKubernetesCluster(t *testing.T) {
78 t.SkipNow()
79 client := NewTestClientForDebug()
80
81 request := &ManagedKubernetesClusterCreationRequest{}
82
83 response, err := client.CreateManagedKubernetesCluster(request)
84 if err != nil {
85 t.Fatalf("Error %++v", err)
86 } else {
87 t.Logf("Response %++v", response)
88 }
89 }
90
91 func Test_DescribeKubernetesClusterDetail(t *testing.T) {
92 client := NewTestClientForDebug()
93
94 cluster, err := client.DescribeKubernetesClusterDetail(TestClusterId)
95
96 if err != nil {
97 t.Fatalf("Error %++v", err)
98 } else {
99 t.Logf("Response = %++v", cluster)
100 t.Logf("MetaData = %++v", cluster.GetMetaData())
101 }
102 }
103
104 func Test_ScaleOutKubernetesCluster(t *testing.T) {
105 client := NewTestClientForDebug()
106
107 request := &ScaleOutKubernetesClusterRequest{
108 LoginPassword: "Hello1234",
109 WorkerVSwitchIds: []string{"vsw-xxxx"},
110 WorkerInstanceTypes: []string{"ecs.n4.xlarge"},
111 WorkerInstanceChargeType: "PostPaid",
112 WorkerPeriod: 1,
113 WorkerPeriodUnit: "1",
114 WorkerAutoRenew: true,
115 WorkerAutoRenewPeriod: 1,
116 WorkerDataDisk: true,
117 WorkerDataDisks: []DataDisk{
118 {
119 Category: "cloud_ssd",
120 Size: "200",
121 },
122 {
123 Category: "cloud_ssd",
124 Size: "300",
125 },
126 },
127 Tags: []Tag{
128 {Key: "k-aaa",
129 Value: "v-aaa",
130 },
131 },
132 Count: 2,
133 }
134
135 response, err := client.ScaleOutKubernetesCluster(TestClusterId, request)
136 if err != nil {
137 t.Fatalf("Error %++v", err)
138 } else {
139 t.Logf("Response %++v", response)
140 }
141 }
142
143 func Test_DeleteKubernetesClusterNodes(t *testing.T) {
144 client := NewTestClientForDebug()
145
146 request := &DeleteKubernetesClusterNodesRequest{
147 ReleaseNode: false,
148 Nodes: []string{"cn-beijing.192.168.0.128"},
149 }
150
151 response, err := client.DeleteKubernetesClusterNodes(TestClusterId, request)
152 if err != nil {
153 t.Fatalf("Error %++v", err)
154 } else {
155 t.Logf("Response %++v", response)
156 }
157 }
0 package cs
1
2 import (
3 "encoding/json"
4 "fmt"
5 "github.com/denverdino/aliyungo/ecs"
6 "net/http"
7 "time"
8
9 "github.com/denverdino/aliyungo/common"
10 )
11
12 type TaskState string
13
14 const (
15 // task status
16 Task_Status_Running = "running"
17 Task_Status_Success = "Success"
18 Task_Status_Failed = "Failed"
19
20 // upgrade state
21 UpgradeStep_NotStart = "not_start"
22 UpgradeStep_Prechecking = "prechecking"
23 UpgradeStep_Upgrading = "upgrading"
24 UpgradeStep_Pause = "pause"
25 UpgradeStep_Success = "success"
26 )
27
28 type WeeklyPeriod string
29 type MaintenanceTime string
30
31 // maintenance window
32 type MaintenanceWindow struct {
33 Enable bool `json:"enable"`
34 MaintenanceTime MaintenanceTime `json:"maintenance_time"`
35 Duration string `json:"duration"`
36 Recurrence string `json:"recurrence,omitempty"`
37 WeeklyPeriod WeeklyPeriod `json:"weekly_period"`
38 }
39
40 //modify cluster,include DeletionProtection and so on
41 type ModifyClusterArgs struct {
42 DeletionProtection bool `json:"deletion_protection"`
43 ResourceGroupId string `json:"resource_group_id"`
44 MaintenanceWindow MaintenanceWindow `json:"maintenance_window"`
45 }
46
47 type UpgradeClusterArgs struct {
48 Version string `json:"version"`
49 }
50
51 type UpgradeClusterResult struct {
52 Status TaskState `json:"status"`
53 PrecheckReportId string `json:"precheck_report_id"`
54 UpgradeStep string `json:"upgrade_step"`
55 ErrorMessage string `json:"error_message"`
56 *UpgradeTask `json:"upgrade_task,omitempty"`
57 }
58
59 type UpgradeTask struct {
60 FieldRetries int `json:"retries,omitempty"`
61 FieldCreatedAt time.Time `json:"created_at"`
62 FieldMessage string `json:"message,omitempty"`
63 FieldStatus string `json:"status"` // empty|running|success|failed
64 FieldFinishedAt time.Time `json:"finished_at,omitempty"`
65 UpgradeStatus UpgradeStatus `json:"upgrade_status"`
66 }
67
68 type UpgradeStatus struct {
69 State string `json:"state"`
70 Phase string `json:"phase"` // {Master1, Master2, Master3, Nodes}
71 Total int `json:"total"`
72 Succeeded int `json:"succeeded"`
73 Failed string `json:"failed"`
74 Events []Event `json:"events"`
75 IsCanceled bool `json:"is_canceled"`
76 }
77
78 type Event struct {
79 Timestamp time.Time
80 Type string
81 Reason string
82 Message string
83 Source string
84 }
85
86 //modify cluster
87 func (client *Client) ModifyCluster(clusterId string, args *ModifyClusterArgs) error {
88 return client.Invoke("", http.MethodPut, "/api/v2/clusters/"+clusterId, nil, args, nil)
89 }
90
91 //upgrade cluster
92 func (client *Client) UpgradeCluster(clusterId string, args *UpgradeClusterArgs) error {
93 return client.Invoke("", http.MethodPost, fmt.Sprintf("/api/v2/clusters/%s/upgrade", clusterId), nil, args, nil)
94 }
95
96 //cancel upgrade cluster
97 func (client *Client) CancelUpgradeCluster(clusterId string) error {
98 return client.Invoke("", http.MethodPost, fmt.Sprintf("/api/v2/clusters/%s/upgrade/cancel", clusterId), nil, nil, nil)
99 }
100
101 func (client *Client) QueryUpgradeClusterResult(clusterId string) (*UpgradeClusterResult, error) {
102 cluster := &UpgradeClusterResult{}
103 err := client.Invoke("", http.MethodGet, fmt.Sprintf("/api/v2/clusters/%s/upgrade/status", clusterId), nil, nil, cluster)
104 if err != nil {
105 return nil, err
106 }
107 return cluster, nil
108 }
109
110 //Cluster Info
111 type KubernetesClusterType string
112
113 var (
114 DelicatedKubernetes = KubernetesClusterType("Kubernetes")
115 ManagedKubernetes = KubernetesClusterType("ManagedKubernetes")
116 ServerlessKubernetes = KubernetesClusterType("Ask")
117 )
118
119 type ProxyMode string
120
121 var (
122 IPTables = "iptables"
123 IPVS = "ipvs"
124 )
125
126 type Effect string
127
128 var (
129 TaintNoExecute = "NoExecute"
130 TaintNoSchedule = "NoSchedule"
131 TaintPreferNoSchedule = "PreferNoSchedule"
132 )
133
134 type ClusterArgs struct {
135 DisableRollback bool `json:"disable_rollback"`
136 TimeoutMins int `json:"timeout_mins"`
137
138 Name string `json:"name"`
139 ClusterType KubernetesClusterType `json:"cluster_type"`
140 Profile string `json:"profile"`
141 KubernetesVersion string `json:"kubernetes_version"`
142 DeletionProtection bool `json:"deletion_protection"`
143
144 NodeCidrMask string `json:"node_cidr_mask"`
145 UserCa string `json:"user_ca"`
146
147 OsType string `json:"os_type"`
148 Platform string `json:"platform"`
149
150 UserData string `json:"user_data"`
151
152 NodePortRange string `json:"node_port_range"`
153 NodeNameMode string `json:"node_name_mode"`
154
155 //ImageId
156 ImageId string `json:"image_id"`
157
158 PodVswitchIds []string `json:"pod_vswitch_ids"` // eni多网卡模式下,需要传额外的vswitchid给addon
159
160 LoginPassword string `json:"login_password"` //和KeyPair 二选一
161 KeyPair string `json:"key_pair"` ////LoginPassword 二选一
162
163 RegionId common.Region `json:"region_id"`
164 VpcId string `json:"vpcid"`
165 ContainerCidr string `json:"container_cidr"`
166 ServiceCidr string `json:"service_cidr"`
167
168 CloudMonitorFlags bool `json:"cloud_monitor_flags"`
169
170 SecurityGroupId string `json:"security_group_id"`
171 IsEnterpriseSecurityGroup bool `json:"is_enterprise_security_group"`
172 EndpointPublicAccess bool `json:"endpoint_public_access"`
173 LoadBalancerSpec string `json:"load_balancer_spec"` //api server slb实例规格
174 ProxyMode ProxyMode `json:"proxy_mode"`
175 SnatEntry bool `json:"snat_entry"`
176 ResourceGroupId string `json:"resource_group_id"`
177
178 Addons []Addon `json:"addons"`
179 Tags []Tag `json:"tags"`
180
181 Taints []Taint `json:"taints"`
182
183 ApiAudiences string `json:"api_audiences,omitempty"`
184 ServiceAccountIssuer string `json:"service_account_issuer,omitempty"`
185 CustomSAN string `json:"custom_san,omitempty"`
186 ClusterSpec string `json:"cluster_spec"`
187 Timezone string `json:"timezone"`
188 ClusterDomain string `json:"cluster_domain"`
189 RdsInstances []string `json:"rds_instances"`
190 EncryptionProviderKey string `json:"encryption_provider_key"`
191 MaintenanceWindow MaintenanceWindow `json:"maintenance_window"`
192 }
193
194 //addon
195 type Addon struct {
196 Name string `json:"name"`
197 Version string `json:"version"`
198 Config string `json:"config"`
199 Disabled bool `json:"disabled"` // 是否禁止默认安装
200 }
201
202 //taint
203 type Taint struct {
204 Key string `json:"key"`
205 Value string `json:"value"`
206 Effect Effect `json:"effect"`
207 }
208
209 type Label struct {
210 Key string `json:"key"`
211 Value string `json:"value"`
212 }
213
214 type MasterArgs struct {
215 MasterCount int `json:"master_count"`
216 MasterVSwitchIds []string `json:"master_vswitch_ids"`
217 MasterInstanceTypes []string `json:"master_instance_types"`
218
219 MasterInstanceChargeType string `json:"master_instance_charge_type"`
220 MasterPeriod int `json:"master_period"`
221 MasterPeriodUnit string `json:"master_period_unit"`
222
223 MasterAutoRenew bool `json:"master_auto_renew"`
224 MasterAutoRenewPeriod int `json:"master_auto_renew_period"`
225
226 MasterSystemDiskCategory ecs.DiskCategory `json:"master_system_disk_category"`
227 MasterSystemDiskSize int64 `json:"master_system_disk_size"`
228 MasterSystemDiskPerformanceLevel string `json:"master_system_disk_performance_level"`
229
230 MasterDataDisks []DataDisk `json:"master_data_disks"` //支持多个数据盘
231
232 //support hpc/scc
233 MasterHpcClusterId string `json:"master_hpc_cluster_id"`
234 MasterDeploymentSetId string `json:"master_deploymentset_id"`
235
236 //master node deletion protection
237 MasterDeletionProtection *bool `json:"master_deletion_protection"`
238
239 // disk snapshot policy
240 MasterSnapshotPolicyId string `json:"master_system_disk_snapshot_policy_id"`
241 }
242
243 type WorkerArgs struct {
244 WorkerVSwitchIds []string `json:"worker_vswitch_ids"`
245 WorkerInstanceTypes []string `json:"worker_instance_types"`
246
247 NumOfNodes int64 `json:"num_of_nodes"`
248
249 WorkerInstanceChargeType string `json:"worker_instance_charge_type"`
250 WorkerPeriod int `json:"worker_period"`
251 WorkerPeriodUnit string `json:"worker_period_unit"`
252
253 WorkerAutoRenew bool `json:"worker_auto_renew"`
254 WorkerAutoRenewPeriod int `json:"worker_auto_renew_period"`
255
256 WorkerSystemDiskCategory ecs.DiskCategory `json:"worker_system_disk_category"`
257 WorkerSystemDiskSize int64 `json:"worker_system_disk_size"`
258 WorkerSystemDiskPerformanceLevel string `json:"worker_system_disk_performance_level"`
259
260 WorkerDataDisk bool `json:"worker_data_disk"`
261 WorkerDataDisks []DataDisk `json:"worker_data_disks"` //支持多个数据盘
262
263 WorkerHpcClusterId string `json:"worker_hpc_cluster_id"`
264 WorkerDeploymentSetId string `json:"worker_deploymentset_id"`
265
266 //worker node deletion protection
267 WorkerDeletionProtection *bool `json:"worker_deletion_protection"`
268
269 //Runtime only for worker nodes
270 Runtime Runtime `json:"runtime"`
271
272 // disk snapshot policy
273 WorkerSnapshotPolicyId string `json:"worker_system_disk_snapshot_policy_id"`
274 }
275
276 type ScaleOutKubernetesClusterRequest struct {
277 LoginPassword string `json:"login_password"` //和KeyPair 二选一
278 KeyPair string `json:"key_pair"` ////LoginPassword 二选一
279
280 WorkerVSwitchIds []string `json:"worker_vswitch_ids"`
281 WorkerInstanceTypes []string `json:"worker_instance_types"`
282
283 WorkerInstanceChargeType string `json:"worker_instance_charge_type"`
284 WorkerPeriod int `json:"worker_period"`
285 WorkerPeriodUnit string `json:"worker_period_unit"`
286
287 WorkerAutoRenew bool `json:"worker_auto_renew"`
288 WorkerAutoRenewPeriod int `json:"worker_auto_renew_period"`
289
290 WorkerSystemDiskCategory ecs.DiskCategory `json:"worker_system_disk_category"`
291 WorkerSystemDiskSize int64 `json:"worker_system_disk_size"`
292 WorkerSnapshotPolicyId string `json:"worker_system_disk_snapshot_policy_id"`
293 WorkerSystemDiskPerformanceLevel string `json:"worker_system_disk_performance_level"`
294
295 WorkerDataDisk bool `json:"worker_data_disk"`
296 WorkerDataDisks []DataDisk `json:"worker_data_disks"` //支持多个数据盘
297
298 Tags []Tag `json:"tags"`
299 Taints []Taint `json:"taints"`
300 ImageId string `json:"image_id"`
301
302 UserData string `json:"user_data"`
303
304 Count int64 `json:"count"`
305 CpuPolicy string `json:"cpu_policy"`
306 Runtime Runtime `json:"runtime"`
307 CloudMonitorFlags bool `json:"cloud_monitor_flags"`
308 RdsInstances []string ` json:"rds_instances"`
309 }
310
311 //datadiks
312 type DataDisk struct {
313 Category string `json:"category"`
314 KMSKeyId string `json:"kms_key_id"`
315 Encrypted string `json:"encrypted"` // true|false
316 Device string `json:"device"` // could be /dev/xvd[a-z]. If not specification, will use default value.
317 Size string `json:"size"`
318 DiskName string `json:"name"`
319 AutoSnapshotPolicyId string `json:"auto_snapshot_policy_id"`
320 PerformanceLevel string `json:"performance_Level"`
321 }
322
323 type NodePoolDataDisk struct {
324 Category string `json:"category"`
325 KMSKeyId string `json:"kms_key_id"`
326 Encrypted string `json:"encrypted"` // true|false
327 Device string `json:"device"` // could be /dev/xvd[a-z]. If not specification, will use default value.
328 Size int `json:"size"`
329 DiskName string `json:"name"`
330 AutoSnapshotPolicyId string `json:"auto_snapshot_policy_id"`
331 PerformanceLevel string `json:"performance_Level"`
332 }
333
334 //runtime
335 type Runtime struct {
336 Name string `json:"name"`
337 Version string `json:"version"`
338 RuntimeClass []string `json:"runtimeClass,omitempty"`
339 Exist bool `json:"exist"`
340 AvailableNetworkComponents []string `json:"availableNetworkComponents,omitempty"`
341 }
342
343 //DelicatedKubernetes
344 type DelicatedKubernetesClusterCreationRequest struct {
345 ClusterArgs
346 MasterArgs
347 WorkerArgs
348 }
349
350 //ManagedKubernetes
351 type ManagedKubernetesClusterCreationRequest struct {
352 ClusterArgs
353 WorkerArgs
354 }
355
356 //Validate
357
358 //Create DelicatedKubernetes Cluster
359 func (client *Client) CreateDelicatedKubernetesCluster(request *DelicatedKubernetesClusterCreationRequest) (*ClusterCommonResponse, error) {
360 response := &ClusterCommonResponse{}
361 path := "/clusters"
362 if request.ResourceGroupId != "" {
363 // 创建集群到指定资源组
364 path = fmt.Sprintf("/resource_groups/%s/clusters", request.ResourceGroupId)
365 }
366 err := client.Invoke(request.RegionId, http.MethodPost, path, nil, request, response)
367 if err != nil {
368 return nil, err
369 }
370
371 return response, nil
372 }
373
374 //Create ManagedKubernetes Cluster
375 func (client *Client) CreateManagedKubernetesCluster(request *ManagedKubernetesClusterCreationRequest) (*ClusterCommonResponse, error) {
376 response := &ClusterCommonResponse{}
377 path := "/clusters"
378 if request.ResourceGroupId != "" {
379 // 创建集群到指定资源组
380 path = fmt.Sprintf("/resource_groups/%s/clusters", request.ResourceGroupId)
381 }
382 err := client.Invoke(request.RegionId, http.MethodPost, path, nil, request, response)
383 if err != nil {
384 return nil, err
385 }
386
387 return response, nil
388 }
389
390 //ScaleKubernetesCluster
391 func (client *Client) ScaleOutKubernetesCluster(clusterId string, request *ScaleOutKubernetesClusterRequest) (*ClusterCommonResponse, error) {
392 response := &ClusterCommonResponse{}
393 err := client.Invoke("", http.MethodPost, fmt.Sprintf("/api/v2/clusters/%s", clusterId), nil, request, response)
394 if err != nil {
395 return nil, err
396 }
397
398 return response, nil
399 }
400
401 //DeleteClusterNodes
402 type DeleteKubernetesClusterNodesRequest struct {
403 ReleaseNode bool `json:"release_node"` //if set to true, the ecs instance will be released
404 Nodes []string `json:"nodes"` //the format is regionId.instanceId|Ip ,for example cn-hangzhou.192.168.1.2 or cn-hangzhou.i-abc
405 DrainNode bool `json:"drain_node"` //same as Nodes
406 }
407
408 //DeleteClusterNodes
409 func (client *Client) DeleteKubernetesClusterNodes(clusterId string, request *DeleteKubernetesClusterNodesRequest) (*common.Response, error) {
410 response := &common.Response{}
411 err := client.Invoke("", http.MethodPost, fmt.Sprintf("/api/v2/clusters/%s/nodes/remove", clusterId), nil, request, response)
412 if err != nil {
413 return nil, err
414 }
415
416 return response, nil
417 }
418
419 //Cluster definition
420 type KubernetesClusterDetail struct {
421 RegionId common.Region `json:"region_id"`
422
423 Name string `json:"name"`
424 ClusterId string `json:"cluster_id"`
425 Size int64 `json:"size"`
426 ClusterType KubernetesClusterType `json:"cluster_type"`
427 Profile string `json:"profile"`
428
429 VpcId string `json:"vpc_id"`
430 VSwitchIds string `json:"vswitch_id"`
431 SecurityGroupId string `json:"security_group_id"`
432 IngressLoadbalancerId string `json:"external_loadbalancer_id"`
433 ResourceGroupId string `json:"resource_group_id"`
434 NetworkMode string `json:"network_mode"`
435 ContainerCIDR string `json:"subnet_cidr"`
436
437 Tags []Tag `json:"tags"`
438 State string `json:"state"`
439
440 InitVersion string `json:"init_version"`
441 CurrentVersion string `json:"current_version"`
442 PrivateZone bool `json:"private_zone"`
443 DeletionProtection bool `json:"deletion_protection"`
444 MetaData string `json:"meta_data"`
445
446 Created time.Time `json:"created"`
447 Updated time.Time `json:"updated"`
448
449 WorkerRamRoleName string `json:"worker_ram_role_name"`
450 ClusterSpec string `json:"cluster_spec"`
451 OSType string `json:"os_type"`
452 MasterURL string `json:"master_url"`
453 MaintenanceWindow MaintenanceWindow `json:"maintenance_window"`
454 }
455
456 //GetMetaData
457 func (c *KubernetesClusterDetail) GetMetaData() map[string]interface{} {
458 m := make(map[string]interface{})
459 _ = json.Unmarshal([]byte(c.MetaData), &m)
460 return m
461 }
462
463 //查询集群详情
464 func (client *Client) DescribeKubernetesClusterDetail(clusterId string) (*KubernetesClusterDetail, error) {
465 cluster := &KubernetesClusterDetail{}
466 err := client.Invoke("", http.MethodGet, "/clusters/"+clusterId, nil, nil, cluster)
467 if err != nil {
468 return nil, err
469 }
470 return cluster, nil
471 }
472
473 //DeleteKubernetesCluster
474 func (client *Client) DeleteKubernetesCluster(clusterId string) error {
475 return client.Invoke("", http.MethodDelete, "/clusters/"+clusterId, nil, nil, nil)
476 }
0 package cs
1
2 import (
3 "fmt"
4 "github.com/denverdino/aliyungo/common"
5 "github.com/denverdino/aliyungo/ecs"
6 "net/http"
7 "time"
8 )
9
10 type SpotPrice struct {
11 InstanceType string `json:"instance_type"`
12 PriceLimit string `json:"price_limit"`
13 }
14
15 type NodePoolInfo struct {
16 NodePoolId string `json:"nodepool_id"`
17 RegionId common.Region `json:"region_id"`
18 Name string `json:"name"`
19 Created time.Time `json:"created"`
20 Updated time.Time `json:"updated"`
21 IsDefault bool `json:"is_default"`
22 NodePoolType string `json:"type"`
23 ResourceGroupId string `json:"resource_group_id"`
24 }
25
26 type ScalingGroup struct {
27 VpcId string `json:"vpc_id"`
28 VswitchIds []string `json:"vswitch_ids"`
29 InstanceTypes []string `json:"instance_types"`
30 LoginPassword string `json:"login_password"`
31 KeyPair string `json:"key_pair"`
32 SecurityGroupId string `json:"security_group_id"`
33 SystemDiskCategory ecs.DiskCategory `json:"system_disk_category"`
34 SystemDiskSize int64 `json:"system_disk_size"`
35 SystemDiskPerformanceLevel string `json:"system_disk_performance_level"`
36 DataDisks []NodePoolDataDisk `json:"data_disks"` //支持多个数据盘
37 Tags []Tag `json:"tags"`
38 ImageId string `json:"image_id"`
39 Platform string `json:"platform"`
40 // 支持包年包月
41 InstanceChargeType string `json:"instance_charge_type"`
42 Period int `json:"period"`
43 PeriodUnit string `json:"period_unit"`
44 AutoRenew bool `json:"auto_renew"`
45 AutoRenewPeriod int `json:"auto_renew_period"`
46 // spot实例
47 SpotStrategy string `json:"spot_strategy"`
48 SpotPriceLimit []SpotPrice `json:"spot_price_limit"`
49
50 RdsInstances []string `json:"rds_instances"`
51 ScalingPolicy string `json:"scaling_policy"`
52 ScalingGroupId string `json:"scaling_group_id"`
53
54 WorkerSnapshotPolicyId string `json:"worker_system_disk_snapshot_policy_id"`
55 // 公网ip
56 InternetChargeType string `json:"internet_charge_type"`
57 InternetMaxBandwidthOut int `json:"internet_max_bandwidth_out"`
58 }
59
60 type AutoScaling struct {
61 Enable bool `json:"enable"`
62 MaxInstances int64 `json:"max_instances"`
63 MinInstances int64 `json:"min_instances"`
64 Type string `json:"type"`
65 // eip
66 IsBindEip *bool `json:"is_bond_eip"`
67 // value: PayByBandwidth / PayByTraffic
68 EipInternetChargeType string `json:"eip_internet_charge_type"`
69 // default 5
70 EipBandWidth int64 `json:"eip_bandwidth"`
71 }
72
73 type KubernetesConfig struct {
74 NodeNameMode string `json:"node_name_mode"`
75 Taints []Taint `json:"taints"`
76 Labels []Label `json:"labels"`
77 CpuPolicy string `json:"cpu_policy"`
78 UserData string `json:"user_data"`
79
80 Runtime string `json:"runtime,omitempty"`
81 RuntimeVersion string `json:"runtime_version"`
82 CmsEnabled bool `json:"cms_enabled"`
83 OverwriteHostname bool `json:"overwrite_hostname"`
84 Unschedulable bool `json:"unschedulable"`
85 }
86
87 // 加密计算节点池
88 type TEEConfig struct {
89 TEEType string `json:"tee_type"`
90 TEEEnable bool `json:"tee_enable"`
91 }
92
93 // 托管节点池配置
94 type Management struct {
95 Enable bool `json:"enable"`
96 AutoRepair bool `json:"auto_repair"`
97 UpgradeConf UpgradeConf `json:"upgrade_config"`
98 }
99
100 type UpgradeConf struct {
101 AutoUpgrade bool `json:"auto_upgrade"`
102 Surge int64 `json:"surge"`
103 SurgePercentage int64 `json:"surge_percentage,omitempty"`
104 MaxUnavailable int64 `json:"max_unavailable"`
105 KeepSurgeOnFailed bool `json:"keep_surge_on_failed"`
106 }
107
108 type CreateNodePoolRequest struct {
109 RegionId common.Region `json:"region_id"`
110 Count int64 `json:"count"`
111 NodePoolInfo `json:"nodepool_info"`
112 ScalingGroup `json:"scaling_group"`
113 KubernetesConfig `json:"kubernetes_config"`
114 AutoScaling `json:"auto_scaling"`
115 TEEConfig `json:"tee_config"`
116 Management `json:"management"`
117 }
118
119 type BasicNodePool struct {
120 NodePoolInfo `json:"nodepool_info"`
121 NodePoolStatus `json:"status"`
122 }
123
124 type NodePoolStatus struct {
125 TotalNodes int `json:"total_nodes"`
126 OfflineNodes int `json:"offline_nodes"`
127 ServingNodes int `json:"serving_nodes"`
128 //DesiredNodes int `json:"desired_nodes"`
129 RemovingNodes int `json:"removing_nodes"`
130 FailedNodes int `json:"failed_nodes"`
131 InitialNodes int `json:"initial_nodes"`
132 HealthyNodes int `json:"healthy_nodes"`
133 State string `json:"state"`
134 }
135
136 type NodePoolDetail struct {
137 BasicNodePool
138 KubernetesConfig `json:"kubernetes_config"`
139 ScalingGroup `json:"scaling_group"`
140 AutoScaling `json:"auto_scaling"`
141 TEEConfig `json:"tee_config"`
142 Management `json:"management"`
143 }
144
145 type CreateNodePoolResponse struct {
146 Response
147 NodePoolID string `json:"nodepool_id"`
148 Message string `json:"Message"`
149 TaskID string `json:"task_id"`
150 }
151
152 type UpdateNodePoolRequest struct {
153 RegionId common.Region `json:"region_id"`
154 Count int64 `json:"count"`
155 NodePoolInfo `json:"nodepool_info"`
156 ScalingGroup `json:"scaling_group"`
157 KubernetesConfig `json:"kubernetes_config"`
158 AutoScaling `json:"auto_scaling"`
159 Management `json:"management"`
160 }
161
162 type NodePoolsDetail struct {
163 Response
164 NodePools []NodePoolDetail `json:"nodepools"`
165 }
166
167 func (client *Client) CreateNodePool(request *CreateNodePoolRequest, clusterId string) (*CreateNodePoolResponse, error) {
168 response := &CreateNodePoolResponse{}
169 err := client.Invoke(request.RegionId, http.MethodPost, fmt.Sprintf("/clusters/%s/nodepools", clusterId), nil, request, response)
170 if err != nil {
171 return nil, err
172 }
173
174 return response, nil
175 }
176
177 func (client *Client) DescribeNodePoolDetail(clusterId, nodePoolId string) (*NodePoolDetail, error) {
178 nodePool := &NodePoolDetail{}
179 err := client.Invoke("", http.MethodGet, fmt.Sprintf("/clusters/%s/nodepools/%s", clusterId, nodePoolId), nil, nil, nodePool)
180 if err != nil {
181 return nil, err
182 }
183 return nodePool, nil
184 }
185
186 func (client *Client) DescribeClusterNodePools(clusterId string) (*[]NodePoolDetail, error) {
187 nodePools := &NodePoolsDetail{}
188 err := client.Invoke("", http.MethodGet, fmt.Sprintf("/clusters/%s/nodepools", clusterId), nil, nil, nodePools)
189 if err != nil {
190 return nil, err
191 }
192 return &nodePools.NodePools, nil
193 }
194
195 func (client *Client) UpdateNodePool(clusterId string, nodePoolId string, request *UpdateNodePoolRequest) (*Response, error) {
196 response := &Response{}
197 err := client.Invoke(request.RegionId, http.MethodPut, fmt.Sprintf("/clusters/%s/nodepools/%s", clusterId, nodePoolId), nil, request, response)
198 if err != nil {
199 return nil, err
200 }
201
202 return response, nil
203 }
204
205 func (client *Client) DeleteNodePool(clusterId, nodePoolId string) error {
206 return client.Invoke("", http.MethodDelete, fmt.Sprintf("/clusters/%s/nodepools/%s", clusterId, nodePoolId), nil, nil, nil)
207 }
0 package cs
1
2 import (
3 "os"
4 "strings"
5 "testing"
6 )
7
8 var (
9 VpcId = os.Getenv("VpcId")
10 VswitchIds = os.Getenv("VswitchIds")
11 LoginPassword = os.Getenv("LoginPassword")
12 NodePoolId = os.Getenv("NodePoolId")
13 )
14
15 func Test_CreateNodePool(t *testing.T) {
16 client := NewTestClientForDebug()
17
18 args := &CreateNodePoolRequest{
19 Count: 1,
20 RegionId: TestRegionID,
21 NodePoolInfo: NodePoolInfo{
22 Name: "test-npl",
23 NodePoolType: "ess",
24 },
25 ScalingGroup: ScalingGroup{
26 VpcId: VpcId,
27 VswitchIds: strings.Split(VswitchIds, ","),
28 InstanceTypes: []string{"ecs.n6.large"},
29 LoginPassword: LoginPassword,
30 SystemDiskCategory: "cloud_efficiency",
31 SystemDiskSize: 120,
32 DataDisks: []NodePoolDataDisk{{Size: 100, Category: "cloud_ssd"}},
33 },
34 KubernetesConfig: KubernetesConfig{
35 NodeNameMode: "customized,aliyun.com,5,test",
36 },
37 }
38
39 resp, err := client.CreateNodePool(args, TestClusterId)
40 if err != nil {
41 t.Errorf("Error %++v", err)
42 } else {
43 t.Logf("response: %++v", resp)
44 }
45 }
46
47 func Test_DescribeNodePoolDetail(t *testing.T) {
48 client := NewTestClientForDebug()
49
50 resp, err := client.DescribeNodePoolDetail(TestClusterId, NodePoolId)
51 if err != nil {
52 t.Errorf("Error %++v", err)
53 } else {
54 t.Logf("response: %++v", resp)
55 }
56 }
57
58 func Test_UpdateNodePool(t *testing.T) {
59 client := NewTestClientForDebug()
60
61 args := &UpdateNodePoolRequest{
62 Count: 2,
63 RegionId: TestRegionID,
64 ScalingGroup: ScalingGroup{
65 InstanceTypes: []string{"ecs.n4.large"},
66 },
67 }
68
69 resp, err := client.UpdateNodePool(TestClusterId, NodePoolId, args)
70 if err != nil {
71 t.Errorf("Error %++v", err)
72 } else {
73 t.Logf("response: %++v", resp)
74 }
75 }
76
77 func Test_DeleteNodePool(t *testing.T) {
78 client := NewTestClientForDebug()
79
80 err := client.DeleteNodePool(TestClusterId, NodePoolId)
81 if err != nil {
82 t.Errorf("Error %++v", err)
83 } else {
84 t.Logf("success")
85 }
86 }
87
88 func Test_DescribeClusterNodePools(t *testing.T) {
89 client := NewTestClientForDebug()
90
91 resp, err := client.DescribeClusterNodePools(TestClusterId)
92 if err != nil {
93 t.Errorf("Error %++v", err)
94 } else {
95 t.Logf("response: %++v", resp)
96 }
97 }
6666 client.userAgent = userAgent
6767 }
6868
69 // SetTransport sets transport to the http client
70 func (client *ProjectClient) SetTransport(transport http.RoundTripper) {
71 if client.httpClient == nil {
72 client.httpClient = &http.Client{}
73 }
74 client.httpClient.Transport = transport
75 }
76
6977 func (client *ProjectClient) ClusterId() string {
7078 return client.clusterId
7179 }
133141
134142 if client.debug {
135143 var prettyJSON bytes.Buffer
136 err = json.Indent(&prettyJSON, body, "", " ")
137 log.Println(string(prettyJSON.Bytes()))
144 _ = json.Indent(&prettyJSON, body, "", " ")
145 log.Println(prettyJSON.String())
138146 }
139147
140148 if statusCode >= 400 && statusCode <= 599 {
141149 errorResponse := common.ErrorResponse{}
142 err = json.Unmarshal(body, &errorResponse)
150 _ = json.Unmarshal(body, &errorResponse)
143151 ecsError := &common.Error{
144152 ErrorResponse: errorResponse,
145153 StatusCode: statusCode,
0 package cs
1
2 import (
3 "fmt"
4 "github.com/denverdino/aliyungo/common"
5 "net/http"
6 "net/url"
7 "strconv"
8 "time"
9 )
10
11 type ServerlessCreationArgs struct {
12 ClusterType KubernetesClusterType `json:"cluster_type"`
13 Name string `json:"name"`
14 RegionId string `json:"region_id"`
15 VpcId string `json:"vpc_id"`
16 VSwitchId string `json:"vswitch_id"`
17 VswitchIds []string `json:"vswitch_ids"`
18 EndpointPublicAccess bool `json:"public_slb"`
19 PrivateZone bool `json:"private_zone"`
20 NatGateway bool `json:"nat_gateway"`
21 KubernetesVersion string `json:"kubernetes_version"`
22 DeletionProtection bool `json:"deletion_protection"`
23 SecurityGroupId string `json:"security_group_id"`
24 Tags []Tag `json:"tags"`
25 Addons []Addon `json:"addons"`
26 ResourceGroupId string `json:"resource_group_id"`
27 ClusterSpec string `json:"cluster_spec"`
28 LoadBalancerSpec string `json:"load_balancer_spec"` //api server slb实例规格
29 ServiceCIDR string `json:"service_cidr"`
30 TimeZone string `json:"timezone"`
31 ServiceDiscoveryTypes []string `json:"service_discovery_types"`
32 ZoneID string `json:"zone_id"`
33 LoggingType string `json:"logging_type"`
34 SLSProjectName string `json:"sls_project_name"`
35 }
36
37 type ServerlessClusterResponse struct {
38 ClusterId string `json:"cluster_id"`
39 Name string `json:"name"`
40 ClusterType KubernetesClusterType `json:"cluster_type"`
41 RegionId string `json:"region_id"`
42 State ClusterState `json:"state"`
43 VpcId string `json:"vpc_id"`
44 VSwitchId string `json:"vswitch_id"`
45 SecurityGroupId string `json:"security_group_id"`
46 Tags []Tag `json:"tags"`
47 Created time.Time `json:"created"`
48 Updated time.Time `json:"updated"`
49 InitVersion string `json:"init_version"`
50 CurrentVersion string `json:"current_version"`
51 PrivateZone bool `json:"private_zone"`
52 DeletionProtection bool `json:"deletion_protection"`
53 ResourceGroupId string `json:"resource_group_id"`
54 ClusterSpec string `json:"cluster_spec"`
55 }
56
57 type Tag struct {
58 Key string `json:"key"`
59 Value string `json:"value"`
60 }
61
62 func (this *ServerlessCreationArgs) Validate() error {
63 if this.Name == "" || this.RegionId == "" || this.VpcId == "" {
64 return common.GetCustomError("InvalidParameters", "The name,region_id,vpc_id not allowed empty")
65 }
66 return nil
67 }
68
69 //create Serverless cluster
70 func (client *Client) CreateServerlessKubernetesCluster(args *ServerlessCreationArgs) (*ClusterCommonResponse, error) {
71 if args == nil {
72 return nil, common.GetCustomError("InvalidArgs", "The args is nil ")
73 }
74
75 if err := args.Validate(); err != nil {
76 return nil, err
77 }
78
79 //reset clusterType,
80 args.ClusterType = ServerlessKubernetes
81 cluster := &ClusterCommonResponse{}
82 path := "/clusters"
83 if args.ResourceGroupId != "" {
84 // 创建集群到指定资源组
85 path = fmt.Sprintf("/resource_groups/%s/clusters", args.ResourceGroupId)
86 }
87 err := client.Invoke(common.Region(args.RegionId), http.MethodPost, path, nil, args, &cluster)
88 if err != nil {
89 return nil, err
90 }
91 return cluster, nil
92 }
93
94 //describe Serverless cluster
95 func (client *Client) DescribeServerlessKubernetesCluster(clusterId string) (*ServerlessClusterResponse, error) {
96 cluster := &ServerlessClusterResponse{}
97 err := client.Invoke("", http.MethodGet, "/clusters/"+clusterId, nil, nil, cluster)
98 if err != nil {
99 return nil, err
100 }
101 return cluster, nil
102 }
103
104 //describe Serverless clsuters
105 func (client *Client) DescribeServerlessKubernetesClusters() ([]*ServerlessClusterResponse, error) {
106 allClusters := make([]*ServerlessClusterResponse, 0)
107 askClusters := make([]*ServerlessClusterResponse, 0)
108
109 err := client.Invoke("", http.MethodGet, "/clusters", nil, nil, &allClusters)
110 if err != nil {
111 return askClusters, err
112 }
113
114 for _, cluster := range allClusters {
115 if cluster.ClusterType == ClusterTypeServerlessKubernetes {
116 askClusters = append(askClusters, cluster)
117 }
118 }
119
120 return askClusters, nil
121 }
122
123 //new api for get cluster kube user config
124 func (client *Client) DescribeClusterUserConfig(clusterId string, privateIpAddress bool) (*ClusterConfig, error) {
125 config := &ClusterConfig{}
126 query := url.Values{}
127 query.Add("PrivateIpAddress", strconv.FormatBool(privateIpAddress))
128
129 err := client.Invoke("", http.MethodGet, "/k8s/"+clusterId+"/user_config", query, nil, &config)
130 if err != nil {
131 return nil, err
132 }
133 return config, nil
134 }
0 package cs
1
2 import (
3 "fmt"
4 "testing"
5 "time"
6 )
7
8 func Test_CreateServerlessKubernetesCluster(t *testing.T) {
9 client := NewTestClientForDebug()
10
11 tags := make([]Tag, 0)
12 tags = append(tags, Tag{
13 Key: "key-a",
14 Value: "key-a",
15 })
16
17 tags = append(tags, Tag{
18 Key: "key-b",
19 Value: "key-b",
20 })
21
22 tags = append(tags, Tag{
23 Key: "key-c",
24 Value: "key-c",
25 })
26
27 args := &ServerlessCreationArgs{
28 ClusterType: ClusterTypeServerlessKubernetes,
29 Name: fmt.Sprintf("Serverless-cluster-%d", time.Now().Unix()),
30 RegionId: string(TestRegionID),
31 VpcId: TestVpcId,
32 VSwitchId: TestVSwitchId,
33 PrivateZone: true,
34 EndpointPublicAccess: true,
35 NatGateway: true,
36 DeletionProtection: true,
37 Tags: tags,
38 }
39
40 response, err := client.CreateServerlessKubernetesCluster(args)
41 if err != nil {
42 t.Errorf("Error %++v", err)
43 } else {
44 t.Logf("Response = %++v", response)
45 }
46 }
47
48 func Test_DescribeCluster(t *testing.T) {
49 client := NewTestClientForDebug()
50
51 cluster, err := client.DescribeServerlessKubernetesCluster(TestClusterId)
52 if err != nil {
53 t.Errorf("Error = %++v", err)
54 } else {
55 t.Logf("Cluster = %#v", cluster)
56 t.Logf("%v ", cluster.Created)
57 }
58 }
59
60 func Test_DescribeClusterUserConfig(t *testing.T) {
61 client := NewTestClientForDebug()
62
63 config, err := client.DescribeClusterUserConfig(TestClusterId, TestPrivateIpAddress)
64 if err != nil {
65 t.Errorf("Error = %++v", err)
66 } else {
67 t.Logf("Config = %#v", config)
68 }
69 }
70
71 func Test_DeleteServerlessCluster(t *testing.T) {
72 client := NewTestClientForDebug()
73
74 err := client.DeleteCluster(TestClusterId)
75 if err != nil {
76 t.Errorf("Error = %++v", err)
77 } else {
78 t.Logf("OK")
79 }
80 }
81
82 func Test_DescribeServerlessKubernetesClusters(t *testing.T) {
83 client := NewTestClientForDebug()
84 clusters, err := client.DescribeServerlessKubernetesClusters()
85 if err != nil {
86 t.Errorf("Error = %++v", err)
87 } else {
88 for _, cluster := range clusters {
89 t.Logf("Cluster = %#v", cluster)
90 }
91 }
92 }
0 package cs
1
2 import (
3 "fmt"
4 "github.com/denverdino/aliyungo/common"
5 "net/http"
6 )
7
8 type ClusterTokenReqeust struct {
9 //token 过期时间,如果不填写,则默认24小时过期
10 Expired int64 `json:"expired"`
11 IsPermanently bool `json:"is_permanently"`
12 }
13
14 type ClusterTokenResponse struct {
15 Created int64 ` json:"created"`
16 Updated int64 `json:"updated"`
17 Expired int64 ` json:"expired"`
18
19 ClusterID string ` json:"cluster_id"`
20
21 Token string ` json:"token"`
22 IsActive int ` json:"is_active"`
23 }
24
25 func (client *Client) CreateClusterToken(clusterId string, request *ClusterTokenReqeust) (*ClusterTokenResponse, error) {
26 response := &ClusterTokenResponse{}
27 err := client.Invoke("", http.MethodPost, "/clusters/"+clusterId+"/token", nil, request, response)
28 return response, err
29 }
30
31 func (client *Client) RevokeToken(token string) error {
32 return client.Invoke("", http.MethodDelete, "/token/"+token+"/revoke", nil, nil, nil)
33 }
34
35 func (client *Client) DescribeClusterTokens(clusterId string) ([]*ClusterTokenResponse, error) {
36 response := make([]*ClusterTokenResponse, 0)
37 err := client.Invoke("", http.MethodGet, "/clusters/"+clusterId+"/tokens", nil, nil, &response)
38 return response, err
39 }
40
41 func (client *Client) DescribeClusterToken(clusterId, token string) (*ClusterTokenResponse, error) {
42 if clusterId == "" || token == "" {
43 return nil, common.GetCustomError("InvalidParamter", "The clusterId or token is empty")
44 }
45 tokenInfo := &ClusterTokenResponse{}
46 err := client.Invoke("", http.MethodGet, fmt.Sprintf("/clusters/%s/tokens/%s", clusterId, token), nil, nil, tokenInfo)
47 return tokenInfo, err
48 }
0 package cs
1
2 import (
3 "testing"
4 "time"
5 )
6
7 func Test_CreateClusterToken(t *testing.T) {
8 client := NewTestClientForDebug()
9
10 req := &ClusterTokenReqeust{
11 Expired: time.Now().Unix() + 86400,
12 IsPermanently: false,
13 }
14
15 token, err := client.CreateClusterToken(TestClusterId, req)
16 if err != nil {
17 t.Fatalf("Error %++v", err)
18 } else {
19 t.Logf("Token = %++v", token)
20 }
21 }
22
23 func Test_RevokeClusterToken(t *testing.T) {
24 client := NewTestClientForDebug()
25 req := &ClusterTokenReqeust{
26 Expired: time.Now().Unix() + 86400,
27 IsPermanently: false,
28 }
29
30 token, err := client.CreateClusterToken(TestClusterId, req)
31 if err != nil {
32 t.Fatalf("Error = %++v", err)
33 } else {
34 err = client.RevokeToken(token.Token)
35 if err != nil {
36 t.Fatalf("Error = %++v", err)
37 } else {
38 tokens, err := client.DescribeClusterTokens(TestClusterId)
39 if err != nil {
40 t.Fatalf("Error %++v", err)
41 } else {
42 for _, token := range tokens {
43 t.Logf("Token = %++v", token)
44 }
45 }
46 }
47 }
48 }
49
50 func Test_DescribeClusterTokens(t *testing.T) {
51 client := NewTestClientForDebug()
52
53 tokens, err := client.DescribeClusterTokens(TestClusterId)
54 if err != nil {
55 t.Fatalf("Error %++v", err)
56 } else {
57 for _, token := range tokens {
58 t.Logf("Token = %++v", token)
59 }
60 }
61 }
62
63 func Test_DescribeClusterToken(t *testing.T) {
64 client := NewTestClientForDebug()
65
66 token, err := client.DescribeClusterToken(TestClusterId, TestToken)
67 if err != nil {
68 t.Fatalf("Error %++v", err)
69 } else {
70 t.Logf("Token = %++v", token)
71 }
72 }
6363 return NewECSClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
6464 }
6565
66 //only for Hangzhou Regional Domain
67 func NewECSClientWithSecurityToken4RegionalDomain(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
68 endpoint := os.Getenv("ECS_ENDPOINT")
69 if endpoint != "" {
70 return NewECSClientWithSecurityToken(accessKeyId, accessKeySecret, securityToken, regionID)
71 }
72
73 return NewECSClientWithEndpointAndSecurityToken4RegionalDomain(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
74 }
75
6676 func NewECSClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
6777 return NewECSClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, "", regionID)
6878 }
7787 WithServiceCode(ECSServiceCode).
7888 WithRegionID(regionID).
7989 InitClient()
90 return client
91 }
92
93 func NewECSClientWithEndpointAndSecurityToken4RegionalDomain(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
94 client := &Client{}
95 client.WithEndpoint(endpoint).
96 WithVersion(ECSAPIVersion).
97 WithAccessKeyId(accessKeyId).
98 WithAccessKeySecret(accessKeySecret).
99 WithSecurityToken(securityToken).
100 WithServiceCode(ECSServiceCode).
101 WithRegionID(regionID).
102 InitClient4RegionalDomain()
80103 return client
81104 }
82105
96119 return NewVPCClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
97120 }
98121
122 //Only for Hangzhou
123 func NewVPCClientWithSecurityToken4RegionalDomain(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
124 endpoint := os.Getenv("VPC_ENDPOINT")
125 if endpoint != "" {
126 return NewVPCClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
127 }
128
129 return NewVPCClientWithEndpointAndSecurityToken4RegionalDomain(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
130 }
131
99132 func NewVPCClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
100133 return NewVPCClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, "", regionID)
101134 }
113146 return client
114147 }
115148
149 //Only for Hangzhou
150 func NewVPCClientWithEndpointAndSecurityToken4RegionalDomain(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
151 client := &Client{}
152 client.WithEndpoint(endpoint).
153 WithVersion(VPCAPIVersion).
154 WithAccessKeyId(accessKeyId).
155 WithAccessKeySecret(accessKeySecret).
156 WithSecurityToken(securityToken).
157 WithServiceCode(VPCServiceCode).
158 WithRegionID(regionID).
159 InitClient4RegionalDomain()
160 return client
161 }
162
116163 // ---------------------------------------
117164 // NewVPCClientWithRegion creates a new instance of VPC client automatically get endpoint
118165 // ---------------------------------------
00 package ecs
11
22 import (
3 "github.com/denverdino/aliyungo/common"
4 "github.com/magiconair/properties/assert"
5 "os"
36 "testing"
47 )
58
7275 }
7376 }
7477 }
78
79 func Test_NewVPCClientWithSecurityToken4RegionalDomain(t *testing.T) {
80 client := NewVPCClientWithSecurityToken4RegionalDomain(TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, common.Beijing)
81 assert.Equal(t, client.GetEndpoint(), "https://vpc-vpc.cn-beijing.aliyuncs.com")
82
83 os.Setenv("VPC_ENDPOINT", "vpc.aliyuncs.com")
84 client = NewVPCClientWithSecurityToken4RegionalDomain(TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, common.Beijing)
85 assert.Equal(t, client.GetEndpoint(), "vpc.aliyuncs.com")
86 }
00 package ecs
11
22 import (
3 "github.com/denverdino/aliyungo/common"
34 "os"
4
5 "github.com/denverdino/aliyungo/common"
65 )
76
87 //Modify with your Access Key Id and Access Key Secret
1312 TestSecurityToken = os.Getenv("SecurityToken")
1413 TestRegionID = common.Region(os.Getenv("RegionId"))
1514 TestVpcId = os.Getenv("VpcId")
15 TestVswitchID = os.Getenv("TestVswitchID")
1616
1717 TestInstanceId = os.Getenv("InstanceId")
1818 TestSecurityGroupId = "MY_TEST_SECURITY_GROUP_ID"
5858
5959 func NetTestLocationClientForDebug() *Client {
6060 if testLocationClient == nil {
61 testLocationClient = NewECSClient(TestAccessKeyId, TestAccessKeySecret, TestRegionID)
61 testLocationClient = NewECSClientWithSecurityToken4RegionalDomain(TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, TestRegionID)
6262 testLocationClient.SetDebug(true)
6363 }
6464
2525 DiskCategoryEphemeralSSD = DiskCategory("ephemeral_ssd")
2626 DiskCategoryCloudEfficiency = DiskCategory("cloud_efficiency")
2727 DiskCategoryCloudSSD = DiskCategory("cloud_ssd")
28 DiskCategoryCloudESSD = DiskCategory("cloud_essd")
29 )
30
31 // Performance Level of disks
32 type DiskPerformanceLevel string
33
34 const (
35 DiskPL0 = DiskCategory("PL0")
36 DiskPL1 = DiskCategory("PL1")
37 DiskPL2 = DiskCategory("PL2")
38 DiskPL3 = DiskCategory("PL3")
2839 )
2940
3041 // Status of disks
95106 AttachedTime util.ISO6801Time
96107 DetachedTime util.ISO6801Time
97108 DiskChargeType DiskChargeType
109 StorageSetId string
110 PerformanceLevel DiskPerformanceLevel
98111 }
99112
100113 type DescribeDisksResponse struct {
138151 Encrypted bool
139152 DiskCategory DiskCategory
140153 Size int
154 Tag map[string]string
141155 SnapshotId string
142156 ClientToken string
157 KMSKeyID string
158 StorageSetId string
159
160 PerformanceLevel DiskPerformanceLevel
143161 }
144162
145163 type CreateDisksResponse struct {
346364 if err != nil {
347365 return err
348366 }
349 if disks == nil || len(disks) == 0 {
367 if len(disks) == 0 {
350368 return common.GetClientErrorFromString("Not found")
351369 }
352370 if disks[0].Status == status {
44 "time"
55
66 "github.com/denverdino/aliyungo/common"
7 "github.com/denverdino/aliyungo/util"
78 )
89
910 type CreateNetworkInterfaceArgs struct {
10 RegionId common.Region
11 VSwitchId string
12 PrimaryIpAddress string // optional
13 SecurityGroupId string
14 NetworkInterfaceName string // optional
15 Description string // optional
16 ClientToken string // optional
11 RegionId common.Region
12 VSwitchId string
13 PrimaryIpAddress string // optional
14 SecurityGroupId string
15 NetworkInterfaceName string // optional
16 Description string // optional
17 ClientToken string // optional
18 Tag map[string]string // optional
19 ResourceGroupId string // optional
20 SecurityGroupIds []string `query:"list"` // optional
21 PrivateIpAddress []string `query:"list"` // optional
22 SecondaryPrivateIpAddressCount int
1723 }
1824
1925 type CreateNetworkInterfaceResponse struct {
3238 type DescribeNetworkInterfacesArgs struct {
3339 RegionId common.Region
3440 VSwitchId string
41 VpcID string
3542 PrimaryIpAddress string
43 PrivateIpAddress []string `query:"list"`
3644 SecurityGroupId string
3745 NetworkInterfaceName string
3846 Type string
4553 NetworkInterfaceId string
4654 NetworkInterfaceName string
4755 PrimaryIpAddress string
48 MacAddress string
49 Status string
50 PrivateIpAddress string
56 PrivateIpSets struct {
57 PrivateIpSet []PrivateIpType
58 }
59 MacAddress string
60 Status string
61 Type string
62 VpcId string
63 VSwitchId string
64 ZoneId string
65 AssociatedPublicIp AssociatedPublicIp
66 SecurityGroupIds struct {
67 SecurityGroupId []string
68 }
69 Description string
70 InstanceId string
71 CreationTime util.ISO6801Time
72 PrivateIpAddress string
73 }
74
75 type PrivateIpType struct {
76 PrivateIpAddress string
77 Primary bool
78 AssociatedPublicIp AssociatedPublicIp
79 }
80
81 type AssociatedPublicIp struct {
82 PublicIpAddress string
83 AllocationId string
5184 }
5285
5386 type DescribeNetworkInterfacesResponse struct {
74107 type ModifyNetworkInterfaceAttributeArgs struct {
75108 RegionId common.Region
76109 NetworkInterfaceId string
77 SecurityGroupId []string
110 SecurityGroupId []string `query:"list"`
78111 NetworkInterfaceName string
79112 Description string
80113 }
81114 type ModifyNetworkInterfaceAttributeResponse common.Response
115
116 type UnassignPrivateIpAddressesArgs struct {
117 RegionId common.Region
118 NetworkInterfaceId string
119 PrivateIpAddress []string `query:"list"`
120 }
121
122 type UnassignPrivateIpAddressesResponse common.Response
123
124 type AssignPrivateIpAddressesArgs struct {
125 RegionId common.Region
126 NetworkInterfaceId string
127 PrivateIpAddress []string `query:"list"` // optional
128 SecondaryPrivateIpAddressCount int // optional
129 }
130
131 type AssignPrivateIpAddressesResponse struct {
132 common.Response
133
134 AssignedPrivateIpAddressesSet struct {
135 NetworkInterfaceId string
136 PrivateIpSet struct {
137 PrivateIpAddress []string
138 }
139 }
140 }
82141
83142 func (client *Client) CreateNetworkInterface(args *CreateNetworkInterfaceArgs) (resp *CreateNetworkInterfaceResponse, err error) {
84143 resp = &CreateNetworkInterfaceResponse{}
116175 return resp, err
117176 }
118177
178 func (client *Client) UnassignPrivateIpAddresses(args *UnassignPrivateIpAddressesArgs) (resp *UnassignPrivateIpAddressesResponse, err error) {
179 resp = &UnassignPrivateIpAddressesResponse{}
180 err = client.Invoke("UnassignPrivateIpAddresses", args, resp)
181 return resp, err
182 }
183
184 func (client *Client) AssignPrivateIpAddresses(args *AssignPrivateIpAddressesArgs) (resp *AssignPrivateIpAddressesResponse, err error) {
185 resp = &AssignPrivateIpAddressesResponse{}
186 err = client.Invoke("AssignPrivateIpAddresses", args, resp)
187 return resp, err
188 }
189
119190 // Default timeout value for WaitForInstance method
120191 const NetworkInterfacesDefaultTimeout = 120
121192
0 package ecs
1
2 import (
3 "github.com/denverdino/aliyungo/common"
4 "testing"
5 "time"
6 )
7
8 func TestAssignPrivateIPAddresses(t *testing.T) {
9 req := AssignPrivateIpAddressesArgs{
10 RegionId: common.Beijing,
11 NetworkInterfaceId: "eni-testeni",
12 PrivateIpAddress: []string{"192.168.1.200", "192.168.1.201"},
13 }
14 client := NewTestClient()
15 assignPrivateIpAddressesResponse, err := client.AssignPrivateIpAddresses(&req)
16 if err != nil {
17 t.Errorf("Failed to AssignPrivateIpAddresses: %v", err)
18 }
19
20 if assignPrivateIpAddressesResponse.AssignedPrivateIpAddressesSet.NetworkInterfaceId != "eni-testeni" {
21 t.Errorf("assert network id mismatch.%s\n", assignPrivateIpAddressesResponse.AssignedPrivateIpAddressesSet.NetworkInterfaceId)
22 }
23
24 time.Sleep(5 * time.Second)
25
26 req = AssignPrivateIpAddressesArgs{
27 RegionId: common.Beijing,
28 NetworkInterfaceId: "eni-testeni",
29 SecondaryPrivateIpAddressCount: 1,
30 }
31
32 _, err = client.AssignPrivateIpAddresses(&req)
33 if err != nil {
34 t.Errorf("Failed to AssignPrivateIpAddresses: %v", err)
35 }
36 }
37
38 func TestDescribeENI(t *testing.T) {
39 req := DescribeNetworkInterfacesArgs{
40 RegionId: common.Beijing,
41 NetworkInterfaceId: []string{"eni-testeni"},
42 }
43 client := NewTestClient()
44 resp, err := client.DescribeNetworkInterfaces(&req)
45 if err != nil {
46 t.Errorf("Failed to DescribeNetworkInterfaces: %v", err)
47 }
48 if len(resp.NetworkInterfaceSets.NetworkInterfaceSet[0].PrivateIpSets.PrivateIpSet) != 4 {
49 t.Errorf("assert network private ip count be 4, %+v", resp.NetworkInterfaceSets.NetworkInterfaceSet[0].PrivateIpSets.PrivateIpSet)
50 }
51 t.Logf("%+v", resp.NetworkInterfaceSets.NetworkInterfaceSet[0])
52 }
53
54 func TestFindENIByPrivateIP(t *testing.T) {
55 req := DescribeNetworkInterfacesArgs{
56 RegionId: common.Shanghai,
57 VpcID: "vpc-xxx",
58 PrivateIpAddress: []string{"192.168.108.191"},
59 }
60 client := NewTestClient()
61 resp, err := client.DescribeNetworkInterfaces(&req)
62 if err != nil {
63 t.Errorf("Failed to DescribeNetworkInterfaces: %v", err)
64 }
65 t.Logf("%+v", resp.NetworkInterfaceSets.NetworkInterfaceSet)
66 }
67
68 func TestUnAssignPrivateIPAddresses(t *testing.T) {
69 req := UnassignPrivateIpAddressesArgs{
70 RegionId: common.Beijing,
71 NetworkInterfaceId: "eni-testeni",
72 PrivateIpAddress: []string{"192.168.1.200", "192.168.1.201"},
73 }
74 client := NewTestClient()
75 _, err := client.UnassignPrivateIpAddresses(&req)
76 if err != nil {
77 t.Errorf("Failed to UnAssignPrivateIpAddresses: %v", err)
78 }
79 }
80
81 func TestModifyNetworkInterfaceAttribute(t *testing.T) {
82 args := &ModifyNetworkInterfaceAttributeArgs{
83 RegionId: common.Shanghai,
84 NetworkInterfaceId: "eni-testeni",
85 SecurityGroupId: []string{"sg-xxx", "sg-yyy"},
86 }
87
88 client := NewTestClient()
89 _, err := client.ModifyNetworkInterfaceAttribute(args)
90 if err != nil {
91 t.Errorf("failed to ModifyNetworkInterfaceAttribute: %v", err)
92 }
93 }
94
95 func TestCreateNetworkInterface(t *testing.T) {
96 args := &CreateNetworkInterfaceArgs{
97 RegionId: common.Shanghai,
98 VSwitchId: "vsw-xxx",
99 SecurityGroupIds: []string{"sg-xxx", "sg-yyy"},
100 SecondaryPrivateIpAddressCount: 9,
101 }
102 client := NewTestClient()
103 resp, err := client.CreateNetworkInterface(args)
104 if err != nil {
105 t.Errorf("failed to CreateNetworkInterface: %v", err)
106 }
107 t.Logf("new eni info: %+v", resp)
108 }
312312 if err != nil {
313313 return err
314314 }
315 if images == nil || len(images) == 0 {
315 if len(images) == 0 {
316316 args.Status = ImageStatusAvailable
317317 images, _, er := client.DescribeImages(&args)
318318 if er == nil && len(images) == 1 {
33
44 type DescribeInstanceTypesArgs struct {
55 InstanceTypeFamily string
6 InstanceTypes []string `query:"list"`
67 }
78
89 //
910 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&instancetypeitemtype
1011 type InstanceTypeItemType struct {
11 InstanceTypeId string
12 CpuCoreCount int
13 MemorySize float64
14 InstanceTypeFamily string
15 GPUAmount int
16 GPUSpec string
17 InitialCredit int
18 BaselineCredit int
19 EniQuantity int
20 LocalStorageCapacity int
21 LocalStorageAmount int
22 LocalStorageCategory string
12 InstanceTypeId string
13 CpuCoreCount int
14 MemorySize float64
15 InstanceTypeFamily string
16 GPUAmount int
17 GPUSpec string
18 InitialCredit int
19 BaselineCredit int
20 EniQuantity int
21 EniPrivateIpAddressQuantity int
22 LocalStorageCapacity int
23 LocalStorageAmount int
24 LocalStorageCategory string
2325 }
2426
2527 type DescribeInstanceTypesResponse struct {
44 )
55
66 func TestDescribeInstanceTypes(t *testing.T) {
7
87 client := NewTestClient()
98 instanceTypes, err := client.DescribeInstanceTypes()
109 if err != nil {
1312 for _, instanceType := range instanceTypes {
1413 t.Logf("InstanceType: %++v", instanceType)
1514 }
15
16 instanceTypes, err = client.DescribeInstanceTypesNew(&DescribeInstanceTypesArgs{
17 InstanceTypes: []string{"ecs.ec5.24xlarge", "ecs.ddh6s.custom.c4m48"},
18 })
19 if err != nil {
20 t.Fatalf("Failed to DescribeInstanceTypesNew: %v", err)
21 }
22 for _, instanceType := range instanceTypes {
23 t.Logf("InstanceType: %++v", instanceType)
24 }
1625 }
490490 }
491491
492492 type SystemDiskType struct {
493 Size int
494 Category DiskCategory //Enum cloud, ephemeral, ephemeral_ssd
495 DiskName string
496 Description string
493 Size int
494 Category DiskCategory //Enum cloud, ephemeral, ephemeral_ssd
495 DiskName string
496 Description string
497 PerformanceLevel DiskPerformanceLevel
498 AutoSnapshotPolicyId string
497499 }
498500
499501 type IoOptimized string
331331 RegionId: TestRegionID,
332332 Pagination: common.Pagination{
333333 PageNumber: 1,
334 PageSize: 100,
334 PageSize: 2,
335335 },
336336 //SecurityToken: TestSecurityToken,
337337 }
11
22 import (
33 "github.com/denverdino/aliyungo/common"
4 )
5
6 type NatType string
7
8 const (
9 NgwNatTypeNormal = NatType("Normal")
10 NgwNatTypeEnhanced = NatType("Enhanced")
411 )
512
613 type BandwidthPackageType struct {
1219 type CreateNatGatewayArgs struct {
1320 RegionId common.Region
1421 VpcId string
22 VSwitchId string
1523 Spec string
1624 BandwidthPackage []BandwidthPackageType
1725 Name string
1826 Description string
27 NatType NatType
1928 ClientToken string
2029 }
2130
7483 Spec string
7584 Status string
7685 VpcId string
86 NatType NatType
7787 }
7888
7989 type DescribeNatGatewayResponse struct {
3737 }
3838 }
3939 }
40
41 func TestClient_CreateNatGateway(t *testing.T) {
42 client := NewVpcTestClientForDebug()
43 args := &CreateNatGatewayArgs{
44 RegionId: TestRegionID,
45 VpcId: TestVpcId,
46 VSwitchId: TestVswitchID,
47 NatType: NgwNatTypeEnhanced,
48 }
49
50 resp, err := client.CreateNatGateway(args)
51 if err != nil {
52 t.Fatalf("Error %++v", err)
53 } else {
54 t.Logf("ngw id :%s", resp.NatGatewayId)
55 }
56 }
8585 type EipInstanceType string
8686
8787 const (
88 EcsInstance = "EcsInstance"
89 SlbInstance = "SlbInstance"
90 Nat = "Nat"
91 HaVip = "HaVip"
88 EcsInstance = "EcsInstance"
89 SlbInstance = "SlbInstance"
90 Nat = "Nat"
91 HaVip = "HaVip"
92 NetworkInterface = "NetworkInterface"
93 )
94
95 type AssociateEipAddressMode string
96
97 const (
98 NAT = "NAT"
99 MULTI_BINDED = "MULTI_BINDED"
100 BINDED = "BINDED"
92101 )
93102
94103 type AssociateEipAddressArgs struct {
95 AllocationId string
96 InstanceId string
97 InstanceType EipInstanceType
104 AllocationId string
105 InstanceId string
106 InstanceRegionId common.Region
107 InstanceType EipInstanceType
108 PrivateIpAddress string
109 Mode AssociateEipAddressMode
98110 }
99111
100112 type AssociateEipAddressResponse struct {
101113 common.Response
102114 }
103115
104 // AssociateEipAddress associates EIP address to VM instance
105 //
106 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/network&associateeipaddress
116 // AssociateEipAddress associates EIP address to instance
117 //
118 // You can read doc at https://help.aliyun.com/api/vpc/AssociateEipAddress.html
107119 func (client *Client) AssociateEipAddress(allocationId string, instanceId string) error {
108120 args := AssociateEipAddressArgs{
109121 AllocationId: allocationId,
131143 type AssociatedInstanceType string
132144
133145 const (
134 AssociatedInstanceTypeEcsInstance = AssociatedInstanceType("EcsInstance")
135 AssociatedInstanceTypeSlbInstance = AssociatedInstanceType("SlbInstance")
136 AssociatedInstanceTypeNat = AssociatedInstanceType("Nat")
137 AssociatedInstanceTypeHaVip = AssociatedInstanceType("HaVip")
146 AssociatedInstanceTypeEcsInstance = AssociatedInstanceType("EcsInstance")
147 AssociatedInstanceTypeSlbInstance = AssociatedInstanceType("SlbInstance")
148 AssociatedInstanceTypeNat = AssociatedInstanceType("Nat")
149 AssociatedInstanceTypeHaVip = AssociatedInstanceType("HaVip")
150 AssociatedInstanceTypeNetworkInterface = AssociatedInstanceType("NetworkInterface")
138151 )
139152
140153 type DescribeEipAddressesArgs struct {
142155 Status EipStatus //enum Associating | Unassociating | InUse | Available
143156 EipAddress string
144157 AllocationId string
145 AssociatedInstanceType AssociatedInstanceType //enum EcsInstance | SlbInstance | Nat | HaVip
158 AssociatedInstanceType AssociatedInstanceType //enum EcsInstance | SlbInstance | Nat | HaVip | NetworkInterface
146159 AssociatedInstanceId string //绑定的资源的Id。 这是一个过滤器性质的参数,若不指定,则表示不适用该条件对结果进行过滤。 如果要使用该过滤器,必须同时使用AssociatedInstanceType。若InstanceType为EcsInstance,则此处填写ECS实例Id。若InstanceType为SlbInstance,则此处填写VPC类型的私网SLB 的实例ID。若InstanceType为Nat,则此处填写NAT 的实例ID。。若InstanceType为HaVip,则此处填写HaVipId。
147160 common.Pagination
148161 }
157170 InstanceId string
158171 InstanceType string
159172 Bandwidth string // Why string
173 PrivateIpAddress string
160174 InternetChargeType common.InternetChargeType
161175 OperationLocks OperationLocksType
162176 AllocationTime util.ISO6801Time
217231 }
218232
219233 type UnallocateEipAddressArgs struct {
220 AllocationId string
221 InstanceId string
234 AllocationId string
235 InstanceId string
236 InstanceType EipInstanceType
237 PrivateIpAddress string
222238 }
223239
224240 type UnallocateEipAddressResponse struct {
235251 }
236252 response := UnallocateEipAddressResponse{}
237253 return client.Invoke("UnassociateEipAddress", &args, &response)
254 }
255
256 func (client *Client) NewUnassociateEipAddress(args *UnallocateEipAddressArgs) error {
257 response := UnallocateEipAddressResponse{}
258 return client.Invoke("UnassociateEipAddress", args, &response)
238259 }
239260
240261 type ReleaseEipAddressArgs struct {
8484
8585 }
8686
87 func TestClient_AssociateEipAddress_ENIIP(t *testing.T) {
88 client := NewVpcTestClientForDebug()
89 eipID := "eip-xxx"
90 eniID := "eni-yyy"
91 eniSecIP := "192.168.0.100"
92 region := common.Region("cn-hangzhou")
93
94 err := client.NewAssociateEipAddress(&AssociateEipAddressArgs{
95 AllocationId: eipID,
96 InstanceId: eniID,
97 InstanceType: NetworkInterface,
98 PrivateIpAddress: eniSecIP,
99 })
100 if err != nil {
101 t.Errorf("Failed to associate EIP address: %v", err)
102 }
103 err = client.WaitForEip(region, eipID, EipStatusInUse, DefaultTimeout)
104 if err != nil {
105 t.Errorf("Failed wait associate EIP address: %v", err)
106 }
107 err = client.NewUnassociateEipAddress(&UnallocateEipAddressArgs{
108 AllocationId: eipID,
109 InstanceId: eniID,
110 InstanceType: NetworkInterface,
111 PrivateIpAddress: eniSecIP,
112 })
113 if err != nil {
114 t.Errorf("Failed unassociate EIP address: %v", err)
115 }
116 }
117
87118 func TestClient_DescribeEipAddresses(t *testing.T) {
88119 client := NewVpcTestClientForDebug()
89120 args := &DescribeEipAddressesArgs{
0 package ecs
1
2 import "github.com/denverdino/aliyungo/common"
3
4 type DescribeRouteEntryListArgs struct {
5 RegionId string
6 RouteTableId string
7 DestinationCidrBlock string
8 IpVersion string
9 MaxResult int
10 NextHopId string
11 NextHopType string
12 NextToken string
13 RouteEntryId string
14 RouteEntryName string
15 RouteEntryType string
16 }
17
18 type DescribeRouteEntryListResponse struct {
19 common.Response
20 NextToken string
21 RouteEntrys struct {
22 RouteEntry []RouteEntry
23 }
24 }
25
26 type RouteEntry struct {
27 DestinationCidrBlock string
28 IpVersion string
29 RouteEntryId string
30 RouteEntryName string
31 RouteTableId string
32 Status string
33 Type string
34 NextHops struct {
35 NextHop []NextHop
36 }
37 }
38
39 type NextHop struct {
40 Enabled int
41 Weight int
42 NextHopId string
43 NextHopRegionId string
44 NextHopType string
45 NextHopRelatedInfo NextHopRelatedInfo
46 }
47
48 type NextHopRelatedInfo struct {
49 RegionId string
50 InstanceId string
51 InstanceType string
52 }
53
54 // DescribeRouteEntryList describes route entries
55 //
56 func (client *Client) DescribeRouteEntryList(args *DescribeRouteEntryListArgs) (*DescribeRouteEntryListResponse, error) {
57 response := &DescribeRouteEntryListResponse{}
58 err := client.Invoke("DescribeRouteEntryList", args, &response)
59 return response, err
60 }
0 package ecs
1
2 import (
3 "testing"
4 )
5
6 func TestClient_DescribeRouteEntry(t *testing.T) {
7 client := NewVpcTestClientForDebug()
8
9 nextHopId := "i-xxxx"
10 destinationCidrBlock := "172.xxx/x"
11 args := &DescribeRouteEntryListArgs{
12 RegionId: "cn-hangzhou",
13 RouteTableId: "vtb-xxxxx",
14 DestinationCidrBlock: destinationCidrBlock,
15 NextHopId: nextHopId,
16 RouteEntryType: "Custom",
17 }
18
19 response, err := client.DescribeRouteEntryList(args)
20
21 if err != nil {
22 t.Fatalf("Error %++v", err)
23 } else {
24 t.Logf("Result %++v", response)
25 }
26 }
9999 type NextHopType string
100100
101101 const (
102 NextHopIntance = NextHopType("Instance") //Default
103 NextHopTunnel = NextHopType("Tunnel")
104 NextHopTunnelRouterInterface = NextHopType("RouterInterface")
102 NextHopInstance = NextHopType("Instance") //Default
103 NextHopHaVip = NextHopType("HaVip")
104 NextHopRouterInterface = NextHopType("RouterInterface")
105 NextHopNetworkInterface = NextHopType("NetworkInterface")
106 NextHopVpnGateway = NextHopType("VpnGateway")
107 NextHopIPv6Gateway = NextHopType("IPv6Gateway")
108 NextHopTunnel = NextHopType("Tunnel")
105109 )
106110
107111 type CreateRouteEntryArgs struct {
1010 createArgs := CreateRouteEntryArgs{
1111 RouteTableId: routeTableId,
1212 DestinationCidrBlock: cidrBlock,
13 NextHopType: NextHopIntance,
13 NextHopType: NextHopInstance,
1414 NextHopId: instanceId,
1515 ClientToken: client.GenerateClientToken(),
1616 }
237237 for {
238238 interfaces, err := client.DescribeRouterInterfaces(&DescribeRouterInterfacesArgs{
239239 RegionId: regionId,
240 Filter: []Filter{Filter{Key: "RouterInterfaceId", Value: []string{interfaceId}}},
240 Filter: []Filter{{Key: "RouterInterfaceId", Value: []string{interfaceId}}},
241241 })
242242 if err != nil {
243243 return err
66
77 type NicType string
88 type Direction string
9 type SecurityGroupType string
910
1011 const (
1112 NicTypeInternet = NicType("internet")
1415 DirectionIngress = Direction("ingress")
1516 DirectionEgress = Direction("egress")
1617 DirectionAll = Direction("all")
18
19 SecurityGroupTypeNormal = SecurityGroupType("normal")
20 SecurityGroupTypeEnterprise = SecurityGroupType("enterprise")
1721 )
1822
1923 type IpProtocol string
9195 }
9296
9397 type DescribeSecurityGroupsArgs struct {
94 RegionId common.Region
95 VpcId string
98 RegionId common.Region
99 VpcId string
100 SecurityGroupIds []string
96101 common.Pagination
97102 }
98103
103108 SecurityGroupName string
104109 Description string
105110 VpcId string
111 SecurityGroupType SecurityGroupType // normal|enterprise
112 ServiceManaged bool
106113 CreationTime util.ISO6801Time
107114 }
108115
00 package ecs
11
22 import (
3 "os"
4 "strings"
35 "testing"
46
57 "github.com/denverdino/aliyungo/common"
3840 t.Logf("SecurityGroup Attribute: %++v", sga)
3941
4042 }
43 }
44 }
45
46 func TestSecurityGroups_BatchQuery(t *testing.T) {
47
48 client := NewTestClient()
49 arg := DescribeSecurityGroupsArgs{
50 RegionId: common.Region(os.Getenv("RegionId")),
51 SecurityGroupIds: strings.Split(os.Getenv("SecurityGroupIDs"), ","),
52 }
53
54 sgs, _, err := client.DescribeSecurityGroups(&arg)
55 if err != nil {
56 t.Errorf("Failed to DescribeSecurityGroups, error:%++v", err)
57 }
58 for _, sg := range sgs {
59 t.Logf("SecurityGroup: %++v", sg)
4160 }
4261 }
4362
125125 if err != nil {
126126 return err
127127 }
128 if snapshots == nil || len(snapshots) == 0 {
128 if len(snapshots) == 0 {
129129 return common.GetClientErrorFromString("Not found")
130130 }
131131 if snapshots[0].Progress == "100%" {
00 package ecs
11
2 import "github.com/denverdino/aliyungo/common"
2 import (
3 "time"
4
5 "github.com/denverdino/aliyungo/common"
6 )
7
8 type SnatEntryStatus string
9
10 const (
11 SnatEntryStatusPending = SnatEntryStatus("Pending")
12 SnatEntryStatusAvailable = SnatEntryStatus("Available")
13 )
314
415 type CreateSnatEntryArgs struct {
516 RegionId common.Region
617 SnatTableId string
718 SourceVSwitchId string
819 SnatIp string
20 SourceCIDR string
921 }
1022
1123 type CreateSnatEntryResponse struct {
2032 SnatTableId string
2133 SourceCIDR string
2234 SourceVSwitchId string
23 Status string
35 Status SnatEntryStatus
2436 }
2537
2638 type DescribeSnatTableEntriesArgs struct {
27 RegionId common.Region
28 SnatTableId string
39 RegionId common.Region
40 SnatTableId string
41 SnatEntryId string
42 SnatEntryName string
43 SnatIp string
44 SourceCIDR string
45 SourceVSwitchId string
2946 common.Pagination
3047 }
3148
3855 }
3956
4057 type ModifySnatEntryArgs struct {
41 RegionId common.Region
42 SnatTableId string
43 SnatEntryId string
44 SnatIp string
58 RegionId common.Region
59 SnatTableId string
60 SnatEntryId string
61 SnatIp string
62 SnatEntryName string
4563 }
4664
4765 type ModifySnatEntryResponse struct {
100118 err := client.Invoke("DeleteSnatEntry", args, &response)
101119 return err
102120 }
121
122 // WaitForSnatEntryAvailable waits for SnatEntry to available status
123 func (client *Client) WaitForSnatEntryAvailable(regionId common.Region, snatTableId, snatEntryId string, timeout int) error {
124 if timeout <= 0 {
125 timeout = DefaultTimeout
126 }
127
128 args := &DescribeSnatTableEntriesArgs{
129 RegionId: regionId,
130 SnatTableId: snatTableId,
131 SnatEntryId: snatEntryId,
132 }
133
134 for {
135 snatEntries, _, err := client.DescribeSnatTableEntries(args)
136 if err != nil {
137 return err
138 }
139
140 if len(snatEntries) == 0 {
141 return common.GetClientErrorFromString("Not found")
142 }
143 if snatEntries[0].Status == SnatEntryStatusAvailable {
144 break
145 }
146
147 timeout = timeout - DefaultWaitForInterval
148 if timeout <= 0 {
149 return common.GetClientErrorFromString("Timeout")
150 }
151 time.Sleep(DefaultWaitForInterval * time.Second)
152 }
153 return nil
154 }
1313 t.Fatalf("Failed to DescribeBandwidthPackages: %v", err)
1414 }
1515 }
16
17 func TestCreateSnatEntryWithSourceCIDR(t *testing.T) {
18 client := NewTestClient()
19 args := CreateSnatEntryArgs{
20 RegionId: "cn-beijing",
21 SnatTableId: "stb-xxx",
22 SnatIp: "47.XX.XX.98",
23 SourceCIDR: "192.168.1.1/32",
24 }
25
26 _, err := client.CreateSnatEntry(&args)
27 if err != nil {
28 t.Errorf("failed to CreateSnatEntry with SourceCIDR: %v", err)
29 }
30 }
7575 VSwitchIds struct {
7676 VSwitchId []string
7777 }
78 CidrBlock string
79 VRouterId string
80 Description string
81 IsDefault bool
82 CreationTime util.ISO6801Time
78 CidrBlock string
79 VRouterId string
80 Description string
81 IsDefault bool
82 CreationTime util.ISO6801Time
83 SecondaryCidrBlocks struct {
84 SecondaryCidrBlock []string
85 }
86 RouterTableIds struct {
87 RouterTableIds []string
88 }
8389 }
8490
8591 type DescribeVpcsResponse struct {
163163 t.Logf("Instance %s is created successfully.", instanceId)
164164 instance, err := client.DescribeInstanceAttribute(instanceId)
165165 t.Logf("Instance: %++v %v", instance, err)
166
166167 err = client.WaitForInstance(instanceId, Stopped, 0)
168 if err != nil {
169 t.Errorf("Failed to wait instance to stopped %s: %v", instanceId, err)
170 }
167171
168172 err = client.StartInstance(instanceId)
169173 if err != nil {
11
22 import (
33 "encoding/base64"
4
54 "github.com/denverdino/aliyungo/common"
65 "github.com/denverdino/aliyungo/ecs"
76 )
8988 CreationTime string
9089 InternetMaxBandwidthIn int
9190 InternetMaxBandwidthOut int
91 SystemDiskSize int
9292 SystemDiskCategory string
9393 DataDisks struct {
9494 DataDisk []DataDiskItemType
166166 }
167167 return &response, nil
168168 }
169
170 type ModifyScalingConfigurationRequest struct {
171 ScalingConfigurationId string
172 IoOptimized string
173 DataDisk []DataDisk
174 SpotStrategy string
175 SpotPriceLimit []SpotPriceLimit
176 ScalingConfigurationName string
177 InstanceName string
178 HostName string
179 ImageId string
180 ImageName string
181 InstanceTypes []string
182 Cpu int
183 Memory int
184 InternetChargeType string
185 InternetMaxBandwidthOut int
186 SystemDisk SystemDisk
187 LoadBalancerWeight string
188 UserData string
189 KeyPairName string
190 RamRoleName string
191 PasswordInherit string
192 Tags string
193 DeploymentSetId string
194 SecurityGroupId string
195 Override bool
196 ResourceGroupId string
197 SecurityGroupIds []string
198 HpcClusterId string
199
200 InstanceDescription string
201 Ipv6AddressCount int
202
203 CreditSpecification string
204 ImageFamily string
205 DedicatedHostId string
206 Affinity string
207 Tenancy string
208 }
209
210 type DataDisk struct {
211 Size int
212 SnapshotId string
213 Category string
214 Device string
215 DeleteWithInstance bool
216 Encrypted bool
217 KMSKeyId string
218 DiskName string
219 Description string
220 AutoSnapshotPolicyId string
221 }
222
223 type SpotPriceLimit struct {
224 InstanceType string
225 PriceLimit float32
226 }
227
228 type SystemDisk struct {
229 Category string
230 Size int
231 DiskName string
232 Description string
233 AutoSnapshotPolicyId string
234 }
235
236 type ModifyScalingConfigurationResponse struct {
237 common.Response
238 }
239
240 func (client *Client) ModifyScalingConfiguration(args *ModifyScalingConfigurationRequest) (resp *ModifyScalingConfigurationResponse, err error) {
241 response := ModifyScalingConfigurationResponse{}
242 err = client.InvokeByFlattenMethod("ModifyScalingConfiguration", args, &response)
243
244 if err != nil {
245 return nil, err
246 }
247 return &response, nil
248 }
1414 InService = LifecycleState("InService")
1515 Pending = LifecycleState("Pending")
1616 Removing = LifecycleState("Removing")
17 )
18
19 type MultiAZPolicy string
20
21 const (
22 MultiAZPolicyPriority = MultiAZPolicy("PRIORITY")
23 MultiAZPolicyCostOptimized = MultiAZPolicy("COST_OPTIMIZED")
24 MultiAZPolicyBalance = MultiAZPolicy("BALANCE")
1725 )
1826
1927 type CreateScalingGroupArgs struct {
2634 // NOTE: Set MinSize, MaxSize and DefaultCooldown type to int pointer to distinguish zero value from unset value.
2735 MinSize *int
2836 MaxSize *int
37 MultiAZPolicy MultiAZPolicy
2938 DefaultCooldown *int
3039 RemovalPolicy common.FlattenArray
3140 DBInstanceIds string
337346 }
338347 return nil
339348 }
349
350 type DescribeScalingActivitiesRequest struct {
351 ScalingActivityId []string
352 ScalingGroupId string
353 StatusCode string
354 RegionId common.Region
355 common.Pagination
356 }
357
358 type DescribeScalingActivitiesResponse struct {
359 common.PaginationResult
360 common.Response
361 ScalingActivities struct {
362 ScalingActivity []ScalingActivity
363 }
364 }
365
366 type ScalingActivity struct {
367 AttachedCapacity int
368 AutoCreatedCapacity int
369 Cause string
370 Description string
371 EndTime string
372 Progress int
373 ScalingActivityId string
374 ScalingGroupId string
375 ScalingInstanceNumber int
376 StartTime string
377 StatusCode string
378 StatusMessage string
379 TotalCapacity int
380 }
381
382 func (client *Client) DescribeScalingActivities(args *DescribeScalingActivitiesRequest) (resp *DescribeScalingActivitiesResponse, err error) {
383 response := DescribeScalingActivitiesResponse{}
384 err = client.InvokeByFlattenMethod("DescribeScalingActivities", args, &response)
385
386 if err != nil {
387 return nil, err
388 }
389 return &response, nil
390 }
00 package ess
11
22 import (
3 "fmt"
34 "testing"
45
56 "github.com/denverdino/aliyungo/common"
7475 }
7576 t.Logf("Instance %s is deleted successfully.", instanceId)
7677 }
78
79 func TestEssScalingActivity(t *testing.T) {
80
81 client := NewTestClient(common.Region(RegionId))
82 id := "asg-uf68jfxw7gqlao0wlc94"
83 result, err := client.DescribeScalingActivities(
84 &DescribeScalingActivitiesRequest{
85 ScalingGroupId: id,
86 RegionId: common.Shanghai,
87 },
88 )
89 if err != nil {
90 t.Errorf("get activity %s: %v", id, err)
91 }
92 fmt.Printf("%+v\n", result)
93 t.Logf("get activity succeed by id %s.", id)
94 }
55 client := NewTestClientForDebug()
66
77 args := &DescribeRegionsArgs{
8 //RegionId: common.Beijing,
8 //RegionId: common.Beijing,
99 }
1010
1111 regions, err := client.DescribeRegions(args)
1313 "encoding/json"
1414 "reflect"
1515
16 "os"
17
1618 "github.com/denverdino/aliyungo/util"
17 "os"
1819 )
1920
2021 const (
4344 VSWITCH_CIDR_BLOCK = "vswitch-cidr-block"
4445 VSWITCH_ID = "vswitch-id"
4546 ZONE = "zone-id"
46 RAM_SECURITY = "Ram/security-credentials"
47 RAM_SECURITY = "ram/security-credentials"
4748 )
4849
4950 type IMetaDataRequest interface {
350351 func (vpc *MetaDataRequest) Decode(data string, api interface{}) error {
351352 if data == "" {
352353 url, _ := vpc.Url()
353 return errors.New(fmt.Sprintf("metadata: alivpc decode data must not be nil. url=[%s]\n", url))
354 return fmt.Errorf("metadata: alivpc decode data must not be nil. url=[%s]\n", url)
354355 }
355356 switch api.(type) {
356357 case *ResultList:
359360 case *RoleAuth:
360361 return json.Unmarshal([]byte(data), api)
361362 default:
362 return errors.New(fmt.Sprintf("metadata: unknow type to decode, type=%s\n", reflect.TypeOf(api)))
363 return fmt.Errorf("metadata: unknow type to decode, type=%s\n", reflect.TypeOf(api))
363364 }
364365 }
365366
2121 client.debug = debug
2222 }
2323
24 // SetTransport sets transport to the http client
25 func (client *Client) SetTransport(transport http.RoundTripper) {
26 if client.httpClient == nil {
27 client.httpClient = &http.Client{}
28 }
29 client.httpClient.Transport = transport
30 }
31
2432 func NewClient(accessKeyId, accessKeySecret, endpoint string) (client *Client) {
2533 client = &Client{
2634 AccessKeyId: accessKeyId,
4343 canonicalizedResource := req.path
4444 var paramNames []string
4545 if req.params != nil && len(req.params) > 0 {
46 for k, _ := range req.params {
46 for k := range req.params {
4747 paramNames = append(paramNames, k)
4848 }
4949 sort.Strings(paramNames)
6161 func canonicalizeHeader(headers map[string]string) string {
6262 var canonicalizedHeaders []string
6363
64 for k, _ := range headers {
64 for k := range headers {
6565 if lower := strings.ToLower(k); strings.HasPrefix(lower, HeaderMNSPrefix) {
6666 canonicalizedHeaders = append(canonicalizedHeaders, lower)
6767 }
3636 Internal bool
3737 Secure bool
3838 ConnectTimeout time.Duration
39 Transport http.RoundTripper
3940
4041 endpoint string
4142 debug bool
5657 // Options struct
5758 //
5859 type Options struct {
59 ServerSideEncryption bool
60 Meta map[string][]string
61 ContentEncoding string
62 CacheControl string
63 ContentMD5 string
64 ContentDisposition string
60 ServerSideEncryption bool
61 ServerSideEncryptionKeyID string
62
63 Meta map[string][]string
64 ContentEncoding string
65 CacheControl string
66 ContentMD5 string
67 ContentDisposition string
6568 //Range string
6669 //Expires int
6770 }
7174 CopySourceOptions string
7275 MetadataDirective string
7376 //ContentType string
77
78 ServerSideEncryption bool
79 ServerSideEncryptionKeyID string
7480 }
7581
7682 // CopyObjectResult is the output from a Copy request
490496
491497 // addHeaders adds o's specified fields to headers
492498 func (o Options) addHeaders(headers http.Header) {
493 if o.ServerSideEncryption {
499 if len(o.ServerSideEncryptionKeyID) != 0 {
500 headers.Set("x-oss-server-side-encryption", "KMS")
501 headers.Set("x-oss-server-side-encryption-key-id", o.ServerSideEncryptionKeyID)
502 } else if o.ServerSideEncryption {
494503 headers.Set("x-oss-server-side-encryption", "AES256")
495504 }
496505 if len(o.ContentEncoding) != 0 {
515524
516525 // addHeaders adds o's specified fields to headers
517526 func (o CopyOptions) addHeaders(headers http.Header) {
527 if len(o.ServerSideEncryptionKeyID) != 0 {
528 headers.Set("x-oss-server-side-encryption", "KMS")
529 headers.Set("x-oss-server-side-encryption-key-id", o.ServerSideEncryptionKeyID)
530 } else if o.ServerSideEncryption {
531 headers.Set("x-oss-server-side-encryption", "AES256")
532 }
533
518534 if len(o.MetadataDirective) != 0 {
519535 headers.Set("x-oss-metadata-directive", o.MetadataDirective)
520536 }
833849 // to retrieve the object at path. The signature is valid until expires.
834850 func (b *Bucket) SignedURLWithArgs(path string, expires time.Time, params url.Values, headers http.Header) string {
835851 return b.SignedURLWithMethod("GET", path, expires, params, headers)
852 }
853
854 func (b *Bucket) SignedURLWithMethodForAssumeRole(method, path string, expires time.Time, params url.Values, headers http.Header) string {
855 var uv = url.Values{}
856 if params != nil {
857 uv = params
858 }
859 if len(b.Client.SecurityToken) != 0 {
860 uv.Set("security-token", b.Client.SecurityToken)
861 }
862 return b.SignedURLWithMethod(method, path, expires, params, headers)
836863 }
837864
838865 // SignedURLWithMethod returns a signed URL that allows anyone holding the URL
10231050 func (client *Client) prepare(req *request) error {
10241051 // Copy so they can be mutated without affecting on retries.
10251052 headers := copyHeader(req.headers)
1026 if len(client.SecurityToken) != 0 {
1053 // security-token should be in either Params or Header, cannot be in both
1054 if len(req.params.Get("security-token")) == 0 && len(client.SecurityToken) != 0 {
10271055 headers.Set("x-oss-security-token", client.SecurityToken)
10281056 }
10291057
11611189 },
11621190 Timeout: req.timeout,
11631191 }
1192 if client.Transport != nil {
1193 c.Transport = client.Transport
1194 }
11641195
11651196 return client.doHttpRequest(c, hreq, resp)
11661197 }
55 "io/ioutil"
66 "math/rand"
77 "net/http"
8 "os"
8 "net/url"
99 "strconv"
1010 "sync"
11
1112 //"net/http"
1213 "testing"
1314 "time"
2223 )
2324
2425 func init() {
25 AccessKeyId := os.Getenv("AccessKeyId")
26 AccessKeySecret := os.Getenv("AccessKeySecret")
27 if len(AccessKeyId) != 0 && len(AccessKeySecret) != 0 {
28 client = oss.NewOSSClient(TestRegion, false, AccessKeyId, AccessKeySecret, false)
29 } else {
30 client = oss.NewOSSClient(TestRegion, false, TestAccessKeyId, TestAccessKeySecret, false)
31 }
32
33 assumeRoleClient = oss.NewOSSClientForAssumeRole(TestRegion, false, TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, false)
26 client = oss.NewOSSClient(TestRegion, false, TestAccessKeyID, TestAccessKeySecret, false)
27 assumeRoleClient = oss.NewOSSClientForAssumeRole(TestRegion, false, TestAccessKeyID, TestAccessKeySecret, TestSecurityToken, false)
3428 assumeRoleClient.SetDebug(true)
3529 }
3630
9791 }
9892 }
9993
100 func aTestGetNotFound(t *testing.T) {
94 func TestGetNotFound(t *testing.T) {
10195
10296 b := client.Bucket("non-existent-bucket")
10397 _, err := b.Get("non-existent")
123117 }
124118 }
125119
120 func TestPutObjectWithSSE(t *testing.T) {
121 const DISPOSITION = "attachment; filename=\"0x1a2b3c.jpg\""
122
123 b := client.Bucket(TestBucket)
124 err := b.Put("name-sse", []byte("content"), "content-type", oss.Private, oss.Options{
125 ContentDisposition: DISPOSITION,
126 ServerSideEncryptionKeyID: TestServerSideEncryptionKeyID,
127 })
128 if err != nil {
129 t.Errorf("Failed for Put: %v", err)
130 }
131 }
132
133 func TestPutCopyWithSSE(t *testing.T) {
134 b := client.Bucket(TestBucket)
135 t.Log("Source: ", b.Path("name-sse"))
136 res, err := b.PutCopy("newname-sse", oss.Private, oss.CopyOptions{
137 ServerSideEncryptionKeyID: TestServerSideEncryptionKeyID,
138 },
139 b.Path("name"))
140 if err == nil {
141 t.Logf("Copy result: %v", res)
142 } else {
143 t.Errorf("Failed for PutCopy: %v", err)
144 }
145 }
146
126147 func TestList(t *testing.T) {
127148
128149 b := client.Bucket(TestBucket)
129150
130151 data, err := b.List("n", "", "", 0)
131 if err != nil || len(data.Contents) != 2 {
152 if err != nil || len(data.Contents) != 4 {
132153 t.Errorf("Failed for List: %v", err)
133154 } else {
134155 t.Logf("Contents = %++v", data)
240261 resp.Body.Close()
241262 }
242263
264 func TestSignedURLWithArgsWithTrafficLimits(t *testing.T) {
265 b := client.Bucket(TestBucket)
266 expires := time.Now().Add(20 * time.Minute)
267 url := b.SignedURLWithArgs("largefile", expires, url.Values{
268 "x-oss-traffic-limit": []string{strconv.FormatInt(5*8<<20, 10)}},
269 nil)
270 resp, err := http.Get(url)
271 t.Logf("Large file response headers: %++v", resp.Header)
272
273 if err != nil {
274 t.Fatalf("Failed for GetResponseWithHeaders: %v", err)
275 }
276 data, err := ioutil.ReadAll(resp.Body)
277
278 if err != nil {
279 t.Errorf("Failed for Read file: %v", err)
280 }
281
282 if len(data) != int(_fileSize) {
283 t.Errorf("Incorrect length for Read with offset: %v", len(data))
284 }
285 resp.Body.Close()
286 }
287
243288 func TestCopyLargeFile(t *testing.T) {
244289 b := client.Bucket(TestBucket)
245290 err := b.CopyLargeFile("largefile", "largefile2", "application/octet-stream", oss.Private, oss.Options{})
271316 t.Fatalf("Failed for Get file: %v", err)
272317 }
273318
274 if bytes.Compare(bytes1, bytes2) != 0 {
319 if !bytes.Equal(bytes1, bytes2) {
275320 t.Fatal("The result should be equal")
276321 }
277322 }
307352 t.Fatalf("Failed for Get file: %v", err)
308353 }
309354
310 if bytes.Compare(bytes1, bytes2) != 0 {
355 if !bytes.Equal(bytes1, bytes2) {
311356 t.Fatal("The result should be equal")
312357 }
313358 }
370415 func TestDelMultiObjects(t *testing.T) {
371416
372417 b := client.Bucket(TestBucket)
373 objects := []oss.Object{oss.Object{Key: "newname"}}
418 objects := []oss.Object{
419 {Key: "newname"},
420 {Key: "name-sse"},
421 {Key: "newname-sse"},
422 }
374423 err := b.DelMulti(oss.Delete{
375424 Quiet: false,
376425 Objects: objects,
1515 //
1616
1717 var (
18 TestAccessKeyId = os.Getenv("AccessKeyId")
18 TestAccessKeyID = os.Getenv("AccessKeyId")
1919 TestAccessKeySecret = os.Getenv("AccessKeySecret")
2020 TestSecurityToken = os.Getenv("SecurityToken")
2121 TestRegion = oss.Region(os.Getenv("RegionId"))
22
23 TestServerSideEncryptionKeyID = os.Getenv("ServerSideEncryptionKeyId")
2224 )
2626 MEEast1 = Region("oss-me-east-1")
2727
2828 EUCentral1 = Region("oss-eu-central-1")
29 EUWest1 = Region("oss-eu-west-1")
2930
3031 DefaultRegion = Hangzhou
3132 )
1212
1313 var ossParamsToSign = map[string]bool{
1414 "acl": true,
15 "append": true,
16 "bucketInfo": true,
17 "cname": true,
18 "comp": true,
19 "cors": true,
1520 "delete": true,
21 "endTime": true,
22 "img": true,
23 "lifecycle": true,
24 "live": true,
1625 "location": true,
1726 "logging": true,
18 "notification": true,
27 "objectMeta": true,
1928 "partNumber": true,
20 "policy": true,
21 "requestPayment": true,
22 "torrent": true,
23 "uploadId": true,
24 "uploads": true,
25 "versionId": true,
26 "versioning": true,
27 "versions": true,
28 "response-content-type": true,
29 "response-content-language": true,
30 "response-expires": true,
29 "position": true,
30 "qos": true,
31 "referer": true,
32 "replication": true,
33 "replicationLocation": true,
34 "replicationProgress": true,
3135 "response-cache-control": true,
3236 "response-content-disposition": true,
3337 "response-content-encoding": true,
34 "bucketInfo": true,
38 "response-content-language": true,
39 "response-content-type": true,
40 "response-expires": true,
41 "security-token": true,
42 "startTime": true,
43 "status": true,
44 "style": true,
45 "styleName": true,
46 "symlink": true,
47 "tagging": true,
48 "uploadId": true,
49 "uploads": true,
50 "vod": true,
51 "website": true,
52 "x-oss-process": true,
53 "x-oss-traffic-limit": true,
3554 }
3655
3756 func (client *Client) signRequest(request *request) {
6180 }
6281
6382 if len(params) > 0 {
64 resource = resource + "?" + util.Encode(params)
83 resource = resource + "?" + util.EncodeWithoutEscape(params)
6584 }
6685
6786 canonicalizedResource := resource
7392 //log.Println("stringToSign: ", stringToSign)
7493 signature := util.CreateSignature(stringToSign, client.AccessKeySecret)
7594
76 if query.Get("OSSAccessKeyId") != "" {
95 if urlSignature {
7796 query.Set("Signature", signature)
7897 } else {
7998 headers.Set("Authorization", "OSS "+client.AccessKeyId+":"+signature)
5959 return NewPVTZClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
6060 }
6161
62 //Onlyfor hangzhou
63 func NewPVTZClientWithSecurityToken4RegionalDomain(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
64 endpoint := os.Getenv("PVTZ_ENDPOINT")
65 if endpoint == "" {
66 endpoint = PVTZDefaultEndpoint
67 }
68
69 return NewPVTZClientWithEndpointAndSecurityToken4RegionalDomain(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
70 }
71
6272 func NewPVTZClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
6373 return NewPVTZClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, "", regionID)
6474 }
7585 InitClient()
7686 return client
7787 }
88
89 //only for hangzhou
90 func NewPVTZClientWithEndpointAndSecurityToken4RegionalDomain(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
91 client := &Client{}
92 client.WithEndpoint(endpoint).
93 WithVersion(PVTZAPIVersion).
94 WithAccessKeyId(accessKeyId).
95 WithAccessKeySecret(accessKeySecret).
96 WithSecurityToken(securityToken).
97 WithServiceCode(PVTZServiceCode).
98 WithRegionID(regionID).
99 InitClient4RegionalDomain()
100 return client
101 }
4242 err = client.BindZoneVpc(&BindZoneVpcArgs{
4343 ZoneId: zoneId,
4444 Vpcs: []VPCType{
45 VPCType{
45 {
4646 RegionId: common.Beijing,
4747 VpcId: TestVPCId,
4848 },
114114 }
115115
116116 func TestDescribeChangeLogs(t *testing.T) {
117 client := NewTestClient()
117 client := NewTestLocationClientForDebug()
118118
119119 changeLogs, err := client.DescribeChangeLogs(&DescribeChangeLogsArgs{})
120120
00 package pvtz
11
22 import (
3 "github.com/denverdino/aliyungo/common"
34 "os"
45 )
56
910 TestAccessKeyId = os.Getenv("AccessKeyId")
1011 TestAccessKeySecret = os.Getenv("AccessKeySecret")
1112 TestVPCId = os.Getenv("VPCId")
13 TestSecurityToken = os.Getenv("SecurityToken")
14 TestRegionID = common.Region(os.Getenv("RegionId"))
1215 )
1316
1417 var testClient *Client
2932 }
3033 return testDebugClient
3134 }
35
36 var testDebugLocationClient *Client
37
38 func NewTestLocationClientForDebug() *Client {
39 if testDebugLocationClient == nil {
40 testDebugLocationClient = NewPVTZClientWithSecurityToken4RegionalDomain(TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, TestRegionID)
41 testDebugLocationClient.SetDebug(true)
42 }
43 return testDebugLocationClient
44 }
151151 // UpdateZoneRecord update zone record
152152 //
153153 // You can read doc at https://help.aliyun.com/document_detail/66250.html
154 func (client *Client) UpdateZoneRecord(args *AddZoneRecordArgs) (err error) {
154 func (client *Client) UpdateZoneRecord(args *UpdateZoneRecordArgs) (err error) {
155155 response := &UpdateZoneRecordResponse{}
156156
157157 err = client.Invoke("UpdateZoneRecord", args, &response)
8080 ClearAccountAlias() (RamCommonResponse, error)
8181 SetPasswordPolicy(passwordPolicy PasswordPolicyRequest) (PasswordPolicyResponse, error)
8282 GetPasswordPolicy() (PasswordPolicyResponse, error)
83
84 // Common Client Methods
85 SetUserAgent(userAgent string)
8386 }
2323
2424 type PolicyResponse struct {
2525 RamCommonResponse
26 Policy Policy
26 Policy Policy
27 DefaultPolicyVersion PolicyVersion
2728 }
2829
2930 type PolicyQueryRequest struct {
1111 policy_name string
1212 policy_document = PolicyDocument{
1313 Statement: []PolicyItem{
14 PolicyItem{
14 {
1515 Action: "*",
1616 Effect: "Allow",
1717 Resource: "*",
202202 t.Logf("pass ListPoliciesForGroup %++v", resp)
203203 }
204204
205 func TEstListEntitiesForPolicy(t *testing.T) {
205 func TestListEntitiesForPolicy(t *testing.T) {
206206 client := NewTestClient()
207207 policyReq := PolicyRequest{
208208 PolicyType: "Custom",
1919
2020 policyDocument = AssumeRolePolicyDocument{
2121 Statement: []AssumeRolePolicyItem{
22 AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal},
22 {Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal},
2323 },
2424 Version: "1"}
2525
2626 newPolicyDocument = AssumeRolePolicyDocument{
2727 Statement: []AssumeRolePolicyItem{
28 AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Deny", Principal: princpal},
28 {Action: "sts:AssumeRole", Effect: "Deny", Principal: princpal},
2929 },
3030 Version: "1"}
3131
204204 }
205205
206206 // CreateOrder create db instance order
207 // you can read doc at http://docs.alibaba-inc.com/pages/viewpage.action?pageId=259349053
208207 func (client *Client) CreateOrder(args *CreateOrderArgs) (resp CreateOrderResponse, err error) {
209208 response := CreateOrderResponse{}
210209 err = client.Invoke("CreateOrder", args, &response)
253253 args := AllocateInstancePublicConnectionArgs{
254254 DBInstanceId: DBInstanceId,
255255 ConnectionStringPrefix: DBInstanceId + "o",
256 Port: "3306",
256 Port: "3306",
257257 }
258258
259259 _, err := client.AllocateInstancePublicConnection(&args)
8585 client.SecurityToken = securityToken
8686 }
8787
88 // SetTransport sets transport to the http client
89 func (client *Client) SetTransport(transport http.RoundTripper) {
90 if client.httpClient == nil {
91 client.httpClient = &http.Client{}
92 }
93 client.httpClient.Transport = transport
94 }
95
8896 type Request struct {
8997 Method string
9098 URL string
143151 httpReq.Header.Set("Date", util.GetGMTime())
144152 httpReq.Header.Set("Accept", "application/json")
145153 //httpReq.Header.Set("x-acs-version", client.Version)
154 httpReq.Header["x-acs-version"] = []string{client.Version}
146155
147156 httpReq.Header["x-acs-signature-version"] = []string{"1.0"}
148157 httpReq.Header["x-acs-signature-nonce"] = []string{util.CreateRandomString()}
179188
180189 if client.debug {
181190 var prettyJSON bytes.Buffer
182 err = json.Indent(&prettyJSON, body, "", " ")
183 log.Println(string(prettyJSON.Bytes()))
191 _ = json.Indent(&prettyJSON, body, "", " ")
192 log.Println(prettyJSON.String())
184193 }
185194
186195 if statusCode >= 400 && statusCode <= 599 {
187196 errorResponse := common.ErrorResponse{}
188 err = json.Unmarshal(body, &errorResponse)
197 _ = json.Unmarshal(body, &errorResponse)
189198 cErr := &common.Error{
190199 ErrorResponse: errorResponse,
191200 StatusCode: statusCode,
1313 TestSecurityToken = os.Getenv("SecurityToken")
1414 TestRegionID = common.Region(os.Getenv("RegionId"))
1515
16 clientForTestCase = NewTestClient()
17 debugClientForTestCase = NewTestClientForDebug()
16 debugClientForTestCase = NewTestClientForDebug()
17 debugRpcClientForTestCase = NewTestRpcClientForDebug()
1818 )
1919
2020 var testClient *Client
2828 }
2929
3030 var testDebugClient *Client
31 var testDebugRpcClient *RpcClient
3132
3233 func NewTestClientForDebug() *Client {
3334 if testDebugClient == nil {
3738 testDebugClient.SetSecurityToken(TestSecurityToken)
3839 return testDebugClient
3940 }
41
42 func NewTestRpcClientForDebug() *RpcClient {
43 if testDebugRpcClient == nil {
44 testDebugRpcClient = NewRpcClient(TestAccessKeyId, TestAccessKeySecret)
45 testDebugRpcClient.SetDebug(true)
46 }
47 testDebugRpcClient.SetSecurityToken(TestSecurityToken)
48 return testDebugRpcClient
49 }
1717 }
1818
1919 type Event struct {
20 Status string
2021 ResourceStatus string
2122 ResourceName string
2223 StatusReason string
3232 func (client *Client) DescribeResource(stackId, stackName, resourceName string) (*Resource, error) {
3333 response := &Resource{}
3434 err := client.Invoke("", http.MethodGet, fmt.Sprintf("/stacks/%s/%s/resources/%s", stackName, stackId, resourceName), nil, nil, &response)
35 if err != nil {
36 return nil, err
37 }
38
39 return response, nil
40 }
41
42 //https://help.aliyun.com/document_detail/28917.html?spm=5176.doc28916.6.589.BUPJqx
43 func (client *Client) DescribeResourceByRegion(regionId common.Region, stackId, stackName, resourceName string) (*Resource, error) {
44 response := &Resource{}
45 err := client.Invoke(regionId, http.MethodGet, fmt.Sprintf("/stacks/%s/%s/resources/%s", stackName, stackId, resourceName), nil, nil, &response)
3546 if err != nil {
3647 return nil, err
3748 }
0 package ros
1
2 import (
3 "github.com/denverdino/aliyungo/common"
4 "os"
5 )
6
7 const (
8 ROS_RPC_APIVersion = "2019-09-10"
9 ROSServiceCode = "ros"
10 )
11
12 type RpcClient struct {
13 common.Client
14 }
15
16 func NewRpcClient(accessKeyId, accessKeySecret string) *RpcClient {
17 endpoint := os.Getenv("ROS_ENDPOINT")
18 if endpoint == "" {
19 endpoint = ROSDefaultEndpoint
20 }
21 return NewRpcClientWithEndpoint(endpoint, accessKeyId, accessKeySecret)
22 }
23
24 func NewRpcClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string) *RpcClient {
25 client := &RpcClient{}
26 client.Init(endpoint, ROS_RPC_APIVersion, accessKeyId, accessKeySecret)
27 return client
28 }
29
30 func NewRosRpcClientWithSecurityToken(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *RpcClient {
31 endpoint := os.Getenv("ROS_ENDPOINT")
32 if endpoint == "" {
33 endpoint = ROSDefaultEndpoint
34 }
35
36 return NewRosRpcClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
37 }
38
39 func NewRosRpcClientWithEndpointAndSecurityToken(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *RpcClient {
40 client := &RpcClient{}
41 client.WithEndpoint(endpoint).
42 WithVersion(ROS_RPC_APIVersion).
43 WithAccessKeyId(accessKeyId).
44 WithAccessKeySecret(accessKeySecret).
45 WithSecurityToken(securityToken).
46 WithServiceCode(ROSServiceCode).
47 WithRegionID(regionID).
48 InitClient()
49 return client
50 }
2121 _, canonicalizedHeader := canonicalizeHeader(headers)
2222
2323 stringToSign := request.Method + "\n" + accept + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedHeader + canonicalizedResource
24
25 log.Println("stringToSign: ", stringToSign)
24 if client.debug {
25 log.Println("stringToSign: ", stringToSign)
26 }
2627 signature := util.CreateSignature(stringToSign, client.AccessKeySecret)
2728 headers.Set("Authorization", "acs "+client.AccessKeyId+":"+signature)
2829 }
88 "github.com/denverdino/aliyungo/util"
99 )
1010
11 type DeletionProtection string
12
13 const (
14 DeletionProtectionEnabled = DeletionProtection("Enabled")
15 DeletionProtectionDisabled = DeletionProtection("Disabled")
16 )
17
1118 //https://help.aliyun.com/document_detail/28910.html?spm=5176.doc50083.6.580.b5wkQr
1219 type CreateStackRequest struct {
13 Name string
14 Template string
15 Parameters interface{}
16 DisableRollback bool
17 TimeoutMins int
20 Name string
21 Template string
22 Parameters interface{}
23 DisableRollback bool
24 DeletionProtection DeletionProtection
25 TimeoutMins int
1826 }
1927
2028 type CreateStackResponse struct {
3543 //https://help.aliyun.com/document_detail/28911.html?spm=5176.doc28910.6.581.etoi2Z
3644 type DeleteStackRequest struct {
3745 RegionId common.Region
46 }
47
48 type DeleteStackRpcRequest struct {
49 RegionId common.Region
50 StackId string
51 RetainResources common.FlattenArray
52 RetainAllResources bool
53 }
54
55 type SetDeletionProtectionRequest struct {
56 RegionId common.Region
57 StackId string
58 DeletionProtection DeletionProtection
3859 }
3960
4061 type DeleteStackResponse struct {
202223
203224 //https://help.aliyun.com/document_detail/49066.html?spm=5176.doc28910.6.586.MJjWQh
204225 type UpdateStackRequest struct {
205 Template string
206 Parameters interface{}
207 DisableRollback bool
208 TimeoutMins int
226 Template string
227 Parameters interface{}
228 DisableRollback bool
229 TimeoutMins int
230 StackPolicy interface{}
231 UpdateAllowPolicy interface{}
232 Existing bool
209233 }
210234
211235 type UpdateStackResponse struct {
223247
224248 return stack, nil
225249 }
250
251 // rpc api: https://help.aliyun.com/document_detail/132113.html?spm=a2c4g.11174283.6.880.1b1d143eCzdE0b
252 func (client *RpcClient) DeleteStack(args *DeleteStackRpcRequest) (*DeleteStackResponse, error) {
253 response := &DeleteStackResponse{}
254 err := client.InvokeByFlattenMethod("DeleteStack", args, response)
255 if err != nil {
256 return nil, err
257 }
258 return response, nil
259 }
260
261 // rpc api: https://help.aliyun.com/document_detail/161560.html?spm=a2c4g.11186623.6.991.18f62842yOfz9G
262 func (client *RpcClient) SetDeletionProtection(args *SetDeletionProtectionRequest) (*common.Response, error) {
263 response := &common.Response{}
264 err := client.Invoke("SetDeletionProtection", args, response)
265 if err != nil {
266 return nil, err
267 }
268 return response, nil
269 }
00 package ros
11
22 import (
3 "strings"
34 "testing"
45
56 "fmt"
9091 }
9192 }
9293
94 func TestClient_CreateStack_DeletionProtection(t *testing.T) {
95 args := &CreateStackRequest{
96 Name: fmt.Sprintf("my-k8s-test-stack-%d", time.Now().Unix()),
97 Template: myTestTemplate,
98 Parameters: map[string]interface{}{},
99 DisableRollback: false,
100 DeletionProtection: DeletionProtectionEnabled,
101 TimeoutMins: 30,
102 }
103
104 response, err := debugClientForTestCase.CreateStack(TestRegionID, args)
105 if err != nil {
106 t.Fatalf("Failed to CreateStack %++v", err)
107 } else {
108 t.Logf("Success %++v", response)
109 }
110 }
111
93112 func TestClient_DeleteStack(t *testing.T) {
94113 stackName := os.Getenv("StackName")
95114 stackId := os.Getenv("StackId")
158177 t.Logf("Success %++v", response)
159178 }
160179 }
180
181 func TestClient_DeleteStackWithParams(t *testing.T) {
182 stackId := os.Getenv("StackId")
183 resourceIds := os.Getenv("RetainResources")
184
185 args := &DeleteStackRpcRequest{
186 RegionId: TestRegionID,
187 StackId: stackId,
188 RetainResources: strings.Split(resourceIds, ","),
189 }
190
191 response, err := debugRpcClientForTestCase.DeleteStack(args)
192 if err != nil {
193 t.Fatalf("Failed to DeleteStack %++v", err)
194 } else {
195 t.Logf("Success %++v", response)
196 }
197 }
198
199 func TestClient_DeleteStack_DisabledProtection(t *testing.T) {
200 stackId := os.Getenv("StackId")
201
202 args := &SetDeletionProtectionRequest{
203 RegionId: TestRegionID,
204 StackId: stackId,
205 DeletionProtection: DeletionProtectionDisabled,
206 }
207
208 response, err := debugRpcClientForTestCase.SetDeletionProtection(args)
209 if err != nil {
210 t.Fatalf("Failed to SetDeletionProtection %++v", err)
211 } else {
212 t.Logf("Success %++v", response)
213 }
214
215 deleteArgs := &DeleteStackRpcRequest{
216 RegionId: TestRegionID,
217 StackId: stackId,
218 }
219
220 resp, err := debugRpcClientForTestCase.DeleteStack(deleteArgs)
221 if err != nil {
222 t.Fatalf("Failed to DeleteStack %++v", err)
223 } else {
224 t.Logf("Success %++v", resp)
225 }
226 }
0 package standard
1
2 import (
3 "github.com/denverdino/aliyungo/common"
4
5 "os"
6 )
7
8 type Client struct {
9 common.Client
10 }
11
12 const (
13 // ROSDefaultEndpoint is the default API endpoint of ESS services
14 ROSDefaultEndpoint = "https://ros.aliyuncs.com"
15 ROSAPIVersion = "2019-09-10"
16 ROSServiceCode = "ros"
17 )
18
19 // NewClient creates a new instance of RDS client
20 func NewClient(accessKeyId, accessKeySecret string) *Client {
21 endpoint := os.Getenv("ROS_ENDPOINT")
22 if endpoint == "" {
23 endpoint = ROSDefaultEndpoint
24 }
25 return NewClientWithEndpoint(endpoint, accessKeyId, accessKeySecret)
26 }
27
28 func NewClientWithEndpoint(endpoint string, accessKeyId, accessKeySecret string) *Client {
29 client := &Client{}
30 client.Init(endpoint, ROSAPIVersion, accessKeyId, accessKeySecret)
31 return client
32 }
33
34 func NewROSClient(accessKeyId, accessKeySecret string, regionID common.Region) *Client {
35 endpoint := os.Getenv("ROS_ENDPOINT")
36 if endpoint == "" {
37 endpoint = ROSDefaultEndpoint
38 }
39
40 return NewClientWithRegion(endpoint, accessKeyId, accessKeySecret, regionID)
41 }
42
43 func NewClientWithRegion(endpoint string, accessKeyId, accessKeySecret string, regionID common.Region) *Client {
44 client := &Client{}
45 client.NewInit(endpoint, ROSAPIVersion, accessKeyId, accessKeySecret, ROSServiceCode, regionID)
46 return client
47 }
0 package standard
1
2 import (
3 "fmt"
4 "github.com/denverdino/aliyungo/common"
5 "testing"
6 )
7
8 const (
9 TestAccessKeyId = ""
10 TestAccessKeySecret = ""
11 Region = common.Shanghai
12 StackID = "c7f1fed9-0104-4596-aae6-aa5215f5a793"
13 )
14
15 func NewTestClient() *Client {
16 return NewROSClient(TestAccessKeyId, TestAccessKeySecret, Region)
17 }
18
19 func TestGetStack(t *testing.T) {
20
21 client := NewTestClient()
22 req := GetStackRequest{
23 RegionId: Region,
24 StackId: StackID,
25 }
26 res, err := client.GetStack(&req)
27 if err != nil {
28 t.Fail()
29 }
30 fmt.Printf("Response: %+v\n", res)
31 }
32
33 func TestListStack(t *testing.T) {
34 client := NewTestClient()
35 req := &ListStacksRequest{}
36 res, err := client.ListStacks(req)
37 if err != nil {
38 t.Fail()
39 }
40 fmt.Printf("ListResponse: %+v\n", res)
41 }
42
43 func TestListStackEvent(t *testing.T) {
44 client := NewTestClient()
45 req := &ListStackEventsRequest{
46 StackId: StackID,
47 }
48 res, err := client.ListStackEvents(req)
49 if err != nil {
50 t.Fail()
51 }
52 fmt.Printf("ListEventResponse: %+v\n", res)
53 }
54
55 func TestCreateStack(t *testing.T) {
56 client := NewTestClient()
57 req := &CreateStackRequest{
58 StackName: "TDDDDDDD",
59 TemplateBody: tpl,
60 DisableRollback: true,
61 TimeoutInMinutes: 60,
62 Parameters: []Parameter{
63 {ParameterKey: "SystemDisk", ParameterValue: ""},
64 },
65 }
66 res, err := client.CreateStack(req)
67 if err != nil {
68 t.Logf("create stack: %s", err.Error())
69 t.Fail()
70 }
71 fmt.Printf("ListEventResponse: %+v\n", res)
72 }
73
74 var tpl = `
75
76 `
0 package standard
1
2 import (
3 "github.com/denverdino/aliyungo/common"
4 )
5
6 type DeletionProtection string
7
8 const (
9 DeletionProtectionEnabled = DeletionProtection("Enabled")
10 DeletionProtectionDisabled = DeletionProtection("Disabled")
11 )
12
13 //https://help.aliyun.com/document_detail/28910.html?spm=5176.doc50083.6.580.b5wkQr
14 type CreateStackRequest struct {
15 RegionId common.Region
16 StackName string
17 DisableRollback bool
18 TemplateBody string
19 TemplateURL string
20 Parameters []Parameter
21 StackPolicyURL string
22 TimeoutInMinutes int
23 StackPolicyBody string
24 ClientToken string
25 NotificationURLs []string
26 DeletionProtection DeletionProtection
27 RamRoleName string
28 }
29
30 type CreateStackResponse struct {
31 StackId string
32 common.Response
33 }
34
35 type ListStackEventsRequest struct {
36 common.Pagination
37 RegionId common.Region
38 StackId string
39 Status []string
40 ResourceType []string
41 LogicalResourceId []string
42 }
43
44 type ListStackEventsResponse struct {
45 common.Response
46 common.PaginationResult
47 RegionId common.Region
48 Events []Event
49 }
50
51 type Event struct {
52 StackId string
53 Status string
54 StackName string
55 StatusReason string
56 EventId string
57 LogicalResourceId string
58 ResourceType string
59 PhysicalResourceId string
60 CreateTime string
61 }
62
63 func (client *Client) ListStackEvents(args *ListStackEventsRequest) (*ListStackEventsResponse, error) {
64 response := &ListStackEventsResponse{}
65 err := client.Invoke("ListStackEvents", args, response)
66 if err != nil {
67 return nil, err
68 }
69
70 return response, nil
71 }
72
73 func (client *Client) CreateStack(args *CreateStackRequest) (*CreateStackResponse, error) {
74 stack := &CreateStackResponse{}
75 err := client.Invoke("CreateStack", args, stack)
76 if err != nil {
77 return nil, err
78 }
79
80 return stack, nil
81 }
82
83 //https://help.aliyun.com/document_detail/28911.html?spm=5176.doc28910.6.581.etoi2Z
84 type DeleteStackRequest struct {
85 RegionId common.Region
86 StackId string
87 RetainAllResources bool
88 RetainResources []string
89 RamRoleName string
90 }
91
92 type DeleteStackResponse struct {
93 common.Response
94 }
95
96 func (client *Client) DeleteStack(req *DeleteStackRequest) (*DeleteStackResponse, error) {
97 response := &DeleteStackResponse{}
98 err := client.Invoke("DeleteStack", req, response)
99 if err != nil {
100 return nil, err
101 }
102
103 return response, nil
104 }
105
106 type GetStackRequest struct {
107 RegionId common.Region
108 StackId string
109 ClientToken string
110 }
111
112 type GetStackResponse struct {
113 CreateTime string
114 Description string
115 DisableRollback bool
116 NotificationURLs []string
117 Outputs []Output
118 ParentStackId string
119 RegionId common.Region
120 Status string
121 StackId string
122 StackName string
123 Parameters []Parameter
124 UpdateTime string
125 StatusReason string
126 TemplateDescription string
127 TimeoutInMinutes int
128
129 RequestId string
130 DeletionProtection DeletionProtection
131 DriftDetectionTime string
132 RamRoleName string
133 RootStackId string
134 StackDriftStatus string
135 StackType string
136 }
137
138 type Parameter struct {
139 ParameterKey string
140 ParameterValue string
141 }
142
143 type Output struct {
144 Description string
145 OutputKey string
146 OutputValue interface{}
147 }
148
149 func (client *Client) GetStack(req *GetStackRequest) (*GetStackResponse, error) {
150 response := &GetStackResponse{}
151 err := client.Invoke("GetStack", req, response)
152 if err != nil {
153 return nil, err
154 }
155
156 return response, nil
157 }
158
159 type ListStacksRequest struct {
160 RegionId common.Region
161 StackId string
162 Status []string
163 ParentStackId string
164 StackName []string
165 ShowNestedStack bool
166 Tag []Tag
167 common.Pagination
168 }
169
170 type ListStacksResponse struct {
171 common.PaginationResult
172 common.Response
173 Stacks []Stack
174 }
175
176 type Stack struct {
177 CreateTime string
178
179 DisableRollback bool
180 DriftDetectionTime string
181 ParentStackId string
182 RegionId common.Region
183 StackDriftStatus string
184 StackId string
185 StackName string
186 Status string
187 StatusReason string
188
189 TimeoutInMinutes int
190 UpdateTime string
191 }
192
193 type Tag struct {
194 Key string
195 Value string
196 }
197
198 func (client *Client) ListStacks(req *ListStacksRequest) (*ListStacksResponse, error) {
199 response := &ListStacksResponse{}
200 err := client.Invoke("ListStacks", req, response)
201 if err != nil {
202 return nil, err
203 }
204
205 return response, nil
206 }
207
208 type UpdateStackRequest struct {
209 Parameters []Parameter
210 RegionId string
211 StackId string
212 ClientToken string
213 StackPolicyDuringUpdateBody string
214 TimeoutInMinutes int
215 TemplateBody string
216 StackPolicyURL string
217 StackPolicyDuringUpdateURL string
218 StackPolicyBody string
219 UsePreviousParameters bool
220 DisableRollback bool
221 TemplateURL string
222 RamRoleName string
223 ReplacementOption string
224 }
225
226 type UpdateStackResponse struct {
227 StackId string
228 common.Response
229 }
230
231 func (client *Client) UpdateStack(req *UpdateStackRequest) (*UpdateStackResponse, error) {
232 response := &UpdateStackResponse{}
233 err := client.Invoke("UpdateStack", req, response)
234 if err != nil {
235 return nil, err
236 }
237
238 return response, nil
239 }
240
241 type GetStackResourceRequest struct {
242 StackId string
243 LogicalResourceId string
244 ClientToken string
245 ShowResourceAttributes bool
246 RegionId common.Region
247 }
248
249 type GetStackResourceResponse struct {
250 Status string
251 Description string
252 LogicalResourceId string
253 StackId string
254
255 StackName string
256 StatusReason string
257 PhysicalResourceId string
258 ResourceType string
259 CreateTime string
260 Metadata map[string]string
261 UpdateTime string
262 ResourceAttributes []ResourceAttribute
263 RequestId string
264 DriftDetectionTime string
265 ResourceDriftStatus string
266 }
267 type ResourceAttribute struct {
268 ResourceAttributeValue string
269 ResourceAttributeKey string
270 }
271
272 func (client *Client) GetStackResource(req *GetStackResourceRequest) (*GetStackResourceResponse, error) {
273 response := &GetStackResourceResponse{}
274 err := client.Invoke("GetStackResource", req, response)
275 if err != nil {
276 return nil, err
277 }
278
279 return response, nil
280 }
281
282 type ListStackResourcesRequest struct {
283 RegionId common.Region
284 StackId string
285 }
286
287 type ListStackResourcesResponse struct {
288 common.Response
289 Resources []Resource
290 }
291
292 type Resource struct {
293 CreateTime string
294 DriftDetectionTime string
295 LogicalResourceId string
296 PhysicalResourceId string
297
298 ResourceDriftStatus string
299 ResourceType string
300 StackId string
301 StackName string
302 Status string
303 StatusReason string
304 UpdateTime string
305 }
306
307 func (client *Client) ListStackResources(req *ListStackResourcesRequest) (*ListStackResourcesResponse, error) {
308 response := &ListStackResourcesResponse{}
309 err := client.Invoke("ListStackResources", req, response)
310 if err != nil {
311 return nil, err
312 }
313
314 return response, nil
315 }
5656 return NewSLBClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
5757 }
5858
59 //Only for hangzhou
60 func NewSLBClientWithSecurityToken4RegionalDomain(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
61 endpoint := os.Getenv("SLB_ENDPOINT")
62 if endpoint == "" {
63 endpoint = SLBDefaultEndpoint
64 }
65
66 return NewSLBClientWithEndpointAndSecurityToken4RegionalDomain(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
67 }
68
5969 func NewSLBClientWithEndpointAndSecurityToken(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
6070 client := &Client{}
6171 client.WithEndpoint(endpoint).
6878 InitClient()
6979 return client
7080 }
81
82 //only for hangzhou
83 func NewSLBClientWithEndpointAndSecurityToken4RegionalDomain(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
84 client := &Client{}
85 client.WithEndpoint(endpoint).
86 WithVersion(SLBAPIVersion).
87 WithAccessKeyId(accessKeyId).
88 WithAccessKeySecret(accessKeySecret).
89 WithSecurityToken(securityToken).
90 WithServiceCode(SLBServiceCode).
91 WithRegionID(regionID).
92 InitClient4RegionalDomain()
93 return client
94 }
1515 TestVServerGroupID = "MY_VSERVER_GROUPID"
1616 TestListenerPort = 9000
1717 TestInstanceId = "MY_INSTANCE_ID"
18 TestENIId = "MY_ENI_ID"
1819 TestRegionID = common.Region(os.Getenv("RegionId"))
1920 TestIAmRich = false
2021 TestQuick = false
4344
4445 func NewTestNewSLBClientForDebug() *Client {
4546 if testDebugNewSLBClient == nil {
46 testDebugNewSLBClient = NewSLBClientWithSecurityToken(TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, TestRegionID)
47 testDebugNewSLBClient = NewSLBClientWithSecurityToken4RegionalDomain(TestAccessKeyId, TestAccessKeySecret, TestSecurityToken, TestRegionID)
4748 testDebugNewSLBClient.SetDebug(true)
4849 }
4950 return testDebugNewSLBClient
9898 UnhealthyThreshold int
9999 HealthCheckTimeout int
100100 HealthCheckInterval int
101 ForwardPort int
102 ListenerForward FlagType
101103 HealthCheckHttpCode HealthCheckHttpCodeType
102104 VServerGroup FlagType
103105 VServerGroupId string
105107 XForwardedFor_SLBID FlagType
106108 XForwardedFor_SLBIP FlagType
107109 XForwardedFor_proto FlagType
110 Description string
111 AclId string
112 AclStatus string
113 AclType string
108114 }
109115 type CreateLoadBalancerHTTPListenerArgs HTTPListenerType
110116
146152 BackendServerPort int
147153 Bandwidth int
148154 Scheduler SchedulerType
149 PersistenceTimeout int
155 PersistenceTimeout *int
150156 HealthCheck FlagType
151157 HealthCheckType HealthCheckType
152158 HealthCheckDomain string
159165 HealthCheckHttpCode HealthCheckHttpCodeType
160166 VServerGroup FlagType
161167 VServerGroupId string
168 Description string
169 AclId string
170 AclStatus string
171 AclType string
162172 }
163173
164174 type CreateLoadBalancerTCPListenerArgs TCPListenerType
178188 BackendServerPort int
179189 Bandwidth int
180190 Scheduler SchedulerType
181 PersistenceTimeout int
191 PersistenceTimeout *int
182192 HealthCheck FlagType
183193 HealthCheckConnectPort int
184194 HealthyThreshold int
187197 HealthCheckInterval int
188198 VServerGroup FlagType
189199 VServerGroupId string
200 Description string
201
202 AclId string
203 AclStatus string
204 AclType string
190205 }
191206 type CreateLoadBalancerUDPListenerArgs UDPListenerType
192207
00 package slb
11
2 import "testing"
2 import (
3 "bytes"
4 "encoding/json"
5 "fmt"
6 "testing"
7 )
38
49 func testListeners(t *testing.T, client *Client, loadBalancerId string) {
510
4045 }
4146 t.Logf("Listener: %++v", *response)
4247 }
48
49 func TestDescribeListener(t *testing.T) {
50
51 response, err := client.DescribeLoadBalancerTCPListenerAttribute(loadBalancerId, 22)
52 if err != nil {
53 t.Error(err)
54 } else {
55 fmt.Println(PrettyJson(response))
56 }
57 }
58
59 func TestDescribeSLB(t *testing.T) {
60
61 response, err := client.DescribeLoadBalancerAttribute(loadBalancerId)
62 if err != nil {
63 t.Error(err)
64 } else {
65 fmt.Println(PrettyJson(response))
66 }
67 }
68
69 func PrettyJson(obj interface{}) string {
70 pretty := bytes.Buffer{}
71 data, err := json.Marshal(obj)
72 if err != nil {
73 return err.Error()
74 }
75 err = json.Indent(&pretty, data, "", " ")
76
77 if err != nil {
78 return err.Error()
79 }
80
81 return pretty.String()
82 }
1919 const (
2020 PayByBandwidth = InternetChargeType("paybybandwidth")
2121 PayByTraffic = InternetChargeType("paybytraffic")
22 )
23
24 type AddressIPVersionType string
25
26 const (
27 IPv4 = AddressIPVersionType("ipv4")
28 IPv6 = AddressIPVersionType("ipv6")
2229 )
2330
2431 type LoadBalancerSpecType string
3239 S3Large = "slb.s3.large"
3340 )
3441
42 type ModificationProtectionType string
43
44 const (
45 NonProtection = ModificationProtectionType("NonProtection")
46 ConsoleProtection = ModificationProtectionType("ConsoleProtection")
47 )
48
3549 type CreateLoadBalancerArgs struct {
36 RegionId common.Region
37 LoadBalancerName string
38 AddressType AddressType
39 VSwitchId string
40 InternetChargeType InternetChargeType
41 Bandwidth int
42 ClientToken string
43 MasterZoneId string
44 SlaveZoneId string
45 LoadBalancerSpec LoadBalancerSpecType
50 RegionId common.Region
51 LoadBalancerName string
52 AddressType AddressType
53 VSwitchId string
54 InternetChargeType InternetChargeType
55 Bandwidth int
56 ClientToken string
57 MasterZoneId string
58 SlaveZoneId string
59 LoadBalancerSpec LoadBalancerSpecType
60 AddressIPVersion AddressIPVersionType
61 DeleteProtection FlagType
62 ModificationProtectionStatus ModificationProtectionType
63 ModificationProtectionReason string
64 ResourceGroupId string
4665 }
4766
4867 type CreateLoadBalancerResponse struct {
5372 VpcId string
5473 VSwitchId string
5574 LoadBalancerName string
75 MasterZoneId string
76 SlaveZoneId string
77 AddressIPVersion AddressIPVersionType
5678 }
5779
5880 // CreateLoadBalancer create loadbalancer
192214 type ListenerPortAndProtocolType struct {
193215 ListenerPort int
194216 ListenerProtocol string
217 Description string
195218 }
196219
197220 type BackendServerType struct {
200223 Type string
201224 }
202225
226 type ServiceManagedModeType string
227
228 const (
229 Managed = ServiceManagedModeType("Managed")
230 Unmanaged = ServiceManagedModeType("Unmanaged")
231 DependencyManaged = ServiceManagedModeType("DependencyManaged")
232 )
233
203234 type LoadBalancerType struct {
204 LoadBalancerId string
205 LoadBalancerName string
206 LoadBalancerStatus string
207 Address string
208 RegionId common.Region
209 RegionIdAlias string
210 AddressType AddressType
211 VSwitchId string
212 VpcId string
213 NetworkType string
214 Bandwidth int
215 InternetChargeType InternetChargeType
216 CreateTime string //Why not ISO 6801
217 CreateTimeStamp util.ISO6801Time
218 ListenerPorts struct {
235 LoadBalancerId string
236 ResourceGroupId string
237 LoadBalancerName string
238 LoadBalancerStatus string
239 Address string
240 RegionId common.Region
241 RegionIdAlias string
242 AddressType AddressType
243 VSwitchId string
244 VpcId string
245 NetworkType string
246 Bandwidth int
247 InternetChargeType InternetChargeType
248 CreateTime string //Why not ISO 6801
249 CreateTimeStamp util.ISO6801Time
250 DeleteProtection FlagType
251 ModificationProtectionStatus ModificationProtectionType
252 ModificationProtectionReason string
253 ListenerPorts struct {
219254 ListenerPort []int
220255 }
221256 ListenerPortsAndProtocol struct {
224259 BackendServers struct {
225260 BackendServer []BackendServerType
226261 }
227 LoadBalancerSpec LoadBalancerSpecType
262 LoadBalancerSpec LoadBalancerSpecType
263 MasterZoneId string
264 SlaveZoneId string
265 AddressIPVersion AddressIPVersionType
266 ServiceManagedMode ServiceManagedModeType
228267 }
229268
230269 type DescribeLoadBalancersResponse struct {
320359 }
321360 return nil
322361 }
362
363 type SetLoadBalancerDeleteProtectionArgs struct {
364 LoadBalancerId string
365 DeleteProtection FlagType
366 RegionId common.Region
367 }
368
369 type SetLoadBalancerDeleteProtectionResponse struct {
370 common.Response
371 }
372
373 // SetLoadBalancerDeleteProtection loadbalancer delete protection
374 //
375 // You can read doc at https://help.aliyun.com/document_detail/122674.html?spm=a2c4g.11186623.6.720.694f4265hwOdXQ
376 func (client *Client) SetLoadBalancerDeleteProtection(args *SetLoadBalancerDeleteProtectionArgs) (err error) {
377 response := &SetLoadBalancerDeleteProtectionResponse{}
378 err = client.Invoke("SetLoadBalancerDeleteProtection", args, response)
379 return err
380 }
381
382 type SetLoadBalancerModificationProtectionArgs struct {
383 RegionId common.Region
384 LoadBalancerId string
385 ModificationProtectionStatus ModificationProtectionType
386 ModificationProtectionReason string
387 }
388
389 type SetLoadBalancerModificationProtectionResponse struct {
390 common.Response
391 }
392
393 func (client *Client) SetLoadBalancerModificationProtection(args *SetLoadBalancerModificationProtectionArgs) (err error) {
394 response := &SetLoadBalancerModificationProtectionResponse{}
395 err = client.Invoke("SetLoadBalancerModificationProtection", args, response)
396 return err
397 }
398
399 type ManagedResourceType string
400
401 const (
402 ManagedLoadBalancer = ManagedResourceType("LoadBalancer")
403 ManagedTls = ManagedResourceType("Tls")
404 ManagedVServerGroup = ManagedResourceType("VServerGroup")
405 ManagedMasterSlaveServerGroup = ManagedResourceType("MasterSlaveServerGroup")
406 ManagedAcl = ManagedResourceType("Acl")
407 ManagedListener = ManagedResourceType("Listener")
408 ManagedRule = ManagedResourceType("Rule")
409 ManagedAppRule = ManagedResourceType("AppRule")
410 ManagedDomainExtension = ManagedResourceType("DomainExtension")
411 )
412
413 type ManagedResourceModel struct {
414 ResourceId string
415 Port int
416 Protocol string
417 }
418
419 type ServiceManagedControlArgs struct {
420 RegionId common.Region
421 ServiceManagedMode ServiceManagedModeType
422 ResourceUid string
423 ResourceBid string
424 ResourceType ManagedResourceType
425 Resources []ManagedResourceModel
426 }
427
428 type ServiceManagedControlResponse struct {
429 common.Response
430 }
431
432 //api: https://yuque.antfin-inc.com/docs/share/63b5a2d3-6fb3-4bd7-a50e-c4b385b866fd?#
433 func (client *Client) ServiceManagedControl(args *ServiceManagedControlArgs) (err error) {
434 response := &ServiceManagedControlResponse{}
435 err = client.Invoke("ServiceManagedControl", args, response)
436 return err
437 }
55 "github.com/denverdino/aliyungo/common"
66 )
77
8 func TestLoadBlancer(t *testing.T) {
8 func TestLoadBalancer(t *testing.T) {
99
1010 client := NewTestClientForDebug()
1111
1212 creationArgs := CreateLoadBalancerArgs{
1313 RegionId: common.Beijing,
1414 LoadBalancerName: "test-slb",
15 LoadBalancerSpec: S2Medium, // eni not support slb.s0.share slb(default slb.s0.share)
1516 AddressType: InternetAddressType,
1617 ClientToken: client.GenerateClientToken(),
1718 }
2930
3031 describeLoadBalancersArgs := DescribeLoadBalancersArgs{
3132 RegionId: common.Beijing,
33 }
34
35 loadBalancers, err := client.DescribeLoadBalancers(&describeLoadBalancersArgs)
36
37 if err != nil {
38 t.Fatalf("Failed to DescribeLoadBalancers: %v", err)
39 }
40 t.Logf("DescribeLoadBalancers result: %++v", loadBalancers)
41
42 err = client.SetLoadBalancerStatus(lbId, InactiveStatus)
43 if err != nil {
44 t.Fatalf("Failed to SetLoadBalancerStatus: %v", err)
45 }
46 err = client.SetLoadBalancerName(lbId, "test-slb2")
47 if err != nil {
48 t.Fatalf("Failed to SetLoadBalancerName: %v", err)
49 }
50 loadBalancer, err := client.DescribeLoadBalancerAttribute(lbId)
51
52 if err != nil {
53 t.Fatalf("Failed to DescribeLoadBalancerAttribute: %v", err)
54 }
55 t.Logf("DescribeLoadBalancerAttribute result: %++v", loadBalancer)
56
57 err = client.DeleteLoadBalancer(lbId)
58 if err != nil {
59 t.Errorf("Failed to DeleteLoadBalancer: %v", err)
60 }
61
62 t.Logf("DeleteLoadBalancer successfully: %s", lbId)
63
64 }
65
66 func TestLoadBalancerIPv6(t *testing.T) {
67
68 client := NewTestClientForDebug()
69
70 creationArgs := CreateLoadBalancerArgs{
71 RegionId: common.Hangzhou,
72 LoadBalancerName: "test-slb-ipv6",
73 AddressType: InternetAddressType,
74 MasterZoneId: "cn-hangzhou-e",
75 SlaveZoneId: "cn-hangzhou-f",
76 ClientToken: client.GenerateClientToken(),
77 AddressIPVersion: IPv6,
78 }
79
80 response, err := client.CreateLoadBalancer(&creationArgs)
81 if err != nil {
82 t.Fatalf("Failed to CreateLoadBalancer: %v", err)
83 }
84
85 t.Logf("CreateLoadBalancer result: %v", *response)
86 lbId := response.LoadBalancerId
87
88 describeLoadBalancersArgs := DescribeLoadBalancersArgs{
89 RegionId: common.Hangzhou,
3290 }
3391
3492 loadBalancers, err := client.DescribeLoadBalancers(&describeLoadBalancersArgs)
78136 t.Logf("Result = %++v", slbs)
79137 }
80138 }
139
140 func TestClient_SetLoadBalancerDeleteProtection(t *testing.T) {
141 client := NewTestNewSLBClientForDebug()
142
143 creationArgs := CreateLoadBalancerArgs{
144 RegionId: common.Beijing,
145 LoadBalancerName: "test-slb",
146 LoadBalancerSpec: S2Medium,
147 AddressType: InternetAddressType,
148 ClientToken: client.GenerateClientToken(),
149 }
150
151 response, err := client.CreateLoadBalancer(&creationArgs)
152 if err != nil {
153 t.Fatalf("Failed to CreateLoadBalancer: %v", err)
154 }
155
156 t.Logf("CreateLoadBalancer result: %v", *response)
157 lbId := response.LoadBalancerId
158
159 args := &SetLoadBalancerDeleteProtectionArgs{
160 LoadBalancerId: lbId,
161 DeleteProtection: OnFlag,
162 RegionId: common.Beijing,
163 }
164
165 err = client.SetLoadBalancerDeleteProtection(args)
166 if err != nil {
167 t.Fatalf("Failed %++v", err)
168 }
169 t.Logf("SetLoadBalancerDeleteProtection result: %v", *response)
170
171 err = client.DeleteLoadBalancer(lbId)
172 if err != nil {
173 t.Logf("DeleteLoadBalancer result: %++v", err)
174 } else {
175 t.Fatalf("Failed to set LoadBalancer delete protection.")
176 }
177 }
178
179 func TestClient_SetLoadBalancerModificationProtection(t *testing.T) {
180 client := NewTestNewSLBClientForDebug()
181
182 creationArgs := CreateLoadBalancerArgs{
183 RegionId: common.Beijing,
184 LoadBalancerName: "test-slb-modification-protection",
185 LoadBalancerSpec: S1Small,
186 AddressType: InternetAddressType,
187 ModificationProtectionStatus: ConsoleProtection,
188 ModificationProtectionReason: "kubernetes.do.not.delete",
189 ClientToken: client.GenerateClientToken(),
190 }
191 response, err := client.CreateLoadBalancer(&creationArgs)
192 if err != nil {
193 t.Fatalf("Failed to CreateLoadBalancer: %v", err)
194 }
195
196 t.Logf("CreateLoadBalancer result: %v", *response)
197 lbId := response.LoadBalancerId
198
199 lb, err := client.DescribeLoadBalancerAttribute(lbId)
200 if err != nil {
201 t.Fatalf("Failed to DescribeLoadBalancerAttribute: %v", err)
202 }
203
204 if lb.ModificationProtectionStatus != ConsoleProtection {
205 t.Fatalf("Failed to SetLoadBalancerModificationProtection, slb %s, expected %s got %s",
206 lbId, ConsoleProtection, lb.ModificationProtectionStatus)
207 }
208
209 args := SetLoadBalancerModificationProtectionArgs{
210 RegionId: common.Beijing,
211 LoadBalancerId: lbId,
212 ModificationProtectionStatus: NonProtection,
213 }
214
215 err = client.SetLoadBalancerModificationProtection(&args)
216 if err != nil {
217 t.Fatalf("Failed to SetLoadBalancerModificationProtection: %v", err)
218 }
219
220 lb, err = client.DescribeLoadBalancerAttribute(lbId)
221 if err != nil {
222 t.Fatalf("Failed to DescribeLoadBalancerAttribute: %v", err)
223 }
224
225 if lb.ModificationProtectionStatus != NonProtection {
226 t.Fatalf("Failed to SetLoadBalancerModificationProtection, slb %s, expected %s got %s",
227 lbId, ConsoleProtection, lb.ModificationProtectionStatus)
228 }
229
230 // Delete Slb
231 err = client.DeleteLoadBalancer(lbId)
232 if err != nil {
233 t.Fatalf("Failed to DeleteLoadBalancer: %v", err)
234 }
235
236 }
237
238 func TestClient_ServiceManagedControl(t *testing.T) {
239 client := NewTestNewSLBClientForDebug()
240
241 creationArgs := CreateLoadBalancerArgs{
242 RegionId: common.Beijing,
243 LoadBalancerName: "test-slb-modification-protection",
244 LoadBalancerSpec: S1Small,
245 AddressType: InternetAddressType,
246 ModificationProtectionStatus: ConsoleProtection,
247 ModificationProtectionReason: "kubernetes.do.not.delete",
248 ClientToken: client.GenerateClientToken(),
249 }
250 response, err := client.CreateLoadBalancer(&creationArgs)
251 if err != nil {
252 t.Fatalf("Failed to CreateLoadBalancer: %v", err)
253 }
254
255 t.Logf("CreateLoadBalancer result: %v", *response)
256 lbId := response.LoadBalancerId
257
258 lb, err := client.DescribeLoadBalancerAttribute(lbId)
259 if err != nil {
260 t.Fatalf("Failed to DescribeLoadBalancerAttribute: %v", err)
261 }
262
263 resource := make([]ManagedResourceModel, 0)
264 resource = append(resource, ManagedResourceModel{ResourceId: lb.LoadBalancerId})
265 args := ServiceManagedControlArgs{
266 RegionId: common.Beijing,
267 ServiceManagedMode: Managed,
268 ResourceType: ManagedLoadBalancer,
269 Resources: resource,
270 }
271
272 err = client.ServiceManagedControl(&args)
273 if err != nil {
274 t.Fatalf("Failed to modify resource managed status: %v", err)
275 }
276
277 lb, err = client.DescribeLoadBalancerAttribute(lbId)
278 if err != nil {
279 t.Fatalf("Failed to DescribeLoadBalancerAttribute: %v", err)
280 }
281
282 if lb.ServiceManagedMode != Managed {
283 t.Fatalf("Failed to modify resource managed status, slb %s, expected %s got %s",
284 lbId, Managed, lb.ServiceManagedMode)
285 }
286
287 // Delete Slb
288 err = client.DeleteLoadBalancer(lbId)
289 if err != nil {
290 t.Fatalf("Failed to DeleteLoadBalancer: %v", err)
291 }
292
293 }
1010 client := NewTestClientForDebug()
1111
1212 rulesArr := []Rule{
13 Rule{RuleName: "rule-001", Domain: "datapaking.com", Url: "/rule0001", VServerGroupId: TestVServerGroupID},
14 Rule{RuleName: "rule-002", Domain: "datapaking.com", Url: "/rule0002", VServerGroupId: TestVServerGroupID},
13 {RuleName: "rule-001", Domain: "datapaking.com", Url: "/rule0001", VServerGroupId: TestVServerGroupID},
14 {RuleName: "rule-002", Domain: "datapaking.com", Url: "/rule0002", VServerGroupId: TestVServerGroupID},
1515 }
1616 ruleStr, _ := json.Marshal(rulesArr)
1717
11
22 import (
33 "encoding/json"
4
54 "github.com/denverdino/aliyungo/common"
65 )
76
6261
6362 type RemoveBackendServersArgs struct {
6463 LoadBalancerId string
65 BackendServers []string
64 BackendServers string
6665 }
6766
6867 type RemoveBackendServersResponse struct {
7675 // RemoveBackendServers Remove backend servers
7776 //
7877 // You can read doc at http://docs.aliyun.com/#/pub/slb/api-reference/api-related-backendserver&RemoveBackendServers
79 func (client *Client) RemoveBackendServers(loadBalancerId string, backendServers []string) (result []BackendServerType, err error) {
78 func (client *Client) RemoveBackendServers(loadBalancerId string, backendServers []BackendServerType) (result []BackendServerType, err error) {
79 bytes, _ := json.Marshal(backendServers)
80
8081 args := &RemoveBackendServersArgs{
8182 LoadBalancerId: loadBalancerId,
82 BackendServers: backendServers,
83 BackendServers: string(bytes),
8384 }
8485 response := &RemoveBackendServersResponse{}
86
8587 err = client.Invoke("RemoveBackendServers", args, response)
8688 if err != nil {
8789 return nil, err
44 func testBackendServers(t *testing.T, client *Client, loadBalancerId string) {
55
66 backendServers := []BackendServerType{
7 BackendServerType{
7 {
88 ServerId: TestInstanceId,
99 Weight: 100,
10 Type: "ecs",
1011 },
12 //BackendServerType{
13 // ServerId: TestENIId,
14 // Weight: 100,
15 // Type: "eni",
16 //},
1117 }
1218
1319 servers, err := client.AddBackendServers(loadBalancerId, backendServers)
2834
2935 t.Logf("Backend servers: %++v", servers)
3036
31 servers, err = client.RemoveBackendServers(loadBalancerId, []string{TestInstanceId})
37 servers, err = client.RemoveBackendServers(loadBalancerId, backendServers)
3238 if err != nil {
3339 t.Errorf("Failed to RemoveBackendServers: %v", err)
3440 }
1010 client := NewTestClientForDebug()
1111
1212 tagItemArr := []TagItem{
13 TagItem{TagKey: "username", TagValue: "test"},
14 TagItem{TagKey: "birdthday", TagValue: "20170101"},
13 {TagKey: "username", TagValue: "test"},
14 {TagKey: "birdthday", TagValue: "20170101"},
1515 }
1616 tagItems, _ := json.Marshal(tagItemArr)
1717
3333 client := NewTestClientForDebug()
3434
3535 tagItemArr := []TagItem{
36 TagItem{TagKey: "username", TagValue: "test"},
37 TagItem{TagKey: "birdthday", TagValue: "20170101"},
36 {TagKey: "username", TagValue: "test"},
37 {TagKey: "birdthday", TagValue: "20170101"},
3838 }
3939 tagItems, _ := json.Marshal(tagItemArr)
4040
44 )
55
66 type VBackendServerType struct {
7 ServerId string
8 Weight int
9 Port int
10 Type string
7 ServerId string
8 Weight int
9 Port int
10 Type string
11 ServerIp string
12 Description string
1113 }
1214
1315 type VServerGroup struct {
16 RegionId common.Region
1417 VServerGroupName string
1518 VServerGroupId string
1619 }
5053 }
5154
5255 type DescribeVServerGroupsArgs struct {
53 LoadBalancerId string
54 RegionId common.Region
56 LoadBalancerId string
57 RegionId common.Region
58 IncludeRule bool
59 IncludeListener bool
5560 }
5661
5762 type DescribeVServerGroupAttributeArgs struct {
8287 VServerGroups struct {
8388 VServerGroup []VServerGroup
8489 }
90 AssociatedObjects struct {
91 Listeners string
92 Rules string
93 }
8594 }
86 type DescribeVServerGroupAttributeResponse CreateVServerGroupResponse
95 type DescribeVServerGroupAttributeResponse struct {
96 common.Response
97 VServerGroupId string
98 VServerGroupName string
99 LoadBalancerId string
100 BackendServers VBackendServers
101 }
87102
88103 func (client *Client) CreateVServerGroup(args *CreateVServerGroupArgs) (response *CreateVServerGroupResponse, err error) {
89104 response = &CreateVServerGroupResponse{}
22 import (
33 "encoding/json"
44 "testing"
5
6 "fmt"
57
68 "github.com/denverdino/aliyungo/common"
79 )
5658
5759 func TestDescribeVServerGroups(t *testing.T) {
5860 arg := &DescribeVServerGroupsArgs{
59 LoadBalancerId: loadBalancerId,
60 RegionId: region,
61 LoadBalancerId: loadBalancerId,
62 RegionId: region,
63 IncludeListener: true,
64 IncludeRule: true,
6165 }
6266 response, err := client.DescribeVServerGroups(arg)
6367 if err != nil {
6468 t.Error(err)
6569 } else {
66 t.Log(response)
67 for _, vserverGroup := range response.VServerGroups.VServerGroup {
68 deleteVServerGroupIdList = append(deleteVServerGroupIdList, vserverGroup.VServerGroupId)
69 }
70 fmt.Println(PrettyJson(response))
7071 }
7172 }
7273
0 package slb
1
2 import (
3 "github.com/denverdino/aliyungo/common"
4 )
5
6 type DescribeZonesArgs struct {
7 RegionId common.Region
8 }
9
10 //
11 // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&zonetype
12 type ZoneType struct {
13 ZoneId string
14 LocalName string
15 SlaveZones struct {
16 SlaveZone []ZoneType
17 }
18 }
19
20 type DescribeZonesResponse struct {
21 common.Response
22 Zones struct {
23 Zone []ZoneType
24 }
25 }
26
27 // DescribeZones describes zones
28 func (client *Client) DescribeZones(regionId common.Region) (zones []ZoneType, err error) {
29 response, err := client.DescribeZonesWithRaw(regionId)
30 if err == nil {
31 return response.Zones.Zone, nil
32 }
33
34 return []ZoneType{}, err
35 }
36
37 func (client *Client) DescribeZonesWithRaw(regionId common.Region) (response *DescribeZonesResponse, err error) {
38 args := DescribeZonesArgs{
39 RegionId: regionId,
40 }
41 response = &DescribeZonesResponse{}
42
43 err = client.Invoke("DescribeZones", &args, response)
44
45 if err == nil {
46 return response, nil
47 }
48
49 return nil, err
50 }
0 package slb
1
2 import (
3 "github.com/denverdino/aliyungo/common"
4 "testing"
5 )
6
7 func TestDescribeZones(t *testing.T) {
8
9 client := NewTestNewSLBClientForDebug()
10
11 zones, err := client.DescribeZones(common.Hangzhou)
12
13 if err == nil {
14 t.Logf("regions: %v", zones)
15 } else {
16 t.Errorf("Failed to DescribeZones: %v", err)
17 }
18
19 }
2525
2626 func (client *Client) SetDebug(debug bool) {
2727 client.debug = debug
28 }
29
30 // SetTransport sets transport to the http client
31 func (client *Client) SetTransport(transport http.RoundTripper) {
32 if client.httpClient == nil {
33 client.httpClient = &http.Client{}
34 }
35 client.httpClient.Transport = transport
2836 }
2937
3038 type Project struct {
11
22 import (
33 "encoding/json"
4 "errors"
54 "fmt"
65 "strconv"
76 )
153152 return v, nil
154153 }
155154
156 return nil, errors.New(fmt.Sprintf("%v is not a string array", configs["config"]))
155 return nil, fmt.Errorf("%v is not a string array", configs["config"])
157156 }
5050 canonicalizedResource := req.path
5151 var paramNames []string
5252 if req.params != nil && len(req.params) > 0 {
53 for k, _ := range req.params {
53 for k := range req.params {
5454 paramNames = append(paramNames, k)
5555 }
5656 sort.Strings(paramNames)
6868 func canonicalizeHeader(headers map[string]string) string {
6969 var canonicalizedHeaders []string
7070
71 for k, _ := range headers {
71 for k := range headers {
7272 if lower := strings.ToLower(k); strings.HasPrefix(lower, HeaderSLSPrefix1) || strings.HasPrefix(lower, HeaderSLSPrefix2) {
7373 canonicalizedHeaders = append(canonicalizedHeaders, lower)
7474 }
2525
2626 princpalPolicyDocument = ram.AssumeRolePolicyDocument{
2727 Statement: []ram.AssumeRolePolicyItem{
28 ram.AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal},
28 {Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal},
2929 },
3030 Version: "1"}
3131
3838
3939 var policyDocument = ram.PolicyDocument{
4040 Statement: []ram.PolicyItem{
41 ram.PolicyItem{
41 {
4242 Action: "oss:GetObject",
4343 Effect: "Allow",
4444 Resource: "acs:oss:*:*:*/anyprefix",
6565 func createPolicyDocument() *ram.PolicyDocument {
6666 return &ram.PolicyDocument{
6767 Statement: []ram.PolicyItem{
68 ram.PolicyItem{
68 {
6969 Action: "oss:GetObject",
7070 Effect: "Allow",
7171 Resource: "acs:oss:*:*:*/*",
1313 got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing
1414 t0 := time.Now()
1515 for a := testAttempt.Start(); a.Next(); {
16 got = append(got, time.Now().Sub(t0))
16 got = append(got, time.Since(t0))
1717 }
18 got = append(got, time.Now().Sub(t0))
18 got = append(got, time.Since(t0))
1919 if len(got) != len(want) {
2020 t.Fatalf("Failed!")
2121 }
4646 // add to support url.Values
4747 mapValues, ok := i.(url.Values)
4848 if ok {
49 for k, _ := range mapValues {
49 for k := range mapValues {
5050 values.Set(k, mapValues.Get(k))
5151 }
5252 return
190190 // add to support url.Values
191191 mapValues, ok := i.(url.Values)
192192 if ok {
193 for k, _ := range mapValues {
193 for k := range mapValues {
194194 values.Set(k, mapValues.Get(k))
195195 }
196196 return
6464 StringArray: []string{"abc", "xyz"},
6565 StringArray2: []string{"abc", "xyz"},
6666 StructArray: []SubStruct{
67 SubStruct{A: "a", B: 1},
68 SubStruct{A: "x", B: 2},
67 {A: "a", B: 1},
68 {A: "x", B: 2},
6969 },
7070 SubStruct: SubStruct{A: "M", B: 0},
7171 test: TestString("test"),
33 "bytes"
44 srand "crypto/rand"
55 "encoding/binary"
6 "encoding/json"
7 "fmt"
68 "math/rand"
79 "net/http"
810 "net/url"
5759 if v != "" {
5860 buf.WriteString("=")
5961 buf.WriteString(url.QueryEscape(v))
62 }
63 }
64 }
65 return buf.String()
66 }
67
68 // Like Encode, but key and value are not escaped
69 func EncodeWithoutEscape(v url.Values) string {
70 if v == nil {
71 return ""
72 }
73 var buf bytes.Buffer
74 keys := make([]string, 0, len(v))
75 for k := range v {
76 keys = append(keys, k)
77 }
78 sort.Strings(keys)
79 for _, k := range keys {
80 vs := v[k]
81 prefix := k
82 for _, v := range vs {
83 if buf.Len() > 0 {
84 buf.WriteByte('&')
85 }
86 buf.WriteString(prefix)
87 if v != "" {
88 buf.WriteString("=")
89 buf.WriteString(v)
6090 }
6191 }
6292 }
144174 return string(s)
145175
146176 }
177
178 func PrettyJson(object interface{}) string {
179 b, err := json.MarshalIndent(object, "", " ")
180 if err != nil {
181 fmt.Printf("ERROR: PrettyJson, %v\n %s\n", err, b)
182 }
183 return string(b)
184 }