Skip to content

Commit dad4b8d

Browse files
authored
aws: Endpoints Usage Refactor for aws.Request and aws.Metadata (#473)
1 parent 275dffc commit dad4b8d

File tree

18 files changed

+136
-40
lines changed

18 files changed

+136
-40
lines changed

CHANGELOG_PENDING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ Breaking Change
88
* `private/model`: Add utility for validating shape names for structs and enums for the service packages ([#471](https://github.com/aws/aws-sdk-go-v2/pull/471))
99
* Fixes bug which allowed service package structs, enums to start with non alphabetic character
1010
* Fixes the incorrect enum types in mediapackage service package, changing enum types __AdTriggersElement, __PeriodTriggersElement to AdTriggersElement, PeriodTriggersElement respectively.
11+
* `aws`: Client, Metadata, and Request structures have been refactored to simplify the usage of resolved endpoints ([#473](https://github.com/aws/aws-sdk-go-v2/pull/473))
12+
* `aws.Client.Endpoint` struct member has been removed, and `aws.Request.Endpoint` struct member has been added of type `aws.Endpoint`
13+
* `aws.Client.Region` structure member has been removed
1114

1215
Services
1316
---
1417

1518
SDK Features
1619
---
20+
* `aws`: `PartitionID` has been added to `aws.Endpoint` structure, and is used by the endpoint resolver to indicate which AWS partition an endpoint was resolved for ([#473](https://github.com/aws/aws-sdk-go-v2/pull/473))
21+
* `aws/endpoints`: Updated resolvers to populate `PartitionID` for a resolved `aws.Endpoint` ([#473](https://github.com/aws/aws-sdk-go-v2/pull/473))
1722

1823
SDK Enhancements
1924
---

aws/client.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type Metadata struct {
1111
EndpointsID string
1212
APIVersion string
1313

14-
Endpoint string
1514
SigningName string
1615
SigningRegion string
1716

@@ -24,7 +23,6 @@ type Metadata struct {
2423
type Client struct {
2524
Metadata Metadata
2625
Config Config
27-
Region string
2826
Credentials CredentialsProvider
2927
EndpointResolver EndpointResolver
3028
Handlers Handlers
@@ -42,7 +40,6 @@ func NewClient(cfg Config, metadata Metadata) *Client {
4240
// TODO remove config when request refactored
4341
Config: cfg,
4442

45-
Region: cfg.Region,
4643
Credentials: cfg.Credentials,
4744
EndpointResolver: cfg.EndpointResolver,
4845
Handlers: cfg.Handlers.Copy(),

aws/defaults/handlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ var AfterRetryHandler = aws.NamedHandler{
216216
// appropriate Region and Endpoint set. Will set r.Error if the endpoint or
217217
// region is not valid.
218218
var ValidateEndpointHandler = aws.NamedHandler{Name: "core.ValidateEndpointHandler", Fn: func(r *aws.Request) {
219-
if r.Metadata.SigningRegion == "" && r.Config.Region == "" {
219+
if r.Endpoint.SigningRegion == "" && r.Config.Region == "" {
220220
r.Error = aws.ErrMissingRegion
221-
} else if r.Metadata.Endpoint == "" {
221+
} else if len(r.Endpoint.URL) == 0 {
222222
r.Error = aws.ErrMissingEndpoint
223223
}
224224
}}

aws/ec2metadata/api_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func unmarshalError(r *aws.Request) {
227227
}
228228

229229
func validateEndpointHandler(r *aws.Request) {
230-
if r.Metadata.Endpoint == "" {
230+
if len(r.Endpoint.URL) == 0 {
231231
r.Error = aws.ErrMissingEndpoint
232232
}
233233
}

aws/endpointcreds/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (p *Provider) getCredentials(ctx context.Context) (*getCredentialsOutput, e
134134
}
135135

136136
func validateEndpointHandler(r *aws.Request) {
137-
if len(r.Metadata.Endpoint) == 0 {
137+
if len(r.Endpoint.URL) == 0 {
138138
r.Error = aws.ErrMissingEndpoint
139139
}
140140
}

aws/endpoints.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type Endpoint struct {
3535
// The URL of the endpoint.
3636
URL string
3737

38+
// The endpoint partition
39+
PartitionID string
40+
3841
// The service name that should be used for signing the requests to the
3942
// endpoint.
4043
SigningName string

aws/endpoints/v3model.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (p partition) EndpointFor(service, region string, opts ResolveOptions) (res
101101
}
102102

103103
defs := []endpoint{p.Defaults, s.Defaults}
104-
return e.resolve(service, region, p.DNSSuffix, defs, opts), nil
104+
return e.resolve(service, p.ID, region, p.DNSSuffix, defs, opts), nil
105105
}
106106

107107
func serviceList(ss services) []string {
@@ -213,7 +213,7 @@ func getByPriority(s []string, p []string, def string) string {
213213
return s[0]
214214
}
215215

216-
func (e endpoint) resolve(service, region, dnsSuffix string, defs []endpoint, opts ResolveOptions) aws.Endpoint {
216+
func (e endpoint) resolve(service, parittionID, region, dnsSuffix string, defs []endpoint, opts ResolveOptions) aws.Endpoint {
217217
var merged endpoint
218218
for _, def := range defs {
219219
merged.mergeIn(def)
@@ -253,6 +253,7 @@ func (e endpoint) resolve(service, region, dnsSuffix string, defs []endpoint, op
253253

254254
return aws.Endpoint{
255255
URL: u,
256+
PartitionID: parittionID,
256257
SigningRegion: signingRegion,
257258
SigningName: signingName,
258259
SigningNameDerived: signingNameDerived,

aws/endpoints/v3model_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,14 @@ func TestEndpointResolve(t *testing.T) {
207207
SSLCommonName: "new sslCommonName",
208208
}
209209

210-
resolved := e.resolve("service", "region", "dnsSuffix",
211-
defs, ResolveOptions{},
212-
)
210+
resolved := e.resolve("service", "partition", "region", "dnsSuffix", defs, ResolveOptions{})
213211

214212
if e, a := "https://service.region.dnsSuffix", resolved.URL; e != a {
215213
t.Errorf("expect %v, got %v", e, a)
216214
}
215+
if e, a := "partition", resolved.PartitionID; e != a {
216+
t.Errorf("expect %v, got %v", e, a)
217+
}
217218
if e, a := "signing_service", resolved.SigningName; e != a {
218219
t.Errorf("expect %v, got %v", e, a)
219220
}

aws/request.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
type Request struct {
3737
Config Config
3838
Metadata Metadata
39+
Endpoint Endpoint
3940
Handlers Handlers
4041
Retryer Retryer
4142
AttemptTime time.Time
@@ -111,12 +112,12 @@ func New(cfg Config, metadata Metadata, handlers Handlers,
111112

112113
if err == nil {
113114
// TODO so ugly
114-
metadata.Endpoint = endpoint.URL
115-
if len(endpoint.SigningName) > 0 && !endpoint.SigningNameDerived {
116-
metadata.SigningName = endpoint.SigningName
115+
if len(endpoint.SigningRegion) == 0 {
116+
endpoint.SigningRegion = metadata.SigningRegion
117117
}
118-
if len(endpoint.SigningRegion) > 0 {
119-
metadata.SigningRegion = endpoint.SigningRegion
118+
119+
if len(endpoint.SigningName) == 0 || (len(endpoint.SigningName) > 0 && endpoint.SigningNameDerived) {
120+
endpoint.SigningName = metadata.SigningName
120121
}
121122

122123
httpReq.URL, err = url.Parse(endpoint.URL + operation.HTTPPath)
@@ -127,10 +128,10 @@ func New(cfg Config, metadata Metadata, handlers Handlers,
127128
}
128129

129130
r := &Request{
130-
Config: cfg,
131-
Metadata: metadata,
132-
Handlers: handlers.Copy(),
133-
131+
Config: cfg,
132+
Metadata: metadata,
133+
Endpoint: endpoint,
134+
Handlers: handlers.Copy(),
134135
Retryer: retryer,
135136
Time: time.Now(),
136137
ExpireTime: 0,

aws/request_test.go

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ func TestSerializationErrConnectionReset(t *testing.T) {
754754
ServiceName: "fooService",
755755
SigningName: "foo",
756756
SigningRegion: "foo",
757-
Endpoint: "localhost",
758757
APIVersion: "2001-01-01",
759758
JSONVersion: "1.1",
760759
TargetPrefix: "Foo",
@@ -1151,3 +1150,97 @@ func (f *stubSeekFail) ReadAt(b []byte, offset int64) (int, error) {
11511150
func (f *stubSeekFail) Seek(offset int64, mode int) (int64, error) {
11521151
return 0, f.Err
11531152
}
1153+
1154+
func TestRequestEndpointConstruction(t *testing.T) {
1155+
cases := map[string]struct {
1156+
EndpointResolver aws.EndpointResolverFunc
1157+
ExpectedEndpoint aws.Endpoint
1158+
}{
1159+
"resolved modeled endpoint": {
1160+
EndpointResolver: func(_, _ string) (aws.Endpoint, error) {
1161+
return aws.Endpoint{
1162+
URL: "https://localhost",
1163+
SigningName: "foo-service",
1164+
SigningRegion: "bar-region",
1165+
}, nil
1166+
},
1167+
ExpectedEndpoint: aws.Endpoint{
1168+
URL: "https://localhost",
1169+
SigningName: "foo-service",
1170+
SigningRegion: "bar-region",
1171+
},
1172+
},
1173+
"resolved endpoint missing signing region": {
1174+
EndpointResolver: func(_, _ string) (aws.Endpoint, error) {
1175+
return aws.Endpoint{
1176+
URL: "https://localhost",
1177+
SigningName: "foo-service",
1178+
}, nil
1179+
},
1180+
ExpectedEndpoint: aws.Endpoint{
1181+
URL: "https://localhost",
1182+
SigningName: "foo-service",
1183+
SigningRegion: "meta-bar-region",
1184+
},
1185+
},
1186+
"resolved endpoint missing signing name": {
1187+
EndpointResolver: func(_, _ string) (aws.Endpoint, error) {
1188+
return aws.Endpoint{
1189+
URL: "https://localhost",
1190+
SigningRegion: "bar-region",
1191+
}, nil
1192+
},
1193+
ExpectedEndpoint: aws.Endpoint{
1194+
URL: "https://localhost",
1195+
SigningName: "meta-foo-service",
1196+
SigningRegion: "bar-region",
1197+
},
1198+
},
1199+
"resolved endpoint signing name derived": {
1200+
EndpointResolver: func(_, _ string) (aws.Endpoint, error) {
1201+
return aws.Endpoint{
1202+
URL: "https://localhost",
1203+
SigningRegion: "bar-region",
1204+
SigningName: "derived-signing-name",
1205+
SigningNameDerived: true,
1206+
}, nil
1207+
},
1208+
ExpectedEndpoint: aws.Endpoint{
1209+
URL: "https://localhost",
1210+
SigningName: "meta-foo-service",
1211+
SigningRegion: "bar-region",
1212+
SigningNameDerived: true,
1213+
},
1214+
},
1215+
"resolved endpoint missing signing region and signing name": {
1216+
EndpointResolver: func(_, _ string) (aws.Endpoint, error) {
1217+
return aws.Endpoint{
1218+
URL: "https://localhost",
1219+
}, nil
1220+
},
1221+
ExpectedEndpoint: aws.Endpoint{
1222+
URL: "https://localhost",
1223+
SigningName: "meta-foo-service",
1224+
SigningRegion: "meta-bar-region",
1225+
},
1226+
},
1227+
}
1228+
1229+
meta := aws.Metadata{
1230+
ServiceName: "FooService",
1231+
SigningName: "meta-foo-service",
1232+
SigningRegion: "meta-bar-region",
1233+
}
1234+
1235+
for name, tt := range cases {
1236+
t.Run(name, func(t *testing.T) {
1237+
client := aws.NewClient(aws.Config{EndpointResolver: tt.EndpointResolver}, meta)
1238+
1239+
request := client.NewRequest(&aws.Operation{Name: "ZapOperation", HTTPMethod: "PUT", HTTPPath: "/"}, nil, nil)
1240+
1241+
if e, a := tt.ExpectedEndpoint, request.Endpoint; !reflect.DeepEqual(e, a) {
1242+
t.Errorf("expected %v, got %v", e, a)
1243+
}
1244+
})
1245+
}
1246+
}

0 commit comments

Comments
 (0)