resolver.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package endpoints
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "sync"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  20. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  22. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  23. )
  24. var debug utils.Debug
  25. func init() {
  26. debug = utils.Init("sdk")
  27. }
  28. const (
  29. ResolveEndpointUserGuideLink = ""
  30. )
  31. var once sync.Once
  32. var resolvers []Resolver
  33. type Resolver interface {
  34. TryResolve(param *ResolveParam) (endpoint string, support bool, err error)
  35. GetName() (name string)
  36. }
  37. // Resolve resolve endpoint with params
  38. // It will resolve with each supported resolver until anyone resolved
  39. func Resolve(param *ResolveParam) (endpoint string, err error) {
  40. supportedResolvers := getAllResolvers()
  41. var lastErr error
  42. for _, resolver := range supportedResolvers {
  43. endpoint, supported, resolveErr := resolver.TryResolve(param)
  44. if resolveErr != nil {
  45. lastErr = resolveErr
  46. }
  47. if supported {
  48. debug("resolve endpoint with %s\n", param)
  49. debug("\t%s by resolver(%s)\n", endpoint, resolver.GetName())
  50. return endpoint, nil
  51. }
  52. }
  53. // not support
  54. errorMsg := fmt.Sprintf(errors.CanNotResolveEndpointErrorMessage, param, ResolveEndpointUserGuideLink)
  55. err = errors.NewClientError(errors.CanNotResolveEndpointErrorCode, errorMsg, lastErr)
  56. return
  57. }
  58. func getAllResolvers() []Resolver {
  59. once.Do(func() {
  60. resolvers = []Resolver{
  61. &LocationResolver{},
  62. &LocalRegionalResolver{},
  63. &LocalGlobalResolver{},
  64. }
  65. })
  66. return resolvers
  67. }
  68. type ResolveParam struct {
  69. Domain string
  70. Product string
  71. RegionId string
  72. LocationProduct string
  73. LocationEndpointType string
  74. CommonApi func(request *requests.CommonRequest) (response *responses.CommonResponse, err error) `json:"-"`
  75. }
  76. func (param *ResolveParam) String() string {
  77. jsonBytes, err := json.Marshal(param)
  78. if err != nil {
  79. return fmt.Sprint("ResolveParam.String() process error:", err)
  80. }
  81. return string(jsonBytes)
  82. }