api_client.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // Code generated by smithy-go-codegen DO NOT EDIT.
  2. package cloudwatchlogs
  3. import (
  4. "context"
  5. "github.com/aws/aws-sdk-go-v2/aws"
  6. "github.com/aws/aws-sdk-go-v2/aws/defaults"
  7. awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
  8. "github.com/aws/aws-sdk-go-v2/aws/retry"
  9. "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
  10. awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
  11. internalConfig "github.com/aws/aws-sdk-go-v2/internal/configsources"
  12. smithy "github.com/aws/smithy-go"
  13. smithydocument "github.com/aws/smithy-go/document"
  14. "github.com/aws/smithy-go/logging"
  15. "github.com/aws/smithy-go/middleware"
  16. smithyhttp "github.com/aws/smithy-go/transport/http"
  17. "net"
  18. "net/http"
  19. "time"
  20. )
  21. const ServiceID = "CloudWatch Logs"
  22. const ServiceAPIVersion = "2014-03-28"
  23. // Client provides the API client to make operations call for Amazon CloudWatch
  24. // Logs.
  25. type Client struct {
  26. options Options
  27. }
  28. // New returns an initialized Client based on the functional options. Provide
  29. // additional functional options to further configure the behavior of the client,
  30. // such as changing the client's endpoint or adding custom middleware behavior.
  31. func New(options Options, optFns ...func(*Options)) *Client {
  32. options = options.Copy()
  33. resolveDefaultLogger(&options)
  34. setResolvedDefaultsMode(&options)
  35. resolveRetryer(&options)
  36. resolveHTTPClient(&options)
  37. resolveHTTPSignerV4(&options)
  38. resolveDefaultEndpointConfiguration(&options)
  39. for _, fn := range optFns {
  40. fn(&options)
  41. }
  42. client := &Client{
  43. options: options,
  44. }
  45. return client
  46. }
  47. type Options struct {
  48. // Set of options to modify how an operation is invoked. These apply to all
  49. // operations invoked for this client. Use functional options on operation call to
  50. // modify this list for per operation behavior.
  51. APIOptions []func(*middleware.Stack) error
  52. // Configures the events that will be sent to the configured logger.
  53. ClientLogMode aws.ClientLogMode
  54. // The credentials object to use when signing requests.
  55. Credentials aws.CredentialsProvider
  56. // The configuration DefaultsMode that the SDK should use when constructing the
  57. // clients initial default settings.
  58. DefaultsMode aws.DefaultsMode
  59. // The endpoint options to be used when attempting to resolve an endpoint.
  60. EndpointOptions EndpointResolverOptions
  61. // The service endpoint resolver.
  62. EndpointResolver EndpointResolver
  63. // Signature Version 4 (SigV4) Signer
  64. HTTPSignerV4 HTTPSignerV4
  65. // The logger writer interface to write logging messages to.
  66. Logger logging.Logger
  67. // The region to send requests to. (Required)
  68. Region string
  69. // RetryMaxAttempts specifies the maximum number attempts an API client will call
  70. // an operation that fails with a retryable error. A value of 0 is ignored, and
  71. // will not be used to configure the API client created default retryer, or modify
  72. // per operation call's retry max attempts. When creating a new API Clients this
  73. // member will only be used if the Retryer Options member is nil. This value will
  74. // be ignored if Retryer is not nil. If specified in an operation call's functional
  75. // options with a value that is different than the constructed client's Options,
  76. // the Client's Retryer will be wrapped to use the operation's specific
  77. // RetryMaxAttempts value.
  78. RetryMaxAttempts int
  79. // RetryMode specifies the retry mode the API client will be created with, if
  80. // Retryer option is not also specified. When creating a new API Clients this
  81. // member will only be used if the Retryer Options member is nil. This value will
  82. // be ignored if Retryer is not nil. Currently does not support per operation call
  83. // overrides, may in the future.
  84. RetryMode aws.RetryMode
  85. // Retryer guides how HTTP requests should be retried in case of recoverable
  86. // failures. When nil the API client will use a default retryer. The kind of
  87. // default retry created by the API client can be changed with the RetryMode
  88. // option.
  89. Retryer aws.Retryer
  90. // The RuntimeEnvironment configuration, only populated if the DefaultsMode is set
  91. // to DefaultsModeAuto and is initialized using config.LoadDefaultConfig. You
  92. // should not populate this structure programmatically, or rely on the values here
  93. // within your applications.
  94. RuntimeEnvironment aws.RuntimeEnvironment
  95. // The initial DefaultsMode used when the client options were constructed. If the
  96. // DefaultsMode was set to aws.DefaultsModeAuto this will store what the resolved
  97. // value was at that point in time. Currently does not support per operation call
  98. // overrides, may in the future.
  99. resolvedDefaultsMode aws.DefaultsMode
  100. // The HTTP client to invoke API calls with. Defaults to client's default HTTP
  101. // implementation if nil.
  102. HTTPClient HTTPClient
  103. }
  104. // WithAPIOptions returns a functional option for setting the Client's APIOptions
  105. // option.
  106. func WithAPIOptions(optFns ...func(*middleware.Stack) error) func(*Options) {
  107. return func(o *Options) {
  108. o.APIOptions = append(o.APIOptions, optFns...)
  109. }
  110. }
  111. // WithEndpointResolver returns a functional option for setting the Client's
  112. // EndpointResolver option.
  113. func WithEndpointResolver(v EndpointResolver) func(*Options) {
  114. return func(o *Options) {
  115. o.EndpointResolver = v
  116. }
  117. }
  118. type HTTPClient interface {
  119. Do(*http.Request) (*http.Response, error)
  120. }
  121. // Copy creates a clone where the APIOptions list is deep copied.
  122. func (o Options) Copy() Options {
  123. to := o
  124. to.APIOptions = make([]func(*middleware.Stack) error, len(o.APIOptions))
  125. copy(to.APIOptions, o.APIOptions)
  126. return to
  127. }
  128. func (c *Client) invokeOperation(ctx context.Context, opID string, params interface{}, optFns []func(*Options), stackFns ...func(*middleware.Stack, Options) error) (result interface{}, metadata middleware.Metadata, err error) {
  129. ctx = middleware.ClearStackValues(ctx)
  130. stack := middleware.NewStack(opID, smithyhttp.NewStackRequest)
  131. options := c.options.Copy()
  132. for _, fn := range optFns {
  133. fn(&options)
  134. }
  135. finalizeRetryMaxAttemptOptions(&options, *c)
  136. finalizeClientEndpointResolverOptions(&options)
  137. for _, fn := range stackFns {
  138. if err := fn(stack, options); err != nil {
  139. return nil, metadata, err
  140. }
  141. }
  142. for _, fn := range options.APIOptions {
  143. if err := fn(stack); err != nil {
  144. return nil, metadata, err
  145. }
  146. }
  147. handler := middleware.DecorateHandler(smithyhttp.NewClientHandler(options.HTTPClient), stack)
  148. result, metadata, err = handler.Handle(ctx, params)
  149. if err != nil {
  150. err = &smithy.OperationError{
  151. ServiceID: ServiceID,
  152. OperationName: opID,
  153. Err: err,
  154. }
  155. }
  156. return result, metadata, err
  157. }
  158. type noSmithyDocumentSerde = smithydocument.NoSerde
  159. func resolveDefaultLogger(o *Options) {
  160. if o.Logger != nil {
  161. return
  162. }
  163. o.Logger = logging.Nop{}
  164. }
  165. func addSetLoggerMiddleware(stack *middleware.Stack, o Options) error {
  166. return middleware.AddSetLoggerMiddleware(stack, o.Logger)
  167. }
  168. func setResolvedDefaultsMode(o *Options) {
  169. if len(o.resolvedDefaultsMode) > 0 {
  170. return
  171. }
  172. var mode aws.DefaultsMode
  173. mode.SetFromString(string(o.DefaultsMode))
  174. if mode == aws.DefaultsModeAuto {
  175. mode = defaults.ResolveDefaultsModeAuto(o.Region, o.RuntimeEnvironment)
  176. }
  177. o.resolvedDefaultsMode = mode
  178. }
  179. // NewFromConfig returns a new client from the provided config.
  180. func NewFromConfig(cfg aws.Config, optFns ...func(*Options)) *Client {
  181. opts := Options{
  182. Region: cfg.Region,
  183. DefaultsMode: cfg.DefaultsMode,
  184. RuntimeEnvironment: cfg.RuntimeEnvironment,
  185. HTTPClient: cfg.HTTPClient,
  186. Credentials: cfg.Credentials,
  187. APIOptions: cfg.APIOptions,
  188. Logger: cfg.Logger,
  189. ClientLogMode: cfg.ClientLogMode,
  190. }
  191. resolveAWSRetryerProvider(cfg, &opts)
  192. resolveAWSRetryMaxAttempts(cfg, &opts)
  193. resolveAWSRetryMode(cfg, &opts)
  194. resolveAWSEndpointResolver(cfg, &opts)
  195. resolveUseDualStackEndpoint(cfg, &opts)
  196. resolveUseFIPSEndpoint(cfg, &opts)
  197. return New(opts, optFns...)
  198. }
  199. func resolveHTTPClient(o *Options) {
  200. var buildable *awshttp.BuildableClient
  201. if o.HTTPClient != nil {
  202. var ok bool
  203. buildable, ok = o.HTTPClient.(*awshttp.BuildableClient)
  204. if !ok {
  205. return
  206. }
  207. } else {
  208. buildable = awshttp.NewBuildableClient()
  209. }
  210. modeConfig, err := defaults.GetModeConfiguration(o.resolvedDefaultsMode)
  211. if err == nil {
  212. buildable = buildable.WithDialerOptions(func(dialer *net.Dialer) {
  213. if dialerTimeout, ok := modeConfig.GetConnectTimeout(); ok {
  214. dialer.Timeout = dialerTimeout
  215. }
  216. })
  217. buildable = buildable.WithTransportOptions(func(transport *http.Transport) {
  218. if tlsHandshakeTimeout, ok := modeConfig.GetTLSNegotiationTimeout(); ok {
  219. transport.TLSHandshakeTimeout = tlsHandshakeTimeout
  220. }
  221. })
  222. }
  223. o.HTTPClient = buildable
  224. }
  225. func resolveRetryer(o *Options) {
  226. if o.Retryer != nil {
  227. return
  228. }
  229. if len(o.RetryMode) == 0 {
  230. modeConfig, err := defaults.GetModeConfiguration(o.resolvedDefaultsMode)
  231. if err == nil {
  232. o.RetryMode = modeConfig.RetryMode
  233. }
  234. }
  235. if len(o.RetryMode) == 0 {
  236. o.RetryMode = aws.RetryModeStandard
  237. }
  238. var standardOptions []func(*retry.StandardOptions)
  239. if v := o.RetryMaxAttempts; v != 0 {
  240. standardOptions = append(standardOptions, func(so *retry.StandardOptions) {
  241. so.MaxAttempts = v
  242. })
  243. }
  244. switch o.RetryMode {
  245. case aws.RetryModeAdaptive:
  246. var adaptiveOptions []func(*retry.AdaptiveModeOptions)
  247. if len(standardOptions) != 0 {
  248. adaptiveOptions = append(adaptiveOptions, func(ao *retry.AdaptiveModeOptions) {
  249. ao.StandardOptions = append(ao.StandardOptions, standardOptions...)
  250. })
  251. }
  252. o.Retryer = retry.NewAdaptiveMode(adaptiveOptions...)
  253. default:
  254. o.Retryer = retry.NewStandard(standardOptions...)
  255. }
  256. }
  257. func resolveAWSRetryerProvider(cfg aws.Config, o *Options) {
  258. if cfg.Retryer == nil {
  259. return
  260. }
  261. o.Retryer = cfg.Retryer()
  262. }
  263. func resolveAWSRetryMode(cfg aws.Config, o *Options) {
  264. if len(cfg.RetryMode) == 0 {
  265. return
  266. }
  267. o.RetryMode = cfg.RetryMode
  268. }
  269. func resolveAWSRetryMaxAttempts(cfg aws.Config, o *Options) {
  270. if cfg.RetryMaxAttempts == 0 {
  271. return
  272. }
  273. o.RetryMaxAttempts = cfg.RetryMaxAttempts
  274. }
  275. func finalizeRetryMaxAttemptOptions(o *Options, client Client) {
  276. if v := o.RetryMaxAttempts; v == 0 || v == client.options.RetryMaxAttempts {
  277. return
  278. }
  279. o.Retryer = retry.AddWithMaxAttempts(o.Retryer, o.RetryMaxAttempts)
  280. }
  281. func resolveAWSEndpointResolver(cfg aws.Config, o *Options) {
  282. if cfg.EndpointResolver == nil && cfg.EndpointResolverWithOptions == nil {
  283. return
  284. }
  285. o.EndpointResolver = withEndpointResolver(cfg.EndpointResolver, cfg.EndpointResolverWithOptions, NewDefaultEndpointResolver())
  286. }
  287. func addClientUserAgent(stack *middleware.Stack) error {
  288. return awsmiddleware.AddSDKAgentKeyValue(awsmiddleware.APIMetadata, "cloudwatchlogs", goModuleVersion)(stack)
  289. }
  290. func addHTTPSignerV4Middleware(stack *middleware.Stack, o Options) error {
  291. mw := v4.NewSignHTTPRequestMiddleware(v4.SignHTTPRequestMiddlewareOptions{
  292. CredentialsProvider: o.Credentials,
  293. Signer: o.HTTPSignerV4,
  294. LogSigning: o.ClientLogMode.IsSigning(),
  295. })
  296. return stack.Finalize.Add(mw, middleware.After)
  297. }
  298. type HTTPSignerV4 interface {
  299. SignHTTP(ctx context.Context, credentials aws.Credentials, r *http.Request, payloadHash string, service string, region string, signingTime time.Time, optFns ...func(*v4.SignerOptions)) error
  300. }
  301. func resolveHTTPSignerV4(o *Options) {
  302. if o.HTTPSignerV4 != nil {
  303. return
  304. }
  305. o.HTTPSignerV4 = newDefaultV4Signer(*o)
  306. }
  307. func newDefaultV4Signer(o Options) *v4.Signer {
  308. return v4.NewSigner(func(so *v4.SignerOptions) {
  309. so.Logger = o.Logger
  310. so.LogSigning = o.ClientLogMode.IsSigning()
  311. })
  312. }
  313. func addRetryMiddlewares(stack *middleware.Stack, o Options) error {
  314. mo := retry.AddRetryMiddlewaresOptions{
  315. Retryer: o.Retryer,
  316. LogRetryAttempts: o.ClientLogMode.IsRetries(),
  317. }
  318. return retry.AddRetryMiddlewares(stack, mo)
  319. }
  320. // resolves dual-stack endpoint configuration
  321. func resolveUseDualStackEndpoint(cfg aws.Config, o *Options) error {
  322. if len(cfg.ConfigSources) == 0 {
  323. return nil
  324. }
  325. value, found, err := internalConfig.ResolveUseDualStackEndpoint(context.Background(), cfg.ConfigSources)
  326. if err != nil {
  327. return err
  328. }
  329. if found {
  330. o.EndpointOptions.UseDualStackEndpoint = value
  331. }
  332. return nil
  333. }
  334. // resolves FIPS endpoint configuration
  335. func resolveUseFIPSEndpoint(cfg aws.Config, o *Options) error {
  336. if len(cfg.ConfigSources) == 0 {
  337. return nil
  338. }
  339. value, found, err := internalConfig.ResolveUseFIPSEndpoint(context.Background(), cfg.ConfigSources)
  340. if err != nil {
  341. return err
  342. }
  343. if found {
  344. o.EndpointOptions.UseFIPSEndpoint = value
  345. }
  346. return nil
  347. }
  348. func addRequestIDRetrieverMiddleware(stack *middleware.Stack) error {
  349. return awsmiddleware.AddRequestIDRetrieverMiddleware(stack)
  350. }
  351. func addResponseErrorMiddleware(stack *middleware.Stack) error {
  352. return awshttp.AddResponseErrorMiddleware(stack)
  353. }
  354. func addRequestResponseLogging(stack *middleware.Stack, o Options) error {
  355. return stack.Deserialize.Add(&smithyhttp.RequestResponseLogger{
  356. LogRequest: o.ClientLogMode.IsRequest(),
  357. LogRequestWithBody: o.ClientLogMode.IsRequestWithBody(),
  358. LogResponse: o.ClientLogMode.IsResponse(),
  359. LogResponseWithBody: o.ClientLogMode.IsResponseWithBody(),
  360. }, middleware.After)
  361. }