-
Notifications
You must be signed in to change notification settings - Fork 181
/
index.js
100 lines (84 loc) · 2.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { get, lowerCase, or, pick, resolvable, resolve } from '@serverless/utils'
import { createBucket, deleteBucket } from './utils'
const DEPLOY = 'deploy'
const REPLACE = 'replace'
const AwsS3Bucket = (SuperClass) =>
class extends SuperClass {
async construct(inputs, context) {
await super.construct(inputs, context)
// TODO: remove this validation once core supports full RAML spec
const bucketNameRegex = new RegExp(this.inputTypes.bucketName.pattern)
if (inputs.bucketName && !bucketNameRegex.test(inputs.bucketName)) {
throw new Error(`Bucket name does not match regex "${bucketNameRegex.toString()}"`)
}
// NOTE: the bucket name needs to be lower case
this.bucketName = resolvable(() =>
lowerCase(or(inputs.bucketName, `bucket-${this.instanceId}`))
)
this.provider = resolvable(() => or(inputs.provider, context.get('provider')))
}
async sync() {
let { provider } = this
provider = resolve(provider)
const AWS = provider.getSdk()
const S3 = new AWS.S3()
try {
await S3.getBucketLocation({ Bucket: resolve(this.bucketName) }).promise()
} catch (e) {
if (e.code === 'NoSuchBucket') {
return 'removed'
}
throw e
}
}
shouldDeploy(prevInstance) {
if (!prevInstance) {
return DEPLOY
}
if (prevInstance.bucketName !== this.bucketName) {
return REPLACE
}
}
async deploy(prevInstance, context) {
context.log(`Creating Bucket: '${get('bucketName', this)}'`)
await createBucket(this)
context.log(`Bucket created: '${get('bucketName', this)}'`)
}
async remove(context) {
context.log(`Removing Bucket: '${this.bucketName}'`)
await deleteBucket(this)
}
async info() {
return {
title: this.name,
type: this.name,
data: pick(['name', 'license', 'version', 'bucketName'], this)
}
}
async updateS3Config({ event, filter, function: func }) {
await this.deploy({ event, filter, function: func })
}
async deploySource(subscription) {
const config = subscription.getConfig()
const sinkConfig = subscription.getSinkConfig()
if (sinkConfig.protocol === 'AwsLambdaFunction') {
await this.updateS3Configuration({
event: config.event,
filter: config.filter,
function: sinkConfig.uri // the Lambda arn
})
} else if (sinkConfig.protocol === 'AwsSNSTopic') {
await this.updateS3Configuration({
event: config.event,
filter: config.filter,
topic: sinkConfig.uri // the SNS Topic arn
})
}
}
getSourceConfig() {
return {
uri: this.arn
}
}
}
export default AwsS3Bucket