code.dwrz.net

Go monorepo.
Log | Files | Refs

metadata.go (6294B)


      1 package middleware
      2 
      3 import (
      4 	"context"
      5 	"github.com/aws/aws-sdk-go-v2/aws"
      6 
      7 	"github.com/aws/smithy-go/middleware"
      8 )
      9 
     10 // RegisterServiceMetadata registers metadata about the service and operation into the middleware context
     11 // so that it is available at runtime for other middleware to introspect.
     12 type RegisterServiceMetadata struct {
     13 	ServiceID     string
     14 	SigningName   string
     15 	Region        string
     16 	OperationName string
     17 }
     18 
     19 // ID returns the middleware identifier.
     20 func (s *RegisterServiceMetadata) ID() string {
     21 	return "RegisterServiceMetadata"
     22 }
     23 
     24 // HandleInitialize registers service metadata information into the middleware context, allowing for introspection.
     25 func (s RegisterServiceMetadata) HandleInitialize(
     26 	ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
     27 ) (out middleware.InitializeOutput, metadata middleware.Metadata, err error) {
     28 	if len(s.ServiceID) > 0 {
     29 		ctx = SetServiceID(ctx, s.ServiceID)
     30 	}
     31 	if len(s.SigningName) > 0 {
     32 		ctx = SetSigningName(ctx, s.SigningName)
     33 	}
     34 	if len(s.Region) > 0 {
     35 		ctx = setRegion(ctx, s.Region)
     36 	}
     37 	if len(s.OperationName) > 0 {
     38 		ctx = setOperationName(ctx, s.OperationName)
     39 	}
     40 	return next.HandleInitialize(ctx, in)
     41 }
     42 
     43 // service metadata keys for storing and lookup of runtime stack information.
     44 type (
     45 	serviceIDKey     struct{}
     46 	signingNameKey   struct{}
     47 	signingRegionKey struct{}
     48 	regionKey        struct{}
     49 	operationNameKey struct{}
     50 	partitionIDKey   struct{}
     51 )
     52 
     53 // GetServiceID retrieves the service id from the context.
     54 //
     55 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
     56 // to clear all stack values.
     57 func GetServiceID(ctx context.Context) (v string) {
     58 	v, _ = middleware.GetStackValue(ctx, serviceIDKey{}).(string)
     59 	return v
     60 }
     61 
     62 // GetSigningName retrieves the service signing name from the context.
     63 //
     64 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
     65 // to clear all stack values.
     66 func GetSigningName(ctx context.Context) (v string) {
     67 	v, _ = middleware.GetStackValue(ctx, signingNameKey{}).(string)
     68 	return v
     69 }
     70 
     71 // GetSigningRegion retrieves the region from the context.
     72 //
     73 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
     74 // to clear all stack values.
     75 func GetSigningRegion(ctx context.Context) (v string) {
     76 	v, _ = middleware.GetStackValue(ctx, signingRegionKey{}).(string)
     77 	return v
     78 }
     79 
     80 // GetRegion retrieves the endpoint region from the context.
     81 //
     82 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
     83 // to clear all stack values.
     84 func GetRegion(ctx context.Context) (v string) {
     85 	v, _ = middleware.GetStackValue(ctx, regionKey{}).(string)
     86 	return v
     87 }
     88 
     89 // GetOperationName retrieves the service operation metadata from the context.
     90 //
     91 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
     92 // to clear all stack values.
     93 func GetOperationName(ctx context.Context) (v string) {
     94 	v, _ = middleware.GetStackValue(ctx, operationNameKey{}).(string)
     95 	return v
     96 }
     97 
     98 // GetPartitionID retrieves the endpoint partition id from the context.
     99 //
    100 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    101 // to clear all stack values.
    102 func GetPartitionID(ctx context.Context) string {
    103 	v, _ := middleware.GetStackValue(ctx, partitionIDKey{}).(string)
    104 	return v
    105 }
    106 
    107 // SetSigningName set or modifies the signing name on the context.
    108 //
    109 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    110 // to clear all stack values.
    111 func SetSigningName(ctx context.Context, value string) context.Context {
    112 	return middleware.WithStackValue(ctx, signingNameKey{}, value)
    113 }
    114 
    115 // SetSigningRegion sets or modifies the region on the context.
    116 //
    117 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    118 // to clear all stack values.
    119 func SetSigningRegion(ctx context.Context, value string) context.Context {
    120 	return middleware.WithStackValue(ctx, signingRegionKey{}, value)
    121 }
    122 
    123 // SetServiceID sets the service id on the context.
    124 //
    125 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    126 // to clear all stack values.
    127 func SetServiceID(ctx context.Context, value string) context.Context {
    128 	return middleware.WithStackValue(ctx, serviceIDKey{}, value)
    129 }
    130 
    131 // setRegion sets the endpoint region on the context.
    132 //
    133 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    134 // to clear all stack values.
    135 func setRegion(ctx context.Context, value string) context.Context {
    136 	return middleware.WithStackValue(ctx, regionKey{}, value)
    137 }
    138 
    139 // setOperationName sets the service operation on the context.
    140 //
    141 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    142 // to clear all stack values.
    143 func setOperationName(ctx context.Context, value string) context.Context {
    144 	return middleware.WithStackValue(ctx, operationNameKey{}, value)
    145 }
    146 
    147 // SetPartitionID sets the partition id of a resolved region on the context
    148 //
    149 // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
    150 // to clear all stack values.
    151 func SetPartitionID(ctx context.Context, value string) context.Context {
    152 	return middleware.WithStackValue(ctx, partitionIDKey{}, value)
    153 }
    154 
    155 // EndpointSource key
    156 type endpointSourceKey struct{}
    157 
    158 // GetEndpointSource returns an endpoint source if set on context
    159 func GetEndpointSource(ctx context.Context) (v aws.EndpointSource) {
    160 	v, _ = middleware.GetStackValue(ctx, endpointSourceKey{}).(aws.EndpointSource)
    161 	return v
    162 }
    163 
    164 // SetEndpointSource sets endpoint source on context
    165 func SetEndpointSource(ctx context.Context, value aws.EndpointSource) context.Context {
    166 	return middleware.WithStackValue(ctx, endpointSourceKey{}, value)
    167 }
    168 
    169 type signingCredentialsKey struct{}
    170 
    171 // GetSigningCredentials returns the credentials that were used for signing if set on context.
    172 func GetSigningCredentials(ctx context.Context) (v aws.Credentials) {
    173 	v, _ = middleware.GetStackValue(ctx, signingCredentialsKey{}).(aws.Credentials)
    174 	return v
    175 }
    176 
    177 // SetSigningCredentials sets the credentails used for signing on the context.
    178 func SetSigningCredentials(ctx context.Context, value aws.Credentials) context.Context {
    179 	return middleware.WithStackValue(ctx, signingCredentialsKey{}, value)
    180 }