8 "github.com/aws/aws-sdk-go/aws/awserr"
11 type modelDefinition map[string]json.RawMessage
13 // A DecodeModelOptions are the options for how the endpoints model definition
15 type DecodeModelOptions struct {
16 SkipCustomizations bool
19 // Set combines all of the option functions together.
20 func (d *DecodeModelOptions) Set(optFns ...func(*DecodeModelOptions)) {
21 for _, fn := range optFns {
26 // DecodeModel unmarshals a Regions and Endpoint model definition file into
27 // a endpoint Resolver. If the file format is not supported, or an error occurs
28 // when unmarshaling the model an error will be returned.
30 // Casting the return value of this func to a EnumPartitions will
31 // allow you to get a list of the partitions in the order the endpoints
32 // will be resolved in.
34 // resolver, err := endpoints.DecodeModel(reader)
36 // partitions := resolver.(endpoints.EnumPartitions).Partitions()
37 // for _, p := range partitions {
38 // // ... inspect partitions
40 func DecodeModel(r io.Reader, optFns ...func(*DecodeModelOptions)) (Resolver, error) {
41 var opts DecodeModelOptions
44 // Get the version of the partition file to determine what
45 // unmarshaling model to use.
46 modelDef := modelDefinition{}
47 if err := json.NewDecoder(r).Decode(&modelDef); err != nil {
48 return nil, newDecodeModelError("failed to decode endpoints model", err)
52 if b, ok := modelDef["version"]; ok {
55 return nil, newDecodeModelError("endpoints version not found in model", nil)
59 return decodeV3Endpoints(modelDef, opts)
62 return nil, newDecodeModelError(
63 fmt.Sprintf("endpoints version %s, not supported", version), nil)
66 func decodeV3Endpoints(modelDef modelDefinition, opts DecodeModelOptions) (Resolver, error) {
67 b, ok := modelDef["partitions"]
69 return nil, newDecodeModelError("endpoints model missing partitions", nil)
73 if err := json.Unmarshal(b, &ps); err != nil {
74 return nil, newDecodeModelError("failed to decode endpoints model", err)
77 if opts.SkipCustomizations {
82 for i := 0; i < len(ps); i++ {
86 custRmIotDataService(p)
87 custFixAppAutoscalingChina(p)
88 custFixAppAutoscalingUsGov(p)
94 func custAddS3DualStack(p *partition) {
99 custAddDualstack(p, "s3")
100 custAddDualstack(p, "s3-control")
103 func custAddDualstack(p *partition, svcName string) {
104 s, ok := p.Services[svcName]
109 s.Defaults.HasDualStack = boxedTrue
110 s.Defaults.DualStackHostname = "{service}.dualstack.{region}.{dnsSuffix}"
112 p.Services[svcName] = s
115 func custAddEC2Metadata(p *partition) {
116 p.Services["ec2metadata"] = service{
117 IsRegionalized: boxedFalse,
118 PartitionEndpoint: "aws-global",
119 Endpoints: endpoints{
120 "aws-global": endpoint{
121 Hostname: "169.254.169.254/latest",
122 Protocols: []string{"http"},
128 func custRmIotDataService(p *partition) {
129 delete(p.Services, "data.iot")
132 func custFixAppAutoscalingChina(p *partition) {
133 if p.ID != "aws-cn" {
137 const serviceName = "application-autoscaling"
138 s, ok := p.Services[serviceName]
143 const expectHostname = `autoscaling.{region}.amazonaws.com`
144 if e, a := s.Defaults.Hostname, expectHostname; e != a {
145 fmt.Printf("custFixAppAutoscalingChina: ignoring customization, expected %s, got %s\n", e, a)
149 s.Defaults.Hostname = expectHostname + ".cn"
150 p.Services[serviceName] = s
153 func custFixAppAutoscalingUsGov(p *partition) {
154 if p.ID != "aws-us-gov" {
158 const serviceName = "application-autoscaling"
159 s, ok := p.Services[serviceName]
164 if a := s.Defaults.CredentialScope.Service; a != "" {
165 fmt.Printf("custFixAppAutoscalingUsGov: ignoring customization, expected empty credential scope service, got %s\n", a)
169 if a := s.Defaults.Hostname; a != "" {
170 fmt.Printf("custFixAppAutoscalingUsGov: ignoring customization, expected empty hostname, got %s\n", a)
174 s.Defaults.CredentialScope.Service = "application-autoscaling"
175 s.Defaults.Hostname = "autoscaling.{region}.amazonaws.com"
177 p.Services[serviceName] = s
180 type decodeModelError struct {
184 func newDecodeModelError(msg string, err error) decodeModelError {
185 return decodeModelError{
186 awsError: awserr.New("DecodeEndpointsModelError", msg, err),