endpoint.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package common
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. )
  9. const (
  10. // LocationDefaultEndpoint is the default API endpoint of Location services
  11. locationDefaultEndpoint = "https://location.aliyuncs.com"
  12. locationAPIVersion = "2015-06-12"
  13. HTTP_PROTOCOL = "http"
  14. HTTPS_PROTOCOL = "https"
  15. )
  16. var (
  17. endpoints = make(map[Region]map[string]string)
  18. )
  19. //init endpoints from file
  20. func init() {
  21. }
  22. func NewLocationClient(accessKeyId, accessKeySecret string) *Client {
  23. endpoint := os.Getenv("LOCATION_ENDPOINT")
  24. if endpoint == "" {
  25. endpoint = locationDefaultEndpoint
  26. }
  27. client := &Client{}
  28. client.Init(endpoint, locationAPIVersion, accessKeyId, accessKeySecret)
  29. return client
  30. }
  31. func (client *Client) DescribeEndpoint(args *DescribeEndpointArgs) (*DescribeEndpointResponse, error) {
  32. response := &DescribeEndpointResponse{}
  33. err := client.Invoke("DescribeEndpoint", args, response)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return response, err
  38. }
  39. func getProductRegionEndpoint(region Region, serviceCode string) string {
  40. if sp, ok := endpoints[region]; ok {
  41. if endpoint, ok := sp[serviceCode]; ok {
  42. return endpoint
  43. }
  44. }
  45. return ""
  46. }
  47. func setProductRegionEndpoint(region Region, serviceCode string, endpoint string) {
  48. endpoints[region] = map[string]string{
  49. serviceCode: endpoint,
  50. }
  51. }
  52. func (client *Client) DescribeOpenAPIEndpoint(region Region, serviceCode string) string {
  53. if endpoint := getProductRegionEndpoint(region, serviceCode); endpoint != "" {
  54. return endpoint
  55. }
  56. defaultProtocols := HTTP_PROTOCOL
  57. args := &DescribeEndpointArgs{
  58. Id: region,
  59. ServiceCode: serviceCode,
  60. Type: "openAPI",
  61. }
  62. endpoint, err := client.DescribeEndpoint(args)
  63. if err != nil || endpoint.Endpoint == "" {
  64. return ""
  65. }
  66. for _, protocol := range endpoint.Protocols.Protocols {
  67. if strings.ToLower(protocol) == HTTPS_PROTOCOL {
  68. defaultProtocols = HTTPS_PROTOCOL
  69. break
  70. }
  71. }
  72. ep := fmt.Sprintf("%s://%s", defaultProtocols, endpoint.Endpoint)
  73. setProductRegionEndpoint(region, serviceCode, ep)
  74. return ep
  75. }
  76. func loadEndpointFromFile(region Region, serviceCode string) string {
  77. data, err := ioutil.ReadFile("./endpoints.xml")
  78. if err != nil {
  79. return ""
  80. }
  81. var endpoints Endpoints
  82. err = xml.Unmarshal(data, &endpoints)
  83. if err != nil {
  84. return ""
  85. }
  86. for _, endpoint := range endpoints.Endpoint {
  87. if endpoint.RegionIds.RegionId == string(region) {
  88. for _, product := range endpoint.Products.Product {
  89. if strings.ToLower(product.ProductName) == serviceCode {
  90. return fmt.Sprintf("%s://%s", HTTPS_PROTOCOL, product.DomainName)
  91. }
  92. }
  93. }
  94. }
  95. return ""
  96. }