-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(awslambda): supports bucket upload.
feat(awslambda): wires up bucket upload. fixup! feat(awslambda): awaits version publish completion.
- Loading branch information
1 parent
b052339
commit 25a51d2
Showing
4 changed files
with
198 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package awslambda | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
awssession "github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/google/uuid" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
func (m LambdaRemote) uploadBundleToBucket(zipContents *[]byte) (bucketName string, localBundlePath string, err error) { | ||
localBundlePath, err = m.writeBundleToTempFile(zipContents) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
bucketName, err = m.getBucketName() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if err = m.uploadToBucket(localBundlePath, bucketName); err != nil { | ||
return "", "", fmt.Errorf("failed to upload file %v to bucket %v: %v", localBundlePath, bucketName, err) | ||
} | ||
return bucketName, localBundlePath, nil | ||
} | ||
|
||
func (m LambdaRemote) writeBundleToTempFile(zipContents *[]byte) (localBundlePath string, err error) { | ||
temp, err := os.CreateTemp(os.TempDir(), "imposter-bundle-*.zip") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create temp file: %v", err) | ||
} | ||
defer temp.Close() | ||
|
||
localBundlePath = temp.Name() | ||
if err = os.WriteFile(localBundlePath, *zipContents, 0644); err != nil { | ||
return "", fmt.Errorf("failed to write bundle to temp file %v: %v", temp, err) | ||
} | ||
logger.Tracef("wrote bundle to temp file: %v", localBundlePath) | ||
return localBundlePath, nil | ||
} | ||
|
||
func (m LambdaRemote) getBucketName() (bucketName string, err error) { | ||
bucketName = m.Config[configKeyBucketName] | ||
if bucketName == "" { | ||
bucketName = "imposter-mock-" + strings.ReplaceAll(uuid.New().String(), "-", "") | ||
m.Config[configKeyBucketName] = bucketName | ||
if err = m.SaveConfig(); err != nil { | ||
return "", fmt.Errorf("failed to save bucket name %v in config: %v", bucketName, err) | ||
} | ||
} | ||
return bucketName, nil | ||
} | ||
|
||
func (m LambdaRemote) uploadToBucket(localPath string, bucketName string) error { | ||
region, _, svc, err := m.initS3Client() | ||
if err != nil { | ||
return fmt.Errorf("failed to initialise S3 client: %v", err) | ||
} | ||
if err = ensureBucket(svc, bucketName, region); err != nil { | ||
return fmt.Errorf("failed to ensure bucket %v exists: %v", bucketName, err) | ||
} | ||
if err = upload(svc, bucketName, localPath); err != nil { | ||
return fmt.Errorf("failed to upload file %v to bucket %v: %v", localPath, bucketName, err) | ||
} | ||
return nil | ||
} | ||
|
||
func ensureBucket(svc *s3.S3, bucketName string, region string) error { | ||
logger.Tracef("checking for bucket %v in region %v", bucketName, region) | ||
|
||
if _, err := svc.HeadBucket(&s3.HeadBucketInput{Bucket: aws.String(bucketName)}); err != nil { | ||
if err = createBucket(svc, bucketName, region); err != nil { | ||
return err | ||
} | ||
} | ||
logger.Debugf("bucket %v exists", bucketName) | ||
return nil | ||
} | ||
|
||
func createBucket(svc *s3.S3, bucketName string, region string) error { | ||
logger.Tracef("creating bucket %v in region %v", bucketName, region) | ||
|
||
_, err := svc.CreateBucket(&s3.CreateBucketInput{ | ||
Bucket: aws.String(bucketName), | ||
CreateBucketConfiguration: &s3.CreateBucketConfiguration{ | ||
LocationConstraint: aws.String(region), | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create bucket %v in region %v: %v", bucketName, region, err) | ||
} | ||
logger.Debugf("created bucket %v in region %v", bucketName, region) | ||
return nil | ||
} | ||
|
||
func upload(svc *s3.S3, bucketName string, localPath string) error { | ||
logger.Tracef("uploading file %v to bucket %v", localPath, bucketName) | ||
|
||
file, err := os.Open(localPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read file: %v: %v", localPath, err) | ||
} | ||
defer file.Close() | ||
|
||
_, err = svc.PutObject(&s3.PutObjectInput{ | ||
Body: aws.ReadSeekCloser(file), | ||
Bucket: aws.String(bucketName), | ||
Key: aws.String(path.Base(localPath)), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to upload file %v to bucket %v: %v", localPath, bucketName, err) | ||
} | ||
logger.Debugf("uploaded file %v to bucket %v", localPath, bucketName) | ||
return nil | ||
} | ||
|
||
func (m LambdaRemote) initS3Client() (region string, sess *awssession.Session, svc *s3.S3, err error) { | ||
if m.Config[configKeyRegion] == "" { | ||
return "", nil, nil, fmt.Errorf("region cannot be null") | ||
} | ||
region, sess = m.startAwsSession() | ||
svc = s3.New(sess) | ||
return region, sess, svc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters