-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
113 lines (102 loc) · 3.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import shell_cmd from "./shell_cmd.js";
import { ExportToCsv } from "export-to-csv";
import fs from "fs";
import _ from "lodash";
import dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
let projects = [];
async function checkForProject() {
try {
// const start = new shell_cmd();
const { stdout } = await shell_cmd(
`gcloud beta billing projects list --billing-account=${process.env.SADA_BILLING_ACCOUNT_ID} --format=json`
);
const data = JSON.parse(stdout);
data.forEach((project) => {
projects.push(project.projectId);
});
} catch (error) {
console.log("**Error querying for projects >>>", error);
return "Error looking for project";
}
}
await checkForProject();
console.log("project", projects);
let finalData = [];
async function getComputeList() {
try {
const results = projects.map(async (project) => {
// console.log("project", project);
// const start = new shell_cmd();
try {
const { stdout } = await shell_cmd(
`gcloud compute instances list --project ${project} --format=json`
);
const data = JSON.parse(stdout);
// console.log("data", data);
data.forEach((instance) => {
finalData.push({
project: project,
name: instance.name,
machineType: instance.machineType.split("/").pop(),
status: instance.status,
zone: instance.zone.split("/").pop(),
diskSizeGb: instance.disks.map((object) => object.diskSizeGb).toString(),
diskType: instance.disks.map((obj) => obj.type).toString(),
});
});
} catch (error) {
console.log(error);
}
// console.log(finalData);
// await parseTOCSV();
});
return await Promise.all(results);
} catch (error) {
console.log("**Error querying for instances >>>", error);
return "Error looking for instance";
}
}
await getComputeList();
console.log(" final data", finalData);
async function parseTOCSV() {
const test = finalData.map(async (obj) => {
console.log("entering map", obj);
let stuff;
await fetch("https://gcpinstances.doit-intl.com/data.json")
.then((res) => res.text())
.then((text) => {
stuff = JSON.parse(text);
});
const result = stuff[obj.machineType.split("-")[0]][obj.machineType];
return {
...obj,
cpu: result.specs.cores,
memory: result.specs.memory,
};
});
const results = await Promise.all(test);
// console.log(results);
const options = {
fieldSeparator: ",",
quoteStrings: '"',
decimalSeparator: ".",
showLabels: true,
showTitle: true,
title: `List of Compute Instances for billing account id ${process.env.SADA_BILLING_ACCOUNT_ID}`,
useTextFile: false,
useBom: true,
useKeysAsHeaders: true,
};
const csvExporter = new ExportToCsv(options);
const csvData = csvExporter.generateCsv(results, true);
fs.writeFileSync("data.csv", csvData);
}
if (!_.isEmpty(finalData)) {
await parseTOCSV();
console.log("success");
process.exit(1);
}
console.log("Failed");
process.exit(1);