|
| 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 | +} |
0 commit comments