-
Notifications
You must be signed in to change notification settings - Fork 102
/
update.ts
75 lines (65 loc) · 2.57 KB
/
update.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Flags, Errors } from '@oclif/core'
import { SchemaApp, SchemaAppRequest } from '@smartthings/core-sdk'
import { APIOrganizationCommand, inputItem, selectFromList, lambdaAuthFlags, SelectFromListConfig, userInputProcessor } from '@smartthings/cli-lib'
import { addSchemaPermission } from '../../lib/aws-utils'
import { getSchemaAppUpdateFromUser } from '../../lib/commands/schema-util'
export default class SchemaUpdateCommand extends APIOrganizationCommand<typeof SchemaUpdateCommand.flags> {
static description = 'update an ST Schema connector' +
this.apiDocsURL('putAppsByEndpointAppId')
static flags = {
...APIOrganizationCommand.flags,
...inputItem.flags,
// eslint-disable-next-line @typescript-eslint/naming-convention
'dry-run': Flags.boolean({
char: 'd',
description: "produce JSON but don't actually submit",
}),
authorize: Flags.boolean({
description: 'authorize Lambda functions to be called by SmartThings',
}),
...lambdaAuthFlags,
}
static args = [{
name: 'id',
description: 'the app id',
}]
async run(): Promise<void> {
const config: SelectFromListConfig<SchemaApp> = {
primaryKeyName: 'endpointAppId',
sortKeyName: 'appName',
listTableFieldDefinitions: ['appName', 'endpointAppId', 'hostingType'],
}
const id = await selectFromList(this, config, {
preselectedId: this.args.id,
listItems: () => this.client.schema.list(),
})
const getInputFromUser = async (): Promise<SchemaAppRequest> => {
const original = await this.client.schema.get(id)
return getSchemaAppUpdateFromUser(this, original, this.flags['dry-run'])
}
const [request] = await inputItem<SchemaAppRequest>(this, userInputProcessor(getInputFromUser))
if (this.flags.authorize) {
if (request.hostingType === 'lambda') {
if (request.lambdaArn) {
await addSchemaPermission(request.lambdaArn, this.flags.principal, this.flags.statement)
}
if (request.lambdaArnAP) {
await addSchemaPermission(request.lambdaArnAP, this.flags.principal, this.flags.statement)
}
if (request.lambdaArnCN) {
await addSchemaPermission(request.lambdaArnCN, this.flags.principal, this.flags.statement)
}
if (request.lambdaArnEU) {
await addSchemaPermission(request.lambdaArnEU, this.flags.principal, this.flags.statement)
}
} else {
throw Error('Authorization is not applicable to WebHook schema connectors')
}
}
const result = await this.client.schema.update(id, request)
if (result.status !== 'success') {
throw new Errors.CLIError(`error ${result.status} updating ${id}`)
}
this.log(`Schema ${id} updated.`)
}
}