Skip to content
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

Stop Comute Cloud from Yandex Functions #4

Closed
ijakparov opened this issue Feb 7, 2020 · 1 comment
Closed

Stop Comute Cloud from Yandex Functions #4

ijakparov opened this issue Feb 7, 2020 · 1 comment

Comments

@ijakparov
Copy link

Hello, how to stop certian Compute Cloud from Yandex functions using this sdk?

@monsterzz
Copy link
Contributor

You can use InstanceService::stop and OperationService::get (to track operation completion).

Example code:

const yc = require('yandex-cloud');
const {InstanceService} = require('yandex-cloud/api/compute/v1');
const {OperationService} = require('yandex-cloud/api/operation');

const session = new yc.Session();
const instanceService = new InstanceService(session);
const operationService = new OperationService(session);

module.exports.handler = async function (event, context) {
    const instanceId = 'xxxxx';

    let op = await instanceService.stop({ instanceId });
    console.log(`op ${op.id} started`)
    
    while (!op.done) {
        op = await operationService.get({ operationId: op.id });
    }
    console.log(`op ${op.id} finished, spent ${op.timeSpent()}ms`);

    if (op.error) {
        throw new Exception(`op ${op.id} failed: ${JSON.stringify(op)}`);
    }

    return op;
}

Don't forget to assign service account for the functions and grant editor role to this service account in required folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants