local_xml_resolver.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/xml"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "sync"
  21. )
  22. var readXmlOnce sync.Once
  23. var v = Endpoints{}
  24. type LocalXmlResolver struct {
  25. }
  26. func (resolver *LocalXmlResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
  27. readXmlOnce.Do(func() {
  28. dir, _ := os.Getwd()
  29. file, err := os.Open(dir + "/endpoints/endpoints.xml")
  30. if err != nil {
  31. fmt.Printf("error: %v", err)
  32. support = false
  33. return
  34. }
  35. defer file.Close()
  36. data, err := ioutil.ReadAll(file)
  37. if err != nil {
  38. fmt.Printf("error: %v", err)
  39. support = false
  40. return
  41. }
  42. err = xml.Unmarshal(data, &v)
  43. if err != nil {
  44. fmt.Printf("error: %v", err)
  45. support = false
  46. return
  47. }
  48. })
  49. for _, xmlEndpoint := range v.EndpointList {
  50. for _, xmlRegionId := range xmlEndpoint.RegionIds.Id {
  51. if xmlRegionId == param.RegionId {
  52. for _, xmlProduct := range xmlEndpoint.Products.ProductList {
  53. if xmlProduct.ProductName == param.Product {
  54. endpoint = xmlProduct.DomainName
  55. support = true
  56. return
  57. }
  58. }
  59. }
  60. }
  61. }
  62. support = false
  63. return
  64. }
  65. type Endpoints struct {
  66. XMLName xml.Name `xml:"Endpoints"`
  67. EndpointList []Endpoint `xml:"Endpoint"`
  68. }
  69. type Endpoint struct {
  70. XMLName xml.Name `xml:"Endpoint"`
  71. Name string `xml:"name,attr"`
  72. RegionIds RegionIds `xml:"RegionIds"`
  73. Products Products `xml:"Products"`
  74. }
  75. type RegionIds struct {
  76. Id []string `xml:"RegionId"`
  77. }
  78. type Products struct {
  79. ProductList []Product `xml:"Product"`
  80. }
  81. type RegionId struct {
  82. XMLName string `xml:"RegionId"`
  83. }
  84. type Product struct {
  85. ProductName string `xml:"ProductName"`
  86. DomainName string `xml:"DomainName"`
  87. }