/** * @return {Promise} Array of names of running instances */ const _listStoppedInstances = async (projectId, zone) => { const res = await compute.instances.list({ project: projectId, zone: zone, }); const instances = res.data.items || []; const stoppedInstances = instances.filter(item => item.status !== 'RUNNING'); return stoppedInstances.map(item => item.name); }
/** * @param {Array} instanceNames Names of instance to stop * @return {Promise} Response from stopping instances */ const _startInstances = async (projectId, zone, instanceNames) => { if (!instanceNames.length) { return 'No stopped instances were found.'; } await Promise.all( instanceNames.map(instanceName => { return compute.instances.start({ project: projectId, zone: zone, instance: instanceName, }); }) ); }
/** * @param {Array} instanceNames Names of instance to stop * @return {Promise} Response from stopping instances */ const _stopInstances = async (projectId, zone, instanceNames) => { await Promise.all( instanceNames.map(instanceName => { return compute.instances .stop({ project: projectId, zone: zone, instance: instanceName, }) .then(res => { console.log('Instance stopped successfully: ' + instanceName); return res.data; }); }) ); }
/** * @return {Promise} Array of names of running instances */ const _listRunningInstances = async (projectId, zone) => { const res = await compute.instances.list({ project: projectId, zone: zone, }); const instances = res.data.items || []; const ranInstances = instances.filter(item => item.status === 'RUNNING'); return ranInstances.map(item => item.name); }