| 1 |
package common |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/xml" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
"log" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
|
| 15 |
locationDefaultEndpoint = "https://location.aliyuncs.com" |
| 16 |
locationAPIVersion = "2015-06-12" |
| 17 |
HTTP_PROTOCOL = "http" |
| 18 |
HTTPS_PROTOCOL = "https" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
endpoints = make(map[Region]map[string]string) |
| 23 |
|
| 24 |
SpecailEnpoints = map[Region]map[string]string{ |
| 25 |
APNorthEast1: { |
| 26 |
"ecs": "https://ecs.ap-northeast-1.aliyuncs.com", |
| 27 |
"slb": "https://slb.ap-northeast-1.aliyuncs.com", |
| 28 |
"rds": "https://rds.ap-northeast-1.aliyuncs.com", |
| 29 |
"vpc": "https://vpc.ap-northeast-1.aliyuncs.com", |
| 30 |
}, |
| 31 |
APSouthEast2: { |
| 32 |
"ecs": "https://ecs.ap-southeast-2.aliyuncs.com", |
| 33 |
"slb": "https://slb.ap-southeast-2.aliyuncs.com", |
| 34 |
"rds": "https://rds.ap-southeast-2.aliyuncs.com", |
| 35 |
"vpc": "https://vpc.ap-southeast-2.aliyuncs.com", |
| 36 |
}, |
| 37 |
APSouthEast3: { |
| 38 |
"ecs": "https://ecs.ap-southeast-3.aliyuncs.com", |
| 39 |
"slb": "https://slb.ap-southeast-3.aliyuncs.com", |
| 40 |
"rds": "https://rds.ap-southeast-3.aliyuncs.com", |
| 41 |
"vpc": "https://vpc.ap-southeast-3.aliyuncs.com", |
| 42 |
}, |
| 43 |
MEEast1: { |
| 44 |
"ecs": "https://ecs.me-east-1.aliyuncs.com", |
| 45 |
"slb": "https://slb.me-east-1.aliyuncs.com", |
| 46 |
"rds": "https://rds.me-east-1.aliyuncs.com", |
| 47 |
"vpc": "https://vpc.me-east-1.aliyuncs.com", |
| 48 |
}, |
| 49 |
EUCentral1: { |
| 50 |
"ecs": "https://ecs.eu-central-1.aliyuncs.com", |
| 51 |
"slb": "https://slb.eu-central-1.aliyuncs.com", |
| 52 |
"rds": "https://rds.eu-central-1.aliyuncs.com", |
| 53 |
"vpc": "https://vpc.eu-central-1.aliyuncs.com", |
| 54 |
}, |
| 55 |
Zhangjiakou: { |
| 56 |
"ecs": "https://ecs.cn-zhangjiakou.aliyuncs.com", |
| 57 |
"slb": "https://slb.cn-zhangjiakou.aliyuncs.com", |
| 58 |
"rds": "https://rds.cn-zhangjiakou.aliyuncs.com", |
| 59 |
"vpc": "https://vpc.cn-zhangjiakou.aliyuncs.com", |
| 60 |
}, |
| 61 |
Huhehaote: { |
| 62 |
"ecs": "https://ecs.cn-huhehaote.aliyuncs.com", |
| 63 |
"slb": "https://slb.cn-huhehaote.aliyuncs.com", |
| 64 |
"rds": "https://rds.cn-huhehaote.aliyuncs.com", |
| 65 |
"vpc": "https://vpc.cn-huhehaote.aliyuncs.com", |
| 66 |
}, |
| 67 |
} |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
func init() { |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
type LocationClient struct { |
| 76 |
Client |
| 77 |
} |
| 78 |
|
| 79 |
func NewLocationClient(accessKeyId, accessKeySecret, securityToken string) *LocationClient { |
| 80 |
endpoint := os.Getenv("LOCATION_ENDPOINT") |
| 81 |
if endpoint == "" { |
| 82 |
endpoint = locationDefaultEndpoint |
| 83 |
} |
| 84 |
|
| 85 |
client := &LocationClient{} |
| 86 |
client.Init(endpoint, locationAPIVersion, accessKeyId, accessKeySecret) |
| 87 |
client.securityToken = securityToken |
| 88 |
return client |
| 89 |
} |
| 90 |
|
| 91 |
func NewLocationClientWithSecurityToken(accessKeyId, accessKeySecret, securityToken string) *LocationClient { |
| 92 |
endpoint := os.Getenv("LOCATION_ENDPOINT") |
| 93 |
if endpoint == "" { |
| 94 |
endpoint = locationDefaultEndpoint |
| 95 |
} |
| 96 |
|
| 97 |
client := &LocationClient{} |
| 98 |
client.WithEndpoint(endpoint). |
| 99 |
WithVersion(locationAPIVersion). |
| 100 |
WithAccessKeyId(accessKeyId). |
| 101 |
WithAccessKeySecret(accessKeySecret). |
| 102 |
WithSecurityToken(securityToken). |
| 103 |
InitClient() |
| 104 |
return client |
| 105 |
} |
| 106 |
|
| 107 |
func (client *LocationClient) DescribeEndpoint(args *DescribeEndpointArgs) (*DescribeEndpointResponse, error) { |
| 108 |
response := &DescribeEndpointResponse{} |
| 109 |
err := client.Invoke("DescribeEndpoint", args, response) |
| 110 |
if err != nil { |
| 111 |
return nil, err |
| 112 |
} |
| 113 |
return response, err |
| 114 |
} |
| 115 |
|
| 116 |
func (client *LocationClient) DescribeEndpoints(args *DescribeEndpointsArgs) (*DescribeEndpointsResponse, error) { |
| 117 |
response := &DescribeEndpointsResponse{} |
| 118 |
err := client.Invoke("DescribeEndpoints", args, response) |
| 119 |
if err != nil { |
| 120 |
return nil, err |
| 121 |
} |
| 122 |
return response, err |
| 123 |
} |
| 124 |
|
| 125 |
func getProductRegionEndpoint(region Region, serviceCode string) string { |
| 126 |
if sp, ok := endpoints[region]; ok { |
| 127 |
if endpoint, ok := sp[serviceCode]; ok { |
| 128 |
return endpoint |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return "" |
| 133 |
} |
| 134 |
|
| 135 |
func setProductRegionEndpoint(region Region, serviceCode string, endpoint string) { |
| 136 |
endpoints[region] = map[string]string{ |
| 137 |
serviceCode: endpoint, |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func (client *LocationClient) DescribeOpenAPIEndpoint(region Region, serviceCode string) string { |
| 142 |
if endpoint := getProductRegionEndpoint(region, serviceCode); endpoint != "" { |
| 143 |
return endpoint |
| 144 |
} |
| 145 |
defaultProtocols := HTTP_PROTOCOL |
| 146 |
|
| 147 |
args := &DescribeEndpointsArgs{ |
| 148 |
Id: region, |
| 149 |
ServiceCode: serviceCode, |
| 150 |
Type: "openAPI", |
| 151 |
} |
| 152 |
|
| 153 |
var endpoint *DescribeEndpointsResponse |
| 154 |
var err error |
| 155 |
for index := 0; index < 5; index++ { |
| 156 |
endpoint, err = client.DescribeEndpoints(args) |
| 157 |
if err == nil && endpoint != nil && len(endpoint.Endpoints.Endpoint) > 0 { |
| 158 |
break |
| 159 |
} |
| 160 |
time.Sleep(500 * time.Millisecond) |
| 161 |
} |
| 162 |
|
| 163 |
if err != nil || endpoint == nil || len(endpoint.Endpoints.Endpoint) <= 0 { |
| 164 |
log.Printf("aliyungo: can not get endpoint from service, use default. endpoint=[%v], error=[%v]\n",endpoint, err) |
| 165 |
return "" |
| 166 |
} |
| 167 |
|
| 168 |
for _, protocol := range endpoint.Endpoints.Endpoint[0].Protocols.Protocols { |
| 169 |
if strings.ToLower(protocol) == HTTPS_PROTOCOL { |
| 170 |
defaultProtocols = HTTPS_PROTOCOL |
| 171 |
break |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
ep := fmt.Sprintf("%s://%s", defaultProtocols, endpoint.Endpoints.Endpoint[0].Endpoint) |
| 176 |
|
| 177 |
setProductRegionEndpoint(region, serviceCode, ep) |
| 178 |
return ep |
| 179 |
} |
| 180 |
|
| 181 |
func loadEndpointFromFile(region Region, serviceCode string) string { |
| 182 |
data, err := ioutil.ReadFile("./endpoints.xml") |
| 183 |
if err != nil { |
| 184 |
return "" |
| 185 |
} |
| 186 |
var endpoints Endpoints |
| 187 |
err = xml.Unmarshal(data, &endpoints) |
| 188 |
if err != nil { |
| 189 |
return "" |
| 190 |
} |
| 191 |
for _, endpoint := range endpoints.Endpoint { |
| 192 |
if endpoint.RegionIds.RegionId == string(region) { |
| 193 |
for _, product := range endpoint.Products.Product { |
| 194 |
if strings.ToLower(product.ProductName) == serviceCode { |
| 195 |
return fmt.Sprintf("%s://%s", HTTPS_PROTOCOL, product.DomainName) |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return "" |
| 202 |
} |