Skip to content

Commit b32e26a

Browse files
New Update Broker cmd
1 parent 6137ff0 commit b32e26a

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Flags } from '@oclif/core'
2+
3+
import { ScCommand } from '../../../sc-command.js'
4+
import { EventBrokerCreateApiResponse, EventBrokerCreateDetail, EventBrokerListApiResponse } from '../../../types/broker.js'
5+
import { camelCaseToTitleCase, renderKeyValueTable } from '../../../util/internal.js'
6+
import { ScConnection } from '../../../util/sc-connection.js'
7+
8+
export default class MissionctrlBrokerUpdate extends ScCommand<typeof MissionctrlBrokerUpdate> {
9+
static override args = {}
10+
static override description = `Update the configuration of an existing event broker service.
11+
You can provide any combination of supported flags. If a flag is not provided, then it will not be updated.
12+
13+
Your token must have one of the permissions listed in the Token Permissions.
14+
15+
Token Permissions: [ mission_control:access or services:put ]`
16+
static override examples = [
17+
'<%= config.bin %> <%= command.id %>',
18+
]
19+
static override flags = {
20+
'broker-id': Flags.string({
21+
char: 'b',
22+
description: 'Id of the event broker service.',
23+
exactlyOne: ['broker-id', 'name'],
24+
}),
25+
locked: Flags.string({
26+
char: 'l',
27+
description: `Indicates whether the event broker service has deletion protection enabled. The valid values are 'true' (enabled) or 'false' (disabled).`
28+
}),
29+
name: Flags.string({
30+
char: 'n',
31+
description: 'Name of the event broker service.',
32+
exactlyOne: ['broker-id', 'name'],
33+
}),
34+
'new-name': Flags.string({
35+
description: 'New name of the event broker service. The new service name must be unique within an organization.'
36+
}),
37+
}
38+
39+
public async run(): Promise<EventBrokerCreateDetail> {
40+
const { flags } = await this.parse(MissionctrlBrokerUpdate)
41+
42+
const brokerId = flags['broker-id'] ?? ''
43+
const locked = flags.locked ?? ''
44+
const lockedBoolValue: boolean = (locked === 'true')
45+
const name = flags.name ?? ''
46+
const newName = flags['new-name'] ?? ''
47+
48+
// API body
49+
const body = {
50+
...(locked && { locked: lockedBoolValue }),
51+
...(newName && { name: newName }),
52+
}
53+
54+
const conn = new ScConnection()
55+
56+
// API url
57+
let apiUrl = `/missionControl/eventBrokerServices`
58+
let brokerIdToUpdate: string | undefined = brokerId
59+
60+
if (name) {
61+
// If broker name provided, get the broker matching provided name. If more than one broker matches, an error will be thrown.
62+
const resp = await conn.get<EventBrokerListApiResponse>(`${apiUrl}?customAttributes=name=="${name}"`)
63+
if (resp.data.length === 0) {
64+
this.error(`No brokers found with name: ${name}.`)
65+
} else if (resp.data.length > 1) {
66+
this.error(`Multiple brokers found with name: ${name}. Exactly one event broker service must match the provided name.`)
67+
} else {
68+
brokerIdToUpdate = resp.data[0]?.id
69+
}
70+
}
71+
72+
// API call to update broker by id
73+
apiUrl += `/${brokerIdToUpdate}`
74+
const resp = await conn.patch<EventBrokerCreateApiResponse>(apiUrl, body)
75+
this.log(`Broker with id '${brokerIdToUpdate}' has been updated successfully.`)
76+
this.print(resp.data)
77+
78+
return resp.data
79+
}
80+
81+
private print(broker: EventBrokerCreateDetail): void {
82+
const tableRows = [
83+
['Key', 'Value'],
84+
...Object.entries(broker).map(([key, value]) => [camelCaseToTitleCase(key), value]),
85+
]
86+
this.log()
87+
this.log(renderKeyValueTable(tableRows))
88+
}
89+
}

src/commands/platform/env/update.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class PlatformEnvUpdate extends ScCommand<typeof PlatformEnvUpdat
7373
}
7474
}
7575

76-
// API call to delete environment by id
76+
// API call to update environment by id
7777
apiUrl += `/${envIdToUdpate}`
7878
const resp = await conn.put<EnvironmentDetail>(apiUrl, body)
7979
this.log(`Environment with id '${envIdToUdpate}' has been updated successfully.`)

src/util/sc-connection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export class ScConnection {
4141
return response.data
4242
}
4343

44+
// PATCH request
45+
async patch<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T> {
46+
const response: AxiosResponse<T> = await this.axiosInstance.patch(url, data, config)
47+
return response.data
48+
}
49+
4450
// POST request
4551
async post<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T> {
4652
const response: AxiosResponse<T> = await this.axiosInstance.post(url, data, config)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { runCommand } from '@oclif/test'
2+
import { expect } from 'chai'
3+
import * as sinon from 'sinon'
4+
5+
import { EventBrokerCreateApiResponse } from '../../../../src/types/broker.js'
6+
import { camelCaseToTitleCase, renderKeyValueTable } from '../../../../src/util/internal.js'
7+
import { ScConnection } from '../../../../src/util/sc-connection.js'
8+
import { aBroker, setEnvVariables } from '../../../util/test-utils.js'
9+
10+
describe('missionctrl:broker:update', () => {
11+
setEnvVariables()
12+
const brokerName: string = 'Default'
13+
const brokerId: string = 'MyTestBrokerId'
14+
let scConnStub: sinon.SinonStub
15+
let scPatchConnStub: sinon.SinonStub
16+
17+
beforeEach(() => {
18+
scConnStub = sinon.stub(ScConnection.prototype, 'get')
19+
scPatchConnStub = sinon.stub(ScConnection.prototype, 'patch')
20+
})
21+
22+
afterEach(() => {
23+
scConnStub.restore()
24+
scPatchConnStub.restore()
25+
})
26+
27+
it('runs missionctrl:broker:update cmd', async () => {
28+
const { stdout } = await runCommand('missionctrl:broker:update')
29+
expect(stdout).to.contain('')
30+
})
31+
32+
it(`runs missionctrl:broker:update -b ${brokerId} -l true`, async () => {
33+
// Arrange
34+
const broker = {
35+
data: [aBroker(brokerId, brokerName)],
36+
meta: {
37+
additionalProp: {}
38+
}
39+
}
40+
const updatedBroker: EventBrokerCreateApiResponse = {
41+
data: {
42+
completedTime: broker.data[0].completedTime,
43+
createdBy: broker.data[0].createdBy,
44+
createdTime: broker.data[0].createdTime,
45+
id: broker.data[0].id,
46+
operationType: broker.data[0].operationType,
47+
resourceId: broker.data[0].resourceId,
48+
resourceType: broker.data[0].resourceType,
49+
status: broker.data[0].status,
50+
type: broker.data[0].type
51+
},
52+
meta: {
53+
additionalProp: {}
54+
}
55+
}
56+
scConnStub.returns(Promise.resolve(broker))
57+
scPatchConnStub.returns(Promise.resolve(updatedBroker))
58+
59+
const tableRows = [
60+
['Key', 'Value'],
61+
...Object.entries(updatedBroker.data).map(([key, value]) => [camelCaseToTitleCase(key), value]),
62+
]
63+
64+
// Act
65+
const { stdout } = await runCommand(`missionctrl:broker:update -b ${brokerId} -l true`)
66+
67+
// Assert
68+
expect(stdout).to.contain(renderKeyValueTable(tableRows))
69+
})
70+
})

test/util/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function anEnv(name: string, isDefault: boolean, isProd: boolean) {
2121
}
2222
}
2323

24-
export function aBroker(brokerId: string, brokerName: string,) {
24+
export function aBroker(brokerId: string, brokerName: string) {
2525
return {
2626
completedTime: '',
2727
createdBy: 'test',

0 commit comments

Comments
 (0)