-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathset-metadata.ts
More file actions
104 lines (97 loc) · 3.34 KB
/
set-metadata.ts
File metadata and controls
104 lines (97 loc) · 3.34 KB
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
101
102
103
104
import {
DistributionBucketOperatorMetadata,
IDistributionBucketOperatorMetadata,
INodeOperationalStatus,
NodeOperationalStatus,
} from '@joystream/metadata-protobuf'
import fs from 'fs'
import AccountsCommandBase from '../../command-base/accounts'
import DefaultCommandBase, { flags } from '../../command-base/default'
import { ValidationService } from '../../services/validation/ValidationService'
export default class OperatorSetMetadata extends AccountsCommandBase {
static description = `Set/update distribution bucket operator metadata.
Requires active distribution bucket operator worker role key.`
static flags = {
bucketId: flags.bucketId({
required: true,
}),
workerId: flags.integer({
char: 'w',
description: 'ID of the operator (distribution group worker)',
required: true,
}),
endpoint: flags.string({
char: 'e',
description: 'Root distribution node endpoint',
exclusive: ['input'],
}),
operationalStatus: flags.enum<Exclude<NodeOperationalStatus['nodeOperationalStatus'], undefined>>({
char: 'o',
options: ['normal', 'noService', 'noServiceFrom', 'noServiceUntil'],
required: false,
description: 'Operational status of the operator',
}),
input: flags.string({
char: 'i',
description: 'Path to JSON metadata file',
exclusive: ['endpoint'],
}),
...DefaultCommandBase.flags,
}
async run(): Promise<void> {
const { bucketId, workerId, input, endpoint, operationalStatus: statusType } = this.parse(OperatorSetMetadata).flags
const workerKey = await this.getDistributorWorkerRoleKey(workerId)
let operationalStatus: INodeOperationalStatus
switch (statusType) {
case 'normal': {
operationalStatus = { normal: {} }
break
}
case 'noService': {
operationalStatus = { noService: {} }
break
}
case 'noServiceFrom': {
operationalStatus = {
noServiceFrom: {
from: (await this.datePrompt({ message: 'Enter No Service period start date' })).toISOString(),
},
}
break
}
case 'noServiceUntil': {
operationalStatus = {
noServiceUntil: {
from: (await this.datePrompt({ message: 'Enter No Service period start date' })).toISOString(),
until: (await this.datePrompt({ message: 'Enter No Service period end date' })).toISOString(),
},
}
}
}
const validation = new ValidationService()
let metadata: IDistributionBucketOperatorMetadata
if (input) {
const params = validation.validate('OperatorMetadata', JSON.parse(fs.readFileSync(input).toString()))
metadata = {
...params,
...(params.operationalStatus && { operationalStatus: params.operationalStatus }),
}
} else {
metadata = { endpoint, operationalStatus }
}
this.log(`Setting bucket operator metadata...`, {
bucketId: bucketId.toHuman(),
workerId,
metadata,
})
await this.sendAndFollowTx(
await this.getDecodedPair(workerKey),
this.api.tx.storage.setDistributionOperatorMetadata(
workerId,
bucketId,
'0x' + Buffer.from(DistributionBucketOperatorMetadata.encode(metadata).finish()).toString('hex')
)
)
this.log('Bucket operator metadata successfully set/updated!')
}
}