Skip to content

Commit 7554482

Browse files
committed
update edge.js to fix minor bugs and refactor
- KVM put was not working properly - import of zip was not working - AdmZip had changed? - added defaults to a bunch of arguments
1 parent b19053f commit 7554482

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
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.24",
12+
"apigee-edge-js": "^0.2.26",
1313
"async": "^2.2.0",
1414
"merge": "^1.2.0",
1515
"mkdirp": "^0.5.1",

lib/edge.js

Lines changed: 52 additions & 37 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-18 11:00:16>
21+
// last saved: <2018-February-27 14:51:13>
2222

2323
(function (){
2424
var path = require('path'),
@@ -104,6 +104,7 @@
104104
}
105105

106106
Organization.prototype.get = function(url, cb) {
107+
if ( ! cb) { cb = url; url = ''; }
107108
var conn = this.conn;
108109
url = urljoin(conn.urlBase, url);
109110
if (conn.verbosity>0) {
@@ -112,6 +113,16 @@
112113
request.get(url, {headers: conn.requestHeaders}, commonCallback(conn, [200], cb));
113114
};
114115

116+
Organization.prototype.getProperties = function(cb) {
117+
var conn = this.conn;
118+
this.get(function(e, result) {
119+
if (e) { return cb(e, result); }
120+
conn.orgProperties = arrayOfKeyValuePairsToHash(result.properties.property);
121+
result.properties = conn.orgProperties;
122+
cb(null, result);
123+
});
124+
};
125+
115126
function Environment(conn) {this.conn = conn;}
116127

117128
function internalGetEnvironments(conn, options, cb) {
@@ -623,15 +634,19 @@
623634

624635
function inferAssetNameFromZip(conn, source, cb) {
625636
// temporarily unzip the file and then scan the dir
626-
var toplevelXmlRe = new RegExp('^apiproxy/[^/]+\\.zip$');
637+
var toplevelXmlRe = new RegExp('^apiproxy/[^/]+\\.xml$');
627638
var zip = new AdmZip(source);
628639
var zipEntries = zip.getEntries();
629-
for (var i = 0; i< zipEntries.length; i++) {
630-
if (toplevelXmlRe.test(entry.entryName)) {
631-
i = zipEntries.length; // stop the loop
632-
doParseForName(conn, entry.data.toString('utf8'), cb);
640+
var foundit = false;
641+
zipEntries.forEach(function(entry){
642+
if ( ! foundit) {
643+
if (toplevelXmlRe.test(entry.entryName)) {
644+
let data = entry.getData();
645+
doParseForName(conn, data.toString('utf8'), cb);
646+
foundit = true;
647+
}
633648
}
634-
}
649+
});
635650
}
636651

637652
/*
@@ -649,7 +664,15 @@
649664
return -1;
650665
}
651666

652-
function uglifyAttrs(hash) {
667+
function arrayOfKeyValuePairsToHash(properties) {
668+
var hash = {};
669+
properties.forEach(function(item) {
670+
hash[item.name] = item.value;
671+
});
672+
return hash;
673+
}
674+
675+
function hashToArrayOfKeyValuePairs(hash) {
653676
return Object.keys(hash).map(function(key){
654677
return { name : key, value : hash[key]};
655678
});
@@ -1024,7 +1047,7 @@
10241047
mergeRequestOptions(conn, function(requestOptions) {
10251048
requestOptions.headers['content-type'] = 'application/json';
10261049
requestOptions.url = urljoin(conn.urlBase, 'developers');
1027-
var devAttributes = uglifyAttrs(merge(options.attributes, {
1050+
var devAttributes = hashToArrayOfKeyValuePairs(merge(options.attributes, {
10281051
"tool": "nodejs " + path.basename(process.argv[1])
10291052
}));
10301053
requestOptions.body = JSON.stringify({
@@ -1155,7 +1178,7 @@
11551178
utility.logWrite(sprintf('Using default expiry of %d', keyExpiresIn));
11561179
}
11571180
}
1158-
var appAttributes = uglifyAttrs(merge(options.attributes || {}, {
1181+
var appAttributes = hashToArrayOfKeyValuePairs(merge(options.attributes || {}, {
11591182
"tool": "nodejs " + path.basename(process.argv[1])
11601183
}));
11611184
requestOptions.body = JSON.stringify({
@@ -1304,7 +1327,7 @@
13041327
mergeRequestOptions(conn, function(requestOptions) {
13051328
requestOptions.headers['content-type'] = 'application/json';
13061329
requestOptions.url = urljoin(conn.urlBase, 'apiproducts');
1307-
var prodAttributes = uglifyAttrs(merge(options.attributes || {}, {
1330+
var prodAttributes = hashToArrayOfKeyValuePairs(merge(options.attributes || {}, {
13081331
"tool": "nodejs " + path.basename(process.argv[1])
13091332
}));
13101333
var rOptions = {
@@ -1544,6 +1567,9 @@
15441567
requestOptions.url = resolveKvmPath(conn, options);
15451568

15461569
if (conn.orgProperties['features.isCpsEnabled']) {
1570+
if (!options.key || !options.value) {
1571+
throw new Error("missing key or value");
1572+
}
15471573
requestOptions.url = urljoin(requestOptions.url, options.kvm, 'entries', options.key);
15481574
if (conn.verbosity>0) {
15491575
utility.logWrite(sprintf('GET %s', requestOptions.url));
@@ -1559,13 +1585,13 @@
15591585
if (response.statusCode == 200) {
15601586
// Update is required if the key already exists.
15611587
if (conn.verbosity>0) {
1562-
utility.logWrite('KVM update');
1588+
utility.logWrite('KVM entry update');
15631589
}
15641590
requestOptions.url = urljoin(requestOptions.url, options.key);
15651591
}
15661592
else if (response.statusCode == 404) {
15671593
if (conn.verbosity>0) {
1568-
utility.logWrite('KVM create');
1594+
utility.logWrite('KVM entry create');
15691595
}
15701596
}
15711597

@@ -1595,6 +1621,9 @@
15951621
});
15961622
}
15971623
else {
1624+
if (!options.entries && (!options.key || !options.value)) {
1625+
throw new Error("missing entries or key/value");
1626+
}
15981627
// for non-CPS KVM, use a different model to add/update an entry.
15991628
//
16001629
// POST :mgmtserver/v1/o/:orgname/e/:env/keyvaluemaps/:mapname
@@ -1607,7 +1636,11 @@
16071636
// }
16081637
requestOptions.url = urljoin(requestOptions.url, options.kvm);
16091638
requestOptions.headers['content-type'] = 'application/json';
1610-
requestOptions.body = JSON.stringify({ name: options.kvm, entry: [{ name: options.key, value : options.value }] });
1639+
var entry = options.entries ?
1640+
hashToArrayOfKeyValuePairs(options.entries) :
1641+
[{ name: options.key, value : options.value }] ;
1642+
1643+
requestOptions.body = JSON.stringify({ name: options.kvm, entry: entry });
16111644
if (conn.verbosity>0) {
16121645
utility.logWrite(sprintf('POST %s', requestOptions.url));
16131646
}
@@ -1653,7 +1686,7 @@
16531686
requestOptions.body = JSON.stringify({
16541687
encrypted : options.encrypted ? "true" : "false",
16551688
name : options.name,
1656-
entry : options.entries ? uglifyAttrs(options.entries) : []
1689+
entry : options.entries ? hashToArrayOfKeyValuePairs(options.entries) : []
16571690
});
16581691
if (conn.verbosity>0) {
16591692
utility.logWrite(sprintf('POST %s', requestOptions.url));
@@ -1662,29 +1695,13 @@
16621695
});
16631696
};
16641697

1665-
function transformToHash(properties) {
1666-
var hash = {};
1667-
properties.forEach(function(item) {
1668-
hash[item.name] = item.value;
1669-
});
1670-
return hash;
1671-
}
1672-
1673-
function checkOrgProperties(conn, cb) {
1674-
var org = new Organization(conn);
1675-
return org.get('', cb);
1676-
}
1677-
16781698
Kvm.prototype.put = function(options, cb) {
16791699
var conn = this.conn;
16801700
if ( ! conn.orgProperties) {
1681-
return checkOrgProperties(conn, function(e, result) {
1682-
if (e) {
1683-
console.log(e);
1684-
return cb(e, result);
1685-
}
1686-
conn.orgProperties = transformToHash(result.properties.property);
1687-
return putKvm0(conn, options, cb);
1701+
var org = new Organization(conn);
1702+
org.getProperties(function(e, result) {
1703+
if (e) { return cb(e, result); }
1704+
putKvm0(conn, options, cb);
16881705
});
16891706
}
16901707
else {
@@ -1711,7 +1728,6 @@
17111728
};
17121729

17131730

1714-
17151731
function ApiProxy(conn) {
17161732
this.conn = conn;
17171733
}
@@ -1820,7 +1836,6 @@
18201836
return import0(conn, options, 'apiproxy', cb);
18211837
};
18221838

1823-
18241839
// Connection properties:
18251840
// get
18261841
// refreshToken

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.24",
3+
"version": "0.2.26",
44
"description": "Dino's nodejs library for the administration API for Apigee Edge.",
55
"main" : "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)