|
3 | 3 | const BbPromise = require('bluebird'); |
4 | 4 | const secrets = require('../../shared/secrets'); |
5 | 5 | const singleSource = require('../../shared/singleSource'); |
| 6 | +const domainUtils = require('../../shared/domains'); |
6 | 7 | const { RUNTIME_STATUS_AVAILABLE, RUNTIME_STATUS_EOL, RUNTIME_STATUS_EOS } = require('../../shared/runtimes'); |
7 | 8 |
|
8 | 9 | module.exports = { |
@@ -53,77 +54,21 @@ module.exports = { |
53 | 54 | }); |
54 | 55 | }, |
55 | 56 |
|
56 | | - applyDomains(funcId, customDomains) { |
| 57 | + applyDomainsFunc(funcId, customDomains) { |
57 | 58 | // we make a diff to know which domains to add or delete |
58 | | - const domainsToCreate = []; |
59 | | - const domainsIdToDelete = []; |
60 | | - const existingDomains = []; |
61 | 59 |
|
62 | | - this.listDomains(funcId).then((domains) => { |
63 | | - domains.forEach((domain) => { |
64 | | - existingDomains.push({ hostname: domain.hostname, id: domain.id }); |
65 | | - }); |
66 | | - |
67 | | - if ( |
68 | | - customDomains !== undefined && |
69 | | - customDomains !== null && |
70 | | - customDomains.length > 0 |
71 | | - ) { |
72 | | - customDomains.forEach((customDomain) => { |
73 | | - domainsIdToDelete.push(customDomain.id); |
74 | | - |
75 | | - let domainFound = false; |
76 | | - |
77 | | - existingDomains.forEach((existingDom) => { |
78 | | - if (existingDom.hostname === customDomain) { |
79 | | - domainFound = true; |
80 | | - return false; |
81 | | - } |
82 | | - }); |
83 | | - if (!domainFound) { |
84 | | - domainsToCreate.push(customDomain); |
85 | | - } |
86 | | - }); |
87 | | - } |
88 | | - |
89 | | - existingDomains.forEach((existingDomain) => { |
90 | | - if ( |
91 | | - (customDomains === undefined || customDomains === null) && |
92 | | - existingDomain.id !== undefined |
93 | | - ) { |
94 | | - domainsIdToDelete.push(existingDomain.id); |
95 | | - } else if (!customDomains.includes(existingDomain.hostname)) { |
96 | | - domainsIdToDelete.push(existingDomain.id); |
97 | | - } |
98 | | - }); |
| 60 | + this.listDomainsFunction(funcId).then((domains) => { |
| 61 | + const existingDomains = domainUtils.formatDomainsStructure(domains); |
| 62 | + const domainsToCreate = domainUtils.getDomainsToCreate(customDomains, existingDomains); |
| 63 | + const domainsIdToDelete = domainUtils.getDomainsToDelete(customDomains, existingDomains); |
99 | 64 |
|
100 | 65 | domainsToCreate.forEach((newDomain) => { |
101 | 66 | const createDomainParams = { function_id: funcId, hostname: newDomain }; |
102 | 67 |
|
103 | | - this.createDomain(createDomainParams) |
104 | | - .then((res) => { |
105 | | - this.serverless.cli.log(`Creating domain ${res.hostname}`); |
106 | | - }) |
107 | | - .then( |
108 | | - () => {}, |
109 | | - (reason) => { |
110 | | - this.serverless.cli.log( |
111 | | - `Error on domain : ${newDomain}, reason : ${reason.message}` |
112 | | - ); |
113 | | - |
114 | | - if (reason.message.includes("could not validate")) { |
115 | | - this.serverless.cli.log( |
116 | | - "Ensure CNAME configuration is ok, it can take some time for a record to propagate" |
117 | | - ); |
118 | | - } |
119 | | - } |
120 | | - ); |
| 68 | + this.createDomainAndLog(createDomainParams); |
121 | 69 | }); |
122 | 70 |
|
123 | 71 | domainsIdToDelete.forEach((domainId) => { |
124 | | - if (domainId === undefined) { |
125 | | - return; |
126 | | - } |
127 | 72 | this.deleteDomain(domainId).then((res) => { |
128 | 73 | this.serverless.cli.log(`Deleting domain ${res.hostname}`); |
129 | 74 | }); |
@@ -226,7 +171,7 @@ Runtime lifecycle doc : https://www.scaleway.com/en/docs/compute/functions/refer |
226 | 171 | params.runtime = this.validateRuntime( |
227 | 172 | func, |
228 | 173 | availableRuntimes, |
229 | | - this.serverless.cli |
| 174 | + this.serverless.cli, |
230 | 175 | ); |
231 | 176 |
|
232 | 177 | // checking if there is custom_domains set on function creation. |
@@ -270,7 +215,7 @@ Runtime lifecycle doc : https://www.scaleway.com/en/docs/compute/functions/refer |
270 | 215 | this.serverless.cli.log(`Updating function ${func.name}...`); |
271 | 216 |
|
272 | 217 | // assign domains |
273 | | - this.applyDomains(foundFunc.id, func.custom_domains); |
| 218 | + this.applyDomainsFunc(foundFunc.id, func.custom_domains); |
274 | 219 |
|
275 | 220 | return this.updateFunction(foundFunc.id, params).then((response) => |
276 | 221 | Object.assign(response, { handler: func.handler }) |
|
0 commit comments