MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / getStaleVMInstances

Function getStaleVMInstances

compute/test/util.js:31–61  ·  view source on GitHub ↗

* Get VM instances created more than one hour ago.

(prefix = PREFIX)

Source from the content-addressed store, hash-verified

29 * Get VM instances created more than one hour ago.
30 */
31async function getStaleVMInstances(prefix = PREFIX) {
32 const projectId = await instancesClient.getProjectId();
33 const result = [];
34 const currentDate = new Date();
35 currentDate.setHours(currentDate.getHours() - 1);
36
37 const aggListRequest = instancesClient.aggregatedListAsync({
38 project: projectId,
39 });
40
41 for await (const [zone, instancesObject] of aggListRequest) {
42 const instances = instancesObject.instances;
43 result.push(
44 ...instances
45 .filter(
46 instance =>
47 new Date(instance.creationTimestamp) < currentDate &&
48 instance.name.startsWith(prefix)
49 )
50 .map(instance => {
51 return {
52 zone: zone.split('zones/')[1],
53 instanceName: instance.name,
54 timestamp: instance.creationTimestamp,
55 };
56 })
57 );
58 }
59
60 return result;
61}
62
63async function deleteInstance(zone, instanceName) {
64 const projectId = await instancesClient.getProjectId();

Calls 1

getProjectIdMethod · 0.80

Tested by

no test coverage detected