Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ distribution:
inputs:
region: us-east-1
enabled: true # optional
comment: 'My distribution' # optional
defaults: # optional
ttl: 15
allowedHttpMethods: ['HEAD', 'GET']
Expand Down
1 change: 1 addition & 0 deletions __tests__/__snapshots__/custom-url-origin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Object {
"Items": Array [],
"Quantity": 0,
},
"Comment": "",
"DefaultCacheBehavior": Object {
"AllowedMethods": Object {
"CachedMethods": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Object {
],
"Quantity": 1,
},
"Comment": "",
"DefaultCacheBehavior": Object {
"AllowedMethods": Object {
"CachedMethods": Object {
Expand Down
1 change: 1 addition & 0 deletions __tests__/__snapshots__/s3-origin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ Object {
"Items": Array [],
"Quantity": 0,
},
"Comment": "",
"DefaultCacheBehavior": Object {
"AllowedMethods": Object {
"CachedMethods": Object {
Expand Down
96 changes: 96 additions & 0 deletions __tests__/general-options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const { createComponent } = require('../test-utils')

const {
mockCreateDistribution,
mockUpdateDistribution,
mockCreateDistributionPromise,
mockGetDistributionConfigPromise,
mockUpdateDistributionPromise
} = require('aws-sdk')

describe('General options propagation', () => {
let component

// sample origins
const origins = ['https://exampleorigin.com']

beforeEach(async () => {
mockCreateDistributionPromise.mockResolvedValueOnce({
Distribution: {
Id: 'distribution123'
}
})

mockGetDistributionConfigPromise.mockResolvedValueOnce({
ETag: 'etag',
DistributionConfig: {
Origins: {
Items: []
}
}
})
mockUpdateDistributionPromise.mockResolvedValueOnce({
Distribution: {
Id: 'xyz'
}
})

component = await createComponent()
})

it('create distribution with comment and update it', async () => {
await component.default({
comment: 'test comment',
origins
})

expect(mockCreateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Comment: 'test comment'
})
})
)

await component.default({
comment: 'updated comment',
origins
})

expect(mockUpdateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Comment: 'updated comment'
})
})
)
})

it('create disabled distribution and update it', async () => {
await component.default({
enabled: false,
origins
})

expect(mockCreateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Enabled: false
})
})
)

await component.default({
enabled: true,
origins
})

expect(mockUpdateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Enabled: true
})
})
)
})
})
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
const params = {
DistributionConfig: {
CallerReference: String(Date.now()),
Comment: '',
Comment: inputs.comment,
Aliases: {
Quantity: 0,
Items: []
Expand All @@ -33,7 +33,7 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
Items: []
},
PriceClass: 'PriceClass_All',
Enabled: inputs.enabled === false ? false : true,
Enabled: inputs.enabled,
HttpVersion: 'http2'
}
}
Expand Down Expand Up @@ -93,7 +93,8 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {

// 5. then make our changes

params.DistributionConfig.Enabled = inputs.enabled === false ? false : true
params.DistributionConfig.Enabled = inputs.enabled
params.DistributionConfig.Comment = inputs.comment

let s3CanonicalUserId
let originAccessIdentityId
Expand Down
9 changes: 8 additions & 1 deletion serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class CloudFront extends Component {
this.context.status('Deploying')

inputs.region = inputs.region || 'us-east-1'
inputs.enabled = inputs.enabled === false ? false : true
inputs.comment =
inputs.comment === null || inputs.comment === undefined ? '' : String(inputs.comment)

this.context.debug(
`Starting deployment of CloudFront distribution to the ${inputs.region} region.`
Expand All @@ -38,7 +41,9 @@ class CloudFront extends Component {
if (this.state.id) {
if (
!equals(this.state.origins, inputs.origins) ||
!equals(this.state.defaults, inputs.defaults)
!equals(this.state.defaults, inputs.defaults) ||
!equals(this.state.enabled, inputs.enabled) ||
!equals(this.state.comment, inputs.comment)
) {
this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`)
this.state = await updateCloudFrontDistribution(cf, s3, this.state.id, inputs)
Expand All @@ -49,6 +54,8 @@ class CloudFront extends Component {
}

this.state.region = inputs.region
this.state.enabled = inputs.enabled
this.state.comment = inputs.comment
this.state.origins = inputs.origins
this.state.defaults = inputs.defaults
await this.save()
Expand Down