Skip to content
This repository has been archived by the owner on Aug 25, 2020. It is now read-only.

Commit

Permalink
Support provider thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Oct 20, 2018
1 parent b5143ad commit a376a64
Show file tree
Hide file tree
Showing 271 changed files with 6,494 additions and 3,860 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ FEATURES:

IMPROVEMENTS:

- Support provider thread safety [GH-432]
- add tags to security group [GH-423]
- Resource router_interface support PrePaid [GH-425]
- resource alicloud_slb_listener support acl [GH-426]
Expand Down
3 changes: 2 additions & 1 deletion alicloud/alicloud_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/denverdino/aliyungo/common"
"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)

func TestMain(m *testing.M) {
Expand All @@ -25,7 +26,7 @@ func sharedClientForRegion(region string) (interface{}, error) {
return nil, fmt.Errorf("empty ALICLOUD_SECRET_KEY")
}

conf := Config{
conf := connectivity.Config{
Region: common.Region(region),
RegionId: region,
AccessKey: accessKey,
Expand Down
210 changes: 68 additions & 142 deletions alicloud/common.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package alicloud

import (
"bytes"
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"strconv"
"strings"

"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"

"time"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/denverdino/aliyungo/common"
"github.com/google/uuid"
"github.com/hashicorp/terraform/helper/schema"
)

type InstanceNetWork string
Expand Down Expand Up @@ -146,14 +148,6 @@ const (
PageSizeLarge = 50
)

func getRegion(d *schema.ResourceData, meta interface{}) common.Region {
return meta.(*AliyunClient).Region
}

func getRegionId(d *schema.ResourceData, meta interface{}) string {
return meta.(*AliyunClient).RegionId
}

// Protocol represents network protocol
type Protocol string

Expand Down Expand Up @@ -197,8 +191,6 @@ const COMMA_SEPARATED = ","

const COLON_SEPARATED = ":"

const DOT_SEPARATED = "."

const LOCAL_HOST_IP = "127.0.0.1"

// Takes the result of flatmap.Expand for an array of strings
Expand Down Expand Up @@ -284,25 +276,6 @@ func getPagination(pageNumber, pageSize int) (pagination common.Pagination) {

const CharityPageUrl = "http://promotion.alicdn.com/help/oss/error.html"

func (client *AliyunClient) JudgeRegionValidation(key, region string) error {
resp, err := client.ecsconn.DescribeRegions(ecs.CreateDescribeRegionsRequest())
if err != nil {
return fmt.Errorf("DescribeRegions got an error: %#v", err)
}
if resp == nil || len(resp.Regions.Region) < 1 {
return GetNotFoundErrorFromString("There is no any available region.")
}

var rs []string
for _, v := range resp.Regions.Region {
if v.RegionId == region {
return nil
}
rs = append(rs, v.RegionId)
}
return fmt.Errorf("'%s' is invalid. Expected on %v.", key, strings.Join(rs, ", "))
}

func userDataHashSum(user_data string) string {
// Check whether the user_data is not Base64 encoded.
// Always calculate hash of base64 decoded value since we
Expand All @@ -322,116 +295,6 @@ func Trim(v string) string {
return strings.Trim(v, " ")
}

// Load endpoints from endpoints.xml or environment variables to meet specified application scenario, like private cloud.
type ServiceCode string

const (
ECSCode = ServiceCode("ECS")
ESSCode = ServiceCode("ESS")
RAMCode = ServiceCode("RAM")
VPCCode = ServiceCode("VPC")
SLBCode = ServiceCode("SLB")
RDSCode = ServiceCode("RDS")
OSSCode = ServiceCode("OSS")
CONTAINCode = ServiceCode("CS")
DOMAINCode = ServiceCode("DOMAIN")
CDNCode = ServiceCode("CDN")
CMSCode = ServiceCode("CMS")
KMSCode = ServiceCode("KMS")
OTSCode = ServiceCode("OTS")
PVTZCode = ServiceCode("PVTZ")
LOGCode = ServiceCode("LOG")
FCCode = ServiceCode("FC")
DDSCode = ServiceCode("DDS")
STSCode = ServiceCode("STS")
CENCode = ServiceCode("CEN")
KVSTORECode = ServiceCode("KVSTORE")
DATAHUBCode = ServiceCode("DATAHUB")
MNSCode = ServiceCode("MNS")
CLOUDAPICode = ServiceCode("CLOUDAPI")
)

//xml
type Endpoints struct {
Endpoint []Endpoint `xml:"Endpoint"`
}

type Endpoint struct {
Name string `xml:"name,attr"`
RegionIds RegionIds `xml:"RegionIds"`
Products Products `xml:"Products"`
}

type RegionIds struct {
RegionId string `xml:"RegionId"`
}

type Products struct {
Product []Product `xml:"Product"`
}

type Product struct {
ProductName string `xml:"ProductName"`
DomainName string `xml:"DomainName"`
}

func LoadEndpoint(region string, serviceCode ServiceCode) string {
endpoint := strings.TrimSpace(os.Getenv(fmt.Sprintf("%s_ENDPOINT", string(serviceCode))))
if endpoint != "" {
return endpoint
}

// Load current path endpoint file endpoints.xml, if failed, it will load from environment variables TF_ENDPOINT_PATH
data, err := ioutil.ReadFile("./endpoints.xml")
if err != nil || len(data) <= 0 {
d, e := ioutil.ReadFile(os.Getenv("TF_ENDPOINT_PATH"))
if e != nil {
return ""
}
data = d
}
var endpoints Endpoints
err = xml.Unmarshal(data, &endpoints)
if err != nil {
return ""
}
for _, endpoint := range endpoints.Endpoint {
if endpoint.RegionIds.RegionId == string(region) {
for _, product := range endpoint.Products.Product {
if strings.ToLower(product.ProductName) == strings.ToLower(string(serviceCode)) {
return product.DomainName
}
}
}
}

return ""
}

const ApiVersion20140526 = "2014-05-26"
const ApiVersion20140828 = "2014-08-28"
const ApiVersion20160815 = "2016-08-15"
const ApiVersion20140515 = "2014-05-15"
const ApiVersion20160428 = "2016-04-28"

type CommonRequestDomain string

const (
ECSDomain = CommonRequestDomain("ecs.aliyuncs.com")
ESSDomain = CommonRequestDomain("ess.aliyuncs.com")
)

func CommonRequestInit(region string, code ServiceCode, domain CommonRequestDomain) *requests.CommonRequest {
request := requests.NewCommonRequest()
request.Version = ApiVersion20140526
request.Domain = string(domain)
d := LoadEndpoint(region, code)
if d != "" {
request.Domain = d
}
return request
}

func ConvertIntegerToInt(value requests.Integer) (v int, err error) {
if strings.TrimSpace(string(value)) == "" {
return
Expand Down Expand Up @@ -556,3 +419,66 @@ func terraformToAPI(field string) string {
}
return result
}

func compareJsonTemplateAreEquivalent(tem1, tem2 string) (bool, error) {
var obj1 interface{}
err := json.Unmarshal([]byte(tem1), &obj1)
if err != nil {
return false, err
}

canonicalJson1, _ := json.Marshal(obj1)

var obj2 interface{}
err = json.Unmarshal([]byte(tem2), &obj2)
if err != nil {
return false, err
}

canonicalJson2, _ := json.Marshal(obj2)

equal := bytes.Compare(canonicalJson1, canonicalJson2) == 0
if !equal {
log.Printf("[DEBUG] Canonical template are not equal.\nFirst: %s\nSecond: %s\n",
canonicalJson1, canonicalJson2)
}
return equal, nil
}

func compareYamlTemplateAreEquivalent(tem1, tem2 string) (bool, error) {
var obj1 interface{}
err := yaml.Unmarshal([]byte(tem1), &obj1)
if err != nil {
return false, err
}

canonicalYaml1, _ := yaml.Marshal(obj1)

var obj2 interface{}
err = yaml.Unmarshal([]byte(tem2), &obj2)
if err != nil {
return false, err
}

canonicalYaml2, _ := yaml.Marshal(obj2)

equal := bytes.Compare(canonicalYaml1, canonicalYaml2) == 0
if !equal {
log.Printf("[DEBUG] Canonical template are not equal.\nFirst: %s\nSecond: %s\n",
canonicalYaml1, canonicalYaml2)
}
return equal, nil
}

// loadFileContent returns contents of a file in a given path
func loadFileContent(v string) ([]byte, error) {
filename, err := homedir.Expand(v)
if err != nil {
return nil, err
}
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return fileContent, nil
}

0 comments on commit a376a64

Please sign in to comment.