Skip to content

Commit 7c3e848

Browse files
committed
saving state
1 parent e9adc8e commit 7c3e848

File tree

3 files changed

+73
-44
lines changed

3 files changed

+73
-44
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ node_modules
33

44
aks.json
55
test_output.json
6-
test.json
6+
test.json
7+
8+
terraform/
9+
output/

index.js

+16-40
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const AzureCompute = require('@azure/arm-compute');
22
const AzureResources = require('@azure/arm-resources')
33
const MsRest = require('@azure/ms-rest-nodeauth');
4+
5+
const FS = require('fs');
6+
47
const ClientID = process.env.ARM_CLIENT_ID;
58
const ClientSecret = process.env.ARM_CLIENT_SECRET;
69
const SubscriptionID = process.env.ARM_SUBSCRIPTION_ID;
710
const TenantID = process.env.ARM_TENANT_ID;
8-
const debug = process.env.DEBUG || false;
9-
const FS = require('fs');
11+
12+
13+
const TFAzureResource = require('./lib/tfobj');
1014

1115
function getCredentials() {
1216
return new Promise((resolve, reject) => {
@@ -21,49 +25,21 @@ function getCredentials() {
2125
})
2226
}
2327

24-
function toTfObject(azObject){
25-
const startTF = `resource "${azObject.armResourceType}" "${azObject.name}" {`
26-
const endTF = `}`
27-
let tfObject = startTF + "\n";
28-
29-
delete azObject.id;
30-
delete azObject.armResourceType;
31-
delete azObject.managedBy;
32-
33-
for (var property in azObject) {
34-
const type = azObject[property].constructor;
35-
36-
if(azObject.hasOwnProperty(property)){
37-
switch(type) {
38-
case Object:
39-
break;
40-
default:
41-
tfObject += objectify(property, azObject[property])
42-
}
43-
}
44-
}
45-
46-
tfObject += endTF;
47-
48-
if(debug){
49-
console.log(tfObject);
50-
}
51-
52-
return tfObject;
53-
}
54-
55-
function objectify(property, value) {
56-
return "\t" + `${property} = "${value}"` + "\n";
57-
}
58-
5928
getCredentials().then(credentials => {
6029
const client = new AzureResources.ResourceManagementClient(credentials, SubscriptionID);
6130

6231
client.resourceGroups.list()
6332
.then((groups)=>{
64-
groups.forEach((group, index) => {
65-
group.armResourceType = 'azurerm_resource_group';
66-
toTfObject(group);
33+
groups.forEach(group => {
34+
let thing = new TFAzureResource('azurerm_resource_group', group);
35+
36+
thing.saveToFile()
37+
.then(obj => {console.log(obj)})
38+
.catch()
6739
})
40+
// groups.forEach((group, index) => {
41+
// group.armResourceType = 'azurerm_resource_group';
42+
// toTfObject(group);
43+
// })
6844
})
6945
})

lib/tfobj.js

+53-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
1+
const fs = require('fs');
2+
13
class TFAzureResource {
2-
constructor(json) {
3-
this.jsonObject = json;
4+
constructor(type, jsonObject) {
5+
this.uuid = jsonObject.id;
6+
this.name = jsonObject.name;
7+
this.type = type;
8+
this.terraformId = `${this.type}.${this.name}`;
9+
this.originalJSON = jsonObject;
410
}
511

12+
toTF(){
13+
const azObject = this.originalJSON;
14+
const startTF = `resource "${this.type}" "${azObject.name}" {`
15+
const endTF = `}`
16+
let tfObject = startTF + "\n";
17+
18+
delete azObject.id;
19+
delete azObject.properties;
20+
delete azObject.managedBy;
21+
22+
for (var property in azObject) {
23+
const type = azObject[property].constructor;
24+
25+
if(azObject.hasOwnProperty(property)){
26+
switch(type) {
27+
case Object:
28+
break;
29+
default:
30+
tfObject += this.objectify(property, azObject[property])
31+
}
32+
}
33+
}
34+
35+
tfObject += endTF;
36+
37+
return tfObject;
38+
}
39+
640
objectify(property, value) {
741
return "\t" + `${property} = "${value}"` + "\n";
842
}
43+
44+
saveToFile(){
45+
return new Promise((resolve, reject) => {
46+
let opts = 'utf8';
47+
const dir = `./output`
48+
const data = this.toTF();
49+
50+
if(!fs.existsSync(dir)) fs.mkdirSync(dir);
51+
52+
53+
fs.writeFile(dir + `/${this.type}.${this.name}.tf`, data, opts, (err) => {
54+
if(err) reject(err)
55+
else resolve(data)
56+
})
57+
});
58+
}
959
}
1060

1161

12-
module.exports.TFAzureResource = TFAzureResource;
62+
module.exports = TFAzureResource;

0 commit comments

Comments
 (0)