forked from aws/aws-dax-go
/
request_options.go
177 lines (161 loc) · 5.47 KB
/
request_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
package client
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client/metadata"
"github.com/aws/aws-sdk-go/aws/request"
"time"
)
type RequestOptions struct {
LogLevel aws.LogLevelType
Logger aws.Logger
RetryDelay time.Duration
MaxRetries int
SleepDelayFn func(time.Duration)
Context aws.Context
}
func (o *RequestOptions) applyTo(r *request.Request) {
if r != nil {
r.Config.LogLevel = aws.LogLevel(o.LogLevel)
r.Config.Logger = o.Logger
r.RetryDelay = o.RetryDelay
r.Config.MaxRetries = aws.Int(o.MaxRetries)
r.Config.SleepDelay = o.SleepDelayFn
if o.Context != nil {
r.SetContext(o.Context)
}
}
}
func (o *RequestOptions) MergeFromRequestOptions(ctx aws.Context, opts ...request.Option) error {
if len(opts) == 0 {
if ctx != nil {
o.Context = ctx
}
return nil
}
// New request has to be created to avoid panics when setting fields
r := request.New(aws.Config{}, metadata.ClientInfo{}, request.Handlers{}, nil, &request.Operation{}, nil, nil)
r.ApplyOptions(opts...)
if err := o.mergeFromRequest(r, true); err != nil {
return err
}
if ctx != nil {
o.Context = ctx
}
return nil
}
func (o *RequestOptions) mergeFromRequest(r *request.Request, validate bool) error {
if r == nil {
return nil
}
if validate {
if err := ValidateRequest(r); err != nil {
return err
}
}
if r.Config.LogLevel != nil {
o.LogLevel = *r.Config.LogLevel
}
if r.Config.Logger != nil {
o.Logger = r.Config.Logger
}
if r.RetryDelay >= 0 {
o.RetryDelay = r.RetryDelay
}
if r.Config.MaxRetries != nil {
o.MaxRetries = *r.Config.MaxRetries
}
if r.Config.SleepDelay != nil {
o.SleepDelayFn = r.Config.SleepDelay
}
if r.Context() != nil { // TODO Should the Context() from Request override the one in RequestOptions
o.Context = r.Context()
}
return nil
}
func ValidateRequest(r *request.Request) error {
if r == nil {
return nil
}
if err := ValidateHandlers(r.Handlers, true); err != nil {
return err
}
if r.Retryable != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: Retryable", nil)
}
if len(r.SignedHeaderVals) > 0 {
return awserr.New(request.InvalidParameterErrCode, "custom signed headers not supported", nil)
}
return ValidateConfig(r.Config, true)
}
func ValidateHandlers(h request.Handlers, expectDaxHandlers bool) error {
if h.Validate.Len() > 0 || h.Sign.Len() > 0 || h.ValidateResponse.Len() > 0 ||
h.Unmarshal.Len() > 0 || h.UnmarshalMeta.Len() > 0 || h.UnmarshalError.Len() > 0 ||
h.Retry.Len() > 0 || h.AfterRetry.Len() > 0 || h.Complete.Len() > 0 {
return awserr.New(request.InvalidParameterErrCode, "custom handlers not supported", nil)
}
e := 0
if expectDaxHandlers {
e = 1
}
if h.Build.Len() > e || h.Send.Len() > e {
return awserr.New(request.InvalidParameterErrCode, "custom build or send handlers not supported", nil)
}
return nil
}
func ValidateConfig(c aws.Config, isRequestConfig bool) error {
if c.CredentialsChainVerboseErrors != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: CredentialsChainVerboseErrors", nil)
}
if c.EndpointResolver != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: EndpointResolver", nil)
}
if c.EnforceShouldRetryCheck != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: EnforceShouldRetryCheck", nil)
}
if c.DisableSSL != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: DisableSSL", nil)
}
if c.HTTPClient != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: HTTPClient", nil)
}
if c.Retryer != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: Retryer", nil)
}
if c.DisableParamValidation != nil && *c.DisableParamValidation {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: DisableParamValidation", nil)
}
if c.DisableComputeChecksums != nil && *c.DisableComputeChecksums {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: DisableComputeChecksums", nil)
}
if c.UseDualStack != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: UseDualStack", nil)
}
if c.DisableRestProtocolURICleaning != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: DisableRestProtocolURICleaning", nil)
}
// Skip validation of S3* and EC2* options
if isRequestConfig {
if c.Credentials != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: Credentials per request. Set Credentials at client init", nil)
}
if c.Endpoint != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: Endpoint per request. Set Endpoint at client init", nil)
}
if c.Region != nil {
return awserr.New(request.InvalidParameterErrCode, "unsupported config: Region per request. Set Region at client init", nil)
}
}
return nil
}