resolver.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  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. "sync"
  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. func Resolve(param *ResolveParam) (endpoint string, err error) {
  38. supportedResolvers := getAllResolvers()
  39. var lastErr error
  40. var supported bool
  41. for _, resolver := range supportedResolvers {
  42. endpoint, supported, lastErr = resolver.TryResolve(param)
  43. if supported {
  44. debug("resolve endpoint with %s\n", param)
  45. debug("\t%s by resolver(%s)\n", endpoint, resolver.GetName())
  46. return endpoint, nil
  47. }
  48. }
  49. // not support
  50. errorMsg := fmt.Sprintf(errors.CanNotResolveEndpointErrorMessage, param, ResolveEndpointUserGuideLink)
  51. err = errors.NewClientError(errors.CanNotResolveEndpointErrorCode, errorMsg, lastErr)
  52. return
  53. }
  54. func getAllResolvers() []Resolver {
  55. once.Do(func() {
  56. resolvers = []Resolver{
  57. &SimpleHostResolver{},
  58. &MappingResolver{},
  59. &LocationResolver{},
  60. &LocalRegionalResolver{},
  61. &LocalGlobalResolver{},
  62. }
  63. })
  64. return resolvers
  65. }
  66. type ResolveParam struct {
  67. Domain string
  68. Product string
  69. RegionId string
  70. LocationProduct string
  71. LocationEndpointType string
  72. CommonApi func(request *requests.CommonRequest) (response *responses.CommonResponse, err error) `json:"-"`
  73. }
  74. func (param *ResolveParam) String() string {
  75. jsonBytes, err := json.Marshal(param)
  76. if err != nil {
  77. return fmt.Sprint("ResolveParam.String() process error:", err)
  78. }
  79. return string(jsonBytes)
  80. }