|
18 | 18 | // limitations under the License.
|
19 | 19 | //
|
20 | 20 | // 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> |
22 | 22 |
|
23 | 23 | (function (){
|
24 | 24 | var path = require('path'),
|
|
104 | 104 | }
|
105 | 105 |
|
106 | 106 | Organization.prototype.get = function(url, cb) {
|
| 107 | + if ( ! cb) { cb = url; url = ''; } |
107 | 108 | var conn = this.conn;
|
108 | 109 | url = urljoin(conn.urlBase, url);
|
109 | 110 | if (conn.verbosity>0) {
|
|
112 | 113 | request.get(url, {headers: conn.requestHeaders}, commonCallback(conn, [200], cb));
|
113 | 114 | };
|
114 | 115 |
|
| 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 | + |
115 | 126 | function Environment(conn) {this.conn = conn;}
|
116 | 127 |
|
117 | 128 | function internalGetEnvironments(conn, options, cb) {
|
|
623 | 634 |
|
624 | 635 | function inferAssetNameFromZip(conn, source, cb) {
|
625 | 636 | // temporarily unzip the file and then scan the dir
|
626 |
| - var toplevelXmlRe = new RegExp('^apiproxy/[^/]+\\.zip$'); |
| 637 | + var toplevelXmlRe = new RegExp('^apiproxy/[^/]+\\.xml$'); |
627 | 638 | var zip = new AdmZip(source);
|
628 | 639 | 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 | + } |
633 | 648 | }
|
634 |
| - } |
| 649 | + }); |
635 | 650 | }
|
636 | 651 |
|
637 | 652 | /*
|
|
649 | 664 | return -1;
|
650 | 665 | }
|
651 | 666 |
|
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) { |
653 | 676 | return Object.keys(hash).map(function(key){
|
654 | 677 | return { name : key, value : hash[key]};
|
655 | 678 | });
|
|
1024 | 1047 | mergeRequestOptions(conn, function(requestOptions) {
|
1025 | 1048 | requestOptions.headers['content-type'] = 'application/json';
|
1026 | 1049 | requestOptions.url = urljoin(conn.urlBase, 'developers');
|
1027 |
| - var devAttributes = uglifyAttrs(merge(options.attributes, { |
| 1050 | + var devAttributes = hashToArrayOfKeyValuePairs(merge(options.attributes, { |
1028 | 1051 | "tool": "nodejs " + path.basename(process.argv[1])
|
1029 | 1052 | }));
|
1030 | 1053 | requestOptions.body = JSON.stringify({
|
|
1155 | 1178 | utility.logWrite(sprintf('Using default expiry of %d', keyExpiresIn));
|
1156 | 1179 | }
|
1157 | 1180 | }
|
1158 |
| - var appAttributes = uglifyAttrs(merge(options.attributes || {}, { |
| 1181 | + var appAttributes = hashToArrayOfKeyValuePairs(merge(options.attributes || {}, { |
1159 | 1182 | "tool": "nodejs " + path.basename(process.argv[1])
|
1160 | 1183 | }));
|
1161 | 1184 | requestOptions.body = JSON.stringify({
|
|
1304 | 1327 | mergeRequestOptions(conn, function(requestOptions) {
|
1305 | 1328 | requestOptions.headers['content-type'] = 'application/json';
|
1306 | 1329 | requestOptions.url = urljoin(conn.urlBase, 'apiproducts');
|
1307 |
| - var prodAttributes = uglifyAttrs(merge(options.attributes || {}, { |
| 1330 | + var prodAttributes = hashToArrayOfKeyValuePairs(merge(options.attributes || {}, { |
1308 | 1331 | "tool": "nodejs " + path.basename(process.argv[1])
|
1309 | 1332 | }));
|
1310 | 1333 | var rOptions = {
|
|
1544 | 1567 | requestOptions.url = resolveKvmPath(conn, options);
|
1545 | 1568 |
|
1546 | 1569 | if (conn.orgProperties['features.isCpsEnabled']) {
|
| 1570 | + if (!options.key || !options.value) { |
| 1571 | + throw new Error("missing key or value"); |
| 1572 | + } |
1547 | 1573 | requestOptions.url = urljoin(requestOptions.url, options.kvm, 'entries', options.key);
|
1548 | 1574 | if (conn.verbosity>0) {
|
1549 | 1575 | utility.logWrite(sprintf('GET %s', requestOptions.url));
|
|
1559 | 1585 | if (response.statusCode == 200) {
|
1560 | 1586 | // Update is required if the key already exists.
|
1561 | 1587 | if (conn.verbosity>0) {
|
1562 |
| - utility.logWrite('KVM update'); |
| 1588 | + utility.logWrite('KVM entry update'); |
1563 | 1589 | }
|
1564 | 1590 | requestOptions.url = urljoin(requestOptions.url, options.key);
|
1565 | 1591 | }
|
1566 | 1592 | else if (response.statusCode == 404) {
|
1567 | 1593 | if (conn.verbosity>0) {
|
1568 |
| - utility.logWrite('KVM create'); |
| 1594 | + utility.logWrite('KVM entry create'); |
1569 | 1595 | }
|
1570 | 1596 | }
|
1571 | 1597 |
|
|
1595 | 1621 | });
|
1596 | 1622 | }
|
1597 | 1623 | else {
|
| 1624 | + if (!options.entries && (!options.key || !options.value)) { |
| 1625 | + throw new Error("missing entries or key/value"); |
| 1626 | + } |
1598 | 1627 | // for non-CPS KVM, use a different model to add/update an entry.
|
1599 | 1628 | //
|
1600 | 1629 | // POST :mgmtserver/v1/o/:orgname/e/:env/keyvaluemaps/:mapname
|
|
1607 | 1636 | // }
|
1608 | 1637 | requestOptions.url = urljoin(requestOptions.url, options.kvm);
|
1609 | 1638 | 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 }); |
1611 | 1644 | if (conn.verbosity>0) {
|
1612 | 1645 | utility.logWrite(sprintf('POST %s', requestOptions.url));
|
1613 | 1646 | }
|
|
1653 | 1686 | requestOptions.body = JSON.stringify({
|
1654 | 1687 | encrypted : options.encrypted ? "true" : "false",
|
1655 | 1688 | name : options.name,
|
1656 |
| - entry : options.entries ? uglifyAttrs(options.entries) : [] |
| 1689 | + entry : options.entries ? hashToArrayOfKeyValuePairs(options.entries) : [] |
1657 | 1690 | });
|
1658 | 1691 | if (conn.verbosity>0) {
|
1659 | 1692 | utility.logWrite(sprintf('POST %s', requestOptions.url));
|
|
1662 | 1695 | });
|
1663 | 1696 | };
|
1664 | 1697 |
|
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 |
| - |
1678 | 1698 | Kvm.prototype.put = function(options, cb) {
|
1679 | 1699 | var conn = this.conn;
|
1680 | 1700 | 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); |
1688 | 1705 | });
|
1689 | 1706 | }
|
1690 | 1707 | else {
|
|
1711 | 1728 | };
|
1712 | 1729 |
|
1713 | 1730 |
|
1714 |
| - |
1715 | 1731 | function ApiProxy(conn) {
|
1716 | 1732 | this.conn = conn;
|
1717 | 1733 | }
|
|
1820 | 1836 | return import0(conn, options, 'apiproxy', cb);
|
1821 | 1837 | };
|
1822 | 1838 |
|
1823 |
| - |
1824 | 1839 | // Connection properties:
|
1825 | 1840 | // get
|
1826 | 1841 | // refreshToken
|
|
0 commit comments