Skip to content

Commit b19053f

Browse files
committed
update to v0.2.24
- make the library more flexible with regard to input arguments, eg, options.environmentName or options.name is accepted - correct attributes for product, developer, or app - consolidate helper fn into ApiProduct.create() - allow ApiProduct.create to accept options.proxy or options.proxies
1 parent ec0332d commit b19053f

File tree

3 files changed

+100
-77
lines changed

3 files changed

+100
-77
lines changed

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"author": "dchiesa@google.com",
1010
"license": "Apache-2.0",
1111
"dependencies": {
12-
"apigee-edge-js": "^0.2.23",
12+
"apigee-edge-js": "^0.2.24",
1313
"async": "^2.2.0",
1414
"merge": "^1.2.0",
1515
"mkdirp": "^0.5.1",

lib/edge.js

Lines changed: 98 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// limitations under the License.
1919
//
2020
// created: Mon Jun 6 17:32:20 2016
21-
// last saved: <2018-February-12 14:25:10>
21+
// last saved: <2018-February-18 11:00:16>
2222

2323
(function (){
2424
var path = require('path'),
@@ -143,22 +143,21 @@
143143

144144
Environment.prototype.getVhosts = function(options, cb) {
145145
var conn = this.conn;
146-
if (!options.name ) {
147-
throw new Error("missing environment name");
148-
}
146+
var name = options.environmentName || options.environment || options.name;
147+
if (!name) {
148+
throw new Error("missing environment name");
149+
}
149150
if (conn.verbosity>0) {
150151
utility.logWrite('get vhosts');
151152
}
152153
mergeRequestOptions(conn, function(requestOptions) {
153154
requestOptions.url = (options.vhost) ?
154-
urljoin(conn.urlBase, 'e', options.name, 'virtualhosts', options.vhost):
155-
urljoin(conn.urlBase, 'e', options.name, 'virtualhosts');
155+
urljoin(conn.urlBase, 'e', name, 'virtualhosts', options.vhost):
156+
urljoin(conn.urlBase, 'e', name, 'virtualhosts');
156157
if (conn.verbosity>0) {
157158
utility.logWrite(sprintf('GET %s', requestOptions.url));
158159
}
159-
request.get(requestOptions, commonCallback(conn, [200], function(e, result){
160-
cb(e, result);
161-
}));
160+
request.get(requestOptions, commonCallback(conn, [200], cb));
162161
});
163162
};
164163

@@ -774,14 +773,16 @@
774773
// DELETE :mgmtserver/v1/o/:orgname/e/:envname/apis/:proxyname/revisions/:revnum/deployments
775774
// Authorization: :edge-auth
776775
var collection = getCollectionNameForAssetType(assetType);
776+
var rev = options.revision.name || options.revision;
777+
var env = options.environment.name || options.environment;
777778
if (conn.verbosity>0) {
778-
utility.logWrite(sprintf('Undeploy %s %s r%d from env:%s', assetType, options.name, options.revision, options.environment));
779+
utility.logWrite(sprintf('Undeploy %s %s r%d from env:%s', assetType, options.name, rev, env));
779780
}
780781
mergeRequestOptions(conn, function(requestOptions) {
781782
requestOptions.url = urljoin(conn.urlBase,
782-
'e', options.environment,
783+
'e', env,
783784
collection, options.name,
784-
'revisions', options.revision,
785+
'revisions', rev,
785786
'deployments');
786787
if (conn.verbosity>0) {
787788
utility.logWrite(sprintf('DELETE %s', requestOptions.url));
@@ -1013,30 +1014,30 @@
10131014
// "email": "tet-3a-HiDxfHvHrB@apigee.com"
10141015
// }
10151016
var conn = this.conn;
1016-
if ( !options.developerEmail || !options.firstName || !options.lastName || !options.userName) {
1017-
return cb({error: "missing required inputs"});
1017+
var email = options.developerEmail || options.email;
1018+
if ( !email || !options.firstName || !options.lastName || !options.userName) {
1019+
return cb({error: "missing required inputs, one of {email, firstName, lastName, userName}"});
10181020
}
10191021
if (conn.verbosity>0) {
1020-
utility.logWrite(sprintf('Create Developer %s', options.developerEmail || 'unknown'));
1022+
utility.logWrite(sprintf('Create Developer %s', email));
10211023
}
10221024
mergeRequestOptions(conn, function(requestOptions) {
10231025
requestOptions.headers['content-type'] = 'application/json';
10241026
requestOptions.url = urljoin(conn.urlBase, 'developers');
1025-
10261027
var devAttributes = uglifyAttrs(merge(options.attributes, {
1027-
"created by": "apigee-edge-js"
1028+
"tool": "nodejs " + path.basename(process.argv[1])
10281029
}));
1029-
10301030
requestOptions.body = JSON.stringify({
10311031
attributes : devAttributes,
10321032
userName : options.userName,
10331033
firstName : options.firstName,
10341034
lastName : options.lastName,
1035-
email: options.developerEmail
1035+
email: email
10361036
});
10371037
if (conn.verbosity>0) {
10381038
utility.logWrite(sprintf('POST %s', requestOptions.url));
10391039
}
1040+
//request.debug = true;
10401041
request.post(requestOptions, commonCallback(conn, [201], cb));
10411042
});
10421043
};
@@ -1062,6 +1063,7 @@
10621063

10631064
Developer.prototype.get = function(options, cb) {
10641065
var conn = this.conn;
1066+
if ( ! cb) { cb = options; options = {}; }
10651067
mergeRequestOptions(conn, function(requestOptions) {
10661068
if (options.developerEmail || options.email) {
10671069
requestOptions.url = urljoin(conn.urlBase, 'developers', options.developerEmail || options.email);
@@ -1098,6 +1100,22 @@
10981100
});
10991101
};
11001102

1103+
App.prototype.del = function(options, cb) {
1104+
// DELETE :mgmtserver/v1/o/:orgname/apps/:appid
1105+
// Authorization: :edge-auth
1106+
var conn = this.conn;
1107+
if (conn.verbosity>0) {
1108+
utility.logWrite(sprintf('Delete App %s', options.appId || options.id));
1109+
}
1110+
mergeRequestOptions(conn, function(requestOptions) {
1111+
requestOptions.url = urljoin(conn.urlBase, 'apps', options.appId || option.id);
1112+
if (conn.verbosity>0) {
1113+
utility.logWrite(sprintf('DELETE %s', requestOptions.url));
1114+
}
1115+
request.del(requestOptions, commonCallback(conn, [200], cb));
1116+
});
1117+
};
1118+
11011119
function DeveloperApp(conn) {this.conn = conn;}
11021120

11031121
DeveloperApp.prototype.create = function(options, cb) {
@@ -1116,14 +1134,17 @@
11161134
// "name" : "ElaineApp2"
11171135
// }
11181136
var conn = this.conn;
1137+
var email = options.developer || options.developerEmail || options.email;
1138+
var name = options.appName || options.name;
1139+
if ( !email || !name || !options.apiProduct) {
1140+
return cb({error: "missing required inputs, one of {developer, appName, apiProduct}"});
1141+
}
11191142
if (conn.verbosity>0) {
1120-
utility.logWrite(sprintf('Create App %s for %s', options.appName, options.developerEmail));
1143+
utility.logWrite(sprintf('Create App %s for %s', name, email));
11211144
}
11221145
mergeRequestOptions(conn, function(requestOptions) {
11231146
requestOptions.headers['content-type'] = 'application/json';
1124-
requestOptions.url = urljoin(conn.urlBase,
1125-
'developers',options.developerEmail,
1126-
'apps');
1147+
requestOptions.url = urljoin(conn.urlBase, 'developers', email, 'apps');
11271148
var DEFAULT_EXPIRY = -1;
11281149
var keyExpiresIn = DEFAULT_EXPIRY;
11291150
if (options.expiry) {
@@ -1134,21 +1155,19 @@
11341155
utility.logWrite(sprintf('Using default expiry of %d', keyExpiresIn));
11351156
}
11361157
}
1137-
1138-
var appAttributes = uglifyAttrs(merge(options.attributes, {
1139-
"created by": "nodejs " + path.basename(process.argv[1])
1158+
var appAttributes = uglifyAttrs(merge(options.attributes || {}, {
1159+
"tool": "nodejs " + path.basename(process.argv[1])
11401160
}));
1141-
11421161
requestOptions.body = JSON.stringify({
11431162
attributes : appAttributes,
11441163
apiProducts: [options.apiProduct],
11451164
keyExpiresIn : keyExpiresIn,
1146-
name: options.appName
1165+
name: name
11471166
});
1148-
11491167
if (conn.verbosity>0) {
11501168
utility.logWrite(sprintf('POST %s', requestOptions.url));
11511169
}
1170+
//request.debug = true;
11521171
request.post(requestOptions, commonCallback(conn, [201], cb));
11531172
});
11541173
};
@@ -1157,11 +1176,12 @@
11571176
// DELETE :mgmtserver/v1/o/:orgname/developers/:developer/apps/:appname
11581177
// Authorization: :edge-auth
11591178
var conn = this.conn;
1179+
var name = options.appName || options.name;
11601180
if (conn.verbosity>0) {
1161-
utility.logWrite(sprintf('Delete App %s for Developer %s', options.appName, options.developerEmail));
1181+
utility.logWrite(sprintf('Delete App %s for Developer %s', name, options.developerEmail));
11621182
}
11631183
mergeRequestOptions(conn, function(requestOptions) {
1164-
requestOptions.url = urljoin(conn.urlBase, 'developers', options.developerEmail, 'apps', options.appName);
1184+
requestOptions.url = urljoin(conn.urlBase, 'developers', options.developerEmail, 'apps', name);
11651185
if (conn.verbosity>0) {
11661186
utility.logWrite(sprintf('DELETE %s', requestOptions.url));
11671187
}
@@ -1171,16 +1191,26 @@
11711191

11721192
DeveloperApp.prototype.get = function(options, cb) {
11731193
var conn = this.conn;
1174-
if (conn.verbosity>0) {
1175-
utility.logWrite(sprintf('Get Developer App %s/apps/%s',
1176-
options.developerEmail,
1177-
options.appName));
1194+
var name = options.appName || options.name;
1195+
var email = options.developerEmail || options.email;
1196+
if (!email) {
1197+
throw new Error("missing developer email");
11781198
}
1199+
// if (conn.verbosity>0) {
1200+
// if (options.appName || options.name) {
1201+
// utility.logWrite(sprintf('Get Developer App %s/apps/%s',
1202+
// options.developerEmail,
1203+
// options.appName));
1204+
// }
1205+
// else {
1206+
// utility.logWrite(sprintf('Get Developer Apps %s',
1207+
// options.developerEmail));
1208+
// }
1209+
// }
11791210
mergeRequestOptions(conn, function(requestOptions) {
1180-
requestOptions.url = urljoin(conn.urlBase,
1181-
sprintf('developers/%s/apps/%s',
1182-
options.developerEmail,
1183-
options.appName));
1211+
requestOptions.url = (options.appName || options.name) ?
1212+
urljoin(conn.urlBase, 'developers', email, 'apps') :
1213+
urljoin(conn.urlBase, 'developers', email, 'apps', name);
11841214
if (conn.verbosity>0) {
11851215
utility.logWrite(sprintf('GET %s', requestOptions.url));
11861216
}
@@ -1246,68 +1276,60 @@
12461276

12471277
function ApiProduct(conn) { this.conn = conn; }
12481278

1249-
function reallyCreateProduct(conn, options, cb) {
1279+
ApiProduct.prototype.create = function(options, cb) {
1280+
// POST :mgmtserver/v1/o/:orgname/apiproducts/:product
1281+
// Content-Type: application/json
1282+
// Authorization: :edge-auth
1283+
//
1284+
// {
1285+
// "name" : ":product",
1286+
// "attributes" : [ {"name": "created by", "value" : "emacs"} ],
1287+
// "approvalType" : "manual",
1288+
// "displayName" : ":product",
1289+
// "proxies" : ["proxy1", "proxy2"],
1290+
// "scopes" : ["read", "write", "something"],
1291+
// "environments" : [ "prod" ]
1292+
// }
1293+
var conn = this.conn;
12501294
if (conn.verbosity>0) {
12511295
if (options.proxy) {
12521296
utility.logWrite(sprintf('Create API Product %s with proxy %s', options.productName, options.proxy));
12531297
}
1254-
else {
1298+
else if (options.proxies) {
1299+
utility.logWrite(sprintf('Create API Product %s with proxies %s', options.productName, JSON.stringify(options.proxies)));
1300+
} else {
12551301
utility.logWrite(sprintf('Create API Product %s with no proxy', options.productName));
12561302
}
12571303
}
12581304
mergeRequestOptions(conn, function(requestOptions) {
12591305
requestOptions.headers['content-type'] = 'application/json';
12601306
requestOptions.url = urljoin(conn.urlBase, 'apiproducts');
1261-
var prodAttributes = uglifyAttrs(merge(options.attributes, {
1262-
"created by": "apigee-edge-js"
1307+
var prodAttributes = uglifyAttrs(merge(options.attributes || {}, {
1308+
"tool": "nodejs " + path.basename(process.argv[1])
12631309
}));
1264-
12651310
var rOptions = {
1266-
name : options.productName,
1311+
name : options.productName || options.name,
12671312
proxies : [ ],
12681313
attributes : prodAttributes,
12691314
approvalType : options.approvalType || "manual",
1270-
displayName : options.productName,
1315+
displayName : options.displayName || options.productName || options.name,
12711316
environments : options.environments || options.envs,
12721317
scopes : options.scopes
12731318
};
1274-
12751319
if (options.proxy) {
12761320
rOptions.proxies.push(options.proxy);
12771321
}
1322+
else if (options.proxies && Array.isArray(options.proxies) ) {
1323+
rOptions.proxies = options.proxies;
1324+
}
12781325
requestOptions.body = JSON.stringify(rOptions);
12791326

12801327
if (conn.verbosity>0) {
12811328
utility.logWrite(sprintf('POST %s', requestOptions.url));
12821329
}
1330+
// request.debug = true;
12831331
request.post(requestOptions, commonCallback(conn, [201], cb));
12841332
});
1285-
}
1286-
1287-
ApiProduct.prototype.create = function(options, cb) {
1288-
// POST :mgmtserver/v1/o/:orgname/apiproducts/:product
1289-
// Content-Type: application/json
1290-
// Authorization: :edge-auth
1291-
//
1292-
// {
1293-
// "name" : ":product",
1294-
// "attributes" : [ {"name": "created by", "value" : "emacs"} ],
1295-
// "approvalType" : "manual",
1296-
// "displayName" : ":product",
1297-
// "proxies" : ["proxy1", "proxy2"],
1298-
// "scopes" : ["read", "write", "something"],
1299-
// "environments" : [ "prod" ]
1300-
// }
1301-
var conn = this.conn;
1302-
// if ( ! options.envs) {
1303-
// conn.getEnvironments(function(e, result) {
1304-
// reallyCreateProduct(conn, merge(options, {envs: result}), cb);
1305-
// });
1306-
// }
1307-
// else {
1308-
// reallyCreateProduct(conn, options, cb);
1309-
// }
1310-
reallyCreateProduct(conn, options, cb);
13111333
};
13121334

13131335
ApiProduct.prototype.get = function(options, cb) {
@@ -1331,11 +1353,12 @@
13311353
// DELETE :mgmtserver/v1/o/:orgname/apiproducts/:apiproductname
13321354
// Authorization: :edge-auth
13331355
var conn = this.conn;
1356+
var name = options.productName || options.name;
13341357
if (conn.verbosity>0) {
1335-
utility.logWrite(sprintf('Delete API Product %s', options.productName));
1358+
utility.logWrite(sprintf('Delete API Product %s', name));
13361359
}
13371360
mergeRequestOptions(conn, function(requestOptions) {
1338-
requestOptions.url = urljoin(conn.urlBase, 'apiproducts', options.productName);
1361+
requestOptions.url = urljoin(conn.urlBase, 'apiproducts', name);
13391362
if (conn.verbosity>0) {
13401363
utility.logWrite(sprintf('DELETE %s', requestOptions.url));
13411364
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apigee-edge-js",
3-
"version": "0.2.23",
3+
"version": "0.2.24",
44
"description": "Dino's nodejs library for the administration API for Apigee Edge.",
55
"main" : "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)