Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemente to specify bucket name of S3 #458

Merged
merged 1 commit into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/s3_deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,33 @@ class S3Deploy {
.digest('hex')
}

_convertRegionStringToEnvVarName (region) {
if (region == null) return 'undefined'
return region.replace(/-/g, '_').toUpperCase()
}

_getBucketNameFromEnvVar (region) {
const key = [
'S3',
this._convertRegionStringToEnvVarName(region),
'BUCKET'
].join('_')
return process.env[key]
}

_getS3KeyPrefixFromEnvVar (region) {
const key = [
'S3',
this._convertRegionStringToEnvVarName(region),
'PREFIX'
].join('_')
return process.env[key]
}

_bucketName (params) {
const bucketNameFromEnvVar = this._getBucketNameFromEnvVar(params.region)
if (bucketNameFromEnvVar != null) return bucketNameFromEnvVar

return [
params.FunctionName,
params.region,
Expand All @@ -43,7 +69,12 @@ class S3Deploy {
}

_s3Key (params) {
return `deploy-package-${params.FunctionName}.zip`
const s3Prefix = this._getS3KeyPrefixFromEnvVar(params.region)
const keys = [`deploy-package-${params.FunctionName}.zip`]
if (s3Prefix != null) {
keys.unshift(s3Prefix.replace(/\/$/, ''))
}
return keys.join('/')
}

_getS3Location (region) {
Expand Down
88 changes: 87 additions & 1 deletion test/s3_deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,65 @@ describe('lib/s3_deploy', () => {
})
})

describe('_convertRegionStringToEnvVarName', () => {
it('Upper case. Replace "-" with "_".', () => {
[{
value: 'us-west-1',
expected: 'US_WEST_1'
}, {
value: 'ap-southeast-2',
expected: 'AP_SOUTHEAST_2'
}].forEach((test) => {
assert.equal(
s3Deploy._convertRegionStringToEnvVarName(test.value),
test.expected,
test
)
})
})
})

describe('_getBucketNameFromEnvVar', () => {
after(() => {
delete process.env.S3_US_WEST_1_BUCKET
})

it('is undefined', () => {
assert.isUndefined(s3Deploy._getBucketNameFromEnvVar('us-west-1'))
})

it('Get values from environment variables', () => {
process.env.S3_US_WEST_1_BUCKET = 'bucketName'
assert.equal(
s3Deploy._getBucketNameFromEnvVar('us-west-1'),
'bucketName'
)
})
})

describe('_getS3KeyPrefixFromEnvVar', () => {
after(() => {
delete process.env.S3_US_WEST_1_PREFIX
})

it('is undefined', () => {
assert.isUndefined(s3Deploy._getS3KeyPrefixFromEnvVar('us-west-1'))
})

it('Get values from environment variables', () => {
process.env.S3_US_WEST_1_PREFIX = 's3KeyPrefix'
assert.equal(
s3Deploy._getS3KeyPrefixFromEnvVar('us-west-1'),
's3KeyPrefix'
)
})
})

describe('_bucketName', () => {
after(() => {
delete process.env.S3_TEST_REGION_BUCKET
})

it('FunctionName + region + md5()', () => {
const params = {
FunctionName: 'node-lambda-name',
Expand All @@ -47,16 +105,44 @@ describe('lib/s3_deploy', () => {
'node-lambda-name-test_region-aac849d59d2be828b793609e03d8241d'
)
})

it('Use environment variables', () => {
process.env.S3_TEST_REGION_BUCKET = 's3-test-region-bucket'
const params = {
FunctionName: 'node-lambda-name',
region: 'test_region'
}
assert.equal(s3Deploy._bucketName(params), 's3-test-region-bucket')
})
})

describe('_s3Key', () => {
after(() => {
delete process.env.S3_TEST_REGION_PREFIX
})

it('"deploy-package" + FunctionName + ".zip"', () => {
const params = {FunctionName: 'node-lambda-name'}
const params = {
FunctionName: 'node-lambda-name',
region: 'test_region'
}
assert.equal(
s3Deploy._s3Key(params),
'deploy-package-node-lambda-name.zip'
)
})

it('Use environment variables', () => {
process.env.S3_TEST_REGION_PREFIX = 's3-test-region-prefix/'
const params = {
FunctionName: 'node-lambda-name',
region: 'test_region'
}
assert.equal(
s3Deploy._s3Key(params),
's3-test-region-prefix/deploy-package-node-lambda-name.zip'
)
})
})

describe('_getS3Location', () => {
Expand Down