forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
95 lines (84 loc) · 2.36 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package connectivity
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"strings"
)
// 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 ""
}