mapping_resolver.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. "fmt"
  17. "strings"
  18. )
  19. const keyFormatter = "%s::%s"
  20. var endpointMapping = make(map[string]string)
  21. // AddEndpointMapping Use product id and region id as key to store the endpoint into inner map
  22. func AddEndpointMapping(regionId, productId, endpoint string) (err error) {
  23. key := fmt.Sprintf(keyFormatter, strings.ToLower(regionId), strings.ToLower(productId))
  24. endpointMapping[key] = endpoint
  25. return nil
  26. }
  27. // MappingResolver the mapping resolver type
  28. type MappingResolver struct {
  29. }
  30. // GetName get the resolver name: "mapping resolver"
  31. func (resolver *MappingResolver) GetName() (name string) {
  32. name = "mapping resolver"
  33. return
  34. }
  35. // TryResolve use Product and RegionId as key to find endpoint from inner map
  36. func (resolver *MappingResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
  37. key := fmt.Sprintf(keyFormatter, strings.ToLower(param.RegionId), strings.ToLower(param.Product))
  38. endpoint, contains := endpointMapping[key]
  39. return endpoint, contains, nil
  40. }