|
| 1 | +const parseInputOrigins = require('./parseInputOrigins') |
| 2 | +const getDefaultCacheBehavior = require('./getDefaultCacheBehavior') |
| 3 | + |
| 4 | +const createCloudFrontDistribution = async (cf, inputs) => { |
| 5 | + const params = { |
| 6 | + DistributionConfig: { |
| 7 | + CallerReference: String(Date.now()), |
| 8 | + Comment: '', |
| 9 | + Aliases: { |
| 10 | + Quantity: 0, |
| 11 | + Items: [] |
| 12 | + }, |
| 13 | + Origins: { |
| 14 | + Quantity: 0, |
| 15 | + Items: [] |
| 16 | + }, |
| 17 | + PriceClass: 'PriceClass_All', |
| 18 | + Enabled: inputs.enabled === false ? false : true, |
| 19 | + HttpVersion: 'http2' |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + const distributionConfig = params.DistributionConfig |
| 24 | + |
| 25 | + const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins) |
| 26 | + |
| 27 | + distributionConfig.Origins = Origins |
| 28 | + |
| 29 | + // set first origin declared as the default cache behavior |
| 30 | + distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(Origins.Items[0].Id) |
| 31 | + |
| 32 | + if (CacheBehaviors) { |
| 33 | + distributionConfig.CacheBehaviors = CacheBehaviors |
| 34 | + } |
| 35 | + |
| 36 | + const res = await cf.createDistribution(params).promise() |
| 37 | + |
| 38 | + return { |
| 39 | + id: res.Distribution.Id, |
| 40 | + arn: res.Distribution.ARN, |
| 41 | + url: `https://${res.Distribution.DomainName}` |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const updateCloudFrontDistribution = async (cf, distributionId, inputs) => { |
| 46 | + // Update logic is a bit weird... |
| 47 | + // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#updateDistribution-property |
| 48 | + |
| 49 | + // 1. we gotta get the config first... |
| 50 | + // todo what if id does not exist? |
| 51 | + const params = await cf.getDistributionConfig({ Id: distributionId }).promise() |
| 52 | + |
| 53 | + // 2. then add this property |
| 54 | + params.IfMatch = params.ETag |
| 55 | + |
| 56 | + // 3. then delete this property |
| 57 | + delete params.ETag |
| 58 | + |
| 59 | + // 4. then set this property |
| 60 | + params.Id = distributionId |
| 61 | + |
| 62 | + // 5. then make our changes |
| 63 | + |
| 64 | + params.DistributionConfig.Enabled = inputs.enabled === false ? false : true |
| 65 | + |
| 66 | + const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins) |
| 67 | + |
| 68 | + params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(Origins.Items[0].Id) |
| 69 | + params.DistributionConfig.Origins = Origins |
| 70 | + |
| 71 | + if (CacheBehaviors) { |
| 72 | + params.DistributionConfig.CacheBehaviors = CacheBehaviors |
| 73 | + } |
| 74 | + |
| 75 | + // 6. then finally update! |
| 76 | + const res = await cf.updateDistribution(params).promise() |
| 77 | + |
| 78 | + return { |
| 79 | + id: res.Distribution.Id, |
| 80 | + arn: res.Distribution.ARN, |
| 81 | + url: `https://${res.Distribution.DomainName}` |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const disableCloudFrontDistribution = async (cf, distributionId) => { |
| 86 | + const params = await cf.getDistributionConfig({ Id: distributionId }).promise() |
| 87 | + |
| 88 | + params.IfMatch = params.ETag |
| 89 | + |
| 90 | + delete params.ETag |
| 91 | + |
| 92 | + params.Id = distributionId |
| 93 | + |
| 94 | + params.DistributionConfig.Enabled = false |
| 95 | + |
| 96 | + const res = await cf.updateDistribution(params).promise() |
| 97 | + |
| 98 | + return { |
| 99 | + id: res.Distribution.Id, |
| 100 | + arn: res.Distribution.ARN, |
| 101 | + url: `https://${res.Distribution.DomainName}` |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +const deleteCloudFrontDistribution = async (cf, distributionId) => { |
| 106 | + try { |
| 107 | + const res = await cf.getDistributionConfig({ Id: distributionId }).promise() |
| 108 | + |
| 109 | + const params = { Id: distributionId, IfMatch: res.ETag } |
| 110 | + await cf.deleteDistribution(params).promise() |
| 111 | + } catch (e) { |
| 112 | + if (e.code === 'DistributionNotDisabled') { |
| 113 | + await disableCloudFrontDistribution(cf, distributionId) |
| 114 | + } else { |
| 115 | + throw e |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +module.exports = { |
| 121 | + createCloudFrontDistribution, |
| 122 | + updateCloudFrontDistribution, |
| 123 | + deleteCloudFrontDistribution |
| 124 | +} |
0 commit comments