github.com/aavshr/aws-sdk-go@v1.41.3/service/rds/customizations.go (about)

     1  package rds
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/aavshr/aws-sdk-go/aws"
     7  	"github.com/aavshr/aws-sdk-go/aws/awsutil"
     8  	"github.com/aavshr/aws-sdk-go/aws/endpoints"
     9  	"github.com/aavshr/aws-sdk-go/aws/request"
    10  )
    11  
    12  func init() {
    13  	ops := []string{
    14  		opCopyDBSnapshot,
    15  		opCreateDBInstanceReadReplica,
    16  		opCopyDBClusterSnapshot,
    17  		opCreateDBCluster,
    18  		opStartDBInstanceAutomatedBackupsReplication,
    19  	}
    20  	initRequest = func(r *request.Request) {
    21  		for _, operation := range ops {
    22  			if r.Operation.Name == operation {
    23  				r.Handlers.Build.PushFront(fillPresignedURL)
    24  			}
    25  		}
    26  	}
    27  }
    28  
    29  func fillPresignedURL(r *request.Request) {
    30  	fns := map[string]func(r *request.Request){
    31  		opCopyDBSnapshot:                             copyDBSnapshotPresign,
    32  		opCreateDBInstanceReadReplica:                createDBInstanceReadReplicaPresign,
    33  		opCopyDBClusterSnapshot:                      copyDBClusterSnapshotPresign,
    34  		opCreateDBCluster:                            createDBClusterPresign,
    35  		opStartDBInstanceAutomatedBackupsReplication: startDBInstanceAutomatedBackupsReplicationPresign,
    36  	}
    37  	if !r.ParamsFilled() {
    38  		return
    39  	}
    40  	if f, ok := fns[r.Operation.Name]; ok {
    41  		f(r)
    42  	}
    43  }
    44  
    45  func copyDBSnapshotPresign(r *request.Request) {
    46  	originParams := r.Params.(*CopyDBSnapshotInput)
    47  
    48  	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
    49  		return
    50  	}
    51  
    52  	originParams.DestinationRegion = r.Config.Region
    53  
    54  	// preSignedUrl is not required for instances in the same region.
    55  	if *originParams.SourceRegion == *originParams.DestinationRegion {
    56  		return
    57  	}
    58  
    59  	newParams := awsutil.CopyOf(r.Params).(*CopyDBSnapshotInput)
    60  	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
    61  }
    62  
    63  func createDBInstanceReadReplicaPresign(r *request.Request) {
    64  	originParams := r.Params.(*CreateDBInstanceReadReplicaInput)
    65  
    66  	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
    67  		return
    68  	}
    69  
    70  	originParams.DestinationRegion = r.Config.Region
    71  	// preSignedUrl is not required for instances in the same region.
    72  	if *originParams.SourceRegion == *originParams.DestinationRegion {
    73  		return
    74  	}
    75  
    76  	newParams := awsutil.CopyOf(r.Params).(*CreateDBInstanceReadReplicaInput)
    77  	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
    78  }
    79  
    80  func copyDBClusterSnapshotPresign(r *request.Request) {
    81  	originParams := r.Params.(*CopyDBClusterSnapshotInput)
    82  
    83  	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
    84  		return
    85  	}
    86  
    87  	originParams.DestinationRegion = r.Config.Region
    88  	// preSignedUrl is not required for instances in the same region.
    89  	if *originParams.SourceRegion == *originParams.DestinationRegion {
    90  		return
    91  	}
    92  
    93  	newParams := awsutil.CopyOf(r.Params).(*CopyDBClusterSnapshotInput)
    94  	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
    95  }
    96  
    97  func createDBClusterPresign(r *request.Request) {
    98  	originParams := r.Params.(*CreateDBClusterInput)
    99  
   100  	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
   101  		return
   102  	}
   103  
   104  	originParams.DestinationRegion = r.Config.Region
   105  	// preSignedUrl is not required for instances in the same region.
   106  	if *originParams.SourceRegion == *originParams.DestinationRegion {
   107  		return
   108  	}
   109  
   110  	newParams := awsutil.CopyOf(r.Params).(*CreateDBClusterInput)
   111  	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
   112  }
   113  
   114  func startDBInstanceAutomatedBackupsReplicationPresign(r *request.Request) {
   115  	originParams := r.Params.(*StartDBInstanceAutomatedBackupsReplicationInput)
   116  
   117  	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
   118  		return
   119  	}
   120  
   121  	originParams.DestinationRegion = r.Config.Region
   122  	// preSignedUrl is not required for instances in the same region.
   123  	if *originParams.SourceRegion == *originParams.DestinationRegion {
   124  		return
   125  	}
   126  
   127  	newParams := awsutil.CopyOf(r.Params).(*StartDBInstanceAutomatedBackupsReplicationInput)
   128  	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
   129  }
   130  
   131  // presignURL will presign the request by using SoureRegion to sign with. SourceRegion is not
   132  // sent to the service, and is only used to not have the SDKs parsing ARNs.
   133  func presignURL(r *request.Request, sourceRegion *string, newParams interface{}) *string {
   134  	cfg := r.Config.Copy(aws.NewConfig().
   135  		WithEndpoint("").
   136  		WithRegion(aws.StringValue(sourceRegion)))
   137  
   138  	clientInfo := r.ClientInfo
   139  	resolved, err := r.Config.EndpointResolver.EndpointFor(
   140  		EndpointsID, aws.StringValue(cfg.Region),
   141  		func(opt *endpoints.Options) {
   142  			opt.DisableSSL = aws.BoolValue(cfg.DisableSSL)
   143  			opt.UseDualStack = aws.BoolValue(cfg.UseDualStack)
   144  		},
   145  	)
   146  	if err != nil {
   147  		r.Error = err
   148  		return nil
   149  	}
   150  
   151  	clientInfo.Endpoint = resolved.URL
   152  	clientInfo.SigningRegion = resolved.SigningRegion
   153  
   154  	// Presign a request with modified params
   155  	req := request.New(*cfg, clientInfo, r.Handlers, r.Retryer, r.Operation, newParams, r.Data)
   156  	req.Operation.HTTPMethod = "GET"
   157  	uri, err := req.Presign(5 * time.Minute) // 5 minutes should be enough.
   158  	if err != nil {                          // bubble error back up to original request
   159  		r.Error = err
   160  		return nil
   161  	}
   162  
   163  	// We have our URL, set it on params
   164  	return &uri
   165  }