-
Notifications
You must be signed in to change notification settings - Fork 370
Extends 'spe container get' command. Closes #6719 #7052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,18 +1,23 @@ | ||||||
| import GlobalOptions from '../../../../GlobalOptions.js'; | ||||||
| import { z } from 'zod'; | ||||||
| import { Logger } from '../../../../cli/Logger.js'; | ||||||
| import { CommandError, globalOptionsZod } from '../../../../Command.js'; | ||||||
| import GraphCommand from '../../../base/GraphCommand.js'; | ||||||
| import commands from '../../commands.js'; | ||||||
| import request, { CliRequestOptions } from '../../../../request.js'; | ||||||
| import { SpeContainer } from '../../../../utils/spe.js'; | ||||||
| import { zod } from '../../../../utils/zod.js'; | ||||||
|
|
||||||
| const options = globalOptionsZod.extend({ | ||||||
| id: zod.alias('i', z.string().optional()), | ||||||
| name: zod.alias('n', z.string().optional()) | ||||||
| }).strict(); | ||||||
|
|
||||||
| type Options = z.infer<typeof options>; | ||||||
|
|
||||||
| interface CommandArgs { | ||||||
| options: Options; | ||||||
| } | ||||||
|
|
||||||
| interface Options extends GlobalOptions { | ||||||
| id: string; | ||||||
| } | ||||||
|
|
||||||
| class SpeContainerGetCommand extends GraphCommand { | ||||||
| public get name(): string { | ||||||
| return commands.CONTAINER_GET; | ||||||
|
|
@@ -22,44 +27,79 @@ class SpeContainerGetCommand extends GraphCommand { | |||||
| return 'Gets a container of a specific container type'; | ||||||
| } | ||||||
|
|
||||||
| constructor() { | ||||||
| super(); | ||||||
|
|
||||||
| this.#initOptions(); | ||||||
| this.#initTypes(); | ||||||
| public get schema(): z.ZodTypeAny { | ||||||
| return options; | ||||||
| } | ||||||
|
|
||||||
| #initOptions(): void { | ||||||
| this.options.unshift( | ||||||
| { option: '-i, --id <id>' } | ||||||
| ); | ||||||
| public getRefinedSchema(schema: z.ZodTypeAny): z.ZodEffects<any> | undefined { | ||||||
| return schema.refine((opts: Options) => [opts.id, opts.name].filter(value => value !== undefined).length === 1, { | ||||||
| message: 'Specify either id or name, but not both.' | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| #initTypes(): void { | ||||||
| this.types.string.push('id'); | ||||||
| public async commandAction(logger: Logger, args: CommandArgs): Promise<void> { | ||||||
| try { | ||||||
| const containerId = await this.resolveContainerId(args.options, logger); | ||||||
|
|
||||||
| if (this.verbose) { | ||||||
| await logger.logToStderr(`Getting a container with id '${containerId}'...`); | ||||||
| } | ||||||
|
|
||||||
| const requestOptions: CliRequestOptions = { | ||||||
| url: `${this.resource}/v1.0/storage/fileStorage/containers/${containerId}`, | ||||||
| headers: { | ||||||
| accept: 'application/json;odata.metadata=none' | ||||||
| }, | ||||||
| responseType: 'json' | ||||||
| }; | ||||||
|
|
||||||
| const res = await request.get<SpeContainer>(requestOptions); | ||||||
| await logger.log(res); | ||||||
| } | ||||||
| catch (err: any) { | ||||||
| if (err instanceof CommandError) { | ||||||
| throw err; | ||||||
| } | ||||||
|
|
||||||
| this.handleRejectedODataJsonPromise(err); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public async commandAction(logger: Logger, args: CommandArgs): Promise<void> { | ||||||
| private async resolveContainerId(options: Options, logger: Logger): Promise<string> { | ||||||
| if (options.id) { | ||||||
| return options.id; | ||||||
| } | ||||||
|
|
||||||
| if (this.verbose) { | ||||||
| await logger.logToStderr(`Getting a container with id '${args.options.id}'...`); | ||||||
| await logger.logToStderr(`Resolving container id from name '${options.name}'...`); | ||||||
| } | ||||||
|
|
||||||
| const requestOptions: CliRequestOptions = { | ||||||
| url: `${this.resource}/v1.0/storage/fileStorage/containers/${args.options.id}`, | ||||||
| url: `${this.resource}/v1.0/storage/fileStorage/containers`, | ||||||
| headers: { | ||||||
| accept: 'application/json;odata.metadata=none' | ||||||
| }, | ||||||
| responseType: 'json' | ||||||
| }; | ||||||
|
|
||||||
| try { | ||||||
| const res = await request.get<SpeContainer>(requestOptions); | ||||||
| await logger.log(res); | ||||||
| const response = await request.get<{ value?: SpeContainer[] }>(requestOptions); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try to use If filtering by the
Suggested change
According to the doc the In fact, if a user wants to retrieve a container by it's name, we will make two calls. One call to get all containers and find the one with the specific name. The second call to get a container by it's id even if we have already retrieved the container and its details in the first call. |
||||||
| const container = response.value?.find(item => item.displayName === options.name); | ||||||
|
|
||||||
| if (!container) { | ||||||
| throw new CommandError(`Container with name '${options.name}' not found.`); | ||||||
| } | ||||||
|
|
||||||
| return container.id; | ||||||
| } | ||||||
| catch (err: any) { | ||||||
| this.handleRejectedODataJsonPromise(err); | ||||||
| catch (error: any) { | ||||||
| if (error instanceof CommandError) { | ||||||
| throw error; | ||||||
| } | ||||||
|
|
||||||
| throw error; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export default new SpeContainerGetCommand(); | ||||||
| export default new SpeContainerGetCommand(); | ||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parse the command option from zod schema to ensure the inputs are correct.
Please do it for all unit tests.