Skip to content

Commit 6673334

Browse files
authored
Merge pull request #10 from evgsil/add-optional-comment
added comment support, fixed enabled state update
2 parents 03ab709 + 9b3e611 commit 6673334

7 files changed

+112
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ distribution:
5050
inputs:
5151
region: us-east-1
5252
enabled: true # optional
53+
comment: 'My distribution' # optional
5354
defaults: # optional
5455
ttl: 15
5556
allowedHttpMethods: ['HEAD', 'GET']

__tests__/__snapshots__/custom-url-origin.test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Object {
112112
"Items": Array [],
113113
"Quantity": 0,
114114
},
115+
"Comment": "",
115116
"DefaultCacheBehavior": Object {
116117
"AllowedMethods": Object {
117118
"CachedMethods": Object {

__tests__/__snapshots__/origin-with-path-pattern.test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Object {
198198
],
199199
"Quantity": 1,
200200
},
201+
"Comment": "",
201202
"DefaultCacheBehavior": Object {
202203
"AllowedMethods": Object {
203204
"CachedMethods": Object {

__tests__/__snapshots__/s3-origin.test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ Object {
259259
"Items": Array [],
260260
"Quantity": 0,
261261
},
262+
"Comment": "",
262263
"DefaultCacheBehavior": Object {
263264
"AllowedMethods": Object {
264265
"CachedMethods": Object {

__tests__/general-options.test.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const { createComponent } = require('../test-utils')
2+
3+
const {
4+
mockCreateDistribution,
5+
mockUpdateDistribution,
6+
mockCreateDistributionPromise,
7+
mockGetDistributionConfigPromise,
8+
mockUpdateDistributionPromise
9+
} = require('aws-sdk')
10+
11+
describe('General options propagation', () => {
12+
let component
13+
14+
// sample origins
15+
const origins = ['https://exampleorigin.com']
16+
17+
beforeEach(async () => {
18+
mockCreateDistributionPromise.mockResolvedValueOnce({
19+
Distribution: {
20+
Id: 'distribution123'
21+
}
22+
})
23+
24+
mockGetDistributionConfigPromise.mockResolvedValueOnce({
25+
ETag: 'etag',
26+
DistributionConfig: {
27+
Origins: {
28+
Items: []
29+
}
30+
}
31+
})
32+
mockUpdateDistributionPromise.mockResolvedValueOnce({
33+
Distribution: {
34+
Id: 'xyz'
35+
}
36+
})
37+
38+
component = await createComponent()
39+
})
40+
41+
it('create distribution with comment and update it', async () => {
42+
await component.default({
43+
comment: 'test comment',
44+
origins
45+
})
46+
47+
expect(mockCreateDistribution).toBeCalledWith(
48+
expect.objectContaining({
49+
DistributionConfig: expect.objectContaining({
50+
Comment: 'test comment'
51+
})
52+
})
53+
)
54+
55+
await component.default({
56+
comment: 'updated comment',
57+
origins
58+
})
59+
60+
expect(mockUpdateDistribution).toBeCalledWith(
61+
expect.objectContaining({
62+
DistributionConfig: expect.objectContaining({
63+
Comment: 'updated comment'
64+
})
65+
})
66+
)
67+
})
68+
69+
it('create disabled distribution and update it', async () => {
70+
await component.default({
71+
enabled: false,
72+
origins
73+
})
74+
75+
expect(mockCreateDistribution).toBeCalledWith(
76+
expect.objectContaining({
77+
DistributionConfig: expect.objectContaining({
78+
Enabled: false
79+
})
80+
})
81+
)
82+
83+
await component.default({
84+
enabled: true,
85+
origins
86+
})
87+
88+
expect(mockUpdateDistribution).toBeCalledWith(
89+
expect.objectContaining({
90+
DistributionConfig: expect.objectContaining({
91+
Enabled: true
92+
})
93+
})
94+
)
95+
})
96+
})

lib/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
2323
const params = {
2424
DistributionConfig: {
2525
CallerReference: String(Date.now()),
26-
Comment: '',
26+
Comment: inputs.comment,
2727
Aliases: {
2828
Quantity: 0,
2929
Items: []
@@ -33,7 +33,7 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
3333
Items: []
3434
},
3535
PriceClass: 'PriceClass_All',
36-
Enabled: inputs.enabled === false ? false : true,
36+
Enabled: inputs.enabled,
3737
HttpVersion: 'http2'
3838
}
3939
}
@@ -93,7 +93,8 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
9393

9494
// 5. then make our changes
9595

96-
params.DistributionConfig.Enabled = inputs.enabled === false ? false : true
96+
params.DistributionConfig.Enabled = inputs.enabled
97+
params.DistributionConfig.Comment = inputs.comment
9798

9899
let s3CanonicalUserId
99100
let originAccessIdentityId

serverless.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class CloudFront extends Component {
2020
this.context.status('Deploying')
2121

2222
inputs.region = inputs.region || 'us-east-1'
23+
inputs.enabled = inputs.enabled === false ? false : true
24+
inputs.comment =
25+
inputs.comment === null || inputs.comment === undefined ? '' : String(inputs.comment)
2326

2427
this.context.debug(
2528
`Starting deployment of CloudFront distribution to the ${inputs.region} region.`
@@ -38,7 +41,9 @@ class CloudFront extends Component {
3841
if (this.state.id) {
3942
if (
4043
!equals(this.state.origins, inputs.origins) ||
41-
!equals(this.state.defaults, inputs.defaults)
44+
!equals(this.state.defaults, inputs.defaults) ||
45+
!equals(this.state.enabled, inputs.enabled) ||
46+
!equals(this.state.comment, inputs.comment)
4247
) {
4348
this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`)
4449
this.state = await updateCloudFrontDistribution(cf, s3, this.state.id, inputs)
@@ -49,6 +54,8 @@ class CloudFront extends Component {
4954
}
5055

5156
this.state.region = inputs.region
57+
this.state.enabled = inputs.enabled
58+
this.state.comment = inputs.comment
5259
this.state.origins = inputs.origins
5360
this.state.defaults = inputs.defaults
5461
await this.save()

0 commit comments

Comments
 (0)