endpoints.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Code generated by smithy-go-codegen DO NOT EDIT.
  2. package cloudwatchlogs
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "github.com/aws/aws-sdk-go-v2/aws"
  8. awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
  9. internalendpoints "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/internal/endpoints"
  10. "github.com/aws/smithy-go/middleware"
  11. smithyhttp "github.com/aws/smithy-go/transport/http"
  12. "net/url"
  13. "strings"
  14. )
  15. // EndpointResolverOptions is the service endpoint resolver options
  16. type EndpointResolverOptions = internalendpoints.Options
  17. // EndpointResolver interface for resolving service endpoints.
  18. type EndpointResolver interface {
  19. ResolveEndpoint(region string, options EndpointResolverOptions) (aws.Endpoint, error)
  20. }
  21. var _ EndpointResolver = &internalendpoints.Resolver{}
  22. // NewDefaultEndpointResolver constructs a new service endpoint resolver
  23. func NewDefaultEndpointResolver() *internalendpoints.Resolver {
  24. return internalendpoints.New()
  25. }
  26. // EndpointResolverFunc is a helper utility that wraps a function so it satisfies
  27. // the EndpointResolver interface. This is useful when you want to add additional
  28. // endpoint resolving logic, or stub out specific endpoints with custom values.
  29. type EndpointResolverFunc func(region string, options EndpointResolverOptions) (aws.Endpoint, error)
  30. func (fn EndpointResolverFunc) ResolveEndpoint(region string, options EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
  31. return fn(region, options)
  32. }
  33. func resolveDefaultEndpointConfiguration(o *Options) {
  34. if o.EndpointResolver != nil {
  35. return
  36. }
  37. o.EndpointResolver = NewDefaultEndpointResolver()
  38. }
  39. // EndpointResolverFromURL returns an EndpointResolver configured using the
  40. // provided endpoint url. By default, the resolved endpoint resolver uses the
  41. // client region as signing region, and the endpoint source is set to
  42. // EndpointSourceCustom.You can provide functional options to configure endpoint
  43. // values for the resolved endpoint.
  44. func EndpointResolverFromURL(url string, optFns ...func(*aws.Endpoint)) EndpointResolver {
  45. e := aws.Endpoint{URL: url, Source: aws.EndpointSourceCustom}
  46. for _, fn := range optFns {
  47. fn(&e)
  48. }
  49. return EndpointResolverFunc(
  50. func(region string, options EndpointResolverOptions) (aws.Endpoint, error) {
  51. if len(e.SigningRegion) == 0 {
  52. e.SigningRegion = region
  53. }
  54. return e, nil
  55. },
  56. )
  57. }
  58. type ResolveEndpoint struct {
  59. Resolver EndpointResolver
  60. Options EndpointResolverOptions
  61. }
  62. func (*ResolveEndpoint) ID() string {
  63. return "ResolveEndpoint"
  64. }
  65. func (m *ResolveEndpoint) HandleSerialize(ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler) (
  66. out middleware.SerializeOutput, metadata middleware.Metadata, err error,
  67. ) {
  68. req, ok := in.Request.(*smithyhttp.Request)
  69. if !ok {
  70. return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
  71. }
  72. if m.Resolver == nil {
  73. return out, metadata, fmt.Errorf("expected endpoint resolver to not be nil")
  74. }
  75. eo := m.Options
  76. eo.Logger = middleware.GetLogger(ctx)
  77. var endpoint aws.Endpoint
  78. endpoint, err = m.Resolver.ResolveEndpoint(awsmiddleware.GetRegion(ctx), eo)
  79. if err != nil {
  80. return out, metadata, fmt.Errorf("failed to resolve service endpoint, %w", err)
  81. }
  82. req.URL, err = url.Parse(endpoint.URL)
  83. if err != nil {
  84. return out, metadata, fmt.Errorf("failed to parse endpoint URL: %w", err)
  85. }
  86. if len(awsmiddleware.GetSigningName(ctx)) == 0 {
  87. signingName := endpoint.SigningName
  88. if len(signingName) == 0 {
  89. signingName = "logs"
  90. }
  91. ctx = awsmiddleware.SetSigningName(ctx, signingName)
  92. }
  93. ctx = awsmiddleware.SetEndpointSource(ctx, endpoint.Source)
  94. ctx = smithyhttp.SetHostnameImmutable(ctx, endpoint.HostnameImmutable)
  95. ctx = awsmiddleware.SetSigningRegion(ctx, endpoint.SigningRegion)
  96. ctx = awsmiddleware.SetPartitionID(ctx, endpoint.PartitionID)
  97. return next.HandleSerialize(ctx, in)
  98. }
  99. func addResolveEndpointMiddleware(stack *middleware.Stack, o Options) error {
  100. return stack.Serialize.Insert(&ResolveEndpoint{
  101. Resolver: o.EndpointResolver,
  102. Options: o.EndpointOptions,
  103. }, "OperationSerializer", middleware.Before)
  104. }
  105. func removeResolveEndpointMiddleware(stack *middleware.Stack) error {
  106. _, err := stack.Serialize.Remove((&ResolveEndpoint{}).ID())
  107. return err
  108. }
  109. type wrappedEndpointResolver struct {
  110. awsResolver aws.EndpointResolverWithOptions
  111. resolver EndpointResolver
  112. }
  113. func (w *wrappedEndpointResolver) ResolveEndpoint(region string, options EndpointResolverOptions) (endpoint aws.Endpoint, err error) {
  114. if w.awsResolver == nil {
  115. goto fallback
  116. }
  117. endpoint, err = w.awsResolver.ResolveEndpoint(ServiceID, region, options)
  118. if err == nil {
  119. return endpoint, nil
  120. }
  121. if nf := (&aws.EndpointNotFoundError{}); !errors.As(err, &nf) {
  122. return endpoint, err
  123. }
  124. fallback:
  125. if w.resolver == nil {
  126. return endpoint, fmt.Errorf("default endpoint resolver provided was nil")
  127. }
  128. return w.resolver.ResolveEndpoint(region, options)
  129. }
  130. type awsEndpointResolverAdaptor func(service, region string) (aws.Endpoint, error)
  131. func (a awsEndpointResolverAdaptor) ResolveEndpoint(service, region string, options ...interface{}) (aws.Endpoint, error) {
  132. return a(service, region)
  133. }
  134. var _ aws.EndpointResolverWithOptions = awsEndpointResolverAdaptor(nil)
  135. // withEndpointResolver returns an EndpointResolver that first delegates endpoint resolution to the awsResolver.
  136. // If awsResolver returns aws.EndpointNotFoundError error, the resolver will use the the provided
  137. // fallbackResolver for resolution.
  138. //
  139. // fallbackResolver must not be nil
  140. func withEndpointResolver(awsResolver aws.EndpointResolver, awsResolverWithOptions aws.EndpointResolverWithOptions, fallbackResolver EndpointResolver) EndpointResolver {
  141. var resolver aws.EndpointResolverWithOptions
  142. if awsResolverWithOptions != nil {
  143. resolver = awsResolverWithOptions
  144. } else if awsResolver != nil {
  145. resolver = awsEndpointResolverAdaptor(awsResolver.ResolveEndpoint)
  146. }
  147. return &wrappedEndpointResolver{
  148. awsResolver: resolver,
  149. resolver: fallbackResolver,
  150. }
  151. }
  152. func finalizeClientEndpointResolverOptions(options *Options) {
  153. options.EndpointOptions.LogDeprecated = options.ClientLogMode.IsDeprecatedUsage()
  154. if len(options.EndpointOptions.ResolvedRegion) == 0 {
  155. const fipsInfix = "-fips-"
  156. const fipsPrefix = "fips-"
  157. const fipsSuffix = "-fips"
  158. if strings.Contains(options.Region, fipsInfix) ||
  159. strings.Contains(options.Region, fipsPrefix) ||
  160. strings.Contains(options.Region, fipsSuffix) {
  161. options.EndpointOptions.ResolvedRegion = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(
  162. options.Region, fipsInfix, "-"), fipsPrefix, ""), fipsSuffix, "")
  163. options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
  164. }
  165. }
  166. }