-
Notifications
You must be signed in to change notification settings - Fork 103
/
create.ts
59 lines (51 loc) · 1.91 KB
/
create.ts
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
import { Flags } from '@oclif/core'
import { SchemaAppRequest, SchemaCreateResponse } from '@smartthings/core-sdk'
import {
APIOrganizationCommand,
inputAndOutputItem,
lambdaAuthFlags,
userInputProcessor,
} from '@smartthings/cli-lib'
import { addSchemaPermission } from '../../lib/aws-utils'
import { SCHEMA_AWS_PRINCIPAL, getSchemaAppCreateFromUser } from '../../lib/commands/schema-util'
export default class SchemaAppCreateCommand extends APIOrganizationCommand<typeof SchemaAppCreateCommand.flags> {
static description = 'create an ST Schema connector' +
this.apiDocsURL('postApps')
static flags = {
...APIOrganizationCommand.flags,
...inputAndOutputItem.flags,
authorize: Flags.boolean({
description: 'authorize connector\'s Lambda functions to be called by SmartThings',
}),
...lambdaAuthFlags,
}
async run(): Promise<void> {
const createApp = async (_: void, data: SchemaAppRequest): Promise<SchemaCreateResponse> => {
if (this.flags.authorize) {
if (data.hostingType === 'lambda') {
const principal = this.flags.principal ?? SCHEMA_AWS_PRINCIPAL
const statementId = this.flags.statement
if (data.lambdaArn) {
await addSchemaPermission(data.lambdaArn, principal, statementId)
}
if (data.lambdaArnAP) {
await addSchemaPermission(data.lambdaArnAP, principal, statementId)
}
if (data.lambdaArnCN) {
await addSchemaPermission(data.lambdaArnCN, principal, statementId)
}
if (data.lambdaArnEU) {
await addSchemaPermission(data.lambdaArnEU, principal, statementId)
}
} else {
this.error('Authorization is not applicable to WebHook schema connectors')
}
}
return this.client.schema.create(data)
}
await inputAndOutputItem(this,
{ tableFieldDefinitions: ['endpointAppId', 'stClientId', 'stClientSecret'] },
createApp, userInputProcessor(() => getSchemaAppCreateFromUser(this, this.flags['dry-run'])),
)
}
}