Skip to content

Commit c19e0c4

Browse files
authored
fix: support autoSetupAcl,autoSetupPolicy config (#32)
* fix: support autoSetupAcl,autoSetupPolicy config * fix: update deps * docs: update configure
1 parent 779b1b3 commit c19e0c4

File tree

10 files changed

+165
-71
lines changed

10 files changed

+165
-71
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ jobs:
4343
env:
4444
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
4545
GIT_AUTHOR_NAME: slsplus
46-
GIT_AUTHOR_EMAIL: yuga.sun.bj@gmail.com
46+
GIT_AUTHOR_EMAIL: slsplus.sz@gmail.com
4747
GIT_COMMITTER_NAME: slsplus
48-
GIT_COMMITTER_EMAIL: yuga.sun.bj@gmail.com
48+
GIT_COMMITTER_EMAIL: slsplus.sz@gmail.com

docs/configure.md

Lines changed: 95 additions & 53 deletions
Large diffs are not rendered by default.

jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { join } = require('path')
2+
require('dotenv').config({ path: join(__dirname, '.env.test') })
3+
4+
const config = {
5+
verbose: true,
6+
silent: false,
7+
testTimeout: 300000,
8+
testEnvironment: 'node',
9+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$',
10+
testPathIgnorePatterns: ['/node_modules/', '/__tests__/lib/'],
11+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
12+
}
13+
14+
module.exports = config

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"description": "Easily deploy serverless websites (e.g. Vue.js, React, static) to Tencent Cloud with the Serverless Framework",
88
"scripts": {
9-
"test": "jest ./tests/integration.test.js --testEnvironment node",
9+
"test": "jest",
1010
"commitlint": "commitlint -f HEAD@{15}",
1111
"lint": "eslint --ext .js,.ts,.tsx .",
1212
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
@@ -46,6 +46,7 @@
4646
"@semantic-release/release-notes-generator": "^9.0.1",
4747
"@serverless/platform-client-china": "^1.0.19",
4848
"@ygkit/secure": "0.0.3",
49+
"axios": "^0.21.0",
4950
"babel-eslint": "^10.1.0",
5051
"dotenv": "^8.2.0",
5152
"eslint": "^6.8.0",

serverless.component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: website
2-
version: 0.1.0
2+
version: 0.1.1
33
author: 'Tencent Cloud, Inc.'
44
org: 'Tencent Cloud, Inc.'
55
description: Deploy a static website on Tencent Cloud.

src/config.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,34 @@ const CONFIGS = {
66
compFullname: 'Website',
77
indexPage: 'index.html',
88
errorPage: 'error.html',
9-
protocol: 'http',
10-
description: 'This is a bucket created by serverless component'
9+
protocol: 'https',
10+
description: 'Created by Serverless Component',
11+
acl: {
12+
permissions: 'public-read',
13+
grantRead: '',
14+
grantWrite: '',
15+
grantFullControl: ''
16+
},
17+
getPolicy(region, bucket, appid) {
18+
return {
19+
Statement: [
20+
{
21+
Principal: { qcs: ['qcs::cam::anyone:anyone'] },
22+
Effect: 'Allow',
23+
Action: [
24+
'name/cos:HeadBucket',
25+
'name/cos:ListMultipartUploads',
26+
'name/cos:ListParts',
27+
'name/cos:GetObject',
28+
'name/cos:HeadObject',
29+
'name/cos:OptionsObject'
30+
],
31+
Resource: [`qcs::cos:${region}:uid/${appid}:${bucket}-${appid}/*`]
32+
}
33+
],
34+
version: '2.0'
35+
}
36+
}
1137
}
1238

1339
module.exports = CONFIGS

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
33
"download": "^8.0.0",
4-
"tencent-component-toolkit": "^1.17.3",
4+
"tencent-component-toolkit": "^1.19.7",
55
"type": "^2.1.0"
66
}
77
}

src/utils.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ const prepareInputs = async (instance, inputs) => {
5858
if (!code.src) {
5959
sourceDirectory = `${sourceDirectory}/src`
6060
}
61-
console.log(`Files unzipped into ${sourceDirectory}...`)
61+
console.log(`Files unzipped into ${sourceDirectory}`)
6262

6363
const region = inputs.region || CONFIGS.region
6464
const bucketName =
6565
removeAppid(inputs.bucketName, appId) || `sls-website-${region}-${generateId()}`
6666

67-
return {
67+
const websiteInputs = {
6868
replace: inputs.replace,
6969
useDefault: !code.src,
7070
code: {
@@ -79,6 +79,18 @@ const prepareInputs = async (instance, inputs) => {
7979
protocol: inputs.protocol || CONFIGS.protocol,
8080
cors: inputs.cors
8181
}
82+
83+
// auto setup acl for public-read
84+
if (inputs.autoSetupAcl !== false) {
85+
websiteInputs.acl = CONFIGS.acl
86+
}
87+
88+
// auto setup policy for public read
89+
if (inputs.autoSetupPolicy === true) {
90+
websiteInputs.policy = CONFIGS.getPolicy(region, bucketName, appId)
91+
}
92+
93+
return websiteInputs
8294
}
8395

8496
module.exports = {

tests/integration.test.js renamed to tests/index.test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
const path = require('path')
22
require('dotenv').config({path: path.join(__dirname, '..', '.env.test')})
3-
const { generateId, getServerlessSdk } = require('./utils')
4-
5-
// set enough timeout for deployment to finish
6-
jest.setTimeout(300000)
3+
const axios = require('axios')
4+
const { generateId, getServerlessSdk } = require('./lib/utils')
75

86
// the yaml file we're testing against
97
const instanceYaml = {
108
org: 'orgDemo',
119
app: 'appDemo',
12-
component: 'website',
10+
component: 'website@dev',
1311
name: `website-integration-tests-${generateId()}`,
1412
stage: 'dev',
1513
inputs: {
16-
src: path.join(__dirname, '..', 'example'),
14+
src: path.join(__dirname, '..', 'example/src'),
1715
bucketName: 'my-bucket',
1816
region: 'ap-guangzhou'
1917
}
@@ -26,19 +24,20 @@ const credentials = {
2624
}
2725
}
2826

29-
// get serverless construct sdk
3027
const sdk = getServerlessSdk(instanceYaml.org)
3128

32-
it('should successfully deploy website app', async () => {
29+
it('should deploy success', async () => {
3330
const instance = await sdk.deploy(instanceYaml, credentials)
3431

3532
expect(instance).toBeDefined()
3633
expect(instance.instanceName).toEqual(instanceYaml.name)
3734
expect(instance.outputs.website).toBeDefined()
3835
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
36+
const content = await axios.get(instance.outputs.website)
37+
expect(content.data).toContain('Serverless Framework');
3938
})
4039

41-
it('should successfully remove website app', async () => {
40+
it('should remove success', async () => {
4241
await sdk.remove(instanceYaml, credentials)
4342
result = await sdk.getInstance(instanceYaml.org, instanceYaml.stage, instanceYaml.app, instanceYaml.name)
4443

File renamed without changes.

0 commit comments

Comments
 (0)