-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-user-clients.js
127 lines (101 loc) · 3.11 KB
/
list-user-clients.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
/*jslint node: true*/
'use strict';
var https = require('https');
var getopt = require('posix-getopt');
var app = {
host: 'localhost',
port: 443,
token: 'my-rhoconnect-token',
getClientsForUser: function (user, callback) {
var options, req;
options = {
hostname: this.host,
port: this.port,
path: '/rc/v1/users/' + user + '/clients',
method: 'GET',
headers: {
'X-RhoConnect-API-TOKEN': this.token
},
rejectUnauthorized: false
};
req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
if (typeof callback !== 'undefined') {
callback(JSON.parse(data)); // FIXME: Catch exceptions.
}
});
res.on('error', function (err) {
console.error(err);
});
});
req.on('error', function (err) {
console.error(err);
});
req.end();
},
getUsers: function (callback) {
var options, req;
options = {
hostname: this.host,
port: this.port,
path: '/rc/v1/users',
method: 'GET',
headers: {
'X-RhoConnect-API-TOKEN': this.token
},
rejectUnauthorized: false
};
req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
if (typeof callback !== 'undefined') {
callback(JSON.parse(data)); // FIXME: Catch exceptions.
}
});
res.on('error', function (err) {
console.error(err);
});
});
req.on('error', function (err) {
console.error(err);
});
req.end();
},
config: function (argv) {
var parser, option;
parser = new getopt.BasicParser('h:(host)p:(port)t:(token)', argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'h':
this.host = option.optarg;
break;
case 'p':
this.port = option.optarg;
break;
case 't':
this.token = option.optarg;
break;
default:
return;
}
}
},
run: function (argv) {
app.config(argv);
app.getUsers(function (users) {
users.forEach(function (user) {
app.getClientsForUser(user, function (clients) {
if (clients.length > 0) {
clients.forEach(function (client) {
console.log(user + ':' + client + (clients.length > 1 ? '+' : ''));
});
} else {
console.log(user + ':');
}
});
});
});
}
};
app.run(process.argv);