-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
86 lines (73 loc) · 2.1 KB
/
Code.gs
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
function openLebDevs() {
moveDevices(true, 'Lebanon');
}
function closeLebDevs() {
moveDevices(false, 'Lebanon');
}
function openKiltonDevs() {
moveDevices(true, 'Kilton');
}
function closeKiltonDevs() {
moveDevices(false, 'Kilton');
}
function getPaths() {
let rootPath = '/PublicChromeDevices/OPACS';
var ous = AdminDirectory.Orgunits.list('my_customer', { orgUnitPath: rootPath, type: 'all' });
return ous.organizationUnits.filter(ou => ou.name === 'Closed');
}
function getPage(source) {
let pageToken;
let page = AdminDirectory.Chromeosdevices.list('my_customer', {
maxResults: 100,
pageToken: pageToken,
orgUnitPath: source,
});
return page;
}
function makeMoveLog(device, destOU) {
Logger.log('Moving %s; SN: %s; Location: %s; from %s to %s',
device.annotatedAssetId,
device.serialNumber,
device.annotatedLocation,
device.orgUnitPath,
destOU);
}
function moveUnitsDevices(ou, deviceLocation, opening) {
let sourceOU;
let destOU;
let pageToken;
let onLocationDevs = [];
if (opening) {
sourceOU = ou.orgUnitPath;
destOU = ou.parentOrgUnitPath;
} else {
sourceOU = ou.parentOrgUnitPath;
destOU = ou.orgUnitPath;
}
do {
let page = getPage(sourceOU);
let devices = page.chromeosdevices;
if (devices) {
onLocationDevs = devices.filter(dev => dev.annotatedLocation === deviceLocation);
}
if (onLocationDevs.length > 0) {
for (let i = 0; i < onLocationDevs.length; i++) {
const device = onLocationDevs[i];
makeMoveLog(device, destOU);
AdminDirectory.Chromeosdevices.update({
orgUnitPath: destOU,
}, 'my_customer', device.deviceId)
AdminDirectory.Customer.Devices.Chromeos.issueCommand({
commandType: "REBOOT"
}, 'my_customer', device.deviceId)
}
} else {
Logger.log(`No ${deviceLocation} devices found in unit ${sourceOU}`)
pageToken = page.nextPageToken;
}
} while (pageToken)
}
function moveDevices(open, deviceLocation) {
const paths = getPaths();
paths.forEach(path => moveUnitsDevices(path, deviceLocation, open));
}