Skip to content

Commit 8432b93

Browse files
committed
feat: scf support publish version and traffic config
1 parent edca386 commit 8432b93

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

src/modules/scf/index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,83 @@ class Scf {
312312
}
313313
}
314314

315+
/**
316+
* publish function version
317+
* @param {object} inputs publish version parameter
318+
*/
319+
async publishVersion(inputs) {
320+
const publishInputs = {
321+
Action: 'PublishVersion',
322+
Version: '2018-04-16',
323+
Region: inputs.region,
324+
FunctionName: inputs.functionName,
325+
Description: inputs.description || 'Published by Serverless Component',
326+
Namespace: inputs.namespace || 'default',
327+
};
328+
const res = await this.scfClient.request(publishInputs);
329+
if (res.Response && res.Response.Error) {
330+
throw new TypeError(
331+
'API_SCF_PublishVersion',
332+
JSON.stringify(res.Response),
333+
null,
334+
res.Response.RequestId,
335+
);
336+
}
337+
return res.Response;
338+
}
339+
340+
async publishVersionAndConfigTraffic(inputs) {
341+
const publishInputs = {
342+
Action: 'CreateAlias',
343+
Version: '2018-04-16',
344+
Region: inputs.region,
345+
FunctionName: inputs.functionName,
346+
FunctionVersion: inputs.functionVersion,
347+
Name: inputs.aliasName,
348+
Namespace: inputs.namespace || 'default',
349+
RoutingConfig: {
350+
AdditionalVersionWeights: [{ Version: inputs.functionVersion, Weight: 1 - inputs.traffic }],
351+
},
352+
Description: inputs.description || 'Published by Serverless Component',
353+
};
354+
const res = await this.scfClient.request(publishInputs);
355+
if (res.Response && res.Response.Error) {
356+
throw new TypeError(
357+
'API_SCF_CreateAlias',
358+
JSON.stringify(res.Response),
359+
null,
360+
res.Response.RequestId,
361+
);
362+
}
363+
return res.Response;
364+
}
365+
366+
async updateAliasTraffic(inputs) {
367+
const publishInputs = {
368+
Action: 'UpdateAlias',
369+
Version: '2018-04-16',
370+
Region: inputs.region,
371+
FunctionName: inputs.functionName,
372+
FunctionVersion: inputs.functionVersion || '$LATEST',
373+
Name: inputs.aliasName || '$DEFAULT',
374+
Namespace: inputs.namespace || 'default',
375+
RoutingConfig: {
376+
AdditionalVersionWeights: [{ Version: inputs.lastVersion, Weight: 1 - inputs.traffic }],
377+
},
378+
Description: inputs.description || 'Configured by Serverless Component',
379+
};
380+
const res = await this.scfClient.request(publishInputs);
381+
if (res.Response && res.Response.Error) {
382+
throw new TypeError(
383+
'API_SCF_UpdateAlias',
384+
JSON.stringify(res.Response),
385+
null,
386+
res.Response.RequestId,
387+
);
388+
}
389+
return res.Response;
390+
}
391+
315392
// deploy SCF flow
316393
async deploy(inputs = {}) {
317394
// whether auto create/bind role

src/modules/scf/index.test.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,35 @@ class ClientTest {
6969
},
7070
],
7171
};
72+
73+
// 1. deploy test
7274
const result = await scf.deploy(scfDemo);
7375
try{
7476
console.log(JSON.stringify(result));
7577
} catch (e) {
7678
console.log(e);
7779
}
78-
// console.log(await scf.invoke(result.FunctionName))
79-
await scf.remove(result);
80+
81+
// 2. publish version test
82+
// const res = await scf.publishVersion({
83+
// functionName: 'test',
84+
// region: 'ap-guangzhou',
85+
// });
86+
87+
// console.log('res', JSON.stringify(res));
88+
89+
// 3. update alias traffic
90+
// const res = await scf.updateAliasTraffic({
91+
// functionName: 'test',
92+
// region: 'ap-guangzhou',
93+
// traffic: 0.8,
94+
// lastVersion: 3,
95+
// });
96+
97+
// console.log('res', JSON.stringify(res));
98+
99+
// 4. remove function
100+
// await scf.remove(result);
80101
}
81102
}
82103

0 commit comments

Comments
 (0)