customizations.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package ec2
  2. import (
  3. "time"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/awsutil"
  6. "github.com/aws/aws-sdk-go/aws/endpoints"
  7. "github.com/aws/aws-sdk-go/aws/request"
  8. )
  9. func init() {
  10. initRequest = func(r *request.Request) {
  11. if r.Operation.Name == opCopySnapshot { // fill the PresignedURL parameter
  12. r.Handlers.Build.PushFront(fillPresignedURL)
  13. }
  14. }
  15. }
  16. func fillPresignedURL(r *request.Request) {
  17. if !r.ParamsFilled() {
  18. return
  19. }
  20. origParams := r.Params.(*CopySnapshotInput)
  21. // Stop if PresignedURL/DestinationRegion is set
  22. if origParams.PresignedUrl != nil || origParams.DestinationRegion != nil {
  23. return
  24. }
  25. origParams.DestinationRegion = r.Config.Region
  26. newParams := awsutil.CopyOf(r.Params).(*CopySnapshotInput)
  27. // Create a new request based on the existing request. We will use this to
  28. // presign the CopySnapshot request against the source region.
  29. cfg := r.Config.Copy(aws.NewConfig().
  30. WithEndpoint("").
  31. WithRegion(aws.StringValue(origParams.SourceRegion)))
  32. clientInfo := r.ClientInfo
  33. resolved, err := r.Config.EndpointResolver.EndpointFor(
  34. clientInfo.ServiceName, aws.StringValue(cfg.Region),
  35. func(opt *endpoints.Options) {
  36. opt.DisableSSL = aws.BoolValue(cfg.DisableSSL)
  37. opt.UseDualStack = aws.BoolValue(cfg.UseDualStack)
  38. },
  39. )
  40. if err != nil {
  41. r.Error = err
  42. return
  43. }
  44. clientInfo.Endpoint = resolved.URL
  45. clientInfo.SigningRegion = resolved.SigningRegion
  46. // Presign a CopySnapshot request with modified params
  47. req := request.New(*cfg, clientInfo, r.Handlers, r.Retryer, r.Operation, newParams, r.Data)
  48. url, err := req.Presign(5 * time.Minute) // 5 minutes should be enough.
  49. if err != nil { // bubble error back up to original request
  50. r.Error = err
  51. return
  52. }
  53. // We have our URL, set it on params
  54. origParams.PresignedUrl = &url
  55. }