This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
createTenants.js
159 lines (128 loc) · 4.93 KB
/
createTenants.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*!
* Copyright 2015 Apereo Foundation (AF) Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
var _ = require('lodash');
var csv = require('csv');
var fs = require('fs');
var path = require('path');
var read = require('read');
var util = require('util');
var RestAPI = require('oae-rest');
var RestContext = require('oae-rest/lib/model').RestContext;
var UnityAPI = require('./api');
var argv = require('yargs')
.usage('Usage: $0 --file path/to/file.csv')
.alias('h', 'help')
.describe('h', 'Show help information')
.alias('u', 'url')
.describe('u', 'The URL of the global admin url (including the protocol)')
.alias('a', 'admin')
.describe('a', 'The username of the global administrator')
.alias('f', 'file')
.describe('f', 'The path to the CSV file')
.alias('c', 'concurrency')
.default('c', 1)
.describe('c', 'The number of threads to run')
.demand(['u', 'a', 'f'])
.argv;
read({'prompt': 'Password: ', 'silent': true}, function(err, password) {
if (err) {
console.log('Error: %s', err.stack);
process.exit(1);
}
var restCtx = new RestContext(argv.url, {
'username': argv.admin,
'userPassword': password,
'strictSSL': false
});
// List the errors
require('oae-rest/lib/util').on('error', function(err, body, response) {
console.log('Error %d: - %s', err.code, body);
});
console.log('Parsing CSV data...');
UnityAPI.getCSVData(argv.file, function(err, records) {
if (err) {
process.exit(1);
}
console.log('Getting all current tenants...');
RestAPI.Tenants.getTenants(restCtx, function(err, tenants) {
if (err) {
console.log('Failed to get all the tenants');
process.exit(1);
}
console.log('Begin creating new tenants...');
var concurrency = parseInt(argv.c, 10);
if (_.isNaN(concurrency)) {
console.log('Invalid argument concurrency argument: "%s"', argv.c);
process.exit(1);
}
for (var i = 0; i < concurrency; i++) {
createTenants(restCtx, tenants, records, function() {
console.log('All done');
});
}
});
});
});
var createTenants = function(restCtx, tenants, records, callback) {
if (_.isEmpty(records)) {
return callback();
}
var record = records.pop();
console.log('%s - %s', record.hostname, record.organisation);
if (!record.organisation || !record.alias || !record.hostname) {
console.log(' Ignoring "%s" because of missing data display name, alias, or host name', record.alias);
return setImmediate(createTenants, restCtx, tenants, records, callback);
} else if (tenants[record.alias]) {
// Ignore existing tenants for now
console.log(' Ignoring because the tenant exists already');
// Move on to the next one
return setImmediate(createTenants, restCtx, tenants, records, callback);
}
// 1. Create the tenant
createTenant(restCtx, tenants[record.alias], record, function(err, tenant) {
if (err) {
console.log(' Failed to create or update the tenant');
console.log(err);
process.exit(1);
}
// 2. Set the tenants configuration
setConfiguration(restCtx, tenant, record, function(err) {
if (err) {
console.log(' Failed to set the configuration');
console.log(err);
process.exit(1);
}
// Move on to the next one
createTenants(restCtx, tenants, records, callback);
});
});
};
var createTenant = function(restCtx, tenant, record, callback) {
console.log(' Creating new tenancy');
// Create the tenant
var opts = {'emailDomain': record.email, 'countryCode': record.country};
RestAPI.Tenants.createTenant(restCtx, record.alias, record.organisation, record.hostname, opts, callback);
};
var setConfiguration = function(restCtx, tenant, record, callback) {
console.log(' Setting configuration');
var update = {};
if (record.language) {
update['oae-principals/user/defaultLanguage'] = record.language;
}
if (record.timezone) {
update['oae-tenants/timezone/timezone'] = record.timezone;
}
RestAPI.Config.updateConfig(restCtx, tenant.alias, update, callback);
};