From a70e8d9564ef6d9322f3d46104e450fe8720e039 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 25 Mar 2018 17:44:22 +0800 Subject: [PATCH 01/38] implemented ali cloude start node & fixed bug --- aliauth/sign.go | 17 +++++++-- compute/ecs/instance.go | 67 ++++++++++++++++++++++++++++++------ compute/ecs/instance_test.go | 13 +++++++ compute/ecs/models.go | 8 ++++- gocloud/gocloud.go | 2 ++ 5 files changed, 94 insertions(+), 13 deletions(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index 4182011..f862a64 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -12,6 +12,7 @@ import ( "sort" "net/http" "io/ioutil" + "strconv" ) const formatISO8601 = "2006-01-02T15:04:05Z" @@ -37,7 +38,7 @@ func SignAndDoRequest(action string, params map[string]interface{}, response map canonicalizedQueryString += "&" canonicalizedQueryString += percentEncode(k) canonicalizedQueryString += "=" - canonicalizedQueryString += percentEncode(v.(string)) + canonicalizedQueryString += percentEncode(getString(v)) } stringToSign := "GET" + "&%2F&" + percentEncode(canonicalizedQueryString[1:]) @@ -47,7 +48,7 @@ func SignAndDoRequest(action string, params map[string]interface{}, response map // Init url query query := url.Values{} for key, value := range params { - query.Add(key, value.(string)) + query.Add(key, getString(value)) } // Generate the request URL @@ -133,3 +134,15 @@ func createRandomString() string { return string(b) } + +func getString(v interface{}) string { + switch v.(type) { + case string: + return v.(string) + case int: + return strconv.Itoa(v.(int)) + case bool: + return strconv.FormatBool(v.(bool)) + } + return "" +} diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index 104bfd2..81d858c 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -4,26 +4,71 @@ import ( "github.com/cloudlibz/gocloud/aliauth" "strconv" "reflect" + "fmt" ) -//TODO +////Start ECS instances accept map[string]interface{} func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { + var options StartInstance + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "InstanceId": + InstanceID, _ := value.(string) + options.InstanceID = InstanceID + case "InitLocalDisk": + switch value.(type) { + case bool: + options.InitLocalDisk = value.(bool) + case string: + options.InitLocalDisk = value.(string) == "true" || value.(string) == "True" + } + } + } + + params := make(map[string]interface{}) + + // Put all of options into params + e := reflect.ValueOf(&options).Elem() + typeOfOptions := e.Type() + for i := 0; i < e.NumField(); i++ { + switch e.Field(i).Type().String() { + case "string": + if e.Field(i).Interface() != "" { + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + } + case "bool": + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + fmt.Println(params[typeOfOptions.Field(i).Name], e.Field(i).Interface()) + } + } + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("StartInstance", params, response) + resp = response return resp, err } + //TODO func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { return resp, err } -//Create Ec2 instances accept map[string]interface{} +//Create ECS instances accept map[string]interface{} func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { var options CreateInstance @@ -58,17 +103,19 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { internetChargeType, _ := value.(string) options.InternetChargeType = internetChargeType case "InternetMaxBandwidthIn": - internetMaxBandwidthIn, ok := value.(int) - if !ok { - internetMaxBandwidthIn, _ = strconv.Atoi(value.(string)) + switch value.(type) { + case int: + options.InternetMaxBandwidthIn = value.(int) + case string: + options.InternetMaxBandwidthIn, _ = strconv.Atoi(value.(string)) } - options.InternetMaxBandwidthIn = internetMaxBandwidthIn case "InternetMaxBandwidthOut": - internetMaxBandwidthOut, ok := value.(int) - if !ok { - internetMaxBandwidthOut, _ = strconv.Atoi(value.(string)) + switch value.(type) { + case int: + options.InternetMaxBandwidthOut = value.(int) + case string: + options.InternetMaxBandwidthOut, _ = strconv.Atoi(value.(string)) } - options.InternetMaxBandwidthOut = internetMaxBandwidthOut case "HostName": hostName, _ := value.(string) options.HostName = hostName diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index 3dd72e0..4f108d3 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -24,3 +24,16 @@ func TestCreatenode(t *testing.T) { } t.Logf("Ali node is created successfully.") } + +func TestStartnode(t *testing.T) { + var aliEcs ECS + start := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + } + _, err := aliEcs.Startnode(start) + if err != nil { + t.Errorf("Test Fail") + return + } + t.Logf("Ali node is started successfully.") +} diff --git a/compute/ecs/models.go b/compute/ecs/models.go index 3fe5459..f3ff365 100644 --- a/compute/ecs/models.go +++ b/compute/ecs/models.go @@ -19,4 +19,10 @@ type CreateInstance struct { SystemDiskSize string SystemDiskName string SystemDiskDescription string -} \ No newline at end of file +} + +//StartInstance to store all attribute to start Ali-cloud ECS instance +type StartInstance struct { + InstanceID string + InitLocalDisk bool +} diff --git a/gocloud/gocloud.go b/gocloud/gocloud.go index ebd3cd2..f484fad 100644 --- a/gocloud/gocloud.go +++ b/gocloud/gocloud.go @@ -9,6 +9,8 @@ import ( "github.com/cloudlibz/gocloud/openstack" "github.com/cloudlibz/gocloud/azure" "github.com/cloudlibz/gocloud/digiocean" + "github.com/cloudlibz/gocloud/aliauth" + "github.com/cloudlibz/gocloud/ali" ) // Gocloud is a interface which hides the difference between different cloud providers. From 18d83745d4c0b5072ef2df362da8618915aaf5ac Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 25 Mar 2018 18:08:25 +0800 Subject: [PATCH 02/38] little work --- compute/ecs/instance.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index 81d858c..6c98730 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -7,7 +7,7 @@ import ( "fmt" ) -////Start ECS instances accept map[string]interface{} +// Start ECS instances accept map[string]interface{} func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { var options StartInstance @@ -68,7 +68,7 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { return resp, err } -//Create ECS instances accept map[string]interface{} +// Create ECS instances accept map[string]interface{} func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { var options CreateInstance From 5cfc27564a2e7f8539d373564f86b7fffdaecf54 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 25 Mar 2018 18:19:32 +0800 Subject: [PATCH 03/38] add test for ali start node --- container/rackspacecontainer/rackspacecontainer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/rackspacecontainer/rackspacecontainer.go b/container/rackspacecontainer/rackspacecontainer.go index e25d10e..528ed08 100644 --- a/container/rackspacecontainer/rackspacecontainer.go +++ b/container/rackspacecontainer/rackspacecontainer.go @@ -1,5 +1,5 @@ package rackspacecontainer // rackspacecontainer struct represents a rackspace container. -type Azurecontainer struct { +type Rackspacecontainer struct { } From a55a89c7d9d450915ae8c0eef7b1f6dbd7d83fa9 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 25 Mar 2018 22:24:26 +0800 Subject: [PATCH 04/38] add test and examples to startnode() stopnode() rebootnode() deletenode() of ali-cloud --- aliauth/sign.go | 3 +- compute/ec2/instance.go | 8 +-- compute/ecs/instance.go | 59 +++++++++++-------- compute/ecs/instance_test.go | 45 +++++++++++++- compute/ecs/models.go | 10 +++- .../rackspacecontainer/rackspacecontainer.go | 2 +- gocloud/gocloud.go | 1 - 7 files changed, 93 insertions(+), 35 deletions(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index f862a64..ac4c94f 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -113,7 +113,7 @@ func percentReplace(str string) string { const dictionary = "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" -//CreateRandomString create random string +// CreateRandomString create random string func createRandomString() string { b := make([]byte, 32) l := len(dictionary) @@ -135,6 +135,7 @@ func createRandomString() string { return string(b) } +// getString return string from interface{} func getString(v interface{}) string { switch v.(type) { case string: diff --git a/compute/ec2/instance.go b/compute/ec2/instance.go index aa80ad7..9d1c6f9 100644 --- a/compute/ec2/instance.go +++ b/compute/ec2/instance.go @@ -16,7 +16,7 @@ func (ec2 *EC2) Startnode(request interface{}) (resp interface{}, err error) { params := makeParams("StartInstances") - addParamsList(params, "InstanceId", ids) + addParamsList(params, "InstanceID", ids) response := make(map[string]interface{}) @@ -35,7 +35,7 @@ func (ec2 *EC2) Stopnode(request interface{}) (resp interface{}, err error) { Region := param["Region"] params := makeParams("StopInstances") - addParamsList(params, "InstanceId", ids) + addParamsList(params, "InstanceID", ids) resp = &StopInstanceResp{} response := make(map[string]interface{}) @@ -60,7 +60,7 @@ func (ec2 *EC2) Rebootnode(request interface{}) (resp interface{}, err error) { Region := param["Region"] params := makeParams("RebootInstances") - addParamsList(params, "InstanceId", ids) + addParamsList(params, "InstanceID", ids) response := make(map[string]interface{}) @@ -81,7 +81,7 @@ func (ec2 *EC2) Deletenode(request interface{}) (resp interface{}, err error) { Region := param["Region"] params := makeParams("TerminateInstances") - addParamsList(params, "InstanceId", instIds) + addParamsList(params, "InstanceID", instIds) response := make(map[string]interface{}) err = ec2.PrepareSignatureV2query(params, Region, response) diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index ba5f84a..8b31123 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -4,10 +4,8 @@ import ( "github.com/cloudlibz/gocloud/aliauth" "strconv" "reflect" - "fmt" ) - // Start ECS instances accept map[string]interface{} func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { var options StartInstance @@ -19,8 +17,8 @@ func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - InstanceID, _ := value.(string) - options.InstanceID = InstanceID + instanceID, _ := value.(string) + options.InstanceID = instanceID case "InitLocalDisk": switch value.(type) { case bool: @@ -44,7 +42,6 @@ func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { } case "bool": params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - fmt.Println(params[typeOfOptions.Field(i).Name], e.Field(i).Interface()) } } @@ -66,8 +63,25 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - InstanceIdV, _ := value.(string) - options.InstanceId = InstanceIdV + instanceId, _ := value.(string) + options.InstanceID = instanceId + case "ForceStop": + switch value.(type) { + case bool: + options.ForceStop = value.(bool) + case string: + options.ForceStop = value.(string) == "true" || value.(string) == "True" + } + case "ConfirmStop": + switch value.(type) { + case bool: + options.ConfirmStop = value.(bool) + case string: + options.ConfirmStop = value.(string) == "true" || value.(string) == "True" + } + case "StoppedMode": + stoppedMode, _ := value.(string) + options.StoppedMode = stoppedMode } } @@ -82,10 +96,8 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { if e.Field(i).Interface() != "" { params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() } - case "int": - if e.Field(i).Interface() != 0 { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } + case "bool": + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() } } @@ -106,8 +118,15 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - InstanceIdV, _ := value.(string) - options.InstanceId = InstanceIdV + instanceId, _ := value.(string) + options.InstanceID = instanceId + case "ForceStop": + switch value.(type) { + case bool: + options.ForceStop = value.(bool) + case string: + options.ForceStop = value.(string) == "true" || value.(string) == "True" + } } } @@ -122,10 +141,8 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { if e.Field(i).Interface() != "" { params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() } - case "int": - if e.Field(i).Interface() != 0 { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } + case "bool": + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() } } @@ -146,8 +163,8 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - InstanceIdV, _ := value.(string) - options.InstanceId = InstanceIdV + instanceId, _ := value.(string) + options.InstanceID = instanceId } } @@ -162,10 +179,6 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { if e.Field(i).Interface() != "" { params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() } - case "int": - if e.Field(i).Interface() != 0 { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } } } diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index 4f108d3..c5dea77 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -19,7 +19,7 @@ func TestCreatenode(t *testing.T) { } _, err := aliEcs.Createnode(create) if err != nil { - t.Errorf("Test Fail") + t.Errorf("Createnode Test Fail") return } t.Logf("Ali node is created successfully.") @@ -32,8 +32,49 @@ func TestStartnode(t *testing.T) { } _, err := aliEcs.Startnode(start) if err != nil { - t.Errorf("Test Fail") + t.Errorf("Startnode Test Fail") return } t.Logf("Ali node is started successfully.") } + +func TestStopnode(t *testing.T) { + var aliEcs ECS + stop := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "ForceStop": false, + } + _, err := aliEcs.Stopnode(stop) + if err != nil { + t.Errorf("Stopnode Test Fail") + return + } + t.Logf("Ali node is stoped successfully.") +} + +func TestRebootnode(t *testing.T) { + var aliEcs ECS + reboot := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "ForceStop": false, + } + _, err := aliEcs.Stopnode(reboot) + if err != nil { + t.Errorf("Rebootnode Test Fail") + return + } + t.Logf("Ali node is rebooted successfully.") +} + +func TestDeletenode(t *testing.T) { + var aliEcs ECS + delete := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + } + _, err := aliEcs.Deletenode(delete) + if err != nil { + t.Errorf("Deletenode Test Fail") + return + } + t.Logf("Ali node is deleted successfully.") +} \ No newline at end of file diff --git a/compute/ecs/models.go b/compute/ecs/models.go index 9709a37..0e7e9c3 100644 --- a/compute/ecs/models.go +++ b/compute/ecs/models.go @@ -29,15 +29,19 @@ type StartInstance struct { // StopInstance to store all attribute to Stop Ali-cloud ECS instance type StopInstance struct { - InstanceId string + InstanceID string + ForceStop bool + ConfirmStop bool + StoppedMode string } // RebootInstance to store all attribute to Reboot Ali-cloud ECS instance type RebootInstance struct { - InstanceId string + InstanceID string + ForceStop bool } // DeleteInstance to store all attribute to Delete Ali-cloud ECS instance type DeleteInstance struct { - InstanceId string + InstanceID string } diff --git a/container/rackspacecontainer/rackspacecontainer.go b/container/rackspacecontainer/rackspacecontainer.go index 528ed08..445496e 100644 --- a/container/rackspacecontainer/rackspacecontainer.go +++ b/container/rackspacecontainer/rackspacecontainer.go @@ -1,5 +1,5 @@ package rackspacecontainer -// rackspacecontainer struct represents a rackspace container. +// Rackspacecontainer struct represents a rackspace container. type Rackspacecontainer struct { } diff --git a/gocloud/gocloud.go b/gocloud/gocloud.go index f1baa53..ccce5c0 100644 --- a/gocloud/gocloud.go +++ b/gocloud/gocloud.go @@ -71,7 +71,6 @@ const ( // CloudProvider returns the instance of respective cloud and maps it to Gocloud so that we can call // the method like Createnode on CloudProvider instance. // This is a delegation of CloudProvider. - func CloudProvider(provider string) (Gocloud, error) { switch provider { From f113325e52f4fbafd7fd80a027f8e5e1b264400d Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 25 Mar 2018 22:37:37 +0800 Subject: [PATCH 05/38] improve code suggessted by codacy --- aliauth/sign.go | 2 +- compute/ecs/instance.go | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index ac4c94f..c3c0a94 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -17,7 +17,7 @@ import ( const formatISO8601 = "2006-01-02T15:04:05Z" -//Sign and do request by action parameter and specific parameters +// SignAndDoRequest sign and do request by action parameter and specific parameters func SignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { // Add common params and action param params = initParams(action, params) diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index 8b31123..e071121 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -6,7 +6,7 @@ import ( "reflect" ) -// Start ECS instances accept map[string]interface{} +// Startnode start ECS instances accept map[string]interface{} func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { var options StartInstance @@ -52,7 +52,7 @@ func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { } -// Stop ECS instances accept map[string]interface{} +// Stopnode stop ECS instances accept map[string]interface{} func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { var options StopInstance @@ -63,8 +63,8 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - instanceId, _ := value.(string) - options.InstanceID = instanceId + instanceID, _ := value.(string) + options.InstanceID = instanceID case "ForceStop": switch value.(type) { case bool: @@ -107,7 +107,7 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { return resp, err } -// Reboot ECS instances accept map[string]interface{} +// Rebootnode reboot ECS instances accept map[string]interface{} func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { var options RebootInstance @@ -118,8 +118,8 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - instanceId, _ := value.(string) - options.InstanceID = instanceId + instanceID, _ := value.(string) + options.InstanceID = instanceID case "ForceStop": switch value.(type) { case bool: @@ -152,7 +152,7 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { return resp, err } -// Delete ECS instances accept map[string]interface{} +// Deletenode delete ECS instances accept map[string]interface{} func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { var options DeleteInstance @@ -163,8 +163,8 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { for key, value := range param { switch key { case "InstanceId": - instanceId, _ := value.(string) - options.InstanceID = instanceId + instanceID, _ := value.(string) + options.InstanceID = instanceID } } @@ -188,7 +188,7 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { return resp, err } -// Create ECS instances accept map[string]interface{} +// Createnode create ECS instances accept map[string]interface{} func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { var options CreateInstance From a82339ed88f016c565e5e0187ca02ed964dcd7cc Mon Sep 17 00:00:00 2001 From: oddcn Date: Mon, 26 Mar 2018 15:51:50 +0800 Subject: [PATCH 06/38] updated examples of ali ecs, updated README --- README.md | 20 +++++++++++- examples/compute/ecs/ecs.md | 65 ++++++++++++++++++++++++++++++++----- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1c20d14..07a510a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,10 @@ Currently, implementations for other cloud providers are being worked on. * DigitalOcean Droplet [Link to example](examples/compute/droplet/droplet.md) +### Ali-cloud + +* ECS Compute [Link to example](examples/compute/ecs/ecs.md) + ## Installation instructions for Linux (Ubuntu) 1. Install golang. ``` @@ -77,7 +81,7 @@ $ go get golang.org/x/oauth2 $ go get cloud.google.com/go/compute/metadata ``` -5. Download your AWS, Google and DigitalOcean access credentials and store them in a file in your HOME directory. +5. Download your AWS, Google, DigitalOcean and Ali-cloud access credentials and store them in a file in your HOME directory. #### AWS: Save your AWS credentials in a file named *amazoncloudconfig.json*. @@ -96,6 +100,14 @@ $ go get cloud.google.com/go/compute/metadata "AccessToken": "xxxxxxxxxxxx" } ``` + #### Ali-cloud: + Save your Ali-cloud credentials in a file named *alicloudconfig.json*. + ```js + { + "AliAccessKeyID":"xxxxxxxxxxxx", + "AliAccessKeySecret":"xxxxxxxxxxxx" + } + ``` You can also set your credentials as environment variables. #### AWS: @@ -120,6 +132,12 @@ $ go get cloud.google.com/go/compute/metadata ``` export DigiOceanAccessToken = "xxxxxxxxxxxx" ``` + #### Ali-cloud: + ``` + export AliAccessKeyID = "xxxxxxxxxxxx" + export AliAccessKeySecret = "xxxxxxxxxxxx" + ``` + 6. You are all set to use gocloud! Check out the following YouTube videos for more information and usage examples: https://youtu.be/4LxsAeoonlY?list=PLOdfztY25UNnxK_0KRRHSngJIyVLDKZxq&t=3 diff --git a/examples/compute/ecs/ecs.md b/examples/compute/ecs/ecs.md index a1c4281..50489fc 100644 --- a/examples/compute/ecs/ecs.md +++ b/examples/compute/ecs/ecs.md @@ -20,7 +20,6 @@ export AliAccessKeySecret = "xxxxxxxxxxxx" ## Initialize library ```js - import "github.com/cloudlibz/gocloud/gocloud" alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) @@ -30,13 +29,63 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) ```js create := map[string]interface{}{ - "RegionId": "cn-qingdao", - "ImageId": "centos_7_04_64_20G_alibase_201701015.vhd", - "InstanceType": "ecs.xn4.small", - "SecurityGroupId": "sg-m5egbo9s5xb21kpu6nk2", + "RegionId": "cn-qingdao", + "ImageId": "centos_7_04_64_20G_alibase_201701015.vhd", + "InstanceType": "ecs.xn4.small", + "SecurityGroupId": "sg-m5egbo9s5xb21kpu6nk2", + } + + resp, err := alicloud.Createnode(create) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Start instance + +```js + start := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + } + + resp, err := alicloud.Startnode(start) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Stop instance + +```js + stop := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "ForceStop": false, + } + + resp, err := alicloud.Stopnode(stop) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Reboot instance + +```js + reboot := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "ForceStop": false, + } + + resp, err := alicloud.Rebootnode(reboot) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Delete instance + +```js + dlete := map[string]interface{}{ + "InstanceId": "i-m5e3ee3z8wdy8ktdq591", } - resp, err := alicloud.Createnode(create) - response := resp.(map[string]interface{}) - fmt.Println(response["body"]) + resp, err := alicloud.Deletenode(delete) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) ``` From b3fcf35ef10876e7bf81754c20c33b160291ea8b Mon Sep 17 00:00:00 2001 From: oddcn Date: Mon, 26 Mar 2018 15:58:53 +0800 Subject: [PATCH 07/38] improve examples of Ali ECS --- examples/compute/ecs/ecs.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/compute/ecs/ecs.md b/examples/compute/ecs/ecs.md index 50489fc..251811b 100644 --- a/examples/compute/ecs/ecs.md +++ b/examples/compute/ecs/ecs.md @@ -33,7 +33,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) "ImageId": "centos_7_04_64_20G_alibase_201701015.vhd", "InstanceType": "ecs.xn4.small", "SecurityGroupId": "sg-m5egbo9s5xb21kpu6nk2", - } + } resp, err := alicloud.Createnode(create) response := resp.(map[string]interface{}) @@ -45,7 +45,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) ```js start := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", - } + } resp, err := alicloud.Startnode(start) response := resp.(map[string]interface{}) @@ -58,7 +58,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) stop := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", "ForceStop": false, - } + } resp, err := alicloud.Stopnode(stop) response := resp.(map[string]interface{}) @@ -71,7 +71,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) reboot := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", "ForceStop": false, - } + } resp, err := alicloud.Rebootnode(reboot) response := resp.(map[string]interface{}) @@ -83,7 +83,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) ```js dlete := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", - } + } resp, err := alicloud.Deletenode(delete) response := resp.(map[string]interface{}) From 359a4048f8ad49ea8ac232dfa404fdd3fc8e4b33 Mon Sep 17 00:00:00 2001 From: oddcn Date: Tue, 27 Mar 2018 15:03:34 +0800 Subject: [PATCH 08/38] Solved a problem by me: mistakenly correcting letter cases --- compute/ec2/instance.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compute/ec2/instance.go b/compute/ec2/instance.go index 9d1c6f9..aa80ad7 100644 --- a/compute/ec2/instance.go +++ b/compute/ec2/instance.go @@ -16,7 +16,7 @@ func (ec2 *EC2) Startnode(request interface{}) (resp interface{}, err error) { params := makeParams("StartInstances") - addParamsList(params, "InstanceID", ids) + addParamsList(params, "InstanceId", ids) response := make(map[string]interface{}) @@ -35,7 +35,7 @@ func (ec2 *EC2) Stopnode(request interface{}) (resp interface{}, err error) { Region := param["Region"] params := makeParams("StopInstances") - addParamsList(params, "InstanceID", ids) + addParamsList(params, "InstanceId", ids) resp = &StopInstanceResp{} response := make(map[string]interface{}) @@ -60,7 +60,7 @@ func (ec2 *EC2) Rebootnode(request interface{}) (resp interface{}, err error) { Region := param["Region"] params := makeParams("RebootInstances") - addParamsList(params, "InstanceID", ids) + addParamsList(params, "InstanceId", ids) response := make(map[string]interface{}) @@ -81,7 +81,7 @@ func (ec2 *EC2) Deletenode(request interface{}) (resp interface{}, err error) { Region := param["Region"] params := makeParams("TerminateInstances") - addParamsList(params, "InstanceID", instIds) + addParamsList(params, "InstanceId", instIds) response := make(map[string]interface{}) err = ec2.PrepareSignatureV2query(params, Region, response) From 685c8e7040c8baef323ba59755958aa2e1d23376 Mon Sep 17 00:00:00 2001 From: oddcn Date: Thu, 29 Mar 2018 13:55:36 +0800 Subject: [PATCH 09/38] Ali compute ECS: Supports less commonly used parameters --- compute/ecs/instance.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index e071121..dc15082 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -196,6 +196,8 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { param = request.(map[string]interface{}) + params := make(map[string]interface{}) + for key, value := range param { switch key { case "RegionId": @@ -257,11 +259,18 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { case "SystemDisk.Description": systemDiskDescription, _ := value.(string) options.SystemDiskDescription = systemDiskDescription + default: + switch value.(type) { + case string: + params[key] = value.(string) + case int: + params[key] = strconv.Itoa(value.(int)) + case bool: + params[key] = strconv.FormatBool(value.(bool)) + } } } - params := make(map[string]interface{}) - // Put all of options into params e := reflect.ValueOf(&options).Elem() typeOfOptions := e.Type() From 86aa4516db3d67306b17eee21842ff0c09ca1ea4 Mon Sep 17 00:00:00 2001 From: oddcn Date: Thu, 29 Mar 2018 13:57:58 +0800 Subject: [PATCH 10/38] Ali compute ECS test case: Added less commonly used parameters for create node --- compute/ecs/instance_test.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index c5dea77..a7ed6ee 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -12,10 +12,12 @@ func init() { func TestCreatenode(t *testing.T) { var aliEcs ECS create := map[string]interface{}{ - "RegionId": "cn-qingdao", - "ImageId": "centos_7_04_64_20G_alibase_201701015.vhd", - "InstanceType": "ecs.xn4.small", - "SecurityGroupId": "sg-m5egbo9s5xb21kpu6nk2", + "RegionId": "cn-qingdao", + "ImageId": "centos_7_04_64_20G_alibase_201701015.vhd", + "InstanceType": "ecs.xn4.small", + "SecurityGroupId": "sg-m5egbo9s5xb21kpu6nk2", + "DataDisk.1.Size": "20", + "DataDisk.1.DiskName": "testName", } _, err := aliEcs.Createnode(create) if err != nil { @@ -38,7 +40,7 @@ func TestStartnode(t *testing.T) { t.Logf("Ali node is started successfully.") } -func TestStopnode(t *testing.T) { +func TestStopnode(t *testing.T) { var aliEcs ECS stop := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", @@ -52,7 +54,7 @@ func TestStopnode(t *testing.T) { t.Logf("Ali node is stoped successfully.") } -func TestRebootnode(t *testing.T) { +func TestRebootnode(t *testing.T) { var aliEcs ECS reboot := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", @@ -66,7 +68,7 @@ func TestRebootnode(t *testing.T) { t.Logf("Ali node is rebooted successfully.") } -func TestDeletenode(t *testing.T) { +func TestDeletenode(t *testing.T) { var aliEcs ECS delete := map[string]interface{}{ "InstanceId": "i-m5e3ee3z8wdy8ktdq591", @@ -77,4 +79,4 @@ func TestDeletenode(t *testing.T) { return } t.Logf("Ali node is deleted successfully.") -} \ No newline at end of file +} From 5b3082a35c072ed37eb226cfe81decce40005b39 Mon Sep 17 00:00:00 2001 From: oddcn Date: Thu, 29 Mar 2018 14:57:45 +0800 Subject: [PATCH 11/38] Ali compute: put duplicated codes into aliauth.PutStructToMap(), making code clean --- aliauth/utils.go | 28 +++++++++++++++++++ compute/ecs/instance.go | 52 ++++-------------------------------- compute/ecs/instance_test.go | 10 +++---- 3 files changed, 38 insertions(+), 52 deletions(-) create mode 100644 aliauth/utils.go diff --git a/aliauth/utils.go b/aliauth/utils.go new file mode 100644 index 0000000..a427f68 --- /dev/null +++ b/aliauth/utils.go @@ -0,0 +1,28 @@ +package aliauth + +import "reflect" + +// PutStructToMap puts key and value of struct into map[string]interface{} +// if value is string and not empty -> put +// if value is bool -> put NOTE: the default value of origin Ali API 's parameter must be false, if not ,do not use this function +// if value is int and not 0 -> put NOTE: the optional values of origin Ali API 's parameter must dose not include 0, if not ,do not use this function +func PutStructToMap(i interface{}) map[string]interface{} { + params := make(map[string]interface{}) + e := reflect.ValueOf(i).Elem() + typeOfOptions := e.Type() + for i := 0; i < e.NumField(); i++ { + switch e.Field(i).Type().String() { + case "string": + if e.Field(i).Interface() != "" { + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + } + case "bool": + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + case "int": + if e.Field(i).Interface() != 0 { + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + } + } + } + return params +} diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index dc15082..d9b6bb9 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -32,18 +32,7 @@ func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - e := reflect.ValueOf(&options).Elem() - typeOfOptions := e.Type() - for i := 0; i < e.NumField(); i++ { - switch e.Field(i).Type().String() { - case "string": - if e.Field(i).Interface() != "" { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - case "bool": - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - } + params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) err = aliauth.SignAndDoRequest("StartInstance", params, response) @@ -88,18 +77,7 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - e := reflect.ValueOf(&options).Elem() - typeOfOptions := e.Type() - for i := 0; i < e.NumField(); i++ { - switch e.Field(i).Type().String() { - case "string": - if e.Field(i).Interface() != "" { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - case "bool": - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - } + params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) err = aliauth.SignAndDoRequest("StopInstance", params, response) @@ -133,18 +111,7 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - e := reflect.ValueOf(&options).Elem() - typeOfOptions := e.Type() - for i := 0; i < e.NumField(); i++ { - switch e.Field(i).Type().String() { - case "string": - if e.Field(i).Interface() != "" { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - case "bool": - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - } + params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) err = aliauth.SignAndDoRequest("RebootInstance", params, response) @@ -171,16 +138,7 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - e := reflect.ValueOf(&options).Elem() - typeOfOptions := e.Type() - for i := 0; i < e.NumField(); i++ { - switch e.Field(i).Type().String() { - case "string": - if e.Field(i).Interface() != "" { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - } - } + params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) err = aliauth.SignAndDoRequest("DeleteInstance", params, response) @@ -262,7 +220,7 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { default: switch value.(type) { case string: - params[key] = value.(string) + params[key] = value.(string) case int: params[key] = strconv.Itoa(value.(int)) case bool: diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index a7ed6ee..39f72b1 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -30,7 +30,7 @@ func TestCreatenode(t *testing.T) { func TestStartnode(t *testing.T) { var aliEcs ECS start := map[string]interface{}{ - "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "InstanceId": "i-m5ea01zic0bg8dufo2eo", } _, err := aliEcs.Startnode(start) if err != nil { @@ -43,7 +43,7 @@ func TestStartnode(t *testing.T) { func TestStopnode(t *testing.T) { var aliEcs ECS stop := map[string]interface{}{ - "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "InstanceId": "i-m5ea01zic0bg8dufo2eo", "ForceStop": false, } _, err := aliEcs.Stopnode(stop) @@ -57,10 +57,10 @@ func TestStopnode(t *testing.T) { func TestRebootnode(t *testing.T) { var aliEcs ECS reboot := map[string]interface{}{ - "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "InstanceId": "i-m5ea01zic0bg8dufo2eo", "ForceStop": false, } - _, err := aliEcs.Stopnode(reboot) + _, err := aliEcs.Rebootnode(reboot) if err != nil { t.Errorf("Rebootnode Test Fail") return @@ -71,7 +71,7 @@ func TestRebootnode(t *testing.T) { func TestDeletenode(t *testing.T) { var aliEcs ECS delete := map[string]interface{}{ - "InstanceId": "i-m5e3ee3z8wdy8ktdq591", + "InstanceId": "i-m5ea01zic0bg8dufo2eo", } _, err := aliEcs.Deletenode(delete) if err != nil { From d51ff40952b3cc88e0c6d2470be8c5b5c2111d66 Mon Sep 17 00:00:00 2001 From: oddcn Date: Thu, 29 Mar 2018 15:08:36 +0800 Subject: [PATCH 12/38] Ali storage: implemented create disk --- compute/ecs/instance.go | 13 +----- storage/alistorage/models.go | 17 +++++++- storage/alistorage/storage.go | 78 ++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 14 deletions(-) diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index dc15082..90790a9 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -171,16 +171,7 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - e := reflect.ValueOf(&options).Elem() - typeOfOptions := e.Type() - for i := 0; i < e.NumField(); i++ { - switch e.Field(i).Type().String() { - case "string": - if e.Field(i).Interface() != "" { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - } - } + params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) err = aliauth.SignAndDoRequest("DeleteInstance", params, response) @@ -262,7 +253,7 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { default: switch value.(type) { case string: - params[key] = value.(string) + params[key] = value.(string) case int: params[key] = strconv.Itoa(value.(int)) case bool: diff --git a/storage/alistorage/models.go b/storage/alistorage/models.go index b85eb5b..a3733e8 100644 --- a/storage/alistorage/models.go +++ b/storage/alistorage/models.go @@ -1,5 +1,18 @@ package alistorage -//Alistorage struct represents Alistorage attribute and method associates with it. +// Alistorage struct represents Alistorage attribute and method associates with it. type Alistorage struct { -} \ No newline at end of file +} + +// CreateDisk to store all attribute to create Ali-cloud ECS-Disk +type CreateDisk struct { + RegionID string + ZoneID string + DiskName string + Description string + Encrypted bool + DiskCategory string + Size int + SnapshotId string + ClientToken string +} diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 2340758..13c266c 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -1,26 +1,102 @@ package alistorage +import ( + "strconv" + "reflect" +) + //TODO func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, err error) { + var options CreateDisk + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "RegionId": + regionID, _ := value.(string) + options.RegionID = regionID + case "ZoneId": + zoneID, _ := value.(string) + options.ZoneID = zoneID + case "DiskName": + diskName, _ := value.(string) + options.DiskName = diskName + case "Description": + description, _ := value.(string) + options.Description = description + case "Encrypted": + switch value.(type) { + case bool: + options.Encrypted = value.(bool) + case string: + options.Encrypted = value.(string) == "true" || value.(string) == "True" + } + case "DiskCategory": + diskCategory, _ := value.(string) + options.DiskCategory = diskCategory + case "Size": + switch value.(type) { + case int: + options.Size = value.(int) + case string: + options.Size, _ = strconv.Atoi(value.(string)) + } + case "SnapshotId": + snapshotId, _ := value.(string) + options.SnapshotId = snapshotId + case "ClientToken": + clientToken, _ := value.(string) + options.ClientToken = clientToken + } + } + + params := make(map[string]interface{}) + + // Put all of options into params + e := reflect.ValueOf(&options).Elem() + typeOfOptions := e.Type() + for i := 0; i < e.NumField(); i++ { + switch e.Field(i).Type().String() { + case "string": + if e.Field(i).Interface() != "" { + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + } + case "bool": + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + case "int": + if e.Field(i).Interface() != 0 { + params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() + } + } + } + return resp, err } + //TODO func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, err error) { return resp, err -} \ No newline at end of file +} From eb741bafd8fba10571e98bf7ff1944c22ae2a872 Mon Sep 17 00:00:00 2001 From: oddcn Date: Thu, 29 Mar 2018 15:26:53 +0800 Subject: [PATCH 13/38] Ali storage: added test case for create disk --- storage/alistorage/storage.go | 26 ++++++-------------------- storage/alistorage/storage_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 storage/alistorage/storage_test.go diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 13c266c..e763432 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -2,10 +2,10 @@ package alistorage import ( "strconv" - "reflect" + "github.com/cloudlibz/gocloud/aliauth" ) -//TODO +// Createdisk create ECS-Disk accept map[string]interface{} func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, err error) { var options CreateDisk @@ -53,26 +53,12 @@ func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, } } - params := make(map[string]interface{}) - // Put all of options into params - e := reflect.ValueOf(&options).Elem() - typeOfOptions := e.Type() - for i := 0; i < e.NumField(); i++ { - switch e.Field(i).Type().String() { - case "string": - if e.Field(i).Interface() != "" { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - case "bool": - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - case "int": - if e.Field(i).Interface() != 0 { - params[typeOfOptions.Field(i).Name] = e.Field(i).Interface() - } - } - } + params := aliauth.PutStructToMap(&options) + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("CreateDisk", params, response) + resp = response return resp, err } diff --git a/storage/alistorage/storage_test.go b/storage/alistorage/storage_test.go new file mode 100644 index 0000000..69bec50 --- /dev/null +++ b/storage/alistorage/storage_test.go @@ -0,0 +1,27 @@ +package alistorage + +import ( + "github.com/cloudlibz/gocloud/aliauth" + "testing" +) + +func init() { + aliauth.LoadConfig() +} + +func TestCreatedisk(t *testing.T) { + var alistorage Alistorage + createdisk := map[string]interface{}{ + "RegionId": "cn-qingdao", + "ZoneId": "cn-qingdao-b", + "Size": 20, + "DiskName": "ThisIsDiskName", + "Description": "ThisIsDescription", + } + _, err := alistorage.Createdisk(createdisk) + if err != nil { + t.Errorf("Createdisk Test Fail") + return + } + t.Logf("Ali disk is created successfully.") +} From 8a69352cd0cc3f77136f722156321caea1da8883 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 1 Apr 2018 22:00:45 +0800 Subject: [PATCH 14/38] Ali storage: delete disk, attach disk, detach disk. test cases --- storage/alistorage/models.go | 18 +++++++ storage/alistorage/storage.go | 76 ++++++++++++++++++++++++++++-- storage/alistorage/storage_test.go | 48 +++++++++++++++++-- 3 files changed, 136 insertions(+), 6 deletions(-) diff --git a/storage/alistorage/models.go b/storage/alistorage/models.go index a3733e8..899c196 100644 --- a/storage/alistorage/models.go +++ b/storage/alistorage/models.go @@ -16,3 +16,21 @@ type CreateDisk struct { SnapshotId string ClientToken string } + +// DeleteDisk to store all attribute to delete Ali-cloud ECS-Disk +type DeleteDisk struct { + DiskID string +} + +// AttachDisk to store all attribute of attach Ali-cloud ECS-Disk to ECS +type AttachDisk struct { + InstanceID string + DiskID string + DeleteWithInstance bool +} + +// DetachDisk to store all attribute of detach Ali-cloud ECS-Disk from ECS +type DetachDisk struct { + InstanceID string + DiskID string +} diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index e763432..458f26e 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -62,8 +62,27 @@ func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, return resp, err } -//TODO +// Deletedisk delete ECS-Disk accept map[string]interface{} func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, err error) { + var options DeleteDisk + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "DiskId": + diskID, _ := value.(string) + options.DiskID = diskID + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("DeleteDisk", params, response) + resp = response return resp, err } @@ -77,12 +96,63 @@ func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interfac return resp, err } -//TODO +// Attachdisk attach ECS-Disk to ECS, accept map[string]interface{} func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, err error) { + var options AttachDisk + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "InstanceId": + instanceId, _ := value.(string) + options.InstanceID = instanceId + case "DiskId": + diskID, _ := value.(string) + options.DiskID = diskID + case "DeleteWithInstance": + switch value.(type) { + case bool: + options.DeleteWithInstance = value.(bool) + case string: + options.DeleteWithInstance = value.(string) == "true" || value.(string) == "True" + } + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("AttachDisk", params, response) + resp = response return resp, err } -//TODO +// Detachdisk detach ECS-Disk from ECS, accept map[string]interface{} func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, err error) { + var options AttachDisk + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "InstanceId": + instanceId, _ := value.(string) + options.InstanceID = instanceId + case "DiskId": + diskID, _ := value.(string) + options.DiskID = diskID + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("DetachDisk", params, response) + resp = response return resp, err } diff --git a/storage/alistorage/storage_test.go b/storage/alistorage/storage_test.go index 69bec50..c0d07ae 100644 --- a/storage/alistorage/storage_test.go +++ b/storage/alistorage/storage_test.go @@ -11,17 +11,59 @@ func init() { func TestCreatedisk(t *testing.T) { var alistorage Alistorage - createdisk := map[string]interface{}{ + createDisk := map[string]interface{}{ "RegionId": "cn-qingdao", "ZoneId": "cn-qingdao-b", "Size": 20, "DiskName": "ThisIsDiskName", "Description": "ThisIsDescription", } - _, err := alistorage.Createdisk(createdisk) + _, err := alistorage.Createdisk(createDisk) if err != nil { - t.Errorf("Createdisk Test Fail") + t.Errorf("CreateDisk Test Fail") return } t.Logf("Ali disk is created successfully.") } + +func TestDeletedisk(t *testing.T) { + var alistorage Alistorage + deleteDisk := map[string]interface{}{ + "DiskId": "d-m5e2g66ws9m7r00ux87h", + } + _, err := alistorage.Deletedisk(deleteDisk) + if err != nil { + t.Errorf("DeleteDisk Test Fail") + return + } + t.Logf("Ali disk is deleted successfully.") +} + +func TestAttachdisk(t *testing.T) { + var alistorage Alistorage + attachDisk := map[string]interface{}{ + "InstanceId": "i-m5e1lploaf58bddf0gah", + "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + "DeleteWithInstance": false, + } + _, err := alistorage.Attachdisk(attachDisk) + if err != nil { + t.Errorf("AttachDisk Test Fail") + return + } + t.Logf("Ali disk is attached successfully.") +} + +func TestDetachdisk(t *testing.T) { + var alistorage Alistorage + detachDisk := map[string]interface{}{ + "InstanceId": "i-m5e1lploaf58bddf0gah", + "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + } + _, err := alistorage.Detachdisk(detachDisk) + if err != nil { + t.Errorf("DetachDisk Test Fail") + return + } + t.Logf("Ali disk is detached successfully.") +} From 7dc3de7a8574d093f7bde83bb98fed758ee34354 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 1 Apr 2018 22:10:43 +0800 Subject: [PATCH 15/38] Ali storage: improve code suggested by codacy --- storage/alistorage/models.go | 2 +- storage/alistorage/storage.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/storage/alistorage/models.go b/storage/alistorage/models.go index 899c196..b89f740 100644 --- a/storage/alistorage/models.go +++ b/storage/alistorage/models.go @@ -13,7 +13,7 @@ type CreateDisk struct { Encrypted bool DiskCategory string Size int - SnapshotId string + SnapshotID string ClientToken string } diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 458f26e..0f4f221 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -45,8 +45,8 @@ func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, options.Size, _ = strconv.Atoi(value.(string)) } case "SnapshotId": - snapshotId, _ := value.(string) - options.SnapshotId = snapshotId + snapshotID, _ := value.(string) + options.SnapshotID = snapshotID case "ClientToken": clientToken, _ := value.(string) options.ClientToken = clientToken @@ -107,8 +107,8 @@ func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, for key, value := range param { switch key { case "InstanceId": - instanceId, _ := value.(string) - options.InstanceID = instanceId + instanceID, _ := value.(string) + options.InstanceID = instanceID case "DiskId": diskID, _ := value.(string) options.DiskID = diskID From 95740c5c838eb7c9a58131a30bec3a4e685833e0 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 1 Apr 2018 22:16:59 +0800 Subject: [PATCH 16/38] Ali storage: improve code suggested by codacy --- storage/alistorage/storage.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 0f4f221..9bb8e3e 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -141,8 +141,8 @@ func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, for key, value := range param { switch key { case "InstanceId": - instanceId, _ := value.(string) - options.InstanceID = instanceId + instanceID, _ := value.(string) + options.InstanceID = instanceID case "DiskId": diskID, _ := value.(string) options.DiskID = diskID From 177b5ea9e8e67c33ad403c1df00749d42e469a2d Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 1 Apr 2018 22:18:50 +0800 Subject: [PATCH 17/38] Ali storage: improve code suggested by codacy --- storage/alistorage/storage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 9bb8e3e..298d627 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -86,7 +86,7 @@ func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, return resp, err } -//TODO +// Createsnapshot func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interface{}, err error) { return resp, err } From eb905cd213ce2a393e73cf765f365c7a0b045678 Mon Sep 17 00:00:00 2001 From: oddcn Date: Sun, 1 Apr 2018 22:22:41 +0800 Subject: [PATCH 18/38] Ali storage: improve code suggested by codacy --- storage/alistorage/storage.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 298d627..de682de 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -86,12 +86,12 @@ func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, return resp, err } -// Createsnapshot +// Createsnapshot create snapshot accept map[string]interface{} func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interface{}, err error) { return resp, err } -//TODO +// Deletesnapshot delete snapshot accept map[string]interface{} func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interface{}, err error) { return resp, err } From bde7be5701072f0e07c83021c28d6b91e43ef940 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Thu, 3 May 2018 01:34:21 +0800 Subject: [PATCH 19/38] builder pattern: ali cloud create node --- compute/ecs/instance_test.go | 19 ++++++ compute/ecs/models.go | 108 ++++++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index 39f72b1..5e96220 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -26,6 +26,25 @@ func TestCreatenode(t *testing.T) { } t.Logf("Ali node is created successfully.") } +func TestCreateNodeBuilder(t *testing.T) { + var aliEcs ECS + createNode, err := NewCreateNodeBuilder(). + RegionID("cn-qingdao"). + ImageID("centos_7_04_64_20G_alibase_201701015.vhd"). + InstanceType("ecs.xn4.small"). + SecurityGroupID("sg-m5egbo9s5xb21kpu6nk2"). + Build() + if err != nil { + t.Errorf("Createnode Test Fail: %s", err.Error()) + return + } + _, err = aliEcs.Createnode(createNode) + if err != nil { + t.Errorf("Createnode Test Fail") + return + } + t.Logf("Ali node is created successfully.") +} func TestStartnode(t *testing.T) { var aliEcs ECS diff --git a/compute/ecs/models.go b/compute/ecs/models.go index 0e7e9c3..f921227 100644 --- a/compute/ecs/models.go +++ b/compute/ecs/models.go @@ -1,5 +1,12 @@ package ecs +import ( + "github.com/cloudlibz/gocloud/aliauth" + "errors" +) + +const errCommon = "miss required parameter: " + // CreateInstance to store all attribute to create Ali-cloud ECS instance type CreateInstance struct { RegionID string @@ -29,8 +36,8 @@ type StartInstance struct { // StopInstance to store all attribute to Stop Ali-cloud ECS instance type StopInstance struct { - InstanceID string - ForceStop bool + InstanceID string + ForceStop bool ConfirmStop bool StoppedMode string } @@ -45,3 +52,100 @@ type RebootInstance struct { type DeleteInstance struct { InstanceID string } + +type CreateNodeBuilder struct { + params *CreateInstance +} + +func NewCreateNodeBuilder() *CreateNodeBuilder { + createInstance := &CreateInstance{} + createNodeBuilder := &CreateNodeBuilder{params: createInstance} + return createNodeBuilder +} +func (b *CreateNodeBuilder) RegionID(regionID string) *CreateNodeBuilder { + b.params.RegionID = regionID + return b +} +func (b *CreateNodeBuilder) ImageID(imageID string) *CreateNodeBuilder { + b.params.ImageID = imageID + return b +} +func (b *CreateNodeBuilder) InstanceType(instanceType string) *CreateNodeBuilder { + b.params.InstanceType = instanceType + return b +} +func (b *CreateNodeBuilder) SecurityGroupID(securityGroupID string) *CreateNodeBuilder { + b.params.SecurityGroupID = securityGroupID + return b +} +func (b *CreateNodeBuilder) ZoneID(zoneID string) *CreateNodeBuilder { + b.params.ZoneID = zoneID + return b +} +func (b *CreateNodeBuilder) InstanceName(instanceName string) *CreateNodeBuilder { + b.params.InstanceName = instanceName + return b +} +func (b *CreateNodeBuilder) Description(description string) *CreateNodeBuilder { + b.params.Description = description + return b +} +func (b *CreateNodeBuilder) InternetChargeType(internetChargeType string) *CreateNodeBuilder { + b.params.InternetChargeType = internetChargeType + return b +} +func (b *CreateNodeBuilder) InternetMaxBandwidthIn(internetMaxBandwidthIn int) *CreateNodeBuilder { + b.params.InternetMaxBandwidthIn = internetMaxBandwidthIn + return b +} +func (b *CreateNodeBuilder) InternetMaxBandwidthOut(internetMaxBandwidthOut int) *CreateNodeBuilder { + b.params.InternetMaxBandwidthOut = internetMaxBandwidthOut + return b +} +func (b *CreateNodeBuilder) HostName(hostName string) *CreateNodeBuilder { + b.params.HostName = hostName + return b +} +func (b *CreateNodeBuilder) Password(password string) *CreateNodeBuilder { + b.params.Password = password + return b +} +func (b *CreateNodeBuilder) IoOptimized(ioOptimized string) *CreateNodeBuilder { + b.params.IoOptimized = ioOptimized + return b +} +func (b *CreateNodeBuilder) SystemDiskCategory(systemDiskCategory string) *CreateNodeBuilder { + b.params.SystemDiskCategory = systemDiskCategory + return b +} +func (b *CreateNodeBuilder) SystemDiskSize(systemDiskSize string) *CreateNodeBuilder { + b.params.SystemDiskSize = systemDiskSize + return b +} +func (b *CreateNodeBuilder) SystemDiskName(systemDiskName string) *CreateNodeBuilder { + b.params.SystemDiskName = systemDiskName + return b +} +func (b *CreateNodeBuilder) SystemDiskDescription(systemDiskDescription string) *CreateNodeBuilder { + b.params.SystemDiskDescription = systemDiskDescription + return b +} + +func (b *CreateNodeBuilder) Build() (map[string]interface{}, error) { + if b.params.RegionID == "" { + return nil, errors.New(errCommon + "RegionID") + } + if b.params.ImageID == "" { + return nil, errors.New(errCommon + "ImageID") + } + if b.params.InstanceType == "" { + return nil, errors.New(errCommon + "InstanceType") + } + if b.params.SecurityGroupID == "" { + return nil, errors.New(errCommon + "SecurityGroupID") + } + params := make(map[string]interface{}) + // Put all of options into params + params = aliauth.PutStructToMap(b.params) + return params, nil +} From 8716c1b8a68252788b1f5deaa4a33e57eb7c5951 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 6 May 2018 00:56:32 +0800 Subject: [PATCH 20/38] added ali ecs and storage APIdocs --- APIDocs/ali.md | 17 +++++ APIDocs/aliauth.md | 46 ++++++++++++ APIDocs/alistorage.md | 83 +++++++++++++++++++++ APIDocs/ecs.md | 141 ++++++++++++++++++++++++++++++++++++ examples/compute/ecs/ecs.md | 19 ++++- 5 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 APIDocs/ali.md create mode 100644 APIDocs/aliauth.md create mode 100644 APIDocs/alistorage.md create mode 100644 APIDocs/ecs.md diff --git a/APIDocs/ali.md b/APIDocs/ali.md new file mode 100644 index 0000000..e72dde8 --- /dev/null +++ b/APIDocs/ali.md @@ -0,0 +1,17 @@ +``` +package ali + import "/home/oddcn/Code/go/src/github.com/cloudlibz/gocloud/ali" +``` + +### TYPES + +``` +type Ali struct { + ecs.ECS + alistorage.Alistorage + aliloadbalancer.Aliloadbalancer + alicontainer.Alicontainer + alidns.Alidns +} + Ali struct represents Ali-cloud provider. +``` diff --git a/APIDocs/aliauth.md b/APIDocs/aliauth.md new file mode 100644 index 0000000..57f756d --- /dev/null +++ b/APIDocs/aliauth.md @@ -0,0 +1,46 @@ +``` +package aliauth + import "/home/oddcn/Code/go/src/github.com/cloudlibz/gocloud/aliauth" +``` + +### FUNCTIONS + +``` +func LoadConfig() +``` + + LoadConfig represents Load Ali-cloud config from alicloudconfig.json or environment variables + +``` +func PutStructToMap(i interface{}) map[string]interface{} +``` + + PutStructToMap puts key and value of struct into map[string]interface{} + + > if value is string and not empty -> put + > if value is bool -> put NOTE: the default value of origin Ali API 's parameter must be false, if not ,do not use this function + > if value is int and not 0 -> put NOTE: the optional values of origin Ali API 's parameter must dose not include 0, if not ,do not use this function + +``` +func SignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error +``` + + SignAndDoRequest sign and do request by action parameter and specific parameters + +### TYPES + +``` +type Configuration struct { + AliAccessKeyID string + AliAccessKeySecret string +} +``` + + Configuration struct for representing Ali-cloud credentials + +``` +var Config Configuration +``` + + Config from alicloudconfig.json + diff --git a/APIDocs/alistorage.md b/APIDocs/alistorage.md new file mode 100644 index 0000000..901fc1f --- /dev/null +++ b/APIDocs/alistorage.md @@ -0,0 +1,83 @@ +``` +package alistorage + import "/home/oddcn/Code/go/src/github.com/cloudlibz/gocloud/storage/alistorage/" +``` + +### TYPES + +``` +type Alistorage struct { +} +``` + Alistorage struct represents Alistorage attribute and method associates with it. + +``` +func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, err error) +``` + Attachdisk attach ECS-Disk to ECS, accept map[string]interface{} + +``` +func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, err error) +``` + Createdisk create ECS-Disk accept map[string]interface{} + +``` +func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interface{}, err error) +``` + Createsnapshot create snapshot accept map[string]interface{} + +``` +func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, err error) +``` + Deletedisk delete ECS-Disk accept map[string]interface{} + +``` +func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interface{}, err error) +``` + Deletesnapshot delete snapshot accept map[string]interface{} + +``` +func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, err error) +``` + Detachdisk detach ECS-Disk from ECS, accept map[string]interface{} + +``` +type AttachDisk struct { + InstanceID string + DiskID string + DeleteWithInstance bool +} +``` + AttachDisk to store all attribute of attach Ali-cloud ECS-Disk to ECS + +``` +type CreateDisk struct { + RegionID string + ZoneID string + DiskName string + Description string + Encrypted bool + DiskCategory string + Size int + SnapshotID string + ClientToken string +} +``` + CreateDisk to store all attribute to create Ali-cloud ECS-Disk + +``` +type DeleteDisk struct { + DiskID string +} +``` + DeleteDisk to store all attribute to delete Ali-cloud ECS-Disk + +``` +type DetachDisk struct { + InstanceID string + DiskID string +} +``` + DetachDisk to store all attribute of detach Ali-cloud ECS-Disk from ECS + + diff --git a/APIDocs/ecs.md b/APIDocs/ecs.md new file mode 100644 index 0000000..0426c42 --- /dev/null +++ b/APIDocs/ecs.md @@ -0,0 +1,141 @@ +``` +package ecs + import "/home/oddcn/Code/go/src/github.com/cloudlibz/gocloud/compute/ecs" +``` + +### TYPES + +``` +type CreateInstance struct { + RegionID string + ZoneID string + ImageID string + InstanceType string + SecurityGroupID string + InstanceName string + Description string + InternetChargeType string + InternetMaxBandwidthIn int + InternetMaxBandwidthOut int + HostName string + Password string + IoOptimized string + SystemDiskCategory string + SystemDiskSize string + SystemDiskName string + SystemDiskDescription string +} +``` + + CreateInstance to store all attribute to create Ali-cloud ECS instance + +``` +type CreateNodeBuilder struct { + // contains filtered or unexported fields +} +``` + +``` +func NewCreateNodeBuilder() *CreateNodeBuilder + +func (b *CreateNodeBuilder) Build() (map[string]interface{}, error) + +func (b *CreateNodeBuilder) Description(description string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) HostName(hostName string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) ImageID(imageID string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) InstanceName(instanceName string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) InstanceType(instanceType string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) InternetChargeType(internetChargeType string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) InternetMaxBandwidthIn(internetMaxBandwidthIn int) *CreateNodeBuilder + +func (b *CreateNodeBuilder) InternetMaxBandwidthOut(internetMaxBandwidthOut int) *CreateNodeBuilder + +func (b *CreateNodeBuilder) IoOptimized(ioOptimized string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) Password(password string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) RegionID(regionID string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) SecurityGroupID(securityGroupID string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) SystemDiskCategory(systemDiskCategory string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) SystemDiskDescription(systemDiskDescription string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) SystemDiskName(systemDiskName string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) SystemDiskSize(systemDiskSize string) *CreateNodeBuilder + +func (b *CreateNodeBuilder) ZoneID(zoneID string) *CreateNodeBuilder +``` + +``` +type DeleteInstance struct { + InstanceID string +} +``` + DeleteInstance to store all attribute to Delete Ali-cloud ECS instance + +``` +type ECS struct { +} +``` + Ali ECS struct + +``` +func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) +``` + Createnode create ECS instances accept map[string]interface{} + +``` +func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) +``` + Deletenode delete ECS instances accept map[string]interface{} + +``` +func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) +``` + Rebootnode reboot ECS instances accept map[string]interface{} + +``` +func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) +``` + Startnode start ECS instances accept map[string]interface{} + +``` +func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) +``` + Stopnode stop ECS instances accept map[string]interface{} + +``` +type RebootInstance struct { + InstanceID string + ForceStop bool +} +``` + RebootInstance to store all attribute to Reboot Ali-cloud ECS instance + +``` +type StartInstance struct { + InstanceID string + InitLocalDisk bool +} +``` + StartInstance to store all attribute to start Ali-cloud ECS instance + +``` +type StopInstance struct { + InstanceID string + ForceStop bool + ConfirmStop bool + StoppedMode string +} +``` + StopInstance to store all attribute to Stop Ali-cloud ECS instance + diff --git a/examples/compute/ecs/ecs.md b/examples/compute/ecs/ecs.md index 251811b..116f79d 100644 --- a/examples/compute/ecs/ecs.md +++ b/examples/compute/ecs/ecs.md @@ -10,7 +10,7 @@ Create `alicloudconfig.json` as follows, } ``` -also You can setup enviroment variables as +also You can setup environment variables as ```js export AliAccessKeyID = "xxxxxxxxxxxx" @@ -40,6 +40,23 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) fmt.Println(response["body"]) ``` +or + +``` + createNode, err := ecs.NewCreateNodeBuilder(). + RegionID("cn-qingdao"). + ImageID("centos_7_04_64_20G_alibase_201701015.vhd"). + InstanceType("ecs.xn4.small"). + SecurityGroupID("sg-m5egbo9s5xb21kpu6nk2"). + Build() + if err != nil { + //... + } + resp, err := alicloud.Createnode(createNode) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + ### Start instance ```js From 92eba13dbd0496d2586e07353b8dfe40ab268ff1 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 6 May 2018 01:00:10 +0800 Subject: [PATCH 21/38] added ali ecs and storage APIdocs --- APIDocs/ali.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/APIDocs/ali.md b/APIDocs/ali.md index e72dde8..6cdfe06 100644 --- a/APIDocs/ali.md +++ b/APIDocs/ali.md @@ -13,5 +13,6 @@ type Ali struct { alicontainer.Alicontainer alidns.Alidns } - Ali struct represents Ali-cloud provider. ``` + Ali struct represents Ali-cloud provider. + From b6fe4f0ebb11f13114bbbceff780dbfc5bfcf62f Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 6 May 2018 01:02:21 +0800 Subject: [PATCH 22/38] added ali ecs and storage APIdocs --- APIDocs/ali.md | 3 ++- APIDocs/aliauth.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/APIDocs/ali.md b/APIDocs/ali.md index 6cdfe06..e3aca57 100644 --- a/APIDocs/ali.md +++ b/APIDocs/ali.md @@ -14,5 +14,6 @@ type Ali struct { alidns.Alidns } ``` - Ali struct represents Ali-cloud provider. + +Ali struct represents Ali-cloud provider. diff --git a/APIDocs/aliauth.md b/APIDocs/aliauth.md index 57f756d..c9e09a5 100644 --- a/APIDocs/aliauth.md +++ b/APIDocs/aliauth.md @@ -9,7 +9,7 @@ package aliauth func LoadConfig() ``` - LoadConfig represents Load Ali-cloud config from alicloudconfig.json or environment variables +LoadConfig represents Load Ali-cloud config from alicloudconfig.json or environment variables ``` func PutStructToMap(i interface{}) map[string]interface{} From 25e36bf31f64968e2367cc742a5fd22ec5975a11 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 6 May 2018 01:03:36 +0800 Subject: [PATCH 23/38] added ali ecs and storage APIdocs --- APIDocs/aliauth.md | 14 +++++++------- APIDocs/alistorage.md | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/APIDocs/aliauth.md b/APIDocs/aliauth.md index c9e09a5..b1601a8 100644 --- a/APIDocs/aliauth.md +++ b/APIDocs/aliauth.md @@ -15,17 +15,17 @@ LoadConfig represents Load Ali-cloud config from alicloudconfig.json or environm func PutStructToMap(i interface{}) map[string]interface{} ``` - PutStructToMap puts key and value of struct into map[string]interface{} +PutStructToMap puts key and value of struct into map[string]interface{} - > if value is string and not empty -> put - > if value is bool -> put NOTE: the default value of origin Ali API 's parameter must be false, if not ,do not use this function - > if value is int and not 0 -> put NOTE: the optional values of origin Ali API 's parameter must dose not include 0, if not ,do not use this function +> if value is string and not empty -> put +> if value is bool -> put NOTE: the default value of origin Ali API 's parameter must be false, if not ,do not use this function +> if value is int and not 0 -> put NOTE: the optional values of origin Ali API 's parameter must dose not include 0, if not ,do not use this function ``` func SignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error ``` - SignAndDoRequest sign and do request by action parameter and specific parameters +SignAndDoRequest sign and do request by action parameter and specific parameters ### TYPES @@ -36,11 +36,11 @@ type Configuration struct { } ``` - Configuration struct for representing Ali-cloud credentials +Configuration struct for representing Ali-cloud credentials ``` var Config Configuration ``` - Config from alicloudconfig.json +Config from alicloudconfig.json diff --git a/APIDocs/alistorage.md b/APIDocs/alistorage.md index 901fc1f..50f7f0c 100644 --- a/APIDocs/alistorage.md +++ b/APIDocs/alistorage.md @@ -9,12 +9,12 @@ package alistorage type Alistorage struct { } ``` - Alistorage struct represents Alistorage attribute and method associates with it. +Alistorage struct represents Alistorage attribute and method associates with it. ``` func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, err error) ``` - Attachdisk attach ECS-Disk to ECS, accept map[string]interface{} +Attachdisk attach ECS-Disk to ECS, accept map[string]interface{} ``` func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, err error) From 2cc61f51c2d10f1e0a45c55802d73bc234fc5f01 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 6 May 2018 01:08:46 +0800 Subject: [PATCH 24/38] added ali ecs and storage APIdocs --- APIDocs/alistorage.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/APIDocs/alistorage.md b/APIDocs/alistorage.md index 50f7f0c..b953069 100644 --- a/APIDocs/alistorage.md +++ b/APIDocs/alistorage.md @@ -19,27 +19,27 @@ Attachdisk attach ECS-Disk to ECS, accept map[string]interface{} ``` func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, err error) ``` - Createdisk create ECS-Disk accept map[string]interface{} +Createdisk create ECS-Disk accept map[string]interface{} ``` func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interface{}, err error) ``` - Createsnapshot create snapshot accept map[string]interface{} +Createsnapshot create snapshot accept map[string]interface{} ``` func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, err error) ``` - Deletedisk delete ECS-Disk accept map[string]interface{} +Deletedisk delete ECS-Disk accept map[string]interface{} ``` func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interface{}, err error) ``` - Deletesnapshot delete snapshot accept map[string]interface{} +Deletesnapshot delete snapshot accept map[string]interface{} ``` func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, err error) ``` - Detachdisk detach ECS-Disk from ECS, accept map[string]interface{} +Detachdisk detach ECS-Disk from ECS, accept map[string]interface{} ``` type AttachDisk struct { @@ -48,7 +48,7 @@ type AttachDisk struct { DeleteWithInstance bool } ``` - AttachDisk to store all attribute of attach Ali-cloud ECS-Disk to ECS +AttachDisk to store all attribute of attach Ali-cloud ECS-Disk to ECS ``` type CreateDisk struct { @@ -63,14 +63,14 @@ type CreateDisk struct { ClientToken string } ``` - CreateDisk to store all attribute to create Ali-cloud ECS-Disk +CreateDisk to store all attribute to create Ali-cloud ECS-Disk ``` type DeleteDisk struct { DiskID string } ``` - DeleteDisk to store all attribute to delete Ali-cloud ECS-Disk +DeleteDisk to store all attribute to delete Ali-cloud ECS-Disk ``` type DetachDisk struct { @@ -78,6 +78,6 @@ type DetachDisk struct { DiskID string } ``` - DetachDisk to store all attribute of detach Ali-cloud ECS-Disk from ECS +DetachDisk to store all attribute of detach Ali-cloud ECS-Disk from ECS From 676dd0d8259144ae26153df16ac3ba10b36000ac Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Tue, 8 May 2018 22:32:39 +0800 Subject: [PATCH 25/38] little change in ali compute unit test --- compute/ecs/instance_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index 5e96220..e7d2cdb 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -21,7 +21,7 @@ func TestCreatenode(t *testing.T) { } _, err := aliEcs.Createnode(create) if err != nil { - t.Errorf("Createnode Test Fail") + t.Errorf("Createnode Test Fail: %s", err) return } t.Logf("Ali node is created successfully.") @@ -40,7 +40,7 @@ func TestCreateNodeBuilder(t *testing.T) { } _, err = aliEcs.Createnode(createNode) if err != nil { - t.Errorf("Createnode Test Fail") + t.Errorf("Createnode Test Fail: %s", err) return } t.Logf("Ali node is created successfully.") From 792b4bab8180cc3f2058151d0de0122f47da8efe Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Tue, 8 May 2018 22:53:16 +0800 Subject: [PATCH 26/38] implement createsnapshot deletesnapshot --- storage/alistorage/models.go | 13 +++++++++ storage/alistorage/storage.go | 47 ++++++++++++++++++++++++++++++ storage/alistorage/storage_test.go | 31 ++++++++++++++++++-- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/storage/alistorage/models.go b/storage/alistorage/models.go index b89f740..cd63874 100644 --- a/storage/alistorage/models.go +++ b/storage/alistorage/models.go @@ -34,3 +34,16 @@ type DetachDisk struct { InstanceID string DiskID string } + +// CreateSnapshot to store all attribute of create Ali-cloud ECS-Disk 's Snapshot +type CreateSnapshot struct { + DiskID string + SnapshotName string + Description string + ClientToken string +} + +// DeleteSnapshot to store all attribute of delete Ali-cloud ECS-Disk 's Snapshot +type DeleteSnapshot struct { + SnapshotID string +} diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index de682de..b88ca8e 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -88,11 +88,58 @@ func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, // Createsnapshot create snapshot accept map[string]interface{} func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interface{}, err error) { + var options CreateSnapshot + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "DiskId": + diskID, _ := value.(string) + options.DiskID = diskID + case "SnapshotName": + snapshotName, _ := value.(string) + options.SnapshotName = snapshotName + case "Description": + description, _ := value.(string) + options.Description = description + case "ClientToken": + clientToken, _ := value.(string) + options.ClientToken = clientToken + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("CreateSnapshot", params, response) + resp = response return resp, err } // Deletesnapshot delete snapshot accept map[string]interface{} func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interface{}, err error) { + var options DeleteSnapshot + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "SnapshotId": + snapshotID, _ := value.(string) + options.SnapshotID = snapshotID + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("DeleteSnapshot", params, response) + resp = response return resp, err } diff --git a/storage/alistorage/storage_test.go b/storage/alistorage/storage_test.go index c0d07ae..3b310fb 100644 --- a/storage/alistorage/storage_test.go +++ b/storage/alistorage/storage_test.go @@ -57,8 +57,8 @@ func TestAttachdisk(t *testing.T) { func TestDetachdisk(t *testing.T) { var alistorage Alistorage detachDisk := map[string]interface{}{ - "InstanceId": "i-m5e1lploaf58bddf0gah", - "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + "InstanceId": "i-m5e1lploaf58bddf0gah", + "DiskId": "d-m5edwiwlyyn7bwz6cdd4", } _, err := alistorage.Detachdisk(detachDisk) if err != nil { @@ -67,3 +67,30 @@ func TestDetachdisk(t *testing.T) { } t.Logf("Ali disk is detached successfully.") } + +func TestCreatesnapshot(t *testing.T) { + var alistorage Alistorage + createsnapshot := map[string]interface{}{ + "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + "SnapshotName": "ThisIsSnapshotName", + } + _, err := alistorage.Createsnapshot(createsnapshot) + if err != nil { + t.Errorf("CreateSnapshot Test Fail: %s", err) + return + } + t.Logf("Ali disk snapshot is created successfully.") +} + +func TestDeletesnapshot(t *testing.T) { + var alistorage Alistorage + deletesnapshot := map[string]interface{}{ + "SnapshotId": "s-923FE2BF0", + } + _, err := alistorage.Deletesnapshot(deletesnapshot) + if err != nil { + t.Errorf("DeleteSnapshot Test Fail: %s", err) + return + } + t.Logf("Ali disk snapshot is deleted successfully.") +} From c1057c41821521e303beeedd20c67541cab90bd1 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Tue, 8 May 2018 23:13:06 +0800 Subject: [PATCH 27/38] update apidocs 's ali storage --- APIDocs/alistorage.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/APIDocs/alistorage.md b/APIDocs/alistorage.md index b953069..fc17530 100644 --- a/APIDocs/alistorage.md +++ b/APIDocs/alistorage.md @@ -80,4 +80,18 @@ type DetachDisk struct { ``` DetachDisk to store all attribute of detach Ali-cloud ECS-Disk from ECS +``` +type DeleteSnapshot struct { + SnapshotID string +} +``` +DeleteSnapshot to store all attribute of delete Ali-cloud ECS-Disk 's Snapshot + +``` +type DetachDisk struct { + InstanceID string + DiskID string +} +``` +DetachDisk to store all attribute of detach Ali-cloud ECS-Disk from ECS From 25b4fb308a5bafa0dda25486326490455f524fd2 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Thu, 10 May 2018 00:18:29 +0800 Subject: [PATCH 28/38] Added examples of ali storage --- examples/storage/alistorage/alistorage.md | 106 ++++++++++++++++++++++ storage/alistorage/storage_test.go | 4 +- 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 examples/storage/alistorage/alistorage.md diff --git a/examples/storage/alistorage/alistorage.md b/examples/storage/alistorage/alistorage.md new file mode 100644 index 0000000..034d417 --- /dev/null +++ b/examples/storage/alistorage/alistorage.md @@ -0,0 +1,106 @@ +# gocloud storage - ECS + +## Configure Ali-cloud credentials + +Create `alicloudconfig.json` as follows, +```js +{ + "AliAccessKeyID": "xxxxxxxxxxxx", + "AliAccessKeySecret": "xxxxxxxxxxxx" +} +``` + +also You can setup environment variables as + +```js +export AliAccessKeyID = "xxxxxxxxxxxx" +export AliAccessKeySecret = "xxxxxxxxxxxx" +``` + +## Initialize library + +```js +import "github.com/cloudlibz/gocloud/gocloud" + +alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) +``` + +### Create disk + +```js + createDisk := map[string]interface{}{ + "RegionId": "cn-qingdao", + "ZoneId": "cn-qingdao-b", + "Size": 20, + "DiskName": "ThisIsDiskName", + "Description": "ThisIsDescription", + } + + resp, err := alicloud.Createdisk(createDisk) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Delete disk + +```js + deleteDisk := map[string]interface{}{ + "DiskId": "d-m5e2g66ws9m7r00ux87h", + } + + resp, err := alicloud.Deletedisk(deleteDisk) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Attach disk + +```js + attachDisk := map[string]interface{}{ + "InstanceId": "i-m5e1lploaf58bddf0gah", + "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + "DeleteWithInstance": false, + } + + resp, err := alicloud.Attachdisk(attachDisk) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Detach disk + +```js + detachDisk := map[string]interface{}{ + "InstanceId": "i-m5e1lploaf58bddf0gah", + "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + } + + resp, err := alicloud.Detachdisk(detachDisk) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Create snapshot + +```js + createsnapshot := map[string]interface{}{ + "DiskId": "d-m5e7k6ycnx8b5zzsm0yp", + "SnapshotName": "ThisIsSnapshotName", + } + + resp, err := alicloud.Createsnapshot(createsnapshot) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Delete snapshot + +```js + deletesnapshot := map[string]interface{}{ + "SnapshotId": "s-m5eave3s6oufpctcxynu", + } + + resp, err := alicloud.Deletesnapshot(deletesnapshot) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` diff --git a/storage/alistorage/storage_test.go b/storage/alistorage/storage_test.go index 3b310fb..59d9cbf 100644 --- a/storage/alistorage/storage_test.go +++ b/storage/alistorage/storage_test.go @@ -71,7 +71,7 @@ func TestDetachdisk(t *testing.T) { func TestCreatesnapshot(t *testing.T) { var alistorage Alistorage createsnapshot := map[string]interface{}{ - "DiskId": "d-m5edwiwlyyn7bwz6cdd4", + "DiskId": "d-m5e7k6ycnx8b5zzsm0yp", "SnapshotName": "ThisIsSnapshotName", } _, err := alistorage.Createsnapshot(createsnapshot) @@ -85,7 +85,7 @@ func TestCreatesnapshot(t *testing.T) { func TestDeletesnapshot(t *testing.T) { var alistorage Alistorage deletesnapshot := map[string]interface{}{ - "SnapshotId": "s-923FE2BF0", + "SnapshotId": "s-m5eave3s6oufpctcxynu", } _, err := alistorage.Deletesnapshot(deletesnapshot) if err != nil { From 75ec14fab4fa2052f6e16291e773e23f42dc0da8 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Fri, 11 May 2018 23:29:03 +0800 Subject: [PATCH 29/38] implemented ali dns module --- dns/alidns/dns.go | 156 ++++++++++++++++++++++++++++++++++++++--- dns/alidns/dns_test.go | 73 +++++++++++++++++++ dns/alidns/models.go | 30 ++++++++ 3 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 dns/alidns/dns_test.go diff --git a/dns/alidns/dns.go b/dns/alidns/dns.go index 5628677..038af33 100644 --- a/dns/alidns/dns.go +++ b/dns/alidns/dns.go @@ -1,18 +1,156 @@ package alidns -//TODO -func (alidns *Alidns) ListResourcednsRecordSets(request interface{}) (resp interface{}, err error) { +import ( + "github.com/cloudlibz/gocloud/aliauth" + "strconv" +) + +// ListResourcednsRecordSets list resource DNS record sets accept map[string]interface{} +func (alidns *Alidns) ListResourcednsRecordSets(request interface{}) (resp interface{}, err error) { + var options ListResourceDNSRecordSets + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "DomainName": + options.DomainName = value.(string) + case "PageNumber": + switch value.(type) { + case int: + options.PageNumber = value.(int) + case string: + options.PageNumber, _ = strconv.Atoi(value.(string)) + } + case "PageSize": + switch value.(type) { + case int: + options.PageSize = value.(int) + case string: + options.PageSize, _ = strconv.Atoi(value.(string)) + } + case "RRKeyWord": + options.RRKeyWord = value.(string) + case "TypeKeyWord": + options.TypeKeyWord = value.(string) + case "ValueKeyWord": + options.ValueKeyWord = value.(string) + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("DescribeDomainRecords", params, response) + resp = response return resp, err } -//TODO -func (alidns *Alidns) Listdns(request interface{}) (resp interface{}, err error) { + +// Listdns list DNS record accept map[string]interface{} +func (alidns *Alidns) Listdns(request interface{}) (resp interface{}, err error) { + var options ListDNS + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "PageNumber": + switch value.(type) { + case int: + options.PageNumber = value.(int) + case string: + options.PageNumber, _ = strconv.Atoi(value.(string)) + } + case "PageSize": + switch value.(type) { + case int: + options.PageSize = value.(int) + case string: + options.PageSize, _ = strconv.Atoi(value.(string)) + } + case "KeyWord": + options.KeyWord = value.(string) + case "GroupId": + options.GroupId = value.(string) + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("DescribeDomains", params, response) + resp = response return resp, err } -//TODO -func (alidns *Alidns) Deletedns(request interface{}) (resp interface{}, err error) { + +// Deletedns delete DNS record accept map[string]interface{} +func (alidns *Alidns) Deletedns(request interface{}) (resp interface{}, err error) { + var options DeleteDNS + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "RecordId": + options.RecordId = value.(string) + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("DeleteDomainRecord", params, response) + resp = response return resp, err } -//TODO -func (alidns *Alidns) Createdns(request interface{}) (resp interface{}, err error) { + +// Createdns add DNS record accept map[string]interface{} +func (alidns *Alidns) Createdns(request interface{}) (resp interface{}, err error) { + var options CreateDNS + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "DomainName": + options.DomainName = value.(string) + case "RR": + options.RR = value.(string) + case "Type": + options.Type = value.(string) + case "Value": + options.Value = value.(string) + case "TTL": + switch value.(type) { + case int: + options.TTL = value.(int) + case string: + options.TTL, _ = strconv.Atoi(value.(string)) + } + case "Priority": + switch value.(type) { + case int: + options.Priority = value.(int) + case string: + options.Priority, _ = strconv.Atoi(value.(string)) + } + case "Line": + options.Line = value.(string) + } + } + // Put all of options into params + params := aliauth.PutStructToMap(&options) + + response := make(map[string]interface{}) + err = aliauth.SignAndDoRequest("AddDomainRecord", params, response) + resp = response return resp, err -} \ No newline at end of file +} diff --git a/dns/alidns/dns_test.go b/dns/alidns/dns_test.go new file mode 100644 index 0000000..a20ef4b --- /dev/null +++ b/dns/alidns/dns_test.go @@ -0,0 +1,73 @@ +package alidns + +import ( + "github.com/cloudlibz/gocloud/aliauth" + "testing" +) + +func init() { + aliauth.LoadConfig() +} + +func TestCreatedns(t *testing.T) { + var aliDNS Alidns + createDNS := map[string]interface{}{ + "DomainName": "oddcn.cn", + "RR": "gocloud.test", + "Type": "A", + "Value": "202.106.0.20", + "TTL": 600, + } + _, err := aliDNS.Createdns(createDNS) + if err != nil { + t.Errorf("Createdns Test Fail: %s", err) + return + } + t.Logf("Ali DNS is created successfully.") +} + +func TestDeletedns(t *testing.T) { + var aliDNS Alidns + deleteDNS := map[string]interface{}{ + "RecordId": "9999985", + } + _, err := aliDNS.Deletedns(deleteDNS) + if err != nil { + t.Errorf("Deletedns Test Fail: %s", err) + return + } + t.Logf("Ali DNS is deleted successfully.") +} + +func TestListdns(t *testing.T) { + var aliDNS Alidns + listDomain := map[string]interface{}{ + "PageNumber": 1, + "PageSize": 20, + "KeyWord": "com", + "GroupId": "2223", + } + _, err := aliDNS.Listdns(listDomain) + if err != nil { + t.Errorf("Listdns Test Fail: %s", err) + return + } + t.Logf("Ali DNS is listed successfully.") +} +func TestListResourcednsRecordSets(t *testing.T) { + var aliDNS Alidns + listResourcednsRecordSets := map[string]interface{}{ + "DomainName": "oddcn.cn", + "PageNumber": 1, + "PageSize": 20, + "RRKeyWord": "www", + "TypeKeyWord": "MX", + "ValueKeyWord": "com", + } + _, err := aliDNS.ListResourcednsRecordSets(listResourcednsRecordSets) + if err != nil { + t.Errorf("ListResourcednsRecordSets Test Fail: %s", err) + return + } + t.Logf("Ali resource DNS record sets is listed successfully.") +} diff --git a/dns/alidns/models.go b/dns/alidns/models.go index c9a704e..b586f70 100644 --- a/dns/alidns/models.go +++ b/dns/alidns/models.go @@ -3,3 +3,33 @@ package alidns //Alidns represents Alidns attribute and method associates with it. type Alidns struct { } + +type CreateDNS struct { + DomainName string + RR string + Type string + Value string + TTL int + Priority int + Line string +} + +type DeleteDNS struct { + RecordId string +} + +type ListDNS struct { + PageNumber int + PageSize int + KeyWord string + GroupId string +} + +type ListResourceDNSRecordSets struct { + DomainName string + PageNumber int + PageSize int + RRKeyWord string + TypeKeyWord string + ValueKeyWord string +} From d69374371ff05237e591b0fb06808a4ada149404 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 13 May 2018 15:49:57 +0800 Subject: [PATCH 30/38] ali auth sign ECS DNS --- aliauth/sign.go | 37 ++++++++++++++++++++++++++++++----- compute/ecs/instance.go | 10 +++++----- dns/alidns/dns.go | 10 +++++----- storage/alistorage/storage.go | 12 ++++++------ 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index c3c0a94..892a50c 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -17,11 +17,38 @@ import ( const formatISO8601 = "2006-01-02T15:04:05Z" -// SignAndDoRequest sign and do request by action parameter and specific parameters -func SignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { +func DNSSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { // Add common params and action param - params = initParams(action, params) + params["Action"] = action + params["Format"] = "XML" + params["Version"] = "2015-01-09" + params["AccessKeyId"] = Config.AliAccessKeyID + params["Timestamp"] = time.Now().UTC().Format(formatISO8601) + params["SignatureMethod"] = "HMAC-SHA1" + params["SignatureVersion"] = "1.0" + params["SignatureNonce"] = createRandomString() + + err := signAndDoRequest("alidns.aliyuncs.com", params, response) + return err +} + +func ECSSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { + // Add common params and action param + params["Action"] = action + params["Format"] = "XML" + params["Version"] = "2014-05-26" + params["AccessKeyId"] = Config.AliAccessKeyID + params["TimeStamp"] = time.Now().UTC().Format(formatISO8601) + params["SignatureMethod"] = "HMAC-SHA1" + params["SignatureVersion"] = "1.0" + params["SignatureNonce"] = createRandomString() + + err := signAndDoRequest("ecs.aliyuncs.com", params, response) + return err +} +// signAndDoRequest sign and do request by action parameter and specific parameters +func signAndDoRequest(domain string, params map[string]interface{}, response map[string]interface{}) error { // Sort the parameters by key keys := make([]string, len(params)) i := 0 @@ -52,7 +79,7 @@ func SignAndDoRequest(action string, params map[string]interface{}, response map } // Generate the request URL - requestURL := "https://ecs.aliyuncs.com/" + "?" + query.Encode() + "&Signature=" + url.QueryEscape(base64Sign) + requestURL := "https://" + domain + "/?" + query.Encode() + "&Signature=" + url.QueryEscape(base64Sign) httpReq, err := http.NewRequest("GET", requestURL, nil) @@ -72,7 +99,7 @@ func SignAndDoRequest(action string, params map[string]interface{}, response map return err } -func initParams(action string, params map[string]interface{}) map[string]interface{} { +func initECSParams(action string, params map[string]interface{}) map[string]interface{} { params["Action"] = action params["Format"] = "XML" diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index d9b6bb9..b685057 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -35,7 +35,7 @@ func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("StartInstance", params, response) + err = aliauth.ECSSignAndDoRequest("StartInstance", params, response) resp = response return resp, err @@ -80,7 +80,7 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("StopInstance", params, response) + err = aliauth.ECSSignAndDoRequest("StopInstance", params, response) resp = response return resp, err } @@ -114,7 +114,7 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("RebootInstance", params, response) + err = aliauth.ECSSignAndDoRequest("RebootInstance", params, response) resp = response return resp, err } @@ -141,7 +141,7 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { params = aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DeleteInstance", params, response) + err = aliauth.ECSSignAndDoRequest("DeleteInstance", params, response) resp = response return resp, err } @@ -246,7 +246,7 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { } response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("CreateInstance", params, response) + err = aliauth.ECSSignAndDoRequest("CreateInstance", params, response) resp = response return resp, err } diff --git a/dns/alidns/dns.go b/dns/alidns/dns.go index 038af33..93398d2 100644 --- a/dns/alidns/dns.go +++ b/dns/alidns/dns.go @@ -43,7 +43,7 @@ func (alidns *Alidns) ListResourcednsRecordSets(request interface{}) (resp inter params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DescribeDomainRecords", params, response) + err = aliauth.DNSSignAndDoRequest("DescribeDomainRecords", params, response) resp = response return resp, err } @@ -82,7 +82,7 @@ func (alidns *Alidns) Listdns(request interface{}) (resp interface{}, err error) params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DescribeDomains", params, response) + err = aliauth.DNSSignAndDoRequest("DescribeDomains", params, response) resp = response return resp, err } @@ -105,7 +105,7 @@ func (alidns *Alidns) Deletedns(request interface{}) (resp interface{}, err erro params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DeleteDomainRecord", params, response) + err = aliauth.DNSSignAndDoRequest("DeleteDomainRecord", params, response) resp = response return resp, err } @@ -126,7 +126,7 @@ func (alidns *Alidns) Createdns(request interface{}) (resp interface{}, err erro options.RR = value.(string) case "Type": options.Type = value.(string) - case "Value": + case "Value": options.Value = value.(string) case "TTL": switch value.(type) { @@ -150,7 +150,7 @@ func (alidns *Alidns) Createdns(request interface{}) (resp interface{}, err erro params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("AddDomainRecord", params, response) + err = aliauth.DNSSignAndDoRequest("AddDomainRecord", params, response) resp = response return resp, err } diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index b88ca8e..6a515ff 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -57,7 +57,7 @@ func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("CreateDisk", params, response) + err = aliauth.ECSSignAndDoRequest("CreateDisk", params, response) resp = response return resp, err } @@ -81,7 +81,7 @@ func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DeleteDisk", params, response) + err = aliauth.ECSSignAndDoRequest("DeleteDisk", params, response) resp = response return resp, err } @@ -114,7 +114,7 @@ func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interfac params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("CreateSnapshot", params, response) + err = aliauth.ECSSignAndDoRequest("CreateSnapshot", params, response) resp = response return resp, err } @@ -138,7 +138,7 @@ func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interfac params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DeleteSnapshot", params, response) + err = aliauth.ECSSignAndDoRequest("DeleteSnapshot", params, response) resp = response return resp, err } @@ -172,7 +172,7 @@ func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("AttachDisk", params, response) + err = aliauth.ECSSignAndDoRequest("AttachDisk", params, response) resp = response return resp, err } @@ -199,7 +199,7 @@ func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, params := aliauth.PutStructToMap(&options) response := make(map[string]interface{}) - err = aliauth.SignAndDoRequest("DetachDisk", params, response) + err = aliauth.ECSSignAndDoRequest("DetachDisk", params, response) resp = response return resp, err } From 69c949ce4ce68a4076e3cadab27fc3ebfffb1c0b Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 13 May 2018 20:34:57 +0800 Subject: [PATCH 31/38] APIDocs and examples --- APIDocs/alidns.md | 74 ++++++++++++++++++++++++++++++++ dns/alidns/models.go | 4 ++ examples/dns/alidns/alidns.md | 80 +++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 APIDocs/alidns.md create mode 100644 examples/dns/alidns/alidns.md diff --git a/APIDocs/alidns.md b/APIDocs/alidns.md new file mode 100644 index 0000000..4e03fdf --- /dev/null +++ b/APIDocs/alidns.md @@ -0,0 +1,74 @@ +``` +package alidns + import "github.com/cloudlibz/gocloud/dns/alidns" +``` + +### TYPES + +``` +type Alidns struct { +} +``` +Alidns represents Alidns attribute and method associates with it. + +``` +func (alidns *Alidns) Createdns(request interface{}) (resp interface{}, err error) +``` +Createdns add DNS record accept map[string]interface{} + +``` +func (alidns *Alidns) Deletedns(request interface{}) (resp interface{}, err error) +``` +Deletedns delete DNS record accept map[string]interface{} + +``` +func (alidns *Alidns) ListResourcednsRecordSets(request interface{}) (resp interface{}, err error) +``` +ListResourcednsRecordSets list resource DNS record sets accept map[string]interface{} + +``` +func (alidns *Alidns) Listdns(request interface{}) (resp interface{}, err error) +``` +Listdns list DNS record accept map[string]interface{} + +``` +type CreateDNS struct { + DomainName string + RR string + Type string + Value string + TTL int + Priority int + Line string +} +``` +CreateDNS to store all attribute to create Ali-cloud DNS + +``` +type DeleteDNS struct { + RecordId string +} +``` +DeleteDNS to store all attribute to delete Ali-cloud DNS + +``` +type ListDNS struct { + PageNumber int + PageSize int + KeyWord string + GroupId string +} +``` +ListDNS to store all attribute to list Ali-cloud DNS + +``` +type ListResourceDNSRecordSets struct { + DomainName string + PageNumber int + PageSize int + RRKeyWord string + TypeKeyWord string + ValueKeyWord string +} +``` +ListResourceDNSRecordSets to store all attribute to list resource Ali-cloud DNS record sets \ No newline at end of file diff --git a/dns/alidns/models.go b/dns/alidns/models.go index b586f70..005c612 100644 --- a/dns/alidns/models.go +++ b/dns/alidns/models.go @@ -4,6 +4,7 @@ package alidns type Alidns struct { } +// CreateDNS to store all attribute to create Ali-cloud DNS type CreateDNS struct { DomainName string RR string @@ -14,10 +15,12 @@ type CreateDNS struct { Line string } +// DeleteDNS to store all attribute to delete Ali-cloud DNS type DeleteDNS struct { RecordId string } +// ListDNS to store all attribute to list Ali-cloud DNS type ListDNS struct { PageNumber int PageSize int @@ -25,6 +28,7 @@ type ListDNS struct { GroupId string } +// ListResourceDNSRecordSets to store all attribute to list resource Ali-cloud DNS record sets type ListResourceDNSRecordSets struct { DomainName string PageNumber int diff --git a/examples/dns/alidns/alidns.md b/examples/dns/alidns/alidns.md new file mode 100644 index 0000000..2994417 --- /dev/null +++ b/examples/dns/alidns/alidns.md @@ -0,0 +1,80 @@ +# gocloud DNS - Ali-cloud + +## Configure Ali-cloud credentials + +Create `alicloudconfig.json` as follows, +``` +{ + "AliAccessKeyID": "xxxxxxxxxxxx", + "AliAccessKeySecret": "xxxxxxxxxxxx" +} +``` + +also You can setup environment variables as + +``` +export AliAccessKeyID = "xxxxxxxxxxxx" +export AliAccessKeySecret = "xxxxxxxxxxxx" +``` + +## Initialize library + +``` +import "github.com/cloudlibz/gocloud/gocloud" + +alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) +``` + +### Create DNS + +``` + createDNS := map[string]interface{}{ + "DomainName": "oddcn.cn", + "RR": "test.gocloud", + "Type": "A", + "Value": "202.106.0.20", + "TTL": 600, + "Line": "default", + } + + resp, err := alicloud.Createdns(createDNS) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Delete DNS + +``` + deleteDNS := map[string]interface{}{ + "RecordId": "3888946862348288", + } + + resp, err := alicloud.Deletedns(deleteDNS) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### List DNS + +``` + listDNS := map[string]interface{}{ + "PageNumber": 1, + "PageSize": 20, + } + + resp, err := alicloud.Listdns(listDNS) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### List resource DNS record sets + +``` + listResourceDNSRecordSets := map[string]interface{}{ + "DomainName": "oddcn.cn", + } + + resp, err := alicloud.ListResourcednsRecordSets(listResourceDNSRecordSets) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` \ No newline at end of file From 707d128ef8d79b3a9a550158d45cba7ede74439a Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Wed, 16 May 2018 15:20:44 +0800 Subject: [PATCH 32/38] updated README.md --- README.md | 2 ++ aliauth/utils.go | 4 ++-- compute/ecs/instance.go | 8 ++++---- compute/ecs/models.go | 2 +- dns/alidns/dns.go | 8 ++++---- storage/alistorage/storage.go | 12 ++++++------ 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 07a510a..37c9a15 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ Currently, implementations for other cloud providers are being worked on. ### Ali-cloud * ECS Compute [Link to example](examples/compute/ecs/ecs.md) +* ECS Storage [Link to example](examples/storage/alistorage/alistorage.md) +* Alibaba Cloud DNS [Link to example](examples/dns/alidns/alidns.md) ## Installation instructions for Linux (Ubuntu) 1. Install golang. diff --git a/aliauth/utils.go b/aliauth/utils.go index a427f68..3dc0df9 100644 --- a/aliauth/utils.go +++ b/aliauth/utils.go @@ -2,11 +2,11 @@ package aliauth import "reflect" -// PutStructToMap puts key and value of struct into map[string]interface{} +// PutStructIntoMap puts key and value of struct into map[string]interface{} // if value is string and not empty -> put // if value is bool -> put NOTE: the default value of origin Ali API 's parameter must be false, if not ,do not use this function // if value is int and not 0 -> put NOTE: the optional values of origin Ali API 's parameter must dose not include 0, if not ,do not use this function -func PutStructToMap(i interface{}) map[string]interface{} { +func PutStructIntoMap(i interface{}) map[string]interface{} { params := make(map[string]interface{}) e := reflect.ValueOf(i).Elem() typeOfOptions := e.Type() diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index b685057..1d224f2 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -32,7 +32,7 @@ func (ecs *ECS) Startnode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - params = aliauth.PutStructToMap(&options) + params = aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("StartInstance", params, response) @@ -77,7 +77,7 @@ func (ecs *ECS) Stopnode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - params = aliauth.PutStructToMap(&options) + params = aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("StopInstance", params, response) @@ -111,7 +111,7 @@ func (ecs *ECS) Rebootnode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - params = aliauth.PutStructToMap(&options) + params = aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("RebootInstance", params, response) @@ -138,7 +138,7 @@ func (ecs *ECS) Deletenode(request interface{}) (resp interface{}, err error) { params := make(map[string]interface{}) // Put all of options into params - params = aliauth.PutStructToMap(&options) + params = aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("DeleteInstance", params, response) diff --git a/compute/ecs/models.go b/compute/ecs/models.go index f921227..be223bf 100644 --- a/compute/ecs/models.go +++ b/compute/ecs/models.go @@ -146,6 +146,6 @@ func (b *CreateNodeBuilder) Build() (map[string]interface{}, error) { } params := make(map[string]interface{}) // Put all of options into params - params = aliauth.PutStructToMap(b.params) + params = aliauth.PutStructIntoMap(b.params) return params, nil } diff --git a/dns/alidns/dns.go b/dns/alidns/dns.go index 93398d2..176f4c4 100644 --- a/dns/alidns/dns.go +++ b/dns/alidns/dns.go @@ -40,7 +40,7 @@ func (alidns *Alidns) ListResourcednsRecordSets(request interface{}) (resp inter } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.DNSSignAndDoRequest("DescribeDomainRecords", params, response) @@ -79,7 +79,7 @@ func (alidns *Alidns) Listdns(request interface{}) (resp interface{}, err error) } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.DNSSignAndDoRequest("DescribeDomains", params, response) @@ -102,7 +102,7 @@ func (alidns *Alidns) Deletedns(request interface{}) (resp interface{}, err erro } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.DNSSignAndDoRequest("DeleteDomainRecord", params, response) @@ -147,7 +147,7 @@ func (alidns *Alidns) Createdns(request interface{}) (resp interface{}, err erro } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.DNSSignAndDoRequest("AddDomainRecord", params, response) diff --git a/storage/alistorage/storage.go b/storage/alistorage/storage.go index 6a515ff..6d6b875 100644 --- a/storage/alistorage/storage.go +++ b/storage/alistorage/storage.go @@ -54,7 +54,7 @@ func (aliStorage *Alistorage) Createdisk(request interface{}) (resp interface{}, } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("CreateDisk", params, response) @@ -78,7 +78,7 @@ func (aliStorage *Alistorage) Deletedisk(request interface{}) (resp interface{}, } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("DeleteDisk", params, response) @@ -111,7 +111,7 @@ func (aliStorage *Alistorage) Createsnapshot(request interface{}) (resp interfac } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("CreateSnapshot", params, response) @@ -135,7 +135,7 @@ func (aliStorage *Alistorage) Deletesnapshot(request interface{}) (resp interfac } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("DeleteSnapshot", params, response) @@ -169,7 +169,7 @@ func (aliStorage *Alistorage) Attachdisk(request interface{}) (resp interface{}, } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("AttachDisk", params, response) @@ -196,7 +196,7 @@ func (aliStorage *Alistorage) Detachdisk(request interface{}) (resp interface{}, } } // Put all of options into params - params := aliauth.PutStructToMap(&options) + params := aliauth.PutStructIntoMap(&options) response := make(map[string]interface{}) err = aliauth.ECSSignAndDoRequest("DetachDisk", params, response) From df220267b67b26b22fa772d4eb93ae0e277e55e0 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Wed, 16 May 2018 16:44:50 +0800 Subject: [PATCH 33/38] ali load balancer 's create delete list --- aliauth/sign.go | 15 ++ loadbalancer/aliloadbalancer/loadbalancer.go | 132 ++++++++++++++++++ .../aliloadbalancer/loadbalancer_test.go | 57 ++++++++ loadbalancer/aliloadbalancer/models.go | 38 +++++ 4 files changed, 242 insertions(+) create mode 100644 loadbalancer/aliloadbalancer/loadbalancer_test.go diff --git a/aliauth/sign.go b/aliauth/sign.go index 892a50c..31fb23b 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -17,6 +17,21 @@ import ( const formatISO8601 = "2006-01-02T15:04:05Z" +func LoadBalancerSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { + // Add common params and action param + params["Action"] = action + params["Format"] = "XML" + params["Version"] = "2014-05-15" + params["AccessKeyId"] = Config.AliAccessKeyID + params["Timestamp"] = time.Now().UTC().Format(formatISO8601) + params["SignatureMethod"] = "HMAC-SHA1" + params["SignatureVersion"] = "1.0" + params["SignatureNonce"] = createRandomString() + + err := signAndDoRequest("slb.aliyuncs.com", params, response) + return err +} + func DNSSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { // Add common params and action param params["Action"] = action diff --git a/loadbalancer/aliloadbalancer/loadbalancer.go b/loadbalancer/aliloadbalancer/loadbalancer.go index fcbbd9a..2403ecb 100644 --- a/loadbalancer/aliloadbalancer/loadbalancer.go +++ b/loadbalancer/aliloadbalancer/loadbalancer.go @@ -1,21 +1,153 @@ package aliloadbalancer +import ( + "strconv" + "github.com/cloudlibz/gocloud/aliauth" +) + //TODO func (aliloadbalancer *Aliloadbalancer) Creatloadbalancer(request interface{}) (resp interface{}, err error) { + var options CreateLoadBalancer + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "RegionId": + regionID, _ := value.(string) + options.RegionID = regionID + case "MasterZoneId": + masterZoneID, _ := value.(string) + options.MasterZoneID = masterZoneID + case "SlaveZoneId": + slaveZoneID, _ := value.(string) + options.SlaveZoneID = slaveZoneID + case "LoadBalancerName": + loadBalancerName, _ := value.(string) + options.LoadBalancerName = loadBalancerName + case "AddressType": + addressType, _ := value.(string) + options.AddressType = addressType + case "VSwitchId": + VSwitchID, _ := value.(string) + options.VSwitchID = VSwitchID + case "PayType": + payType, _ := value.(string) + options.PayType = payType + case "PricingCycle": + PricingCycle, _ := value.(string) + options.PricingCycle = PricingCycle + case "Duration": + Duration, _ := value.(string) + options.Duration = Duration + case "Autopay": + switch value.(type) { + case bool: + options.Autopay = value.(bool) + case string: + options.Autopay = value.(string) == "true" || value.(string) == "True" + } + case "InternetChargeType": + InternetChargeType, _ := value.(string) + options.InternetChargeType = InternetChargeType + case "Bandwidth": + switch value.(type) { + case int: + options.Bandwidth = value.(int) + case string: + options.Bandwidth, _ = strconv.Atoi(value.(string)) + } + case "ClientToken": + ClientToken, _ := value.(string) + options.ClientToken = ClientToken + case "ResourceGroupID": + ResourceGroupID, _ := value.(string) + options.ResourceGroupID = ResourceGroupID + } + } + + // Put all of options into params + params := aliauth.PutStructIntoMap(&options) + + response := make(map[string]interface{}) + err = aliauth.LoadBalancerSignAndDoRequest("CreateLoadBalancer", params, response) + resp = response return resp, err } + //TODO func (aliloadbalancer *Aliloadbalancer) Deleteloadbalancer(request interface{}) (resp interface{}, err error) { + var options DeleteLoadBalancer + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "RegionId": + regionID, _ := value.(string) + options.RegionID = regionID + case "LoadBalancerId": + LoadBalancerID, _ := value.(string) + options.LoadBalancerID = LoadBalancerID + } + } + + // Put all of options into params + params := aliauth.PutStructIntoMap(&options) + + response := make(map[string]interface{}) + err = aliauth.LoadBalancerSignAndDoRequest("DeleteLoadBalancer", params, response) + resp = response return resp, err } + //TODO func (aliloadbalancer *Aliloadbalancer) Listloadbalancer(request interface{}) (resp interface{}, err error) { + var options ListLoadBalancer + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + params := make(map[string]interface{}) + + for key, value := range param { + switch key { + case "RegionId": + regionID, _ := value.(string) + options.RegionID = regionID + case "LoadBalancerId": + LoadBalancerID, _ := value.(string) + options.LoadBalancerID = LoadBalancerID + default: + switch value.(type) { + case string: + params[key] = value.(string) + case int: + params[key] = strconv.Itoa(value.(int)) + case bool: + params[key] = strconv.FormatBool(value.(bool)) + } + } + } + + // Put all of options into params + params = aliauth.PutStructIntoMap(&options) + + response := make(map[string]interface{}) + err = aliauth.LoadBalancerSignAndDoRequest("DescribeLoadBalancers", params, response) + resp = response return resp, err } + //TODO func (aliloadbalancer *Aliloadbalancer) Detachnodewithloadbalancer(request interface{}) (resp interface{}, err error) { return resp, err } + //TODO func (aliloadbalancer *Aliloadbalancer) Attachnodewithloadbalancer(request interface{}) (resp interface{}, err error) { return resp, err diff --git a/loadbalancer/aliloadbalancer/loadbalancer_test.go b/loadbalancer/aliloadbalancer/loadbalancer_test.go new file mode 100644 index 0000000..bf575a2 --- /dev/null +++ b/loadbalancer/aliloadbalancer/loadbalancer_test.go @@ -0,0 +1,57 @@ +package aliloadbalancer + +import ( + "testing" + "github.com/cloudlibz/gocloud/aliauth" + "fmt" +) + +func init() { + aliauth.LoadConfig() +} + +func TestCreateLoadBalancer(t *testing.T) { + var alilb Aliloadbalancer + create := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerName": "abc", + "AddressType": "internet", + "InternetChargeType": "paybytraffic", + } + _, err := alilb.Creatloadbalancer(create) + if err != nil { + t.Errorf("CreateLoadBalancer Test Fail: %s", err) + return + } + t.Logf("Ali LoadBalancer is created successfully.") +} + +func TestDeleteLoadBalancer(t *testing.T) { + var alilb Aliloadbalancer + delete := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerId": "lb-m5ehdbs3p10a7kmq344je", + } + _, err := alilb.Deleteloadbalancer(delete) + if err != nil { + t.Errorf("DeleteLoadBalancer Test Fail: %s", err) + return + } + t.Logf("Ali LoadBalancer is deleted successfully.") +} + +func TestListLoadBalancer(t *testing.T) { + var alilb Aliloadbalancer + list := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerId": "lb-m5ehdbs3p10a7kmq344je", + } + resp, err := alilb.Listloadbalancer(list) + if err != nil { + t.Errorf("DeleteLoadBalancer Test Fail: %s", err) + return + } + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) + t.Logf("Ali LoadBalancer is listed successfully.") +} diff --git a/loadbalancer/aliloadbalancer/models.go b/loadbalancer/aliloadbalancer/models.go index af292eb..f16afa1 100644 --- a/loadbalancer/aliloadbalancer/models.go +++ b/loadbalancer/aliloadbalancer/models.go @@ -3,3 +3,41 @@ package aliloadbalancer //Aliloadbalancer represents Aliloadbalancer struct. type Aliloadbalancer struct { } + +type CreateLoadBalancer struct { + RegionID string + MasterZoneID string + SlaveZoneID string + LoadBalancerSpec string + LoadBalancerName string + AddressType string + VSwitchID string + PayType string + PricingCycle string + Duration string + Autopay bool + InternetChargeType string + Bandwidth int + ClientToken string + ResourceGroupID string +} +type DeleteLoadBalancer struct { + RegionID string + LoadBalancerID string +} + +type ListLoadBalancer struct { + RegionID string + LoadBalancerID string + LoadBalancerName string + AddressType string + NetworkType string + VpcID string + VswitchID string + Address string + ServerIntranetAddress int + InternetChargeType string + ServerID string + MasterZoneID string + SlaveZoneID string +} From ff78755b5191ce05f7e67bd8c6a2642c7d6bb4d6 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Wed, 16 May 2018 22:55:06 +0800 Subject: [PATCH 34/38] ali load balancer all functions, unit tests, examples --- examples/dns/alidns/alidns.md | 6 +- .../aliloadbalancer/aliloadbalancer.md | 95 +++++++++++++++++++ loadbalancer/aliloadbalancer/loadbalancer.go | 59 +++++++++++- .../aliloadbalancer/loadbalancer_test.go | 34 ++++++- loadbalancer/aliloadbalancer/models.go | 17 ++++ 5 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 examples/loadbalancer/aliloadbalancer/aliloadbalancer.md diff --git a/examples/dns/alidns/alidns.md b/examples/dns/alidns/alidns.md index 2994417..1a2b262 100644 --- a/examples/dns/alidns/alidns.md +++ b/examples/dns/alidns/alidns.md @@ -44,7 +44,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) ### Delete DNS -``` +```js deleteDNS := map[string]interface{}{ "RecordId": "3888946862348288", } @@ -56,7 +56,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) ### List DNS -``` +```js listDNS := map[string]interface{}{ "PageNumber": 1, "PageSize": 20, @@ -69,7 +69,7 @@ alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) ### List resource DNS record sets -``` +```j listResourceDNSRecordSets := map[string]interface{}{ "DomainName": "oddcn.cn", } diff --git a/examples/loadbalancer/aliloadbalancer/aliloadbalancer.md b/examples/loadbalancer/aliloadbalancer/aliloadbalancer.md new file mode 100644 index 0000000..be98afb --- /dev/null +++ b/examples/loadbalancer/aliloadbalancer/aliloadbalancer.md @@ -0,0 +1,95 @@ +# gocloud loadbancer - Ali-cloud + +## Configure Ali-cloud credentials + +Create `alicloudconfig.json` as follows, +``` +{ + "AliAccessKeyID": "xxxxxxxxxxxx", + "AliAccessKeySecret": "xxxxxxxxxxxx" +} +``` + +also You can setup environment variables as + +``` +export AliAccessKeyID = "xxxxxxxxxxxx" +export AliAccessKeySecret = "xxxxxxxxxxxx" +``` + +## Initialize library + +``` +import "github.com/cloudlibz/gocloud/gocloud" + +alicloud, _ := gocloud.CloudProvider(gocloud.Aliprovider) +``` + +### Create loadbalancer + +``` + createLoadBalancer := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerName": "abc", + "AddressType": "internet", + "InternetChargeType": "paybytraffic", + } + + resp, err := alicloud.Creatloadbalancer(createLoadBalancer) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Delete loadbalancer + +``` + deleteLoadBalancer := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerId": "lb-m5eavuvgjh0pho3hm4ub5", + } + + resp, err := alicloud.Deleteloadbalancer(deleteLoadBalancer) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### List loadbalancer + +``` + list := map[string]interface{}{ + "RegionId": "cn-qingdao", + } + + resp, err := alicloud.Listloadbalancer(list) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Attach node with loadbalancer + +``` + attach := map[string]interface{}{ + "LoadBalancerId": "lb-m5eemmwmtmt4l6jf73d72", + "BackendServers": "[{'ServerId':'i-m5ecx5g9m0cflv1k83zu','Weight':'100','Type':'ecs'}," + + "{'ServerId':'i-m5eahbbwqxawpj1opww9','Weight':'100','Type':'ecs'}]", + } + + resp, err := alicloud.Attachnodewithloadbalancer(attach) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` + +### Detach node with loadbalancer + +``` + detach := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerId": "lb-m5eemmwmtmt4l6jf73d72", + "BackendServers": "[{'ServerId':'i-m5ecx5g9m0cflv1k83zu','Type':'ecs'}," + + "{'ServerId':'i-m5eahbbwqxawpj1opww9','Type':'ecs'}]", + } + + resp, err := alicloud.Detachnodewithloadbalancer(detach) + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) +``` \ No newline at end of file diff --git a/loadbalancer/aliloadbalancer/loadbalancer.go b/loadbalancer/aliloadbalancer/loadbalancer.go index 2403ecb..872e369 100644 --- a/loadbalancer/aliloadbalancer/loadbalancer.go +++ b/loadbalancer/aliloadbalancer/loadbalancer.go @@ -5,7 +5,7 @@ import ( "github.com/cloudlibz/gocloud/aliauth" ) -//TODO +// Creatloadbalancer creates ali loadbalancer func (aliloadbalancer *Aliloadbalancer) Creatloadbalancer(request interface{}) (resp interface{}, err error) { var options CreateLoadBalancer @@ -77,7 +77,7 @@ func (aliloadbalancer *Aliloadbalancer) Creatloadbalancer(request interface{}) ( return resp, err } -//TODO +// Deleteloadbalancer deletes ali loadbalancer func (aliloadbalancer *Aliloadbalancer) Deleteloadbalancer(request interface{}) (resp interface{}, err error) { var options DeleteLoadBalancer @@ -105,7 +105,7 @@ func (aliloadbalancer *Aliloadbalancer) Deleteloadbalancer(request interface{}) return resp, err } -//TODO +// Listloadbalancer lists ali loadbalancer func (aliloadbalancer *Aliloadbalancer) Listloadbalancer(request interface{}) (resp interface{}, err error) { var options ListLoadBalancer @@ -143,12 +143,61 @@ func (aliloadbalancer *Aliloadbalancer) Listloadbalancer(request interface{}) (r return resp, err } -//TODO +// Detachnodewithloadbalancer detach ali ecs instance from ali loadbalancer func (aliloadbalancer *Aliloadbalancer) Detachnodewithloadbalancer(request interface{}) (resp interface{}, err error) { + var options DetachLoadBalancer + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "RegionId": + RegionID, _ := value.(string) + options.RegionID = RegionID + case "LoadBalancerId": + LoadBalancerID, _ := value.(string) + options.LoadBalancerID = LoadBalancerID + case "BackendServers": + BackendServers, _ := value.(string) + options.BackendServers = BackendServers + } + } + + // Put all of options into params + params := aliauth.PutStructIntoMap(&options) + + response := make(map[string]interface{}) + err = aliauth.LoadBalancerSignAndDoRequest("RemoveBackendServers", params, response) + resp = response return resp, err } -//TODO +//Attachnodewithloadbalancer attach ali ecs instance to ali loadbalancer func (aliloadbalancer *Aliloadbalancer) Attachnodewithloadbalancer(request interface{}) (resp interface{}, err error) { + var options AttachLoadBalancer + + param := make(map[string]interface{}) + + param = request.(map[string]interface{}) + + for key, value := range param { + switch key { + case "LoadBalancerId": + LoadBalancerID, _ := value.(string) + options.LoadBalancerID = LoadBalancerID + case "BackendServers": + BackendServers, _ := value.(string) + options.BackendServers = BackendServers + } + } + + // Put all of options into params + params := aliauth.PutStructIntoMap(&options) + + response := make(map[string]interface{}) + err = aliauth.LoadBalancerSignAndDoRequest("AddBackendServers", params, response) + resp = response return resp, err } diff --git a/loadbalancer/aliloadbalancer/loadbalancer_test.go b/loadbalancer/aliloadbalancer/loadbalancer_test.go index bf575a2..7f00981 100644 --- a/loadbalancer/aliloadbalancer/loadbalancer_test.go +++ b/loadbalancer/aliloadbalancer/loadbalancer_test.go @@ -44,14 +44,44 @@ func TestListLoadBalancer(t *testing.T) { var alilb Aliloadbalancer list := map[string]interface{}{ "RegionId": "cn-qingdao", - "LoadBalancerId": "lb-m5ehdbs3p10a7kmq344je", } resp, err := alilb.Listloadbalancer(list) if err != nil { - t.Errorf("DeleteLoadBalancer Test Fail: %s", err) + t.Errorf("ListLoadBalancer Test Fail: %s", err) return } response := resp.(map[string]interface{}) fmt.Println(response["body"]) t.Logf("Ali LoadBalancer is listed successfully.") } + +func TestAttachLoadBalancer(t *testing.T) { + var alilb Aliloadbalancer + attach := map[string]interface{}{ + "LoadBalancerId": "lb-m5eemmwmtmt4l6jf73d72", + "BackendServers": "[{'ServerId':'i-m5ecx5g9m0cflv1k83zu','Weight':'100','Type':'ecs'}," + + "{'ServerId':'i-m5eahbbwqxawpj1opww9','Weight':'100','Type':'ecs'}]", + } + _, err := alilb.Attachnodewithloadbalancer(attach) + if err != nil { + t.Errorf("Attachnodewithloadbalancer Test Fail: %s", err) + return + } + t.Logf("Ali LoadBalancer is listed successfully.") +} + +func TestDettachLoadBalancer(t *testing.T) { + var alilb Aliloadbalancer + dettach := map[string]interface{}{ + "RegionId": "cn-qingdao", + "LoadBalancerId": "lb-m5eemmwmtmt4l6jf73d72", + "BackendServers": "[{'ServerId':'i-m5ecx5g9m0cflv1k83zu','Type':'ecs'}," + + "{'ServerId':'i-m5eahbbwqxawpj1opww9','Type':'ecs'}]", + } + _, err := alilb.Detachnodewithloadbalancer(dettach) + if err != nil { + t.Errorf("DetachnodewithLoadBalancer Test Fail: %s", err) + return + } + t.Logf("Ali LoadBalancer is detached with node successfully.") +} diff --git a/loadbalancer/aliloadbalancer/models.go b/loadbalancer/aliloadbalancer/models.go index f16afa1..2784827 100644 --- a/loadbalancer/aliloadbalancer/models.go +++ b/loadbalancer/aliloadbalancer/models.go @@ -4,6 +4,7 @@ package aliloadbalancer type Aliloadbalancer struct { } +// CreateLoadBalancer struct represents attribute of create LoadBalancer. type CreateLoadBalancer struct { RegionID string MasterZoneID string @@ -21,11 +22,14 @@ type CreateLoadBalancer struct { ClientToken string ResourceGroupID string } + +// DeleteLoadBalancer struct represents attribute of delete LoadBalancer. type DeleteLoadBalancer struct { RegionID string LoadBalancerID string } +// ListLoadBalancer struct represents attribute of list LoadBalancer. type ListLoadBalancer struct { RegionID string LoadBalancerID string @@ -41,3 +45,16 @@ type ListLoadBalancer struct { MasterZoneID string SlaveZoneID string } + +// AttachLoadBalancer represents Attach node with loadbalancer attribute. +type AttachLoadBalancer struct { + LoadBalancerID string + BackendServers string +} + +// DetachLoadBalancer represents Detach node with loadbalancer attribute. +type DetachLoadBalancer struct { + RegionID string + LoadBalancerID string + BackendServers string +} From 2ec8b172d1132299c89e24b3a2cbb76dc1d34cad Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Wed, 16 May 2018 23:02:07 +0800 Subject: [PATCH 35/38] APIDocs for ali loadbalancer --- APIDocs/aliloadbalancer.md | 103 +++++++++++++++++++++++++++++++++++++ APIDocs/ecs.md | 2 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 APIDocs/aliloadbalancer.md diff --git a/APIDocs/aliloadbalancer.md b/APIDocs/aliloadbalancer.md new file mode 100644 index 0000000..855e9a9 --- /dev/null +++ b/APIDocs/aliloadbalancer.md @@ -0,0 +1,103 @@ +``` +package aliloadbalancer + import "github.com/cloudlibz/gocloud/loadbalancer/aliloadbalancer" +``` + +### TYPES + +``` +type Aliloadbalancer struct { +} +``` +Aliloadbalancer represents Aliloadbalancer struct. + +``` +func (aliloadbalancer *Aliloadbalancer) Attachnodewithloadbalancer(request interface{}) (resp interface{}, err error) +``` +Attachnodewithloadbalancer attach ali ecs instance to ali loadbalancer + +``` +func (aliloadbalancer *Aliloadbalancer) Creatloadbalancer(request interface{}) (resp interface{}, err error) +``` +Creatloadbalancer creates ali loadbalancer + +``` +func (aliloadbalancer *Aliloadbalancer) Deleteloadbalancer(request interface{}) (resp interface{}, err error) +``` +Deleteloadbalancer deletes ali loadbalancer + +``` +func (aliloadbalancer *Aliloadbalancer) Detachnodewithloadbalancer(request interface{}) (resp interface{}, err error) +``` +Detachnodewithloadbalancer detach ali ecs instance from ali loadbalancer + +``` +func (aliloadbalancer *Aliloadbalancer) Listloadbalancer(request interface{}) (resp interface{}, err error) +``` +Listloadbalancer lists ali loadbalancer + +``` +type AttachLoadBalancer struct { + LoadBalancerID string + BackendServers string +} +``` +AttachLoadBalancer represents Attach node with loadbalancer attribute. + +``` +type CreateLoadBalancer struct { + RegionID string + MasterZoneID string + SlaveZoneID string + LoadBalancerSpec string + LoadBalancerName string + AddressType string + VSwitchID string + PayType string + PricingCycle string + Duration string + Autopay bool + InternetChargeType string + Bandwidth int + ClientToken string + ResourceGroupID string +} +``` +CreateLoadBalancer struct represents attribute of create LoadBalancer. + +``` +type DeleteLoadBalancer struct { + RegionID string + LoadBalancerID string +} +``` +DeleteLoadBalancer struct represents attribute of delete LoadBalancer. + +``` +type DetachLoadBalancer struct { + RegionID string + LoadBalancerID string + BackendServers string +} +``` +DetachLoadBalancer represents Detach node with loadbalancer attribute. + +``` +type ListLoadBalancer struct { + RegionID string + LoadBalancerID string + LoadBalancerName string + AddressType string + NetworkType string + VpcID string + VswitchID string + Address string + ServerIntranetAddress int + InternetChargeType string + ServerID string + MasterZoneID string + SlaveZoneID string +} +``` +ListLoadBalancer struct represents attribute of list LoadBalancer. + diff --git a/APIDocs/ecs.md b/APIDocs/ecs.md index 0426c42..aee9c8c 100644 --- a/APIDocs/ecs.md +++ b/APIDocs/ecs.md @@ -27,7 +27,7 @@ type CreateInstance struct { } ``` - CreateInstance to store all attribute to create Ali-cloud ECS instance +CreateInstance to store all attribute to create Ali-cloud ECS instance ``` type CreateNodeBuilder struct { From 55994a4e284e1dd643645b82ddc029dc98465e41 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Fri, 18 May 2018 14:20:24 +0800 Subject: [PATCH 36/38] implement list node types, ecs region --- aliauth/sign.go | 4 +++- compute/ecs/ecs.go | 23 +++++++++++++++++++++++ compute/ecs/instance.go | 8 ++++++++ compute/ecs/instance_test.go | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index 31fb23b..4073181 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -17,6 +17,8 @@ import ( const formatISO8601 = "2006-01-02T15:04:05Z" +var EcsRegion string + func LoadBalancerSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { // Add common params and action param params["Action"] = action @@ -58,7 +60,7 @@ func ECSSignAndDoRequest(action string, params map[string]interface{}, response params["SignatureVersion"] = "1.0" params["SignatureNonce"] = createRandomString() - err := signAndDoRequest("ecs.aliyuncs.com", params, response) + err := signAndDoRequest(EcsRegion, params, response) return err } diff --git a/compute/ecs/ecs.go b/compute/ecs/ecs.go index b712842..b9d277a 100644 --- a/compute/ecs/ecs.go +++ b/compute/ecs/ecs.go @@ -1,5 +1,28 @@ package ecs +import "github.com/cloudlibz/gocloud/aliauth" + //Ali ECS struct type ECS struct { } + +func init() { + SetRegion(DefaultRegion) +} + +const ( + DefaultRegion = "ecs.aliyuncs.com" + Zhangjiakou = "ecs.cn-zhangjiakou.aliyuncs.com" + Hohhot = "ecs.cn-huhehaote.aliyuncs.com" + Tokyo = "ecs.ap-northeast-1.aliyuncs.com" + Sydney = "ecs.ap-southeast-2.aliyuncs.com" + KualaLumpur = "ecs.ap-southeast-3.aliyuncs.com" + Jakarta = "ecs.ap-southeast-5.aliyuncs.com" + Mumbai = "ecs.ap-south-1.aliyuncs.com" + Dubai = "ecs.me-east-1.aliyuncs.com" + Frankfurt = "ecs.eu-central-1.aliyuncs.com" +) + +func SetRegion(region string) { + aliauth.EcsRegion = region +} diff --git a/compute/ecs/instance.go b/compute/ecs/instance.go index 1d224f2..931cdfd 100644 --- a/compute/ecs/instance.go +++ b/compute/ecs/instance.go @@ -250,3 +250,11 @@ func (ecs *ECS) Createnode(request interface{}) (resp interface{}, err error) { resp = response return resp, err } + +func (ecs *ECS) ListNodeType(request interface{}) (resp interface{}, err error) { + params := make(map[string]interface{}) + response := make(map[string]interface{}) + err = aliauth.ECSSignAndDoRequest("DescribeInstanceTypes", params, response) + resp = response + return resp, err +} diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index e7d2cdb..47c4f0b 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -3,6 +3,7 @@ package ecs import ( "github.com/cloudlibz/gocloud/aliauth" "testing" + "fmt" ) func init() { @@ -99,3 +100,16 @@ func TestDeletenode(t *testing.T) { } t.Logf("Ali node is deleted successfully.") } + +func TestListNodeType(t *testing.T) { + var aliEcs ECS + SetRegion(Mumbai) + resp, err := aliEcs.ListNodeType(nil) + if err != nil { + t.Errorf("ListNodeType Test Fail") + return + } + response := resp.(map[string]interface{}) + fmt.Println(response["body"]) + t.Logf("Ali node type is listed successfully.") +} From 15f5a455220ac0c4ad3b22c0f3f60c456172e798 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Fri, 18 May 2018 14:27:37 +0800 Subject: [PATCH 37/38] implement loadbalancer region --- aliauth/sign.go | 4 +++- .../aliloadbalancer/loadbalancer_test.go | 3 ++- loadbalancer/aliloadbalancer/models.go | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index 4073181..21c8c25 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -19,6 +19,8 @@ const formatISO8601 = "2006-01-02T15:04:05Z" var EcsRegion string +var LoadBalancerRegion string + func LoadBalancerSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { // Add common params and action param params["Action"] = action @@ -30,7 +32,7 @@ func LoadBalancerSignAndDoRequest(action string, params map[string]interface{}, params["SignatureVersion"] = "1.0" params["SignatureNonce"] = createRandomString() - err := signAndDoRequest("slb.aliyuncs.com", params, response) + err := signAndDoRequest(LoadBalancerRegion, params, response) return err } diff --git a/loadbalancer/aliloadbalancer/loadbalancer_test.go b/loadbalancer/aliloadbalancer/loadbalancer_test.go index 7f00981..6e72e7d 100644 --- a/loadbalancer/aliloadbalancer/loadbalancer_test.go +++ b/loadbalancer/aliloadbalancer/loadbalancer_test.go @@ -12,8 +12,9 @@ func init() { func TestCreateLoadBalancer(t *testing.T) { var alilb Aliloadbalancer + SetRegion(Mumbai) create := map[string]interface{}{ - "RegionId": "cn-qingdao", + "RegionId": "ap-south-1", "LoadBalancerName": "abc", "AddressType": "internet", "InternetChargeType": "paybytraffic", diff --git a/loadbalancer/aliloadbalancer/models.go b/loadbalancer/aliloadbalancer/models.go index 2784827..938f145 100644 --- a/loadbalancer/aliloadbalancer/models.go +++ b/loadbalancer/aliloadbalancer/models.go @@ -1,5 +1,7 @@ package aliloadbalancer +import "github.com/cloudlibz/gocloud/aliauth" + //Aliloadbalancer represents Aliloadbalancer struct. type Aliloadbalancer struct { } @@ -58,3 +60,24 @@ type DetachLoadBalancer struct { LoadBalancerID string BackendServers string } + +func init() { + SetRegion(DefaultRegion) +} + +const ( + DefaultRegion = "slb.aliyuncs.com" + Zhangjiakou = "slb.cn-zhangjiakou.aliyuncs.com" + Hohhot = "slb.cn-huhehaote.aliyuncs.com" + Tokyo = "slb.ap-northeast-1.aliyuncs.com" + Sydney = "slb.ap-southeast-2.aliyuncs.com" + KualaLumpur = "slb.ap-southeast-3.aliyuncs.com" + Jakarta = "slb.ap-southeast-5.aliyuncs.com" + Mumbai = "slb.ap-south-1.aliyuncs.com" + Dubai = "slb.me-east-1.aliyuncs.com" + Frankfurt = "slb.eu-central-1.aliyuncs.com" +) + +func SetRegion(region string) { + aliauth.LoadBalancerRegion = region +} From b5ef1c2b2187d71ef411dddfb2215cda05447f28 Mon Sep 17 00:00:00 2001 From: Junqiu Zheng Date: Sun, 20 May 2018 21:22:01 +0800 Subject: [PATCH 38/38] ali ecs and slb 's endpoint --- aliauth/sign.go | 38 +++++++++++++++---- aliauth/utils.go | 25 ++++++++++++ compute/ecs/ecs.go | 10 ----- compute/ecs/instance_test.go | 1 - .../aliloadbalancer/loadbalancer_test.go | 3 +- loadbalancer/aliloadbalancer/models.go | 10 ----- 6 files changed, 56 insertions(+), 31 deletions(-) diff --git a/aliauth/sign.go b/aliauth/sign.go index 21c8c25..07aaf99 100644 --- a/aliauth/sign.go +++ b/aliauth/sign.go @@ -17,10 +17,6 @@ import ( const formatISO8601 = "2006-01-02T15:04:05Z" -var EcsRegion string - -var LoadBalancerRegion string - func LoadBalancerSignAndDoRequest(action string, params map[string]interface{}, response map[string]interface{}) error { // Add common params and action param params["Action"] = action @@ -32,7 +28,20 @@ func LoadBalancerSignAndDoRequest(action string, params map[string]interface{}, params["SignatureVersion"] = "1.0" params["SignatureNonce"] = createRandomString() - err := signAndDoRequest(LoadBalancerRegion, params, response) + var endpoint string + if params["RegionID"] != nil { + if params["RegionID"].(string) != "" { + endpoint = getEndpointWithRegion(params["RegionID"].(string)) + } + } + + if endpoint == "" { + endpoint = "slb.aliyuncs.com" + } else { + endpoint = "slb." + endpoint + ".aliyuncs.com" + } + + err := signAndDoRequest(endpoint, params, response) return err } @@ -62,12 +71,25 @@ func ECSSignAndDoRequest(action string, params map[string]interface{}, response params["SignatureVersion"] = "1.0" params["SignatureNonce"] = createRandomString() - err := signAndDoRequest(EcsRegion, params, response) + var endpoint string + if params["RegionID"] != nil { + if params["RegionID"].(string) != "" { + endpoint = getEndpointWithRegion(params["RegionID"].(string)) + } + } + + if endpoint == "" { + endpoint = "ecs.aliyuncs.com" + } else { + endpoint = "ecs." + endpoint + ".aliyuncs.com" + } + + err := signAndDoRequest(endpoint, params, response) return err } // signAndDoRequest sign and do request by action parameter and specific parameters -func signAndDoRequest(domain string, params map[string]interface{}, response map[string]interface{}) error { +func signAndDoRequest(endpoint string, params map[string]interface{}, response map[string]interface{}) error { // Sort the parameters by key keys := make([]string, len(params)) i := 0 @@ -98,7 +120,7 @@ func signAndDoRequest(domain string, params map[string]interface{}, response map } // Generate the request URL - requestURL := "https://" + domain + "/?" + query.Encode() + "&Signature=" + url.QueryEscape(base64Sign) + requestURL := "https://" + endpoint + "/?" + query.Encode() + "&Signature=" + url.QueryEscape(base64Sign) httpReq, err := http.NewRequest("GET", requestURL, nil) diff --git a/aliauth/utils.go b/aliauth/utils.go index 3dc0df9..9a05af1 100644 --- a/aliauth/utils.go +++ b/aliauth/utils.go @@ -26,3 +26,28 @@ func PutStructIntoMap(i interface{}) map[string]interface{} { } return params } + +func getEndpointWithRegion(region string) string { + switch region { + case "cn-zhangjiakou": + return region + case "cn-huhehaote": + return region + case "ap-northeast-1": + return region + case "ap-southeast-2": + return region + case "ap-southeast-3": + return region + case "ap-southeast-5": + return region + case "ap-south-1": + return region + case "me-east-1": + return region + case "eu-central-1": + return region + default: + return "" + } +} diff --git a/compute/ecs/ecs.go b/compute/ecs/ecs.go index b9d277a..16d9ce5 100644 --- a/compute/ecs/ecs.go +++ b/compute/ecs/ecs.go @@ -1,15 +1,9 @@ package ecs -import "github.com/cloudlibz/gocloud/aliauth" - //Ali ECS struct type ECS struct { } -func init() { - SetRegion(DefaultRegion) -} - const ( DefaultRegion = "ecs.aliyuncs.com" Zhangjiakou = "ecs.cn-zhangjiakou.aliyuncs.com" @@ -22,7 +16,3 @@ const ( Dubai = "ecs.me-east-1.aliyuncs.com" Frankfurt = "ecs.eu-central-1.aliyuncs.com" ) - -func SetRegion(region string) { - aliauth.EcsRegion = region -} diff --git a/compute/ecs/instance_test.go b/compute/ecs/instance_test.go index 47c4f0b..46e848b 100644 --- a/compute/ecs/instance_test.go +++ b/compute/ecs/instance_test.go @@ -103,7 +103,6 @@ func TestDeletenode(t *testing.T) { func TestListNodeType(t *testing.T) { var aliEcs ECS - SetRegion(Mumbai) resp, err := aliEcs.ListNodeType(nil) if err != nil { t.Errorf("ListNodeType Test Fail") diff --git a/loadbalancer/aliloadbalancer/loadbalancer_test.go b/loadbalancer/aliloadbalancer/loadbalancer_test.go index 6e72e7d..7f00981 100644 --- a/loadbalancer/aliloadbalancer/loadbalancer_test.go +++ b/loadbalancer/aliloadbalancer/loadbalancer_test.go @@ -12,9 +12,8 @@ func init() { func TestCreateLoadBalancer(t *testing.T) { var alilb Aliloadbalancer - SetRegion(Mumbai) create := map[string]interface{}{ - "RegionId": "ap-south-1", + "RegionId": "cn-qingdao", "LoadBalancerName": "abc", "AddressType": "internet", "InternetChargeType": "paybytraffic", diff --git a/loadbalancer/aliloadbalancer/models.go b/loadbalancer/aliloadbalancer/models.go index 938f145..106a279 100644 --- a/loadbalancer/aliloadbalancer/models.go +++ b/loadbalancer/aliloadbalancer/models.go @@ -1,7 +1,5 @@ package aliloadbalancer -import "github.com/cloudlibz/gocloud/aliauth" - //Aliloadbalancer represents Aliloadbalancer struct. type Aliloadbalancer struct { } @@ -61,10 +59,6 @@ type DetachLoadBalancer struct { BackendServers string } -func init() { - SetRegion(DefaultRegion) -} - const ( DefaultRegion = "slb.aliyuncs.com" Zhangjiakou = "slb.cn-zhangjiakou.aliyuncs.com" @@ -77,7 +71,3 @@ const ( Dubai = "slb.me-east-1.aliyuncs.com" Frankfurt = "slb.eu-central-1.aliyuncs.com" ) - -func SetRegion(region string) { - aliauth.LoadBalancerRegion = region -}