diff --git a/docs/docs/cmd/spe/container/container-get.mdx b/docs/docs/cmd/spe/container/container-get.mdx new file mode 100644 index 0000000000..f53b59e17c --- /dev/null +++ b/docs/docs/cmd/spe/container/container-get.mdx @@ -0,0 +1,92 @@ +import Global from '/docs/cmd/_global.mdx'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +# spe container get + +Gets a container of a specific container type + +## Usage + +```sh +m365 spe container get [options] +``` + +## Options + +```md definition-list +`-i, --id ` +: The Id of the container instance. +``` + + + +## Examples + +Gets a container of a specific type. + +```sh +m365 spe container get --id "b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z" +``` + +## Response + + + + + ```json + { + "id": "b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z", + "displayName": "My Application Storage Container", + "description": "Description of My Application Storage Container", + "containerTypeId": "91710488-5756-407f-9046-fbe5f0b4de73", + "status": "active", + "createdDateTime": "2021-11-24T15:41:52.347Z", + "settings": { + "isOcrEnabled": false + } + } + ``` + + + + + ```text + id : b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z + displayName : My Application Storage Container + description : Description of My Application Storage Container + containerTypeId : 91710488-5756-407f-9046-fbe5f0b4de73 + status : active + createdDateTime : 2021-11-24T15:41:52.347Z + ``` + + + + + ```csv + id,displayName,description,containerTypeId,status,createdDateTime + b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z,My Application Storage Container,Description of My Application Storage Container,91710488-5756-407f-9046-fbe5f0b4de73,active,2021-11-24T15:41:52.347Z + ``` + + + + + ```md + # spe container get --id "b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z" + + Date: 14/03/2024 + + ## My Application Storage Container (b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z) + + Property | Value + ---------|------- + id | b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z + displayName | My Application Storage Container + description | Description of My Application Storage Container + containerTypeId | 91710488-5756-407f-9046-fbe5f0b4de73 + status | active + createdDateTime | 2021-11-24T15:41:52.347Z + ``` + + + \ No newline at end of file diff --git a/docs/src/config/sidebars.ts b/docs/src/config/sidebars.ts index 06ed8558b5..1ff78e76fa 100644 --- a/docs/src/config/sidebars.ts +++ b/docs/src/config/sidebars.ts @@ -1962,6 +1962,11 @@ const sidebars: SidebarsConfig = { 'SharePoint Embedded (spe)': [ { container: [ + { + type: 'doc', + label: 'container get', + id: 'cmd/spe/container/container-get' + }, { type: 'doc', label: 'container list', diff --git a/src/m365/spe/commands.ts b/src/m365/spe/commands.ts index c5e579ce64..f870b41b2b 100644 --- a/src/m365/spe/commands.ts +++ b/src/m365/spe/commands.ts @@ -1,6 +1,7 @@ const prefix: string = 'spe'; export default { + CONTAINER_GET: `${prefix} container get`, CONTAINER_LIST: `${prefix} container list`, CONTAINERTYPE_ADD: `${prefix} containertype add`, CONTAINERTYPE_GET: `${prefix} containertype get`, diff --git a/src/m365/spe/commands/container/container-get.spec.ts b/src/m365/spe/commands/container/container-get.spec.ts new file mode 100644 index 0000000000..ff34e2a3ec --- /dev/null +++ b/src/m365/spe/commands/container/container-get.spec.ts @@ -0,0 +1,99 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import auth from '../../../../Auth.js'; +import { Logger } from '../../../../cli/Logger.js'; +import { CommandError } from '../../../../Command.js'; +import request from '../../../../request.js'; +import { telemetry } from '../../../../telemetry.js'; +import { pid } from '../../../../utils/pid.js'; +import { session } from '../../../../utils/session.js'; +import { sinonUtil } from '../../../../utils/sinonUtil.js'; +import commands from '../../commands.js'; +import command from './container-get.js'; + +describe(commands.CONTAINER_GET, () => { + let log: string[]; + let logger: Logger; + let loggerLogSpy: sinon.SinonSpy; + + before(() => { + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').returns(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); + auth.connection.active = true; + }); + + beforeEach(() => { + log = []; + logger = { + log: async (msg: string) => { + log.push(msg); + }, + logRaw: async (msg: string) => { + log.push(msg); + }, + logToStderr: async (msg: string) => { + log.push(msg); + } + }; + loggerLogSpy = sinon.spy(logger, 'log'); + }); + + afterEach(() => { + sinonUtil.restore([ + request.get + ]); + }); + + after(() => { + sinon.restore(); + auth.connection.active = false; + }); + + it('has correct name', () => { + assert.strictEqual(command.name, commands.CONTAINER_GET); + }); + + it('has a description', () => { + assert.notStrictEqual(command.description, null); + }); + + it('correctly handles error', async () => { + const errorMessage = 'Bad request.'; + sinon.stub(request, 'get').rejects({ + error: { + message: errorMessage + } + }); + + await assert.rejects(command.action(logger, { options: { id: 'invalid', verbose: true } } as any), + new CommandError(errorMessage)); + }); + + it('gets container by id', async () => { + const containerId = 'eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiIxNTU1MjcwOTQyNzIifQ'; + const response = { + id: containerId, + displayName: "My Application Storage Container", + description: "Description of My Application Storage Container", + containerTypeId: "91710488-5756-407f-9046-fbe5f0b4de73", + status: "active", + createdDateTime: "2021-11-24T15:41:52.347Z", + settings: { + isOcrEnabled: false + } + }; + + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://graph.microsoft.com/v1.0/storage/fileStorage/containers/${containerId}`) { + return response; + } + + throw 'Invalid Request'; + }); + + await command.action(logger, { options: { id: containerId } } as any); + assert.deepStrictEqual(loggerLogSpy.lastCall.args[0], response); + }); +}); \ No newline at end of file diff --git a/src/m365/spe/commands/container/container-get.ts b/src/m365/spe/commands/container/container-get.ts new file mode 100644 index 0000000000..b01d7ee21c --- /dev/null +++ b/src/m365/spe/commands/container/container-get.ts @@ -0,0 +1,65 @@ +import GlobalOptions from '../../../../GlobalOptions.js'; +import { Logger } from '../../../../cli/Logger.js'; +import GraphCommand from '../../../base/GraphCommand.js'; +import commands from '../../commands.js'; +import request, { CliRequestOptions } from '../../../../request.js'; +import { ContainerProperties } from '../../ContainerProperties.js'; + +interface CommandArgs { + options: Options; +} + +interface Options extends GlobalOptions { + id: string; +} + +class SpeContainerGetCommand extends GraphCommand { + public get name(): string { + return commands.CONTAINER_GET; + } + + public get description(): string { + return 'Gets a container of a specific container type'; + } + + constructor() { + super(); + + this.#initOptions(); + this.#initTypes(); + } + + #initOptions(): void { + this.options.unshift( + { option: '-i, --id ' } + ); + } + + #initTypes(): void { + this.types.string.push('id'); + } + + public async commandAction(logger: Logger, args: CommandArgs): Promise { + if (this.verbose) { + await logger.logToStderr(`Getting a container with id '${args.options.id}'...`); + } + + const requestOptions: CliRequestOptions = { + url: `${this.resource}/v1.0/storage/fileStorage/containers/${args.options.id}`, + headers: { + accept: 'application/json;odata.metadata=none' + }, + responseType: 'json' + }; + + try { + const res = await request.get(requestOptions); + await logger.log(res); + } + catch (err: any) { + this.handleRejectedODataJsonPromise(err); + } + } +} + +export default new SpeContainerGetCommand(); \ No newline at end of file