-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackfill_ldif.js
99 lines (85 loc) · 2.62 KB
/
backfill_ldif.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
#!/usr/bin/node
// node backfill_ldif.js address.ldif mozillaCustom4 apikey
var infile = process.argv[2];
var fieldForGPSInfo = process.argv[3];
var googleapikey = process.argv[4];
var ldif = require('ldif');
var googleMapsClient = require('@google/maps').createClient({
key: googleapikey
});
var fs = require('fs');
var outputfilename = 'outputfile.ldif';
if (fs.existsSync(outputfilename)) {
console.log('Output file exists already');
process.exit();
}
fs.writeFile(outputfilename, '', function(){console.log('done');});
var parsed = ldif.parseFile(infile);
var updateLDIF = function (record,attributes) {
if ( record instanceof ldif.Record) {
try {
// ~console.log(record,attributes);
attributes.forEach( function (attribute) {
let index = record.attributes.findIndex( function (element, index, array) {
if (element.attribute.attribute === attribute.attribute) {
return true;
}
});
if (index !== -1) {
if (record.attributes[index].attribute.attribute === attribute.attribute) {
record.attributes[index].value.value = attribute.value;
}
} else {
record.populate([{
'attribute': attribute.attribute,
'value': attribute.value
}]);
}
});
let LDIFSTRING = record.toLDIF();
fs.appendFileSync(outputfilename, LDIFSTRING+ "\n\n", (err) => {
if (err) throw err;
});
} catch (err) {
console.log(err);
}
}
};
parsed.entries.forEach( function (record) {
let contact = record.toObject({});
let attr = contact.attributes;
let updateAttributes = [];
if (attr.hasOwnProperty('objectclass')
&& attr.objectclass.includes('organizationalPerson')
) {
updateAttributes.push({attribute:'c',value:''});
updateAttributes.push({attribute:'mozillaHomeCountryName',value:'Canada'});
attr.mozillaHomeCountryName = 'Canada';
if (attr.mozillaHomeStreet
&& attr.mozillaHomeStreet.length > 5
&& !attr.hasOwnProperty(fieldForGPSInfo)
) {
googleMapsClient.geocode({
region: 'ca',
address: attr.mozillaHomeStreet +', '+ attr.mozillaHomeLocalityName +', '+ attr.mozillaHomeState +', '+ attr.mozillaHomePostalCode
+', '+ attr.mozillaHomeCountryName
}, function(err, response) {
if (!err) {
console.log(JSON.stringify(response.json.results[0].geometry.location));
updateAttributes.push({
attribute: fieldForGPSInfo,
value: JSON.stringify(response.json.results[0].geometry.location)
});
} else {
console.log(err);
console.log('GPS Coords not found for:'+contact.dn);
}
updateLDIF(record,updateAttributes);
});
} else {
updateLDIF(record,[]);
}
} else {
updateLDIF(record,[]);
}
});