]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/aws/aws-sdk-go/aws/endpoints/decode.go
Initial transfer of provider code
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / aws / aws-sdk-go / aws / endpoints / decode.go
CommitLineData
bae9f6d2
JC
1package endpoints
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7
8 "github.com/aws/aws-sdk-go/aws/awserr"
9)
10
11type modelDefinition map[string]json.RawMessage
12
13// A DecodeModelOptions are the options for how the endpoints model definition
14// are decoded.
15type DecodeModelOptions struct {
16 SkipCustomizations bool
17}
18
19// Set combines all of the option functions together.
20func (d *DecodeModelOptions) Set(optFns ...func(*DecodeModelOptions)) {
21 for _, fn := range optFns {
22 fn(d)
23 }
24}
25
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.
29//
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.
33//
34// resolver, err := endpoints.DecodeModel(reader)
35//
36// partitions := resolver.(endpoints.EnumPartitions).Partitions()
37// for _, p := range partitions {
38// // ... inspect partitions
39// }
40func DecodeModel(r io.Reader, optFns ...func(*DecodeModelOptions)) (Resolver, error) {
41 var opts DecodeModelOptions
42 opts.Set(optFns...)
43
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)
49 }
50
51 var version string
52 if b, ok := modelDef["version"]; ok {
53 version = string(b)
54 } else {
55 return nil, newDecodeModelError("endpoints version not found in model", nil)
56 }
57
58 if version == "3" {
59 return decodeV3Endpoints(modelDef, opts)
60 }
61
62 return nil, newDecodeModelError(
63 fmt.Sprintf("endpoints version %s, not supported", version), nil)
64}
65
66func decodeV3Endpoints(modelDef modelDefinition, opts DecodeModelOptions) (Resolver, error) {
67 b, ok := modelDef["partitions"]
68 if !ok {
69 return nil, newDecodeModelError("endpoints model missing partitions", nil)
70 }
71
72 ps := partitions{}
73 if err := json.Unmarshal(b, &ps); err != nil {
74 return nil, newDecodeModelError("failed to decode endpoints model", err)
75 }
76
77 if opts.SkipCustomizations {
78 return ps, nil
79 }
80
81 // Customization
82 for i := 0; i < len(ps); i++ {
83 p := &ps[i]
84 custAddEC2Metadata(p)
85 custAddS3DualStack(p)
86 custRmIotDataService(p)
87 }
88
89 return ps, nil
90}
91
92func custAddS3DualStack(p *partition) {
93 if p.ID != "aws" {
94 return
95 }
96
97 s, ok := p.Services["s3"]
98 if !ok {
99 return
100 }
101
102 s.Defaults.HasDualStack = boxedTrue
103 s.Defaults.DualStackHostname = "{service}.dualstack.{region}.{dnsSuffix}"
104
105 p.Services["s3"] = s
106}
107
108func 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"},
116 },
117 },
118 }
119}
120
121func custRmIotDataService(p *partition) {
122 delete(p.Services, "data.iot")
123}
124
125type decodeModelError struct {
126 awsError
127}
128
129func newDecodeModelError(msg string, err error) decodeModelError {
130 return decodeModelError{
131 awsError: awserr.New("DecodeEndpointsModelError", msg, err),
132 }
133}