Skip to content

Commit

Permalink
Support load endpoint from environment variable or specified file
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed May 4, 2018
1 parent f8396c2 commit 877cf7e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
IMPROVEMENTS:

- *New Resource*: _alicloud_ots_table_(([#442](https://github.com/alibaba/terraform-provider/pull/442)))
- Support load endpoint from environment variable or specified file (([#459](https://github.com/alibaba/terraform-provider/pull/459)))
- Update example (([#457](https://github.com/alibaba/terraform-provider/pull/457)))
- Remove terraform/example (([#458](https://github.com/alibaba/terraform-provider/pull/458)))

Expand Down
80 changes: 80 additions & 0 deletions alicloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (

"encoding/base64"

"encoding/xml"
"io/ioutil"
"os"

"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -263,3 +267,79 @@ 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")
)

//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 ""
}
10 changes: 8 additions & 2 deletions alicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
Expand Down Expand Up @@ -186,6 +187,7 @@ func (c *Config) ecsConn() (*ecs.Client, error) {
}

func (c *Config) rdsConn() (*rds.Client, error) {
endpoints.AddEndpointMapping(c.RegionId, string(RDSCode), LoadEndpoint(c.RegionId, RDSCode))
return rds.NewClientWithOptions(c.RegionId, getSdkConfig(), c.getAuthCredential(false))
}

Expand All @@ -197,6 +199,7 @@ func (c *Config) slbConn() (*slb.Client, error) {
}

func (c *Config) vpcConn() (*vpc.Client, error) {
endpoints.AddEndpointMapping(c.RegionId, string(VPCCode), LoadEndpoint(c.RegionId, VPCCode))
return vpc.NewClientWithOptions(c.RegionId, getSdkConfig(), c.getAuthCredential(true))

}
Expand Down Expand Up @@ -272,10 +275,13 @@ func (c *Config) kmsConn() (*kms.Client, error) {
}

func (c *Config) otsConn() (*tablestore.TableStoreClient, error) {
endpoint := os.Getenv("OTS_ENDPOINT")
endpoint := LoadEndpoint(c.RegionId, OTSCode)
instanceName := c.OtsInstanceName
if endpoint == "" {
endpoint = "https://" + instanceName + "." + c.RegionId + ".ots.aliyuncs.com"
endpoint = fmt.Sprintf("%s.%s.ots.aliyuncs.com", instanceName, c.RegionId)
}
if !strings.HasPrefix(endpoint, string(Https)) && !strings.HasPrefix(endpoint, string(Http)) {
endpoint = fmt.Sprintf("%s://%s", Https, endpoint)
}
client := tablestore.NewClient(endpoint, instanceName, c.AccessKey, c.SecretKey)
return client, nil
Expand Down

0 comments on commit 877cf7e

Please sign in to comment.