-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathset.ts
98 lines (92 loc) · 3.38 KB
/
set.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* eslint-disable complexity */
import Command from '../../base'
import {flags} from '@oclif/command'
import chalk from 'chalk'
import cli from 'cli-ux'
import getStream from 'get-stream'
import * as utils from '../../utils'
export default class ServiceSet extends Command {
static description = 'Set PagerDuty Service attributes'
static flags = {
...Command.flags,
name: flags.string({
char: 'n',
description: 'Select services whose names contain the given text',
}),
ids: flags.string({
char: 'i',
description: 'Select services with the given ID. Specify multiple times for multiple services.',
multiple: true,
}),
key: flags.string({
char: 'k',
description: 'Attribute key to set',
required: true,
}),
value: flags.string({
char: 'v',
description: 'Attribute value to set',
required: true,
}),
pipe: flags.boolean({
char: 'p',
description: 'Read service ID\'s from stdin.',
exclusive: ['name', 'ids'],
}),
}
async run() {
const {flags} = this.parse(ServiceSet)
if (!(flags.name || flags.ids || flags.pipe)) {
this.error('You must specify one of: -i, -n, -p', {exit: 1})
}
let service_ids: string[] = []
if (flags.name) {
cli.action.start('Getting service IDs from PD')
const services = await this.pd.fetch('services', {params: {query: flags.name}})
if (!services || services.length === 0) {
cli.action.stop(chalk.bold.red('none found'))
}
service_ids = services.map((e: { id: any }) => e.id)
} else if (flags.ids) {
service_ids = utils.splitDedupAndFlatten(flags.ids)
} else if (flags.pipe) {
const str: string = await getStream(process.stdin)
service_ids = utils.splitDedupAndFlatten([str])
}
const invalid_ids = utils.invalidPagerDutyIDs(service_ids)
if (invalid_ids && invalid_ids.length > 0) {
this.error(`Invalid service ID's: ${invalid_ids.join(', ')}`, {exit: 1})
}
const key = flags.key
const value = flags.value.trim().length > 0 ? flags.value : null
const requests: any[] = []
for (const service_id of service_ids) {
const body: Record<string, any> = utils.putBodyForSetAttribute('service', service_id, key, value)
requests.push({
endpoint: `/services/${service_id}`,
method: 'PUT',
params: {},
data: body,
})
}
const r = await this.pd.batchedRequestWithSpinner(requests, {
activityDescription: `Setting ${chalk.bold.blue(flags.key)} = '${chalk.bold.blue(flags.value)}' on ${service_ids.length} services`,
})
for (const failure of r.getFailedIndices()) {
// eslint-disable-next-line no-console
console.error(`${chalk.bold.red('Failed to set service ')}${chalk.bold.blue(requests[failure].data.service.id)}: ${r.results[failure].getFormattedError()}`)
}
for (const s of r.getDatas()) {
if (s.service[key] !== value) {
if (key === 'status' && value === 'active') {
// special case when setting status = active, it can come back as active, warning or critical
if (['active', 'warning', 'critical'].indexOf(s.service[key]) > -1) {
continue
}
}
// eslint-disable-next-line no-console
console.error(`${chalk.bold.red('Failed to set value on service ')}${chalk.bold.blue(s.service.id)}`)
}
}
}
}