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)
92 func custAddS3DualStack(p *partition) {
97 s, ok := p.Services["s3"]
102 s.Defaults.HasDualStack = boxedTrue
103 s.Defaults.DualStackHostname = "{service}.dualstack.{region}.{dnsSuffix}"
108 func custAddEC2Metadata(p *partition) {
109 p.Services["ec2metadata"] = service{
110 IsRegionalized: boxedFalse,
111 PartitionEndpoint: "aws-global",
112 Endpoints: endpoints{
113 "aws-global": endpoint{
114 Hostname: "169.254.169.254/latest",
115 Protocols: []string{"http"},
121 func custRmIotDataService(p *partition) {
122 delete(p.Services, "data.iot")
125 type decodeModelError struct {
129 func newDecodeModelError(msg string, err error) decodeModelError {
130 return decodeModelError{
131 awsError: awserr.New("DecodeEndpointsModelError", msg, err),