Skip to content

Commit

Permalink
spe container activate
Browse files Browse the repository at this point in the history
  • Loading branch information
nanddeepn committed Oct 18, 2024
1 parent 1a88ed2 commit 0632397
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/docs/cmd/spe/container/container-activate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Global from '/docs/cmd/_global.mdx';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# spe container activate

Activates a container

## Usage

```sh
m365 spe container activate [options]
```

## Options

```md definition-list
`-i, --id <id>`
: The Id of the container instance.
```

<Global />

## Examples

Activates a container.

```sh
m365 spe container activate --id "b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z"
```

## Response

The command won't return a response on success.
9 changes: 9 additions & 0 deletions docs/src/config/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,15 @@ const sidebars: SidebarsConfig = {
},
{
'SharePoint Embedded (spe)': [
{
container: [
{
type: 'doc',
label: 'container activate',
id: 'cmd/spe/container/container-activate'
}
]
},
{
containertype: [
{
Expand Down
1 change: 1 addition & 0 deletions src/m365/spe/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const prefix: string = 'spe';

export default {
CONTAINER_ACTIVATE: `${prefix} container activate`,
CONTAINERTYPE_ADD: `${prefix} containertype add`,
CONTAINERTYPE_LIST: `${prefix} containertype list`
};
100 changes: 100 additions & 0 deletions src/m365/spe/commands/container/container-activate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import assert from 'assert';
import sinon from 'sinon';
import auth from '../../../../Auth.js';
import { Logger } from '../../../../cli/Logger.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-activate.js';
import { CommandError } from '../../../../Command.js';
import { formatting } from '../../../../utils/formatting.js';

describe(commands.CONTAINER_ACTIVATE, () => {
let log: string[];
let logger: Logger;

const containerId = 'b!ISJs1WRro0y0EWgkUYcktDa0mE8zSlFEqFzqRn70Zwp1CEtDEBZgQICPkRbil_5Z';

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);
}
};
});

afterEach(() => {
sinonUtil.restore([
request.post
]);
});

after(() => {
sinon.restore();
auth.connection.active = false;
});

it('has correct name', () => {
assert.strictEqual(command.name, commands.CONTAINER_ACTIVATE);
});

it('has a description', () => {
assert.notStrictEqual(command.description, null);
});

it('activates container by id', async () => {
const postStub = sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/storage/fileStorage/containers/${formatting.encodeQueryParameter(containerId)}/activate`) {
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { id: containerId, verbose: true } });
assert(postStub.calledOnce);
});

it('correctly handles error when container specified by id is not found', async () => {
const error = {
error: {
code: 'itemNotFound',
message: 'Item not found',
innerError: {
date: '2024-10-18T09:58:41',
'request-id': 'ec6a7cf6-4017-4af2-a3aa-82cac95dced7',
'client-request-id': '2453c2e7-e937-52ff-1478-647fc551d4e4'
}
}
};

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/storage/fileStorage/containers/${formatting.encodeQueryParameter(containerId)}/activate`) {
throw error;
}

throw 'Invalid request';
});

await assert.rejects(command.action(logger, { options: { id: containerId, verbose: true } } as any),
new CommandError(error.error.message));
});
});
65 changes: 65 additions & 0 deletions src/m365/spe/commands/container/container-activate.ts
Original file line number Diff line number Diff line change
@@ -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 { formatting } from '../../../../utils/formatting.js';

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
id: string;
}

class SpeContainerActivateCommand extends GraphCommand {
public get name(): string {
return commands.CONTAINER_ACTIVATE;
}

public get description(): string {
return 'Activates a container';
}

constructor() {
super();

this.#initOptions();
this.#initTypes();
}

#initOptions(): void {
this.options.unshift(
{ option: '-i, --id <id>' }
);
}

#initTypes(): void {
this.types.string.push('id');
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
if (this.verbose) {
await logger.logToStderr(`Activating a container with id '${args.options.id}'...`);
}

try {
const requestOptions: CliRequestOptions = {
url: `${this.resource}/v1.0/storage/fileStorage/containers/${formatting.encodeQueryParameter(args.options.id)}/activate`,
headers: {
'content-type': 'application/json;odata=nometadata',
'accept': 'application/json;odata.metadata=none'
},
responseType: 'json'
};

await request.post(requestOptions);
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}
}

export default new SpeContainerActivateCommand();

0 comments on commit 0632397

Please sign in to comment.