1package aws
2
3import (
4	"net/http"
5	"time"
6
7	"github.com/aws/aws-sdk-go/aws/credentials"
8)
9
10// UseServiceDefaultRetries instructs the config to use the service's own
11// default number of retries. This will be the default action if
12// Config.MaxRetries is nil also.
13const UseServiceDefaultRetries = -1
14
15// RequestRetryer is an alias for a type that implements the request.Retryer
16// interface.
17type RequestRetryer interface{}
18
19// A Config provides service configuration for service clients. By default,
20// all clients will use the defaults.DefaultConfig tructure.
21//
22//     // Create Session with MaxRetry configuration to be shared by multiple
23//     // service clients.
24//     sess, err := session.NewSession(&aws.Config{
25//         MaxRetries: aws.Int(3),
26//     })
27//
28//     // Create S3 service client with a specific Region.
29//     svc := s3.New(sess, &aws.Config{
30//         Region: aws.String("us-west-2"),
31//     })
32type Config struct {
33	// Enables verbose error printing of all credential chain errors.
34	// Should be used when wanting to see all errors while attempting to
35	// retrieve credentials.
36	CredentialsChainVerboseErrors *bool
37
38	// The credentials object to use when signing requests. Defaults to a
39	// chain of credential providers to search for credentials in environment
40	// variables, shared credential file, and EC2 Instance Roles.
41	Credentials *credentials.Credentials
42
43	// An optional endpoint URL (hostname only or fully qualified URI)
44	// that overrides the default generated endpoint for a client. Set this
45	// to `""` to use the default generated endpoint.
46	//
47	// @note You must still provide a `Region` value when specifying an
48	//   endpoint for a client.
49	Endpoint *string
50
51	// The region to send requests to. This parameter is required and must
52	// be configured globally or on a per-client basis unless otherwise
53	// noted. A full list of regions is found in the "Regions and Endpoints"
54	// document.
55	//
56	// @see http://docs.aws.amazon.com/general/latest/gr/rande.html
57	//   AWS Regions and Endpoints
58	Region *string
59
60	// Set this to `true` to disable SSL when sending requests. Defaults
61	// to `false`.
62	DisableSSL *bool
63
64	// The HTTP client to use when sending requests. Defaults to
65	// `http.DefaultClient`.
66	HTTPClient *http.Client
67
68	// An integer value representing the logging level. The default log level
69	// is zero (LogOff), which represents no logging. To enable logging set
70	// to a LogLevel Value.
71	LogLevel *LogLevelType
72
73	// The logger writer interface to write logging messages to. Defaults to
74	// standard out.
75	Logger Logger
76
77	// The maximum number of times that a request will be retried for failures.
78	// Defaults to -1, which defers the max retry setting to the service
79	// specific configuration.
80	MaxRetries *int
81
82	// Retryer guides how HTTP requests should be retried in case of
83	// recoverable failures.
84	//
85	// When nil or the value does not implement the request.Retryer interface,
86	// the request.DefaultRetryer will be used.
87	//
88	// When both Retryer and MaxRetries are non-nil, the former is used and
89	// the latter ignored.
90	//
91	// To set the Retryer field in a type-safe manner and with chaining, use
92	// the request.WithRetryer helper function:
93	//
94	//   cfg := request.WithRetryer(aws.NewConfig(), myRetryer)
95	//
96	Retryer RequestRetryer
97
98	// Disables semantic parameter validation, which validates input for
99	// missing required fields and/or other semantic request input errors.
100	DisableParamValidation *bool
101
102	// Disables the computation of request and response checksums, e.g.,
103	// CRC32 checksums in Amazon DynamoDB.
104	DisableComputeChecksums *bool
105
106	// Set this to `true` to force the request to use path-style addressing,
107	// i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client
108	// will use virtual hosted bucket addressing when possible
109	// (`http://BUCKET.s3.amazonaws.com/KEY`).
110	//
111	// @note This configuration option is specific to the Amazon S3 service.
112	// @see http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
113	//   Amazon S3: Virtual Hosting of Buckets
114	S3ForcePathStyle *bool
115
116	// Set this to `true` to disable the SDK adding the `Expect: 100-Continue`
117	// header to PUT requests over 2MB of content. 100-Continue instructs the
118	// HTTP client not to send the body until the service responds with a
119	// `continue` status. This is useful to prevent sending the request body
120	// until after the request is authenticated, and validated.
121	//
122	// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
123	//
124	// 100-Continue is only enabled for Go 1.6 and above. See `http.Transport`'s
125	// `ExpectContinueTimeout` for information on adjusting the continue wait
126	// timeout. https://golang.org/pkg/net/http/#Transport
127	//
128	// You should use this flag to disble 100-Continue if you experience issues
129	// with proxies or third party S3 compatible services.
130	S3Disable100Continue *bool
131
132	// Set this to `true` to enable S3 Accelerate feature. For all operations
133	// compatible with S3 Accelerate will use the accelerate endpoint for
134	// requests. Requests not compatible will fall back to normal S3 requests.
135	//
136	// The bucket must be enable for accelerate to be used with S3 client with
137	// accelerate enabled. If the bucket is not enabled for accelerate an error
138	// will be returned. The bucket name must be DNS compatible to also work
139	// with accelerate.
140	//
141	// Not compatible with UseDualStack requests will fail if both flags are
142	// specified.
143	S3UseAccelerate *bool
144
145	// Set this to `true` to disable the EC2Metadata client from overriding the
146	// default http.Client's Timeout. This is helpful if you do not want the
147	// EC2Metadata client to create a new http.Client. This options is only
148	// meaningful if you're not already using a custom HTTP client with the
149	// SDK. Enabled by default.
150	//
151	// Must be set and provided to the session.NewSession() in order to disable
152	// the EC2Metadata overriding the timeout for default credentials chain.
153	//
154	// Example:
155	//    sess, err := session.NewSession(aws.NewConfig().WithEC2MetadataDiableTimeoutOverride(true))
156	//
157	//    svc := s3.New(sess)
158	//
159	EC2MetadataDisableTimeoutOverride *bool
160
161	// Instructs the endpiont to be generated for a service client to
162	// be the dual stack endpoint. The dual stack endpoint will support
163	// both IPv4 and IPv6 addressing.
164	//
165	// Setting this for a service which does not support dual stack will fail
166	// to make requets. It is not recommended to set this value on the session
167	// as it will apply to all service clients created with the session. Even
168	// services which don't support dual stack endpoints.
169	//
170	// If the Endpoint config value is also provided the UseDualStack flag
171	// will be ignored.
172	//
173	// Only supported with.
174	//
175	//     sess, err := session.NewSession()
176	//
177	//     svc := s3.New(sess, &aws.Config{
178	//         UseDualStack: aws.Bool(true),
179	//     })
180	UseDualStack *bool
181
182	// SleepDelay is an override for the func the SDK will call when sleeping
183	// during the lifecycle of a request. Specifically this will be used for
184	// request delays. This value should only be used for testing. To adjust
185	// the delay of a request see the aws/client.DefaultRetryer and
186	// aws/request.Retryer.
187	SleepDelay func(time.Duration)
188}
189
190// NewConfig returns a new Config pointer that can be chained with builder
191// methods to set multiple configuration values inline without using pointers.
192//
193//     // Create Session with MaxRetry configuration to be shared by multiple
194//     // service clients.
195//     sess, err := session.NewSession(aws.NewConfig().
196//         WithMaxRetries(3),
197//     )
198//
199//     // Create S3 service client with a specific Region.
200//     svc := s3.New(sess, aws.NewConfig().
201//         WithRegion("us-west-2"),
202//     )
203func NewConfig() *Config {
204	return &Config{}
205}
206
207// WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
208// a Config pointer.
209func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
210	c.CredentialsChainVerboseErrors = &verboseErrs
211	return c
212}
213
214// WithCredentials sets a config Credentials value returning a Config pointer
215// for chaining.
216func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
217	c.Credentials = creds
218	return c
219}
220
221// WithEndpoint sets a config Endpoint value returning a Config pointer for
222// chaining.
223func (c *Config) WithEndpoint(endpoint string) *Config {
224	c.Endpoint = &endpoint
225	return c
226}
227
228// WithRegion sets a config Region value returning a Config pointer for
229// chaining.
230func (c *Config) WithRegion(region string) *Config {
231	c.Region = &region
232	return c
233}
234
235// WithDisableSSL sets a config DisableSSL value returning a Config pointer
236// for chaining.
237func (c *Config) WithDisableSSL(disable bool) *Config {
238	c.DisableSSL = &disable
239	return c
240}
241
242// WithHTTPClient sets a config HTTPClient value returning a Config pointer
243// for chaining.
244func (c *Config) WithHTTPClient(client *http.Client) *Config {
245	c.HTTPClient = client
246	return c
247}
248
249// WithMaxRetries sets a config MaxRetries value returning a Config pointer
250// for chaining.
251func (c *Config) WithMaxRetries(max int) *Config {
252	c.MaxRetries = &max
253	return c
254}
255
256// WithDisableParamValidation sets a config DisableParamValidation value
257// returning a Config pointer for chaining.
258func (c *Config) WithDisableParamValidation(disable bool) *Config {
259	c.DisableParamValidation = &disable
260	return c
261}
262
263// WithDisableComputeChecksums sets a config DisableComputeChecksums value
264// returning a Config pointer for chaining.
265func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
266	c.DisableComputeChecksums = &disable
267	return c
268}
269
270// WithLogLevel sets a config LogLevel value returning a Config pointer for
271// chaining.
272func (c *Config) WithLogLevel(level LogLevelType) *Config {
273	c.LogLevel = &level
274	return c
275}
276
277// WithLogger sets a config Logger value returning a Config pointer for
278// chaining.
279func (c *Config) WithLogger(logger Logger) *Config {
280	c.Logger = logger
281	return c
282}
283
284// WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
285// pointer for chaining.
286func (c *Config) WithS3ForcePathStyle(force bool) *Config {
287	c.S3ForcePathStyle = &force
288	return c
289}
290
291// WithS3Disable100Continue sets a config S3Disable100Continue value returning
292// a Config pointer for chaining.
293func (c *Config) WithS3Disable100Continue(disable bool) *Config {
294	c.S3Disable100Continue = &disable
295	return c
296}
297
298// WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
299// pointer for chaining.
300func (c *Config) WithS3UseAccelerate(enable bool) *Config {
301	c.S3UseAccelerate = &enable
302	return c
303}
304
305// WithUseDualStack sets a config UseDualStack value returning a Config
306// pointer for chaining.
307func (c *Config) WithUseDualStack(enable bool) *Config {
308	c.UseDualStack = &enable
309	return c
310}
311
312// WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
313// returning a Config pointer for chaining.
314func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
315	c.EC2MetadataDisableTimeoutOverride = &enable
316	return c
317}
318
319// WithSleepDelay overrides the function used to sleep while waiting for the
320// next retry. Defaults to time.Sleep.
321func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
322	c.SleepDelay = fn
323	return c
324}
325
326// MergeIn merges the passed in configs into the existing config object.
327func (c *Config) MergeIn(cfgs ...*Config) {
328	for _, other := range cfgs {
329		mergeInConfig(c, other)
330	}
331}
332
333func mergeInConfig(dst *Config, other *Config) {
334	if other == nil {
335		return
336	}
337
338	if other.CredentialsChainVerboseErrors != nil {
339		dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
340	}
341
342	if other.Credentials != nil {
343		dst.Credentials = other.Credentials
344	}
345
346	if other.Endpoint != nil {
347		dst.Endpoint = other.Endpoint
348	}
349
350	if other.Region != nil {
351		dst.Region = other.Region
352	}
353
354	if other.DisableSSL != nil {
355		dst.DisableSSL = other.DisableSSL
356	}
357
358	if other.HTTPClient != nil {
359		dst.HTTPClient = other.HTTPClient
360	}
361
362	if other.LogLevel != nil {
363		dst.LogLevel = other.LogLevel
364	}
365
366	if other.Logger != nil {
367		dst.Logger = other.Logger
368	}
369
370	if other.MaxRetries != nil {
371		dst.MaxRetries = other.MaxRetries
372	}
373
374	if other.Retryer != nil {
375		dst.Retryer = other.Retryer
376	}
377
378	if other.DisableParamValidation != nil {
379		dst.DisableParamValidation = other.DisableParamValidation
380	}
381
382	if other.DisableComputeChecksums != nil {
383		dst.DisableComputeChecksums = other.DisableComputeChecksums
384	}
385
386	if other.S3ForcePathStyle != nil {
387		dst.S3ForcePathStyle = other.S3ForcePathStyle
388	}
389
390	if other.S3Disable100Continue != nil {
391		dst.S3Disable100Continue = other.S3Disable100Continue
392	}
393
394	if other.S3UseAccelerate != nil {
395		dst.S3UseAccelerate = other.S3UseAccelerate
396	}
397
398	if other.UseDualStack != nil {
399		dst.UseDualStack = other.UseDualStack
400	}
401
402	if other.EC2MetadataDisableTimeoutOverride != nil {
403		dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
404	}
405
406	if other.SleepDelay != nil {
407		dst.SleepDelay = other.SleepDelay
408	}
409}
410
411// Copy will return a shallow copy of the Config object. If any additional
412// configurations are provided they will be merged into the new config returned.
413func (c *Config) Copy(cfgs ...*Config) *Config {
414	dst := &Config{}
415	dst.MergeIn(c)
416
417	for _, cfg := range cfgs {
418		dst.MergeIn(cfg)
419	}
420
421	return dst
422}
423