Skip to content

Commit 8df9d00

Browse files
Updated Broker type interfaces; improving unit test; code formatting.
1 parent 9ee0d2c commit 8df9d00

File tree

14 files changed

+584
-292
lines changed

14 files changed

+584
-292
lines changed

src/commands/missionctrl/broker/create.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Flags} from '@oclif/core'
22

33
import {ScCommand} from '../../../sc-command.js'
4-
import {EventBrokerCreateApiResponse, EventBrokerCreateDetail} from '../../../types/broker.js'
4+
import {EventBrokerOperationApiResponse, EventBrokerOperationDetail} from '../../../types/broker.js'
55
import {camelCaseToTitleCase, renderKeyValueTable} from '../../../util/internal.js'
66
import {ScConnection} from '../../../util/sc-connection.js'
77

@@ -65,7 +65,7 @@ Token Permissions: [ \`services:post\` ]`
6565
}),
6666
}
6767

68-
public async run(): Promise<EventBrokerCreateDetail> {
68+
public async run(): Promise<EventBrokerOperationDetail> {
6969
const {flags} = await this.parse(MissionctrlBrokerCreate)
7070

7171
const datacenterId = flags['datacenter-id'] ?? ''
@@ -109,14 +109,14 @@ Token Permissions: [ \`services:post\` ]`
109109
}
110110

111111
// API call
112-
const resp = await conn.post<EventBrokerCreateApiResponse>(apiUrl, body)
112+
const resp = await conn.post<EventBrokerOperationApiResponse>(apiUrl, body)
113113
// Display results
114114
this.log('Event broker service created successfully.')
115115
this.print(resp.data)
116116
return resp.data
117117
}
118118

119-
private print(broker: EventBrokerCreateDetail): void {
119+
private print(broker: EventBrokerOperationDetail): void {
120120
const tableRows = [
121121
['Key', 'Value'],
122122
...Object.entries(broker).map(([key, value]) => [camelCaseToTitleCase(key), value]),

src/commands/missionctrl/broker/delete.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { Flags } from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import { ScCommand } from '../../../sc-command.js'
4-
import { EventBrokerListApiResponse } from '../../../types/broker.js'
5-
import { ScConnection } from '../../../util/sc-connection.js'
3+
import {ScCommand} from '../../../sc-command.js'
4+
import {
5+
EventBrokerListApiResponse,
6+
EventBrokerOperationApiResponse,
7+
EventBrokerOperationDetail,
8+
} from '../../../types/broker.js'
9+
import {camelCaseToTitleCase, renderKeyValueTable} from '../../../util/internal.js'
10+
import {ScConnection} from '../../../util/sc-connection.js'
611

712
export default class MissionctrlBrokerDelete extends ScCommand<typeof MissionctrlBrokerDelete> {
813
static override args = {}
@@ -28,8 +33,8 @@ Token Permissions: [ \`services:delete\` **or** \`services:delete:self\` **or**
2833
}),
2934
}
3035

31-
public async run(): Promise<{ message: string }> {
32-
const { flags } = await this.parse(MissionctrlBrokerDelete)
36+
public async run(): Promise<EventBrokerOperationDetail> {
37+
const {flags} = await this.parse(MissionctrlBrokerDelete)
3338

3439
const name = flags.name ?? ''
3540
const brokerId = flags['broker-id'] ?? ''
@@ -57,9 +62,20 @@ Token Permissions: [ \`services:delete\` **or** \`services:delete:self\` **or**
5762

5863
// API call to delete environment by id
5964
apiUrl += `/${brokerIdToDelete}`
60-
await conn.delete<string>(apiUrl)
61-
const message = `Event broker service with id '${brokerIdToDelete}' has been deleted successfully.`
62-
this.log(message)
63-
return { message }
65+
const resp = await conn.delete<EventBrokerOperationApiResponse>(apiUrl)
66+
67+
// Display results
68+
this.print(resp.data)
69+
70+
return resp.data
71+
}
72+
73+
private print(broker: EventBrokerOperationDetail): void {
74+
const tableRows = [
75+
['Key', 'Value'],
76+
...Object.entries(broker).map(([key, value]) => [camelCaseToTitleCase(key), value]),
77+
]
78+
this.log()
79+
this.log(renderKeyValueTable(tableRows))
6480
}
6581
}

src/commands/missionctrl/broker/display.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Flags } from '@oclif/core'
22

33
import { ScCommand } from '../../../sc-command.js'
4-
import { EventBrokerListApiResponse, EventBrokerServiceDetail } from '../../../types/broker.js'
4+
import { EventBrokerApiResponse, EventBrokerListApiResponse, EventBrokerServiceDetail } from '../../../types/broker.js'
55
import { camelCaseToTitleCase, renderKeyValueTable } from '../../../util/internal.js'
66
import { ScConnection } from '../../../util/sc-connection.js'
77

@@ -43,9 +43,9 @@ export default class MissionctrlBrokerDisplay extends ScCommand<typeof Missionct
4343
if (brokerId) {
4444
// API call to get broker by id
4545
apiUrl += `/${brokerId}`
46-
const resp = await conn.get<EventBrokerServiceDetail>(apiUrl)
47-
this.print(resp)
48-
rawResp = [resp]
46+
const resp = await conn.get<EventBrokerApiResponse>(apiUrl)
47+
this.print(resp.data)
48+
rawResp = [resp.data]
4949
} else if (name) {
5050
// API call to get broker by name
5151
apiUrl += `?customAttributes=name=="${name}"`

src/commands/missionctrl/broker/list.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Flags } from '@oclif/core'
1+
import {Flags} from '@oclif/core'
22

3-
import { ScCommand } from '../../../sc-command.js'
4-
import { EventBrokerListApiResponse, EventBrokerServiceDetail } from '../../../types/broker.js'
5-
import { renderTable } from '../../../util/internal.js'
6-
import { ScConnection } from '../../../util/sc-connection.js'
3+
import {ScCommand} from '../../../sc-command.js'
4+
import {EventBrokerListApiResponse, EventBrokerServiceDetail} from '../../../types/broker.js'
5+
import {renderTable} from '../../../util/internal.js'
6+
import {ScConnection} from '../../../util/sc-connection.js'
77

88
export default class MissionctrlBrokerList extends ScCommand<typeof MissionctrlBrokerList> {
99
static override args = {}
@@ -12,17 +12,19 @@ export default class MissionctrlBrokerList extends ScCommand<typeof MissionctrlB
1212
Your token must have one of the permissions listed in the Token Permissions.
1313
1414
Token Permissions: [ \`mission_control:access\` **or** \`services:get\` **or** \`services:get:self\` **or** \`services:view\` **or** \`services:view:self\` ]`
15-
static override examples = ['<%= config.bin %> <%= command.id %> --name=MyBrokerName --pageNumber=1 --pageSize=10 --sort=name:asc']
15+
static override examples = [
16+
'<%= config.bin %> <%= command.id %> --name=MyBrokerName --pageNumber=1 --pageSize=10 --sort=name:asc',
17+
]
1618
static override flags = {
1719
name: Flags.string({
1820
char: 'n',
19-
description: 'Name of the event broker service to match on.'
21+
description: 'Name of the event broker service to match on.',
2022
}),
2123
pageNumber: Flags.integer({
22-
description: 'The page number to get. Defaults to 1'
24+
description: 'The page number to get. Defaults to 1',
2325
}),
2426
pageSize: Flags.integer({
25-
description: 'The number of event broker services to return per page. Defaults to 100',
27+
description: 'The number of event broker services to return per page. Defaults to 10',
2628
max: 100,
2729
min: 1,
2830
}),
@@ -37,7 +39,7 @@ Token Permissions: [ \`mission_control:access\` **or** \`services:get\` **or** \
3739
}
3840

3941
public async run(): Promise<EventBrokerServiceDetail[]> {
40-
const { flags } = await this.parse(MissionctrlBrokerList)
42+
const {flags} = await this.parse(MissionctrlBrokerList)
4143

4244
const conn = new ScConnection()
4345

src/commands/missionctrl/broker/opstatus.ts

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { Flags } from '@oclif/core'
2-
import { MultiBar, Presets, SingleBar } from 'cli-progress'
3-
4-
import { ScCommand } from '../../../sc-command.js'
5-
import { AllOperationResponse, EventBrokerListApiResponse, OperationData, OperationResponse } from '../../../types/broker.js'
6-
import { renderTable, sleep } from '../../../util/internal.js'
7-
import { ScConnection } from '../../../util/sc-connection.js'
1+
import {Flags} from '@oclif/core'
2+
import {MultiBar, Presets, SingleBar} from 'cli-progress'
3+
4+
import {ScCommand} from '../../../sc-command.js'
5+
import {
6+
EventBrokerAllOperationsApiResponse,
7+
EventBrokerListApiResponse,
8+
EventBrokerOperationApiResponse,
9+
EventBrokerOperationDetail,
10+
} from '../../../types/broker.js'
11+
import {renderTable, sleep} from '../../../util/internal.js'
12+
import {ScConnection} from '../../../util/sc-connection.js'
813

914
export default class MissionctrlBrokerOpstatus extends ScCommand<typeof MissionctrlBrokerOpstatus> {
1015
static override args = {}
@@ -29,16 +34,18 @@ export default class MissionctrlBrokerOpstatus extends ScCommand<typeof Missionc
2934
}),
3035
'show-progress': Flags.boolean({
3136
char: 'p',
32-
description: 'Displays a status bar of the in-progress operations. The command will wait for completion of each step of the operation.'
37+
description:
38+
'Displays a status bar of the in-progress operations. The command will wait for completion of each step of the operation.',
3339
}),
3440
'wait-ms': Flags.integer({
3541
char: 'w',
36-
description: 'The milliseconds to wait between API calls for checking progress of the operation. Default is 5000 ms.'
42+
description:
43+
'The milliseconds to wait between API calls for checking progress of the operation. Default is 5000 ms.',
3744
}),
3845
}
3946

40-
public async run(): Promise<OperationData[]> {
41-
const { flags } = await this.parse(MissionctrlBrokerOpstatus)
47+
public async run(): Promise<EventBrokerOperationDetail[]> {
48+
const {flags} = await this.parse(MissionctrlBrokerOpstatus)
4249

4350
const name = flags.name ?? ''
4451
let brokerId = flags['broker-id'] ?? ''
@@ -56,23 +63,25 @@ export default class MissionctrlBrokerOpstatus extends ScCommand<typeof Missionc
5663
// API call to get broker by name
5764
apiUrl += `?customAttributes=name=="${name}"`
5865
const resp = await conn.get<EventBrokerListApiResponse>(apiUrl)
59-
// FUTURE: show status of multiple brokers operations that match the name
66+
// FUTURE: show status of multiple brokers operations that match the name
6067
if (resp.data.length === 0) {
6168
this.error(`No brokers found with name: ${name}.`)
6269
} else if (resp.data.length > 1) {
63-
this.error(`Multiple broker services found with: ${name}. Exactly one broker service must match the provided name.`)
70+
this.error(
71+
`Multiple broker services found with: ${name}. Exactly one broker service must match the provided name.`,
72+
)
6473
} else {
6574
brokerId = resp.data[0]?.id
6675
}
6776
}
6877

6978
// API call to retrieve status of the broker operation
7079
apiUrl = `/missionControl/eventBrokerServices/${brokerId}/operations`
71-
72-
const resp = await conn.get<AllOperationResponse>(apiUrl)
80+
81+
const resp = await conn.get<EventBrokerAllOperationsApiResponse>(apiUrl)
7382
const opStatusArray = [
7483
['Operation Id', 'Operation Type', 'Status', 'Created Time', 'Completed Time'],
75-
...resp.data.map((item: OperationData) => [
84+
...resp.data.map((item: EventBrokerOperationDetail) => [
7685
item.id,
7786
item.operationType,
7887
item.status,
@@ -85,42 +94,51 @@ export default class MissionctrlBrokerOpstatus extends ScCommand<typeof Missionc
8594
this.log(renderTable(opStatusArray))
8695

8796
// If show-progress flag is set, display progress bars for each operation
88-
if(showProgress && resp.data.length > 0) {
97+
if (showProgress && resp.data.length > 0) {
8998
// Create progress bar for each operation
90-
const multiProgressBar = new MultiBar({
91-
clearOnComplete: false,
92-
format: ' {bar} | {operationType} | {value}/{total}',
93-
hideCursor: true,
94-
}, Presets.shades_classic)
95-
99+
const multiProgressBar = new MultiBar(
100+
{
101+
clearOnComplete: false,
102+
format: ' {bar} | {operationType} | {value}/{total}',
103+
hideCursor: true,
104+
},
105+
Presets.shades_classic,
106+
)
107+
96108
// Get the initial progress for each operation
97109
const progressBars: [string, SingleBar][] = []
98110
let completedOperations = 0
99111
let allCompleted = false
100112
for (const operationData of resp.data) {
101113
const opStatusApiUrl = `/missionControl/eventBrokerServices/${brokerId}/operations/${operationData.id}?expand=progressLogs`
102114
// eslint-disable-next-line no-await-in-loop
103-
const opStatusResp = await conn.get<OperationResponse>(opStatusApiUrl)
104-
this.debug(`Operation ID: ${operationData.id}, Type: ${operationData.operationType}, Status: ${operationData.status}`)
115+
const opStatusResp = await conn.get<EventBrokerOperationApiResponse>(opStatusApiUrl)
116+
this.debug(
117+
`Operation ID: ${operationData.id}, Type: ${operationData.operationType}, Status: ${operationData.status}`,
118+
)
105119
if (opStatusResp.data.progressLogs) {
106120
const numSteps = opStatusResp.data.progressLogs.length
107121
// start a new progress bar for the operation with a total value of size of the steps
108-
const progressBar = multiProgressBar.create(numSteps, 0, { operationType: operationData.operationType })
122+
const progressBar = multiProgressBar.create(numSteps, 0, {operationType: operationData.operationType})
109123
// Update the progress with the steps completed
110-
const completedNumSteps = opStatusResp.data.progressLogs.filter(log => log.status === 'success').length
124+
const completedNumSteps = opStatusResp.data.progressLogs.filter((log) => log.status === 'success').length
111125
progressBar.update(completedNumSteps)
112-
if (completedNumSteps === numSteps || opStatusResp.data.status === 'SUCCEEDED' || opStatusResp.data.status === 'FAILED') {
126+
if (
127+
completedNumSteps === numSteps ||
128+
opStatusResp.data.status === 'SUCCEEDED' ||
129+
opStatusResp.data.status === 'FAILED'
130+
) {
113131
completedOperations += 1
114132
progressBar.stop()
115133
}
116-
134+
117135
// Add the operation ID and progress bar to the list
118136
progressBars.push([operationData.id, progressBar])
119137
}
120138
}
121139

122140
// Check if all operations are completed
123-
if(completedOperations === progressBars.length) {
141+
if (completedOperations === progressBars.length) {
124142
allCompleted = true
125143
}
126144

@@ -132,29 +150,37 @@ export default class MissionctrlBrokerOpstatus extends ScCommand<typeof Missionc
132150
// eslint-disable-next-line no-await-in-loop
133151
allCompleted = await this.pollAllOperationStatus(conn, brokerId, progressBars)
134152
}
135-
153+
136154
multiProgressBar.stop()
137155
this.log() // Add a new line for better readability
138156
}
139157

140158
return resp.data
141159
}
142160

143-
private async pollAllOperationStatus(conn: ScConnection, brokerId: string, progressBars: [string, SingleBar][]): Promise<boolean> {
161+
private async pollAllOperationStatus(
162+
conn: ScConnection,
163+
brokerId: string,
164+
progressBars: [string, SingleBar][],
165+
): Promise<boolean> {
144166
let completedOperations = 0
145167
let allCompleted = false
146168
// For each operation, get the latest status and update the progress bar
147169
for (const [operationId, progressBar] of progressBars) {
148170
const opStatusApiUrl = `/missionControl/eventBrokerServices/${brokerId}/operations/${operationId}?expand=progressLogs`
149171
// eslint-disable-next-line no-await-in-loop
150-
const opStatusResp = await conn.get<OperationResponse>(opStatusApiUrl)
172+
const opStatusResp = await conn.get<EventBrokerOperationApiResponse>(opStatusApiUrl)
151173
if (opStatusResp.data.progressLogs) {
152174
const numSteps = opStatusResp.data.progressLogs.length
153-
const completedNumSteps = opStatusResp.data.progressLogs.filter(log => log.status === 'success').length
175+
const completedNumSteps = opStatusResp.data.progressLogs.filter((log) => log.status === 'success').length
154176
// Update progress bar
155177
progressBar.setTotal(numSteps)
156178
progressBar.update(completedNumSteps)
157-
if (completedNumSteps === numSteps || opStatusResp.data.status === 'SUCCEEDED' || opStatusResp.data.status === 'FAILED') {
179+
if (
180+
completedNumSteps === numSteps ||
181+
opStatusResp.data.status === 'SUCCEEDED' ||
182+
opStatusResp.data.status === 'FAILED'
183+
) {
158184
completedOperations += 1
159185
progressBar.stop()
160186
}
@@ -164,10 +190,10 @@ export default class MissionctrlBrokerOpstatus extends ScCommand<typeof Missionc
164190
}
165191

166192
// Check if all operations are completed
167-
if(completedOperations === progressBars.length) {
193+
if (completedOperations === progressBars.length) {
168194
allCompleted = true
169195
}
170196

171197
return allCompleted
172198
}
173-
}
199+
}

0 commit comments

Comments
 (0)