diff --git a/.gitignore b/.gitignore index 8946080..6bbc147 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules +remotetests/testconfig.js testdir.zip test.zip -*.tgz \ No newline at end of file +*.tgz diff --git a/README.md b/README.md index 9addd94..d36cb56 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,12 @@ This is a tool for deploying API proxies and Node.js applications to the Apigee You must have an account on Apigee Edge to perform any `apigeetool` functions. These functions include: -* deploying an API proxy to Edge, -* undeploying an API proxy from Edge, +* deploying an API proxy or shared flow to Edge, +* undeploying an API proxy or shared flow from Edge, * deploying Node.js apps to Edge, -* listing deployed API proxies on Edge, -* retrieving deployed proxies and apps from Edge, -* deleting proxy definitions from Edge, and +* listing deployed API proxies or shared flows on Edge, +* retrieving deployed proxies or shared flows from Edge, +* deleting proxy or shared flow definitions from Edge, and * retreiving log messages from Node.js apps deployed to Edge. * create or delete an API product in Edge * create or delete a Developer in Edge @@ -34,6 +34,12 @@ You must have an account on Apigee Edge to perform any `apigeetool` functions. T * create or delete a Cache resource in Edge * create, retrieve or delete a KVM Map in Edge * create, retrieve or delete a KVM Entry in Edge +* attach, detach, or get a FlowHook +* create, get, delete, list Target Servers +* create, get, delete, List Roles +* get, set Role Permisions +* assign, remove, verify Users for a Role +* list all Users in a Role You need to be familiar with basic concepts and features of Apigee Edge such as API proxies, organizations, and environments. @@ -99,37 +105,51 @@ Currently this only affects file uploads in the `deploynodeapp` command. Default # Command reference and examples -* [deploynodeapp](#deploynodeapp) -* [deployhostedtarget](#deployhostedtarget) -* [deployproxy](#deployproxy) -* [undeploy](#undeploy) -* [listdeployments](#listdeployments) -* [fetchproxy](#fetchproxy) -* [getlogs](#getlogs) -* [delete](#delete) -* [deploySharedflow](#deploySharedflow) -* [undeploySharedflow](#undeploySharedflow) -* [listSharedflowDeployments](#listSharedflowDeployments) -* [fetchSharedflow](#fetchSharedflow) -* [deleteSharedflow](#deleteSharedflow) -* [createdeveloper](#createdeveloper) -* [deletedeveloper](#deletedeveloper) -* [createproduct](#createproduct) -* [deleteproduct](#deleteproduct) -* [createapp](#createapp) -* [deleteapp](#deleteapp) +* [addEntryToKVM](#addEntryToKVM) +* [assignUserRole](#assignUserRole) +* [attachFlowHook](#attachFlowHook) * [createappkey](#createappkey) +* [createapp](#createapp) * [createcache](#createcache) +* [createdeveloper](#createdeveloper) +* [createKVMmap](#createKVMmap) +* [createProduct](#createproduct) +* [createRole](#createRole) +* [createTargetServer](#createTargetServer) +* [deleteapp](#deleteapp) * [deletecache](#deletecache) -* [createkvmmap](#createkvmmap) -* [addEntryToKVM](#addEntryToKVM) -* [getkvmmap](#getkvmmap) -* [getKVMentry](#getKVMentry) -* [deletekvmmap](#deletekvmmap) +* [deletedeveloper](#deletedeveloper) * [deleteKVMentry](#deleteKVMentry) -* [createTargetServer](#createTargetServer) +* [deleteKVMmap](#deleteKVMmap) +* [deleteproduct](#deleteproduct) +* [deleteRole](#deleteRole) +* [deleteSharedflow](#deleteSharedflow) * [deleteTargetServer](#deleteTargetServer) - +* [delete](#delete) +* [deployhostedtarget](#deployhostedtarget) +* [deploynodeapp](#deploynodeapp) +* [deployproxy](#deployproxy) +* [deploySharedflow](#deploySharedflow) +* [detachFlowHook](#detachFlowHook) +* [fetchproxy](#fetchproxy) +* [fetchSharedflow](#fetchSharedflow) +* [getFlowHook](#getFlowHook) +* [getKVMentry](#getKVMentry) +* [getKVMmap](#getKVMmap) +* [getlogs](#getlogs) +* [getRole](#getRole) +* [getRolePermissions](#getRolePermissions) +* [getTargetServer](#getTargetServer) +* [listdeployments](#listdeployments) +* [listRoles](#listRoles) +* [listRoleUsers](#listRoleUsers) +* [listSharedflowDeployments](#listSharedflowDeployments) +* [listTargetServers](#listTargetServers) +* [removeUserRole](#removeUserRole) +* [setRolePermissions](#setRolePermissions) +* [undeploySharedflow](#undeploySharedflow) +* [undeploy](#undeploy) +* [verifyUserRole](#verifyUserRole) ## deploynodeapp @@ -574,7 +594,7 @@ for organization name, all of which are required. ## undeploySharedflow -Undeploys a named API proxy or Node.js app deployed on Apigee Edge. +Undeploys a SharedFlow deployed on Apigee Edge. #### Example @@ -707,14 +727,14 @@ When the `--organization` and `--environment` options are present, the operation When the `--organization` and `--api` options are present, the operation will correspond to the API-scoped KVM. -### createkvmmap +### createKVMmap Creates a map in the Apigee KVM with the given name. #### Example Create KVM map named "test-map" - apigeetool createkvmmap -u sdoe@example.com -o sdoe -e test --mapName test-map + apigeetool createKVMmap -u sdoe@example.com -o sdoe -e test --mapName test-map #### Required parameters @@ -777,7 +797,7 @@ for organization name, all of which are required. `--api -n` (optional) The API to target for an API-scoped KVM operation. -### getkvmmap +### getKVMmap Retrieves an entire KVM map with all of its entries, by name. @@ -785,7 +805,7 @@ Retrieves an entire KVM map with all of its entries, by name. Get map named "test-map". - apigeetool getkvmmap -u sdoe@example.com -o sdoe -e test --mapName test-map + apigeetool getKVMmap -u sdoe@example.com -o sdoe -e test --mapName test-map #### Required parameters @@ -842,7 +862,7 @@ for organization name, all of which are required. `--api -n` (optional) The API to target for an API-scoped KVM operation. -### deletekvmmap +### deleteKVMmap Deletes an entire map from the Apigee KVM along with all of its entries. @@ -850,7 +870,7 @@ Deletes an entire map from the Apigee KVM along with all of its entries. Delete map named "test-map". - apigeetool deletekvmmap -u sdoe@example.com -o sdoe -e test --mapName test-map + apigeetool deleteKVMmap -u sdoe@example.com -o sdoe -e test --mapName test-map #### Required parameters @@ -881,7 +901,7 @@ Deletes a single entry by name from an Apigee KVM map. Delete entry named "test1" from the map named "test-map". - apigeetool deletekvmmap -u sdoe@example.com -o sdoe -e test --mapName test-map --entryName test1 + apigeetool deleteKVMmmap -u sdoe@example.com -o sdoe -e test --mapName test-map --entryName test1 #### Required parameters @@ -984,6 +1004,347 @@ the "-u" and "-p" parameters for username and password or preferably -N for .net `--environment -e` (required) The environment to target. `--targetServerName` (required) The name of the Target Server to be deleted. +### getTargetServer + +Get details for a Target Server with the given name. + +#### Example +Get Target Server named "test-target". + + apigeetool getTargetServer -N -o $ORG -e $ENV --targetServerName test-target + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--environment -e` (required) The environment to target. +`--targetServerName` (required) The name of the Target Server to be deleted. + +### listTargetServers + +List Target Servers in a given environment. + +#### Example +List Target Servers. + + apigeetool listTargetServers -N -o $ORG -e $ENV + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--environment -e` (required) The environment to target. + +## FlowHook Operations + +Operations on the pre-defined FlowHook names: + +* PreProxyFlowHook +* PreTargetFlowHook +* PostTargetFlowHook +* PostProxyFlowHook + +### attachFlowHook + +Attach a deployed SharedFlow to one of the [named FlowHooks](#FlowHook Operations). + +#### Example +Attach SharedFlow "GetLogValues" to "PreProxyFlowHook". + + apigeetool attachFlowHook -N -o $ORG -e $ENV --flowHookName PreProxyFlowHook --sharedFlowName GetLogValues + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--environment -e` (required) The environment to target. +`--flowHookName` (required) The pre-defined name of the FlowHook. +`--sharedFlowName` (required) The name of a deployed SharedFlow. + +### detachFlowHook + +Detach a SharedFlow from one of the [named FlowHooks](#FlowHook Operations). + +#### Example +Detach "PreProxyFlowHook". + + apigeetool detachFlowHook -N -o $ORG -e $ENV --flowHookName PreProxyFlowHook + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--environment -e` (required) The environment to target. +`--flowHookName` (required) The pre-defined name of the FlowHook. + +### getFlowHook + +Get the SharedFlow currently attached to one of the [named FlowHooks](#FlowHook Operations). + +#### Example +Detach "PreProxyFlowHook". + + apigeetool getFlowHook -N -o $ORG -e $ENV --flowHookName PreProxyFlowHook + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--environment -e` (required) The environment to target. +`--flowHookName` (required) The pre-defined name of the FlowHook. + +## Roles and Permissions Operations + +Operations on Roles, Permissions and User assignment. The general flow is: + +* Create a role +* Assign Permissions to the Role +* Assign the Role to a User + +### createRole + +Create a role. + +#### Example +Create role "AllowGetUserRoles". + + apigeetool createRole -N -o $ORG --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--roleName` (required) The name for the role. + +### getRole + +Get a role. + +#### Example +Get role "AllowGetUserRoles". + + apigeetool getRole -N -o $ORG --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--roleName` (required) The name for the role. + +### deleteRole + +Delete a role. + +#### Example +Delete role "AllowGetUserRoles". + + apigeetool deleteRole -N -o $ORG --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--roleName` (required) The name for the role. + +### listRoles + +List roles. + +#### Example +List roles. + + apigeetool listRoles -N -o $ORG + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. + +### setRolePermissions + +Set Role Permissions for a Role. + +#### Example +Set Permissions on Role "AllowGetUserRoles" to allow access to list Roles. + + apigeetool setRolePermissions -N -o $ORG --roleName AllowGetUserRoles --permissions '[{"path":"/userroles","permissions":["get"]}]' + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--roleName` (required) The name for the role. +`--permissions` Permissions array for path and verbs. + +### getRolePermissions + +Get Role Permissions for a Role. + +#### Example +Get Permissions on Role "AllowGetUserRoles". + + apigeetool getRolePermissions -N -o $ORG --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--roleName` (required) The name for the role. + +### assignUserRole + +Assign existing User to a Role. NOTE: User must already exist in Edge. + +#### Example +Assign "somedeveloper@any.com" to Role "AllowGetUserRoles". + + apigeetool assignUserRole -N -o $ORG --email "somedeveloper@any.com" --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--email` (required) Email for an existing User in Edge. +`--roleName` (required) The name for the role. + +### removeUserRole + +Remove existing User from a Role. + +#### Example +Remove "somedeveloper@any.com" from Role "AllowGetUserRoles". + + apigeetool removeUserRole -N -o $ORG --email "somedeveloper@any.com" --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--email` (required) Email for an existing User in Edge. +`--roleName` (required) The name for the role. + +### verifyUserRole + +Verify User assigned to a Role. + +#### Example +Verify "somedeveloper@any.com" assigned to Role "AllowGetUserRoles". + + apigeetool verifyUserRole -N -o $ORG --email "somedeveloper@any.com" --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--email` (required) Email for an existing User in Edge. +`--roleName` (required) The name for the role. + +### listRoleUsers + +Get Users assigned to a Role. + +#### Example +List Users assigned to Role "AllowGetUserRoles". + + apigeetool listRoleUsers -N -o $ORG --roleName AllowGetUserRoles + +#### Required parameters + +The following parameters are required. However, if any are left unspecified +on the command line, and if apigeetool is running in an interactive shell, +then apigeetool will prompt for them. + +See [Common Parameters](#commonargs) for a list of additional parameters, including +the "-u" and "-p" parameters for username and password or preferably -N for .netrc usage. + +`--organization -o` (required) The organization to target. +`--email` (required) Email for an existing User in Edge. +`--roleName` (required) The name for the role. + # SDK Reference You could use apigeetool as an SDK to orchestrate tasks that you want to perform with Edge, for eg, deploying an api proxy or running tests etc. @@ -1052,7 +1413,7 @@ Delete a Developer in Edge //developer delete failed }) ; -## Create Product +## Create Product Creates a new API Product in Edge diff --git a/lib/cli.js b/lib/cli.js index 4c48857..c86675b 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -64,11 +64,13 @@ function runCommand() { } process.exit(6); } + if (!opts.json && commandModule.format) { console.log(commandModule.format(result)); } else { console.log(JSON.stringify(result)); } + process.exit(0); }); } diff --git a/lib/commands/assignUserRole.js b/lib/commands/assignUserRole.js new file mode 100644 index 0000000..014aced --- /dev/null +++ b/lib/commands/assignUserRole.js @@ -0,0 +1,41 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + }, + email: { + name: 'Developer email', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('assignUserRole: %j', opts); + } + + var formData = util.format('id=%s', encodeURIComponent(opts.email)); + var uri = util.format('%s/v1/o/%s/userroles/%s/users', opts.baseuri, opts.organization, opts.roleName); + var requestOptions = { + uri: uri, + method:'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: formData, + json:true + } + command_utils.run('assignUserRole', opts,requestOptions,cb) +}; diff --git a/lib/commands/attachFlowHook.js b/lib/commands/attachFlowHook.js new file mode 100644 index 0000000..3d113d7 --- /dev/null +++ b/lib/commands/attachFlowHook.js @@ -0,0 +1,52 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + environment: { + name: 'Environment', + shortOption: 'e', + required: true, + prompt: true + }, + flowHookName: { + name: 'One of: PreProxyFlowHook\n PreTargetFlowHook\n PostTargetFlowHook\n PostProxyFlowHook', + required: true, + prompt: true + }, + sharedFlowName: { + name: 'Shared Flow name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('attachFlowHook: %j', opts); + } + var payload = { + "continueOnError" : true, + "sharedFlow" : opts.sharedFlowName + } + if(opts.targetSSL){ + payload.sSLInfo = {enabled: true} + } + + var uri = util.format('%s/v1/o/%s/e/%s/flowhooks/%s', opts.baseuri, opts.organization, opts.environment, opts.flowHookName); + var requestOpts = { + uri: uri, + method:'POST', + body: payload, + json:true + } + command_utils.run('attachFlowHook',opts, requestOpts, cb) +}; diff --git a/lib/commands/command-utils.js b/lib/commands/command-utils.js index e999af4..c8ef829 100644 --- a/lib/commands/command-utils.js +++ b/lib/commands/command-utils.js @@ -11,7 +11,7 @@ module.exports.run = function(command, opts, requestOpts,done){ var jsonBody = body if(err){ done(err) - }else if (res.statusCode == 200 || res.statusCode == 201) { + }else if (res.statusCode == 200 || res.statusCode == 201 || res.statusCode == 204) { if (opts.verbose) { console.log(command + ' successful'); } diff --git a/lib/commands/commands.js b/lib/commands/commands.js index 35dd8b7..26445fb 100644 --- a/lib/commands/commands.js +++ b/lib/commands/commands.js @@ -7,6 +7,24 @@ var Table = require('cli-table'); var options = require('../options'); var Commands = { + attachFlowHook: { + description: 'Attach a Shared Flow to a Flow Hook', + load: function() { + return require('./attachFlowHook'); + } + }, + detachFlowHook: { + description: 'Detach a Shared Flow from a Flow Hook', + load: function() { + return require('./detachFlowHook'); + } + }, + getFlowHook: { + description: 'Get the Shared Flow attached to a Flow Hook', + load: function() { + return require('./getFlowHook'); + } + }, createTargetServer: { description: 'Create a Target Server', load: function() { @@ -19,6 +37,18 @@ var Commands = { return require('./delete-TargetServer'); } }, + getTargetServer: { + description: 'Get a Target Server', + load: function() { + return require('./get-TargetServer'); + } + }, + listTargetServers: { + description: 'List Target Servers', + load: function() { + return require('./list-TargetServers'); + } + }, createkvmmap: { description: 'Create a KVM map', load: function() { @@ -186,6 +216,66 @@ var Commands = { load: function () { return require('./deployExistingRevision'); } + }, + listRoles: { + description: "List roles in an organziation", + load: function () { + return require('./listRoles'); + } + }, + createRole: { + description: "Create a userrole in an organziation", + load: function () { + return require('./createRole'); + } + }, + getRole: { + description: "Get a userrole in an organziation", + load: function () { + return require('./getRole'); + } + }, + deleteRole: { + description: "Delete a userrole in an organziation", + load: function () { + return require('./deleteRole'); + } + }, + getRolePermissions: { + description: "Get resource permissions for a role", + load: function () { + return require('./getRolePermissions'); + } + }, + setRolePermissions: { + description: "Set resource permissions for a role", + load: function () { + return require('./setRolePermissions'); + } + }, + assignUserRole: { + description: "Assign user to a role", + load: function () { + return require('./assignUserRole'); + } + }, + removeUserRole: { + description: "Remove user from a role", + load: function () { + return require('./removeUserRole'); + } + }, + verifyUserRole: { + description: "Verify a user is in a role", + load: function () { + return require('./verifyUserRole'); + } + }, + listRoleUsers: { + description: "List users in role", + load: function () { + return require('./listRoleUsers'); + } } }; diff --git a/lib/commands/create-TargetServer.js b/lib/commands/create-TargetServer.js index cfa8c6f..9721306 100644 --- a/lib/commands/create-TargetServer.js +++ b/lib/commands/create-TargetServer.js @@ -12,22 +12,26 @@ var descriptor = defaults.defaultDescriptor({ environment: { name: 'Environment', shortOption: 'e', - required: true + required: true, + prompt: true }, targetServerName: { name: 'Target Server Name', - required: true + required: true, + prompt: true }, targetHost: { name: 'Target Host', - required: true + required: true, + prompt: true }, targetEnabled: { name: 'Target Enabled' }, targetPort: { name: 'Target Port', - required: true + required: true, + prompt: true }, targetSSL:{ name: 'SSL Info' diff --git a/lib/commands/createRole.js b/lib/commands/createRole.js new file mode 100644 index 0000000..fe56346 --- /dev/null +++ b/lib/commands/createRole.js @@ -0,0 +1,39 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('createRole: %j', opts); + } + var payload = { + "role" : [ + {"name" : opts.roleName} + ] + }; + + var uri = util.format('%s/v1/o/%s/userroles', opts.baseuri, opts.organization); + var requestOptions = { + uri: uri, + method:'POST', + body: payload, + json:true + } + command_utils.run('createRole', opts,requestOptions,cb) +}; diff --git a/lib/commands/delete-TargetServer.js b/lib/commands/delete-TargetServer.js index 35751ea..a2a4fa2 100644 --- a/lib/commands/delete-TargetServer.js +++ b/lib/commands/delete-TargetServer.js @@ -12,11 +12,13 @@ var descriptor = defaults.defaultDescriptor({ environment: { name: 'Environment', shortOption: 'e', - required: true + required: true, + prompt: true }, targetServerName: { name: 'Target Server Name', - required: true + required: true, + prompt: true } }); @@ -30,7 +32,7 @@ module.exports.run = function(opts, cb) { var requestOptions = { uri: uri, method:'DELETE', - json:false + json:true } command_utils.run('deleteTargetServer', opts,requestOptions,cb) }; diff --git a/lib/commands/deleteRole.js b/lib/commands/deleteRole.js new file mode 100644 index 0000000..99623ba --- /dev/null +++ b/lib/commands/deleteRole.js @@ -0,0 +1,32 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('deleteRole: %j', opts); + } + var uri = util.format('%s/v1/o/%s/userroles/%s', opts.baseuri, opts.organization, opts.roleName); + var requestOptions = { + uri: uri, + method:'DELETE', + json:true + } + command_utils.run('deleteRole', opts,requestOptions,cb) +}; diff --git a/lib/commands/detachFlowHook.js b/lib/commands/detachFlowHook.js new file mode 100644 index 0000000..4de2d49 --- /dev/null +++ b/lib/commands/detachFlowHook.js @@ -0,0 +1,42 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + environment: { + name: 'Environment', + shortOption: 'e', + required: true, + prompt: true + }, + flowHookName: { + name: 'One of: PreProxyFlowHook\n PreTargetFlowHook\n PostTargetFlowHook\n PostProxyFlowHook', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('detachFlowHook: %j', opts); + } + if(opts.targetSSL){ + payload.sSLInfo = {enabled: true} + } + + var uri = util.format('%s/v1/o/%s/e/%s/flowhooks/%s', opts.baseuri, opts.organization, opts.environment, opts.flowHookName); + var requestOpts = { + uri: uri, + method:'DELETE', + json:true + } + command_utils.run('detachFlowHook',opts, requestOpts, cb) +}; diff --git a/lib/commands/fetchproxy.js b/lib/commands/fetchproxy.js index 65b996a..ec982d7 100644 --- a/lib/commands/fetchproxy.js +++ b/lib/commands/fetchproxy.js @@ -69,7 +69,7 @@ module.exports.run = function(opts, cb) { console.log( "Error text: " + err ); } else { - console.log( 'Save file: ' + f ); + if (opts.verbose) { console.log('Save file: ' + f); } } cb(err); }); diff --git a/lib/commands/fetchsharedflow.js b/lib/commands/fetchsharedflow.js index be7746f..e6147f1 100644 --- a/lib/commands/fetchsharedflow.js +++ b/lib/commands/fetchsharedflow.js @@ -69,11 +69,12 @@ module.exports.run = function (opts, cb) { if (err) { console.log("Failed to write file: " + f); console.log("Error text: " + err); + cb(err); } else { - console.log('Save file: ' + f); + if (opts.verbose) { console.log('Save file: ' + f); } + cb(undefined, res.statusCode + " - wrote: " + f); } - cb(err); }); } } diff --git a/lib/commands/get-TargetServer.js b/lib/commands/get-TargetServer.js new file mode 100644 index 0000000..03ddd63 --- /dev/null +++ b/lib/commands/get-TargetServer.js @@ -0,0 +1,38 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + environment: { + name: 'Environment', + shortOption: 'e', + required: true, + prompt: true + }, + targetServerName: { + name: 'Target Server Name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('getTargetServer: %j', opts); + } + var uri = util.format('%s/v1/o/%s/e/%s/targetservers/%s', opts.baseuri, opts.organization, opts.environment,opts.targetServerName); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('getTargetServer', opts,requestOptions,cb) +}; diff --git a/lib/commands/getFlowHook.js b/lib/commands/getFlowHook.js new file mode 100644 index 0000000..4a7e2c0 --- /dev/null +++ b/lib/commands/getFlowHook.js @@ -0,0 +1,42 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + environment: { + name: 'Environment', + shortOption: 'e', + required: true, + prompt: true + }, + flowHookName: { + name: 'One of: PreProxyFlowHook\n PreTargetFlowHook\n PostTargetFlowHook\n PostProxyFlowHook', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('getFlowHook: %j', opts); + } + if(opts.targetSSL){ + payload.sSLInfo = {enabled: true} + } + + var uri = util.format('%s/v1/o/%s/e/%s/flowhooks/%s', opts.baseuri, opts.organization, opts.environment, opts.flowHookName); + var requestOpts = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('getFlowHook',opts, requestOpts, cb) +}; diff --git a/lib/commands/getRole.js b/lib/commands/getRole.js new file mode 100644 index 0000000..8c9b4cc --- /dev/null +++ b/lib/commands/getRole.js @@ -0,0 +1,32 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('getRole: %j', opts); + } + var uri = util.format('%s/v1/o/%s/userroles/%s', opts.baseuri, opts.organization, opts.roleName); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('getRole', opts,requestOptions,cb) +}; diff --git a/lib/commands/getRolePermissions.js b/lib/commands/getRolePermissions.js new file mode 100644 index 0000000..86cdb2c --- /dev/null +++ b/lib/commands/getRolePermissions.js @@ -0,0 +1,32 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('getRolePermissions: %j', opts); + } + var uri = util.format('%s/v1/o/%s/userroles/%s/permissions', opts.baseuri, opts.organization, opts.roleName); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('getRolePermissions', opts,requestOptions,cb) +}; diff --git a/lib/commands/list-TargetServers.js b/lib/commands/list-TargetServers.js new file mode 100644 index 0000000..32a72e5 --- /dev/null +++ b/lib/commands/list-TargetServers.js @@ -0,0 +1,33 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + environment: { + name: 'Environment', + shortOption: 'e', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('listTargetServers: %j', opts); + } + var uri = util.format('%s/v1/o/%s/e/%s/targetservers', opts.baseuri, opts.organization, opts.environment); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('listTargetServers', opts,requestOptions,cb) +}; diff --git a/lib/commands/listRoleUsers.js b/lib/commands/listRoleUsers.js new file mode 100644 index 0000000..10e760b --- /dev/null +++ b/lib/commands/listRoleUsers.js @@ -0,0 +1,33 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('getRoleUsers: %j', opts); + } + + var uri = util.format('%s/v1/o/%s/userroles/%s/users', opts.baseuri, opts.organization, opts.roleName); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('getRoleUsers', opts,requestOptions,cb) +}; diff --git a/lib/commands/listRoles.js b/lib/commands/listRoles.js new file mode 100644 index 0000000..df4e062 --- /dev/null +++ b/lib/commands/listRoles.js @@ -0,0 +1,27 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('listRoles: %j', opts); + } + var uri = util.format('%s/v1/o/%s/userroles', opts.baseuri, opts.organization); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('listRoles', opts,requestOptions,cb) +}; diff --git a/lib/commands/listsharedflowdeployments.js b/lib/commands/listsharedflowdeployments.js index 4ed009e..67c6234 100644 --- a/lib/commands/listsharedflowdeployments.js +++ b/lib/commands/listsharedflowdeployments.js @@ -17,10 +17,6 @@ var descriptor = defaults.defaultDescriptor({ name: 'Environment', shortOption: 'e' }, - revision: { - name: 'Revision', - shortOption: 'r' - }, long: { name: 'Long', shortOption: 'l', diff --git a/lib/commands/removeUserRole.js b/lib/commands/removeUserRole.js new file mode 100644 index 0000000..2fcff92 --- /dev/null +++ b/lib/commands/removeUserRole.js @@ -0,0 +1,38 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + }, + email: { + name: 'Developer email', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('removeUserRole: %j', opts); + } + + var uri = util.format('%s/v1/o/%s/userroles/%s/users/%s', opts.baseuri, opts.organization, opts.roleName, opts.email ); + var requestOptions = { + uri: uri, + method:'DELETE', + json:true + } + command_utils.run('removeUserRole', opts,requestOptions,cb) +}; diff --git a/lib/commands/setRolePermissions.js b/lib/commands/setRolePermissions.js new file mode 100644 index 0000000..79f47aa --- /dev/null +++ b/lib/commands/setRolePermissions.js @@ -0,0 +1,45 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + }, + permissions: { + name: 'Permissions array for path and verbs', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('setRolePermissions: %j', opts); + } + + if( opts.permissions ) { + var permissions = JSON.parse(opts.permissions); + } + var payload = { + "resourcePermission" : permissions + }; + var uri = util.format('%s/v1/o/%s/userroles/%s/resourcepermissions', opts.baseuri, opts.organization, opts.roleName); + var requestOptions = { + uri: uri, + method:'POST', + body: payload, + json:true + } + command_utils.run('setRolePermissions', opts,requestOptions,cb) +}; diff --git a/lib/commands/verifyUserRole.js b/lib/commands/verifyUserRole.js new file mode 100644 index 0000000..a960f66 --- /dev/null +++ b/lib/commands/verifyUserRole.js @@ -0,0 +1,38 @@ +/* jshint node: true */ +'use strict'; + +var util = require('util'); +var _ = require('underscore'); + +var defaults = require('../defaults'); +var options = require('../options'); +var command_utils = require('./command-utils') + +var descriptor = defaults.defaultDescriptor({ + roleName: { + name: 'Role Name', + required: true, + prompt: true + }, + email: { + name: 'EMail for the user', + required: true, + prompt: true + } +}); + +module.exports.descriptor = descriptor; + +module.exports.run = function(opts, cb) { + if (opts.debug) { + console.log('verifyUserRole: %j', opts); + } + + var uri = util.format('%s/v1/o/%s/userroles/%s/users/%s', opts.baseuri, opts.organization, opts.roleName, opts.email ); + var requestOptions = { + uri: uri, + method:'GET', + json:true + } + command_utils.run('verifyUserRole', opts,requestOptions,cb) +}; diff --git a/lib/defaults.js b/lib/defaults.js index 922fc68..1ed0ec2 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -14,17 +14,20 @@ var DefaultDescriptor = { help: { name: 'Help', shortOption: 'h', + scope: 'default', toggle: true }, username: { name: 'Username', shortOption: 'u', + scope: 'default', required: true, prompt: true }, password: { name: 'Password', shortOption: 'p', + scope: 'default', required: true, prompt: true, secure: true @@ -32,6 +35,7 @@ var DefaultDescriptor = { header: { name: 'Header', shortOption: 'H', + scope: 'default', required: false, prompt: false, multiple: true @@ -39,32 +43,38 @@ var DefaultDescriptor = { token: { name: 'Token', shortOption: 't', - required: true, + scope: 'default', + required: false, prompt: false }, netrc: { name: 'netrc', shortOption: 'N', + scope: 'default', required: false, toggle: true }, organization: { name: 'Organization', shortOption: 'o', + scope: 'default', required: true }, baseuri: { name: 'Base URI', - shortOption: 'L' + shortOption: 'L', + scope: 'default' }, debug: { name: 'Debug', shortOption: 'D', - toggle: true + scope: 'default', + toggle: true, }, verbose: { name: 'Verbose', shortOption: 'V', + scope: 'default', toggle: true }, json: { @@ -74,24 +84,29 @@ var DefaultDescriptor = { }, cafile: { name: 'CA file', - shortOption: 'c' + shortOption: 'c', + scope: 'default' }, keyfile: { name: 'Key file', - shortOption: 'K' + shortOption: 'K', + scope: 'default' }, certfile: { name: 'Cert file', - shortOption: 'C' + shortOption: 'C', + scope: 'default' }, insecure: { name: 'insecure', shortOption: 'k', + scope: 'default', toggle: true }, asynclimit: { name: 'Async limit', shortOption: 'a', + scope: 'default', type: 'int' } }; diff --git a/lib/main.js b/lib/main.js index 82d89b6..f81dffb 100644 --- a/lib/main.js +++ b/lib/main.js @@ -103,18 +103,28 @@ ApigeeTool.createTargetServer = function(opts, cb) { runCommand(cmd, opts, cb); }; +ApigeeTool.getTargetServer = function (opts, cb) { + var cmd = require('./commands/get-TargetServer.js'); + runCommand(cmd, opts, cb); +}; + ApigeeTool.deleteTargetServer = function(opts,cb){ var cmd = require('./commands/delete-TargetServer'); runCommand(cmd, opts, cb); }; +ApigeeTool.listTargetServers = function(opts, cb) { + var cmd = require('./commands/list-TargetServers'); + runCommand(cmd, opts, cb); +}; + ApigeeTool.createKVM = function(opts, cb) { var cmd = require('./commands/create-KVM'); runCommand(cmd, opts, cb); }; -ApigeeTool.getkvmmap = function(opts, cb) { - var cmd = require('./commands/getkvmmap'); +ApigeeTool.getKVMmap = function(opts, cb) { + var cmd = require('./commands/getKVMmap'); runCommand(cmd, opts, cb); }; @@ -163,6 +173,62 @@ ApigeeTool.deleteSharedflow = function (opts, cb) { runCommand(cmd, opts, cb); }; +ApigeeTool.attachFlowHook = function (opts, cb) { + var cmd = require('./commands/attachFlowHook.js'); + runCommand(cmd, opts, cb); +}; + +ApigeeTool.detachFlowHook = function (opts, cb) { + var cmd = require('./commands/detachFlowHook.js'); + runCommand(cmd, opts, cb); +}; + +ApigeeTool.getFlowHook = function (opts, cb) { + var cmd = require('./commands/getFlowHook.js'); + runCommand(cmd, opts, cb); +}; + +ApigeeTool.listRoles = function (opts, cb) { + var cmd = require('./commands/listRoles.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.getRole = function (opts, cb) { + var cmd = require('./commands/getRole.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.createRole = function (opts, cb) { + var cmd = require('./commands/createRole.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.deleteRole = function (opts, cb) { + var cmd = require('./commands/deleteRole.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.getRolePermissions = function (opts, cb) { + var cmd = require('./commands/getRolePermissions.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.setRolePermissions = function (opts, cb) { + var cmd = require('./commands/setRolePermissions.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.assignUserRole = function (opts, cb) { + var cmd = require('./commands/assignUserRole.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.removeUserRole = function (opts, cb) { + var cmd = require('./commands/removeUserRole.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.verifyUserRole = function (opts, cb) { + var cmd = require('./commands/verifyUserRole.js'); + runCommand(cmd, opts, cb); +}; +ApigeeTool.listRoleUsers = function (opts, cb) { + var cmd = require('./commands/listRoleUsers.js'); + runCommand(cmd, opts, cb); +}; + function runCommand(cmd, opts, cb) { options.validate(opts, cmd.descriptor, function(err) { if (err) { diff --git a/lib/options.js b/lib/options.js index 4da1735..8dc0da4 100644 --- a/lib/options.js +++ b/lib/options.js @@ -47,8 +47,9 @@ module.exports.getopts = function(argv, start, descriptor) { } else { badArg(argv[i]); } + } else { + badArg(argv[i]); } - } else { var shortArg = /^-([A-Za-z])/.exec(argv[i]); @@ -124,27 +125,33 @@ module.exports.validate = function(opts, descriptor, cb) { function checkProperty(opts, descriptor, propName, done) { var desc = descriptor[propName]; + // console.log( "DEBUG " + desc.required + " " + opts[propName] + " " + opts.prompt + " " + desc.prompt + " " + propName); + // console.log( "DEBUG OPTs" + JSON.stringify(opts) ); if (desc === null || desc === undefined) { done(new Error(util.format('Invalid property %s', propName))); return; } - if (desc.required && !opts[propName] && (!opts.prompt && desc.prompt)) { - if (opts.interactive) { - var pn = (desc.name ? desc.name : propName); - prompt(pn, desc.secure, function(err, val) { - if (err) { - done(err); - } else { - if (desc.secure === true) { - opts[propName] = new SecureValue(val); + if (desc.required && !opts[propName]) { + if (desc.prompt && !opts.prompt ) { + if (opts.interactive) { + var pn = (desc.name ? desc.name : propName); + prompt(pn, desc.secure, function(err, val) { + if (err) { + done(err); } else { - opts[propName] = val; + if (desc.secure === true) { + opts[propName] = new SecureValue(val); + } else { + opts[propName] = val; + } + done(); } - done(); - } - }); + }); + } else { + done(new Error(util.format('Missing required option "%s"', propName))); + } } else { - done(new Error(util.format('Missing required option "%s"', propName))); + done(new Error(util.format('Missing required option with no prompt "%s"', propName))); } } else { if (opts[propName] && (desc.secure === true)) { @@ -207,9 +214,13 @@ module.exports.getHelp = function(descriptor) { var tab = new Table(TableFormat); _.each(_.sortBy(_.pairs(descriptor)), function(d) { - tab.push([d[0], + tab.push([ + d[0], (d[1].shortOption ? '-' + d[1].shortOption : ''), - ((d[1].required && !d[1].prompt) ? '(required)': '(optional)')]); + ((d[1].required && !d[1].prompt) ? '(required)': '(optional)'), + ((d[1].name ? d[1].name : 'undefined')), + ((d[1].scope == 'default') ? '' : '(command specific)') + ]); }); return tab.toString(); diff --git a/lib/promisesdk.js b/lib/promisesdk.js index 2d9be0c..e80fe43 100644 --- a/lib/promisesdk.js +++ b/lib/promisesdk.js @@ -175,6 +175,19 @@ ApigeeTool.deleteTargetServer = function(opts){ return cb.promise } +ApigeeTool.getTargetServer = function(opts) { + var cb = q.defer() + var cmd = require('./commands/get-TargetServer'); + runCommand(cmd, opts, cb); + return cb.promise +}; + +ApigeeTool.listTargetServers = function(opts) { + var cb = q.defer() + var cmd = require('./commands/list-TargetServers'); + runCommand(cmd, opts, cb); + return cb.promise +}; ApigeeTool.createKVM = function(opts) { var cb = q.defer() @@ -197,9 +210,9 @@ ApigeeTool.getKVMentry = function(opts) { return cb.promise } -ApigeeTool.getkvmmap = function(opts) { +ApigeeTool.getKVMmap = function(opts) { var cb = q.defer() - var cmd = require('./commands/getkvmmap'); + var cmd = require('./commands/getKVMmap'); runCommand(cmd, opts, cb); return cb.promise } @@ -232,6 +245,88 @@ ApigeeTool.createAppKey = function(opts) { return cb.promise } +ApigeeTool.attachFlowHook = function(opts) { + var cb = q.defer() + var cmd = require('./commands/attachFlowHook'); + runCommand(cmd, opts, cb); + return cb.promise +} + +ApigeeTool.detachFlowHook = function(opts) { + var cb = q.defer() + var cmd = require('./commands/detachFlowHook'); + runCommand(cmd, opts, cb); + return cb.promise +} + +ApigeeTool.getFlowHook = function(opts) { + var cb = q.defer() + var cmd = require('./commands/getFlowHook'); + runCommand(cmd, opts, cb); + return cb.promise +} + +ApigeeTool.listRoles = function(opts) { + var cb = q.defer() + var cmd = require('./commands/listRoles'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.createRole = function(opts) { + var cb = q.defer() + var cmd = require('./commands/createRole'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.getRole = function(opts) { + var cb = q.defer() + var cmd = require('./commands/getRole'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.deleteRole = function(opts) { + var cb = q.defer() + var cmd = require('./commands/deleteRole'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.setRolePermissions = function(opts) { + var cb = q.defer() + var cmd = require('./commands/setRolePermissions'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.getRolePermissions = function(opts) { + var cb = q.defer() + var cmd = require('./commands/getRolePermissions'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.assignUserRole = function(opts) { + var cb = q.defer() + var cmd = require('./commands/assignUserRole'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.removeUserRole = function(opts) { + var cb = q.defer() + var cmd = require('./commands/removeUserRole'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.verifyUserRole = function(opts) { + var cb = q.defer() + var cmd = require('./commands/verifyUserRole'); + runCommand(cmd, opts, cb); + return cb.promise +} +ApigeeTool.listRoleUsers = function(opts) { + var cb = q.defer() + var cmd = require('./commands/listRoleUsers'); + runCommand(cmd, opts, cb); + return cb.promise +} + function runCommand(cmd, opts, cb) { options.validate(opts, cmd.descriptor, function(err) { if (err) { diff --git a/package-lock.json b/package-lock.json index 04f851b..374be77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "apigeetool", - "version": "0.9.0", + "version": "0.13.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -487,9 +487,9 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "fstream": { - "version": "1.0.11", - "resolved": "http://repo.nwie.net/nexus/repository/npm/fstream/-/fstream-1.0.11.tgz", - "integrity": "sha1-XB+x8RdHcRTwYyoOtLcbPLD9MXE=", + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fstream/-/fstream-1.0.12.tgz", + "integrity": "sha512-WvJ193OHa0GHPEL+AycEJgxvBEwyfRkN1vhjca23OaPVMCaLCXTd5qAu82AjTcgP1UJmytkOKb63Ypde7raDIg==", "requires": { "graceful-fs": "^4.1.2", "inherits": "~2.0.0", @@ -509,6 +509,7 @@ "version": "7.1.2", "resolved": "http://repo.nwie.net/nexus/repository/npm/glob/-/glob-7.1.2.tgz", "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -616,6 +617,38 @@ "resolved": "http://repo.nwie.net/nexus/repository/npm/isstream/-/isstream-0.1.2.tgz", "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" }, + "jasmine": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.5.0.tgz", + "integrity": "sha512-DYypSryORqzsGoMazemIHUfMkXM7I7easFaxAvNM3Mr6Xz3Fy36TupTrAOxZWN8MVKEU5xECv22J4tUQf3uBzQ==", + "dev": true, + "requires": { + "glob": "^7.1.4", + "jasmine-core": "~3.5.0" + }, + "dependencies": { + "glob": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", + "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } + } + }, + "jasmine-core": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.5.0.tgz", + "integrity": "sha512-nCeAiw37MIMA9w9IXso7bRaLl+c/ef3wnxsoSAlYrzS+Ot0zTG6nU8G/cIfGkqpkjX2wNaIW9RFG0TwIFnG6bA==", + "dev": true + }, "jsbn": { "version": "0.1.1", "resolved": "http://repo.nwie.net/nexus/repository/npm/jsbn/-/jsbn-0.1.1.tgz", @@ -1031,11 +1064,26 @@ } }, "rimraf": { - "version": "2.6.2", - "resolved": "http://repo.nwie.net/nexus/repository/npm/rimraf/-/rimraf-2.6.2.tgz", - "integrity": "sha512-lreewLK/BlghmxtfH36YYVg1i8IAce4TI7oao75I1g245+6BctqTVQiBP3YUJ9C6DQOXJmkYR9X9fCLtCOJc5w==", + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", "requires": { - "glob": "^7.0.5" + "glob": "^7.1.3" + }, + "dependencies": { + "glob": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", + "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } } }, "safe-buffer": { diff --git a/package.json b/package.json index ca443e9..32438b9 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "apigeetool", - "version": "0.12.0", + "version": "0.13.0", "description": "A CLI for Apigee Edge", "main": "lib/main.js", "bin": { "apigeetool": "./lib/cli.js" }, "scripts": { - "test": "mocha -R spec", + "test": "jasmine test/testoptions.js", "remotetest": "mocha -R spec remotetests" }, "keywords": [ @@ -38,6 +38,7 @@ "devDependencies": { "connect": "^3.4.0", "express": "^4.13.3", + "jasmine": "^3.5.0", "mocha": "^5.2.0" }, "repository": { diff --git a/remotetests/remotetest.js b/remotetests/remotetest.js index 9f735c5..d639913 100644 --- a/remotetests/remotetest.js +++ b/remotetests/remotetest.js @@ -24,28 +24,21 @@ var TARGET_SERVER_NAME = 'apigee-cli-test-servername'; var MAP_NAME = 'apigee-cli-test-kvm'; var MAP_NAME_ENCRYPTED = 'apigee-cli-test-kvm-encrypted'; var SHARED_FLOW_NAME = 'apigee-cli-sf'; +var ROLE_NAME = 'apigee-cli-test-role'; var verbose = false; +var deployedRevision; +var deployedUri; +var prevSharedFlow; -describe('Remote Tests', function() { - this.timeout(REASONABLE_TIMEOUT); +// Run all using: mocha remotetests +// Run all "describe" tests using: mocha remotetests --grep "SharedFlows and FlowHooks" +// Run one "it" test using: mocha remotetests --grep "fetchSharedFlow" +// To see tests use 'grep " it" remotetest.j' - var deployedRevision; - var deployedUri; +describe('Remote Tests', function() { // it + this.timeout(REASONABLE_TIMEOUT); - after(function(done) { - // cleanup encrypted kvm - var opts = baseOpts(); - opts.mapName = MAP_NAME_ENCRYPTED; - opts.environment = config.environment; - apigeetool.deleteKVM(opts,function(err,result) { - if (verbose) { - console.log('Delete KVM result = %j', result); - } - done(); - }); - }); - - it('Deploy Apigee Proxy with Promise SDK', function(done) { + it('Deploy Apigee Proxy', function(done) { var opts = baseOpts(); opts.api = APIGEE_PROXY_NAME; opts.directory = path.join(__dirname, '../test/fixtures/employees'); @@ -73,149 +66,149 @@ describe('Remote Tests', function() { done(err); }) }); + + it('Create Product', function(done){ + var opts = baseOpts() ; + var displayName = 'custom name'; + opts.productName = APIGEE_PRODUCT_NAME; + opts.productDesc = 'abc123'; + opts.displayName = displayName; + opts.proxies = APIGEE_PROXY_NAME; + opts.quota = '1'; + opts.quotaInterval = '1'; + opts.quotaTimeUnit = 'minute'; + opts.approvalType = "auto"; - describe('Products / Developers', function() { - - it('Create Product', function(done){ - var opts = baseOpts() ; - var displayName = 'custom name'; - opts.productName = APIGEE_PRODUCT_NAME; - opts.productDesc = 'abc123'; - opts.displayName = displayName; - opts.proxies = APIGEE_PROXY_NAME; - opts.quota = '1'; - opts.quotaInterval = '1'; - opts.quotaTimeUnit = 'minute'; - - var sdk = apigeetool.getPromiseSDK() - - sdk.createProduct(opts) - .then(function(result){ - try { - assert.equal(result.displayName, displayName); - done(); - } catch (e) { - done(e); - } - },function(err){ - done(err) - }) ; - }); + var sdk = apigeetool.getPromiseSDK() - it('Create Private Product', function(done){ - var opts = baseOpts() ; - var displayName = 'custom name'; - opts.productName = APIGEE_PRIVATE_PRODUCT_NAME; - opts.productDesc = 'abc123'; - opts.displayName = displayName; - opts.proxies = APIGEE_PROXY_NAME; - opts.quota = '1'; - opts.quotaInterval = '1'; - opts.quotaTimeUnit = 'minute'; - opts.attributes = [ {"name": "access", "value": "private"} ]; - var sdk = apigeetool.getPromiseSDK() - - sdk.createProduct(opts) - .then(function(result){ - try { - assert.equal(result.displayName, displayName); - assert.equal(result.attributes.length, 1); - assert.equal(result.attributes[0].name, 'access'); - assert.equal(result.attributes[0].value, 'private'); - done(); - } catch (e) { - done(e); - } - },function(err){ - done(err) - }) ; - }); + sdk.createProduct(opts) + .then(function(result){ + try { + assert.equal(result.displayName, displayName); + done(); + } catch (e) { + done(e); + } + },function(err){ + done(err) + }) ; + }); - it('Create Developer' , function(done){ - var opts = baseOpts() - opts.email = DEVELOPER_EMAIL - opts.firstName = 'Test' - opts.lastName = 'Test1' - opts.userName = 'runningFromTest123' - - var sdk = apigeetool.getPromiseSDK() - - sdk.createDeveloper(opts) - .then(function(result){ - done() - },function(err){ - done(err) - }) ; - }); + it('Create Private Product', function(done){ + var opts = baseOpts() ; + var displayName = 'custom name'; + opts.productName = APIGEE_PRIVATE_PRODUCT_NAME; + opts.productDesc = 'abc123'; + opts.displayName = displayName; + opts.proxies = APIGEE_PROXY_NAME; + opts.quota = '1'; + opts.quotaInterval = '1'; + opts.quotaTimeUnit = 'minute'; + opts.attributes = [ {"name": "access", "value": "private"} ]; + opts.approvalType = "auto"; + + var sdk = apigeetool.getPromiseSDK() + + sdk.createProduct(opts) + .then(function(result){ + try { + assert.equal(result.displayName, displayName); + assert.equal(result.attributes.length, 1); + assert.equal(result.attributes[0].name, 'access'); + assert.equal(result.attributes[0].value, 'private'); + done(); + } catch (e) { + done(e); + } + },function(err){ + done(err) + }) ; + }); - it('Create App' , function(done){ - var opts = baseOpts() - opts.name = APP_NAME - opts.apiproducts = APIGEE_PRODUCT_NAME - opts.email = DEVELOPER_EMAIL + it('Create Developer' , function(done){ + var opts = baseOpts() + opts.email = DEVELOPER_EMAIL + opts.firstName = 'Test' + opts.lastName = 'Test1' + opts.userName = 'runningFromTest123' - var sdk = apigeetool.getPromiseSDK() + var sdk = apigeetool.getPromiseSDK() - sdk.createApp(opts) - .then(function(result){ - done() - },function(err){ - done(err) - }) ; - }); + sdk.createDeveloper(opts) + .then(function(result){ + done() + },function(err){ + done(err) + }) ; + }); - it('Delete App' , function(done){ - var opts = baseOpts() - opts.email = DEVELOPER_EMAIL - opts.name = APP_NAME - var sdk = apigeetool.getPromiseSDK() - sdk.deleteApp(opts) - .then(function(result){ - done() - },function(err){ - done(err) - }) ; - }); + it('Create App' , function(done){ + var opts = baseOpts() + opts.name = APP_NAME + opts.apiProducts = APIGEE_PRODUCT_NAME + opts.email = DEVELOPER_EMAIL - it('Delete Developer' , function(done){ - var opts = baseOpts() - opts.email = DEVELOPER_EMAIL - var sdk = apigeetool.getPromiseSDK() - sdk.deleteDeveloper(opts) - .then(function(result){ - done() - },function(err){ - done(err) - }) ; - }); + var sdk = apigeetool.getPromiseSDK() - it('Delete API Product',function(done){ - var opts = baseOpts() ; - opts.productName = APIGEE_PRODUCT_NAME - - var sdk = apigeetool.getPromiseSDK() - - sdk.deleteProduct(opts) - .then(function(result){ - done() - },function(err){ - done(err) - }) ; - }); + sdk.createApp(opts) + .then(function(result){ + done() + },function(err){ + done(err) + }); + }); - it('Delete API private Product',function(done){ - var opts = baseOpts() ; - opts.productName = APIGEE_PRIVATE_PRODUCT_NAME - - var sdk = apigeetool.getPromiseSDK() - - sdk.deleteProduct(opts) - .then(function(result){ - done() - },function(err){ - done(err) - }) ; - }); + it('Delete App' , function(done){ + var opts = baseOpts() + opts.email = DEVELOPER_EMAIL + opts.name = APP_NAME + var sdk = apigeetool.getPromiseSDK() + sdk.deleteApp(opts) + .then(function(result){ + done() + },function(err){ + done(err) + }) ; + }); + + it('Delete Developer' , function(done){ + var opts = baseOpts() + opts.email = DEVELOPER_EMAIL + var sdk = apigeetool.getPromiseSDK() + sdk.deleteDeveloper(opts) + .then(function(result){ + done() + },function(err){ + done(err) + }) ; + }); + + it('Delete API Product',function(done){ + var opts = baseOpts() ; + opts.productName = APIGEE_PRODUCT_NAME + + var sdk = apigeetool.getPromiseSDK() + + sdk.deleteProduct(opts) + .then(function(result){ + done() + },function(err){ + done(err) + }) ; + }); + + it('Delete API private Product',function(done){ + var opts = baseOpts() ; + opts.productName = APIGEE_PRIVATE_PRODUCT_NAME + + var sdk = apigeetool.getPromiseSDK() + + sdk.deleteProduct(opts) + .then(function(result){ + done() + },function(err){ + done(err) + }) ; }); it('Deploy Apigee Proxy', function(done) { @@ -468,6 +461,36 @@ describe('Remote Tests', function() { }); }); + it('Fetch proxy', function(done) { + var opts = baseOpts(); + opts.api = APIGEE_PROXY_NAME; + opts.revision = deployedRevision; + + apigeetool.fetchProxy(opts, function(err, result) { + if (verbose) { + console.log('Fetch proxy result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('Delete proxy', function(done) { + var opts = baseOpts(); + opts.api = APIGEE_PROXY_NAME; + + apigeetool.delete(opts, function(err, result) { + if (verbose) { + console.log('Delete proxy result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + +}); + +describe('Node.js Apps', function() { // it + this.timeout(REASONABLE_TIMEOUT); + it('Deploy Node.js App', function(done) { var opts = baseOpts(); opts.api = NODE_PROXY_NAME; @@ -649,393 +672,788 @@ describe('Remote Tests', function() { }); }); - describe('Hosted Target', function() { - - it('Deploy Hosted Targets App', function(done) { - var opts = baseOpts(); - opts.api = HOSTED_TARGETS_PROXY_NAME; - opts.directory = path.join(__dirname, '../test/fixtures/hellohostedtargets'); - opts.main = 'server.js'; - opts['base-path'] = '/cli-hosted-targets-test'; - - apigeetool.deployHostedTarget(opts, function(err, result) { - if (verbose) { - console.log('Deploy result = %j', result); - } - if (err) { - done(err); - } else { - try { - if(Array.isArray(result)) result = result[0] - assert.equal(result.name, HOSTED_TARGETS_PROXY_NAME); - assert.equal(result.environment, config.environment); - assert.equal(result.state, 'deployed'); - //it will be 2 for remote testing public cloud/ http & https - assert.equal(result.uris.length, 2); - assert(typeof result.revision === 'number'); - deployedRevision = result.revision; - deployedUri = result.uris[0]; - setTimeout(done, 10000); - } catch (e) { - done(e); - } - } - }); - }); - - it('List Deployments by app', function(done) { - var opts = baseOpts(); - delete opts.environment; - opts.api = HOSTED_TARGETS_PROXY_NAME; - opts.long = true; - - apigeetool.listDeployments(opts, function(err, result) { - if (verbose) { - console.log('List result = %j', result); - } - if (err) { - done(err); - } else { - var deployment = _.find(result.deployments, function(d) { - return (d.name === HOSTED_TARGETS_PROXY_NAME); - }); - try { - assert.equal(deployment.name, HOSTED_TARGETS_PROXY_NAME); - assert.equal(deployment.environment, config.environment); - assert.equal(deployment.state, 'deployed'); - assert.equal(deployment.revision, deployedRevision); - assert.equal(deployment.uris.length, 2); - assert.equal(deployment.uris[0], deployedUri); - done(); - } catch (e) { - done(e); - } - } - }); - }); + it('Delete node proxy', function(done) { + var opts = baseOpts(); + opts.api = NODE_PROXY_NAME; - it('Verify deployed URI', function(done) { + apigeetool.delete(opts, function(err, result) { if (verbose) { - console.log('Testing %s', deployedUri); + console.log('Delete node proxy result = %j', result); } - request(deployedUri, function(err, resp, body) { - if (err) { - console.error(err, resp.statusCode, body); - done(err); - } else { - try { - assert.equal(resp.statusCode, 200); - done(); - } catch (e) { - done(e); - } - } - }); - }); - - it('Check build logs from deployed URI', function(done) { - var opts = baseOpts(); - opts['hosted-build'] = true; - opts.api = HOSTED_TARGETS_PROXY_NAME; - - var logStream = new stream.PassThrough(); - logStream.setEncoding('utf8'); - opts.stream = logStream; - apigeetool.getLogs(opts, function(err) { - assert.ifError(err); - - var allLogs = ''; - logStream.on('data', function(chunk) { - allLogs += chunk; - }); - logStream.on('end', function() { - assert(/DONE/.test(allLogs)); - done(); - }); - }); - }); - - it('Check runtime logs from deployed URI', function(done) { - var opts = baseOpts(); - opts['hosted-runtime'] = true; - opts.api = HOSTED_TARGETS_PROXY_NAME; - - var logStream = new stream.PassThrough(); - logStream.setEncoding('utf8'); - opts.stream = logStream; - - apigeetool.getLogs(opts, function(err) { - assert.ifError(err); - - var allLogs = ''; - logStream.on('data', function(chunk) { - allLogs += chunk; - }); - logStream.on('end', function() { - //Validate runtime logs - assert(/Node HTTP server is listening/.test(allLogs)); - done(); - }); - }); + if (err) { done(err); } else { done(); } }); + }); - it('Undeploy Hosted Targets App Without Revision', function(done) { - var opts = baseOpts(); - opts.api = HOSTED_TARGETS_PROXY_NAME; +}); // End Node.js Apps - apigeetool.undeploy(opts, function(err, result) { - if (verbose) { - console.log('Undeploy result = %j', result); - } - if (err) { - done(err); - } else { - try { - assert.equal(result.name, HOSTED_TARGETS_PROXY_NAME); - assert.equal(result.environment, config.environment); - assert.equal(result.state, 'undeployed'); - assert.equal(result.revision, deployedRevision); - done(); - } catch (e) { - done(e); - } - } - }); - }); - }); // end hosted target tests +describe('Hosted Target', function() { // it + this.timeout(REASONABLE_TIMEOUT); - it('Create an Cache Resource',function(done){ + it('Deploy Hosted Targets App', function(done) { var opts = baseOpts(); - opts.cache = CACHE_RESOURCE_NAME; - apigeetool.createcache(opts,function(err,result) { + opts.api = HOSTED_TARGETS_PROXY_NAME; + opts.directory = path.join(__dirname, '../test/fixtures/hellohostedtargets'); + opts.main = 'server.js'; + opts['base-path'] = '/cli-hosted-targets-test'; + + apigeetool.deployHostedTarget(opts, function(err, result) { if (verbose) { - console.log('Create Cache result = %j', result); + console.log('Deploy result = %j', result); } if (err) { done(err); } else { - done() + try { + if(Array.isArray(result)) result = result[0] + assert.equal(result.name, HOSTED_TARGETS_PROXY_NAME); + assert.equal(result.environment, config.environment); + assert.equal(result.state, 'deployed'); + //it will be 2 for remote testing public cloud/ http & https + assert.equal(result.uris.length, 2); + assert(typeof result.revision === 'number'); + deployedRevision = result.revision; + deployedUri = result.uris[0]; + setTimeout(done, 10000); + } catch (e) { + done(e); + } } }); }); - it('Delete Cache Resource',function(done){ + it('List Deployments by app', function(done) { var opts = baseOpts(); - opts.cache = CACHE_RESOURCE_NAME; - apigeetool.deletecache(opts,function(err,result) { + delete opts.environment; + opts.api = HOSTED_TARGETS_PROXY_NAME; + opts.long = true; + + apigeetool.listDeployments(opts, function(err, result) { if (verbose) { - console.log('Delete Cache result = %j', result); + console.log('List result = %j', result); } if (err) { done(err); } else { - done() + var deployment = _.find(result.deployments, function(d) { + return (d.name === HOSTED_TARGETS_PROXY_NAME); + }); + try { + assert.equal(deployment.name, HOSTED_TARGETS_PROXY_NAME); + assert.equal(deployment.environment, config.environment); + assert.equal(deployment.state, 'deployed'); + assert.equal(deployment.revision, deployedRevision); + assert.equal(deployment.uris.length, 2); + assert.equal(deployment.uris[0], deployedUri); + done(); + } catch (e) { + done(e); + } } }); }); - it('Create Target Server',function(done){ - var opts = baseOpts(); - opts.targetServerName = TARGET_SERVER_NAME; - opts.targetHost = 'localhost'; - opts.targetEnabled = true; - opts.targetPort = 443; - opts.targetSSL=true; - opts.environment = config.environment; - apigeetool.getPromiseSDK() - .createTargetServer(opts) - .then(function(){done()}, - function(err){ - console.log(err) - done(err)}) - }); - - it('Delete Target Server',function(done){ - var opts = baseOpts(); - opts.targetServerName = TARGET_SERVER_NAME; - opts.environment = config.environment; - - apigeetool.deleteTargetServer(opts,function(err,result) { - if (verbose) { - console.log('Delete TargetServer result = %j', result); - } + it('Verify deployed URI', function(done) { + if (verbose) { + console.log('Testing %s', deployedUri); + } + request(deployedUri, function(err, resp, body) { if (err) { + console.error(err, resp.statusCode, body); done(err); } else { - done() + try { + assert.equal(resp.statusCode, 200); + done(); + } catch (e) { + done(e); + } } }); }); - describe('KVM', function() { - it('Create KVM',function(done){ - var opts = baseOpts(); - opts.mapName = MAP_NAME; - opts.environment = config.environment; - apigeetool.getPromiseSDK() - .createKVM(opts) - .then(function(){done()}, - function(err){ - console.log(err) - done(err)}) - }); - - it('Create Encrypted KVM',function(done){ - var opts = baseOpts(); - opts.mapName = MAP_NAME_ENCRYPTED; - opts.environment = config.environment; - opts.encrypted = true; - apigeetool.getPromiseSDK() - .createKVM(opts) - .then(function(res){ - if (!res.encrypted) { - return done(new Error('Map was not encrypted')); - } - done(); - }, function(err){ - console.log(err) - done(err) - }) - }); + it('Check build logs from deployed URI', function(done) { + var opts = baseOpts(); + opts['hosted-build'] = true; + opts.api = HOSTED_TARGETS_PROXY_NAME; - it('Add Entry to KVM',function(done){ - // This will not work for non-cps orgs - var opts = baseOpts(); - opts.mapName = MAP_NAME; - opts.environment = config.environment; - opts.entryName = 'test'; - opts.entryValue = 'test1'; - apigeetool.getPromiseSDK() - .addEntryToKVM(opts) - .then(function(){done()}, - function(err){ - console.log(err); - done(err)}) - }); + var logStream = new stream.PassThrough(); + logStream.setEncoding('utf8'); + opts.stream = logStream; + apigeetool.getLogs(opts, function(err) { + assert.ifError(err); - it('Get KVM Entry', function(done) { - var opts = baseOpts(); - opts.mapName = MAP_NAME; - opts.environment = config.environment; - opts.entryName = 'test'; - apigeetool.getPromiseSDK() - .getKVMentry(opts) - .then(function(body){ - assert.equal(body.value, 'test1') - done() - }, - function(err) { - console.log(err); - done(err); - }) - }) + var allLogs = ''; + logStream.on('data', function(chunk) { + allLogs += chunk; + }); + logStream.on('end', function() { + assert(/DONE/.test(allLogs)); + done(); + }); + }); + }); - it('Get KVM Map', function(done) { - var opts = baseOpts(); - opts.mapName = MAP_NAME; - opts.environment = config.environment; - apigeetool.getPromiseSDK() - .getkvmmap(opts) - .then(function(body){ - assert.equal(body.entry.length, 1) - done() - }, - function(err) { - console.log(err); - done(err); - }) - }) + it('Check runtime logs from deployed URI', function(done) { + var opts = baseOpts(); + opts['hosted-runtime'] = true; + opts.api = HOSTED_TARGETS_PROXY_NAME; - it('Delete KVM Entry', function(done) { - var opts = baseOpts(); - opts.mapName = MAP_NAME; - opts.environment = config.environment; - opts.entryName = 'test'; - apigeetool.getPromiseSDK() - .deleteKVMentry(opts) - .then(function(body){ - assert.equal(body.value, 'test1') - done() - }, - function(err) { - console.log(err); - done(err); - }) - }) + var logStream = new stream.PassThrough(); + logStream.setEncoding('utf8'); + opts.stream = logStream; - it('Delete KVM',function(done){ - var opts = baseOpts(); - opts.mapName = MAP_NAME; - opts.environment = config.environment; - apigeetool.deleteKVM(opts,function(err,result) { + apigeetool.getLogs(opts, function(err) { + assert.ifError(err); + + var allLogs = ''; + logStream.on('data', function(chunk) { + allLogs += chunk; + }); + logStream.on('end', function() { + //Validate runtime logs + assert(/Node HTTP server is listening/.test(allLogs)); + done(); + }); + }); + }); + + it('Undeploy Hosted Targets App Without Revision', function(done) { + var opts = baseOpts(); + opts.api = HOSTED_TARGETS_PROXY_NAME; + + apigeetool.undeploy(opts, function(err, result) { + if (verbose) { + console.log('Undeploy result = %j', result); + } + if (err) { + done(err); + } else { + try { + assert.equal(result.name, HOSTED_TARGETS_PROXY_NAME); + assert.equal(result.environment, config.environment); + assert.equal(result.state, 'undeployed'); + assert.equal(result.revision, deployedRevision); + done(); + } catch (e) { + done(e); + } + } + }); + }); + + it('Delete hosted target proxy', function(done) { + var opts = baseOpts(); + opts.api = HOSTED_TARGETS_PROXY_NAME; + + apigeetool.delete(opts, function(err, result) { + if (verbose) { + console.log('Delete hosted target proxy result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + +}); // end hosted target tests + +describe('Caches', function() { // it + it('Create an Cache Resource',function(done){ + var opts = baseOpts(); + opts.cache = CACHE_RESOURCE_NAME; + apigeetool.createcache(opts,function(err,result) { + if (verbose) { + console.log('Create Cache result = %j', result); + } + if (err) { + done(err); + } else { + done() + } + }); + }); + + it('Delete Cache Resource',function(done){ + var opts = baseOpts(); + opts.cache = CACHE_RESOURCE_NAME; + apigeetool.deletecache(opts,function(err,result) { + if (verbose) { + console.log('Delete Cache result = %j', result); + } + if (err) { + done(err); + } else { + done() + } + }); + }); +}); // end cache tests + +describe('Target Servers', function() { // it + this.timeout(REASONABLE_TIMEOUT); + + it('Create Target Server',function(done){ + var opts = baseOpts(); + opts.environment = config.environment; + opts.targetServerName = TARGET_SERVER_NAME; + opts.targetHost = 'localhost'; + opts.targetEnabled = true; + opts.targetPort = 443; + opts.targetSSL=true; + opts.environment = config.environment + + apigeetool.createTargetServer(opts, function(err, result) { + if (verbose) { + console.log('Create Target Server result = %j', result); + } + if (err) { + done(err); + } else { + try { + assert.equal(result.name,TARGET_SERVER_NAME); + assert.equal(result.port,443); + assert.equal(result.isEnabled,true); + done(); + } catch (e) { + done(e); + } + } + }); + }); + + it('List Target Servers',function(done){ + var opts = baseOpts(); + opts.environment = config.environment; + apigeetool.listTargetServers(opts, function(err, result) { + if (verbose) { + console.log('List Target Servers result = %j', result); + } + if (err) { + done(err); + } else { + try { + assert.equal(result.includes(TARGET_SERVER_NAME),true); + done(); + } catch (e) { + done(e); + } + } + }); + }); + + it('Get Target Server',function(done){ + var opts = baseOpts(); + opts.environment = config.environment; + opts.targetServerName = TARGET_SERVER_NAME; + apigeetool.getTargetServer(opts, function(err, result) { + if (verbose) { + console.log('Get Target Server result = %j', result); + } + if (err) { + done(err); + } else { + try { + assert.equal(result.name,TARGET_SERVER_NAME); + assert.equal(result.port,443); + assert.equal(result.isEnabled,true); + done(); + } catch (e) { + done(e); + } + } + }); + }); + + it('Delete Target Server',function(done){ + var opts = baseOpts(); + opts.environment = config.environment; + opts.targetServerName = TARGET_SERVER_NAME; + apigeetool.deleteTargetServer(opts, function(err, result) { + if (verbose) { + console.log('Delete Target Server result = %j', result); + } + if (err) { + done(err); + } else { + try { + assert.equal(result.name,TARGET_SERVER_NAME); + done(); + } catch (e) { + done(e); + } + } + }); + }); + +}); // end target server tests + +describe('KVM', function() { // it + it('Create KVM',function(done){ + var opts = baseOpts(); + opts.mapName = MAP_NAME; + opts.environment = config.environment; + apigeetool.getPromiseSDK() + .createKVM(opts) + .then(function(res){ if (verbose) { - console.log('Delete KVM result = %j', result); + console.log('Create KVM result = %j', res); } - if (err) { - done(err); - } else { - done() + done() + },function(err){ + console.log(err) + done(err) + }) + }); + + it('Create Encrypted KVM',function(done){ + var opts = baseOpts(); + opts.mapName = MAP_NAME_ENCRYPTED; + opts.environment = config.environment; + opts.encrypted = true; + apigeetool.getPromiseSDK() + .createKVM(opts) + .then(function(res){ + if (!res.encrypted) { + return done(new Error('Map was not encrypted')); + } else if (verbose) { + console.log('Create KVM result = %j', res); } - }); + done(); + }, function(err){ + console.log(err) + done(err) + }) + }); + + it('Delete Encrypted KVM',function(done){ + var opts = baseOpts(); + opts.mapName = MAP_NAME_ENCRYPTED; + opts.environment = config.environment; + apigeetool.deleteKVM(opts,function(err,result) { + if (verbose) { + console.log('Delete Encrypted KVM result = %j', result); + } + if (err) { + done(err); + } else { + done() + } }); - }) + }); + it('Add Entry to KVM',function(done){ + // This will not work for non-cps orgs + var opts = baseOpts(); + opts.mapName = MAP_NAME; + opts.environment = config.environment; + opts.entryName = 'test'; + opts.entryValue = 'test1'; + apigeetool.getPromiseSDK() + .addEntryToKVM(opts) + .then(function(res){ + if (verbose) { + console.log('Add Entry to KVM result = %j', res); + } + done() + },function(err){ + console.log(err) + done(err) + }) + }); - describe('SharedFlows', function() { - it('Deploy SharedFlow', function (done) { - var opts = baseOpts(); - opts.name = SHARED_FLOW_NAME; - opts.directory = path.join(__dirname, '../test/fixtures/employees-sf'); - apigeetool.deploySharedflow(opts, function (err, result) { + it('Get KVM Entry', function(done) { + var opts = baseOpts(); + opts.mapName = MAP_NAME; + opts.environment = config.environment; + opts.entryName = 'test'; + apigeetool.getPromiseSDK() + .getKVMentry(opts) + .then(function(body){ if (verbose) { - console.log('Deploy result = %j', result); + console.log('Get KVM Entry result = %j', body); } - if (err) { - done(err); - } else { - try { - if (Array.isArray(result)) { - result = result[0] - } - assert.equal(result.name, SHARED_FLOW_NAME); - assert.equal(result.environment, config.environment); - assert.equal(result.state, 'deployed'); - // assert.equal(result.uris.length, 1); - assert(typeof result.revision === 'number'); - deployedRevision = result.revision; - // deployedUri = result.uris[0]; - done(); - } catch (e) { - done(e); + assert.equal(body.value, 'test1') + done() + }, + function(err) { + console.log(err); + done(err); + }) + }); + + it('Get KVM Map', function(done) { + var opts = baseOpts(); + opts.mapName = MAP_NAME; + opts.environment = config.environment; + apigeetool.getPromiseSDK() + .getKVMmap(opts) + .then(function(body){ + if (verbose) { + console.log('Get KVM Map result = %j', body); + } + assert.equal(body.entry.length, 1) + done() + }, + function(err) { + console.log(err); + done(err); + }) + }); + + it('Delete KVM Entry', function(done) { + var opts = baseOpts(); + opts.mapName = MAP_NAME; + opts.environment = config.environment; + opts.entryName = 'test'; + apigeetool.getPromiseSDK() + .deleteKVMentry(opts) + .then(function(body){ + if (verbose) { + console.log('Get KVM Map result = %j', body); + } + assert.equal(body.value, 'test1') + done() + }, + function(err) { + console.log(err); + done(err); + }) + }); + + it('Delete KVM',function(done){ + var opts = baseOpts(); + opts.mapName = MAP_NAME; + opts.environment = config.environment; + apigeetool.deleteKVM(opts,function(err,result) { + if (verbose) { + console.log('Delete KVM result = %j', result); + } + if (err) { + done(err); + } else { + done() + } + }); + }); +}); // end KVM tests + +describe('SharedFlows and FlowHooks', function() { // it + this.timeout(REASONABLE_TIMEOUT); + it('Deploy SharedFlow', function (done) { + var opts = baseOpts(); + var deployedRevision; + opts.name = SHARED_FLOW_NAME; + opts.directory = path.join(__dirname, '../test/fixtures/employees-sf'); + apigeetool.deploySharedflow(opts, function (err, result) { + if (verbose) { + console.log('Deploy result = %j', result); + } + if (err) { + done(err); + } else { + try { + if (Array.isArray(result)) { + result = result[0] } + assert.equal(result.name, SHARED_FLOW_NAME); + assert.equal(result.environment, config.environment); + assert.equal(result.state, 'deployed'); + // assert.equal(result.uris.length, 1); + assert(typeof result.revision === 'number'); + deployedRevision = result.revision; + // deployedUri = result.uris[0]; + done(); + } catch (e) { + done(e); } - }); + } + }); + }); + + it('listSharedFlowDeployments', function(done) { + var opts = baseOpts(); + delete opts.environment; + opts.name = SHARED_FLOW_NAME; + opts.revision = 1; + + apigeetool.listSharedflowDeployments(opts, function(err, result) { + if (verbose) { + console.log('listSharedFlowDeployments result = %j', result); + } + if (err) { + done(err); + } else { + try { + result = result.deployments[0]; + assert.equal(result.name, SHARED_FLOW_NAME); + assert.equal(result.environment, config.environment); + assert.equal(result.state, 'deployed'); + assert(typeof result.revision === 'number'); + done(); + } catch (e) { + done(e); + } + } + }); + }); + + + it('fetchSharedFlow', function(done) { + var opts = baseOpts(); + opts.name = SHARED_FLOW_NAME; + opts.revision = 1 + + apigeetool.fetchSharedflow(opts, function(err, result) { + if (verbose) { + console.log('fetchSharedFlow result: %j', result); + } + if (err) { + done(err); + } else { done(); } + }); + }); + + it('getPreviousSharedFlow', function(done) { + var opts = baseOpts(); + opts.flowHookName = "PreProxyFlowHook"; + + apigeetool.getFlowHook(opts, function(err, result) { + if (verbose) { + console.log('getPreviousSharedFlow result = %j', result); + } + if (err) { + done(err); + } else { + if( result.sharedFlow ) { + prevSharedFlow = result.sharedFlow; + } + done(); + } + }); + }); + + it('attachFlowHook', function(done) { + var opts = baseOpts(); + opts.flowHookName = "PreProxyFlowHook"; + opts.sharedFlowName = SHARED_FLOW_NAME; + + apigeetool.attachFlowHook(opts, function(err, result) { + if (verbose) { + console.log('attachFlowHook result = %j', result); + } + if (err) { + done(err); + } else { + done(); + } }); + }); - it('listSharedflowDeployments'); // Until MGMT-3671 is merged, will not work + it('detachFlowHook', function(done) { + var opts = baseOpts(); + opts.flowHookName = "PreProxyFlowHook"; - it('fetchSharedflow'); + apigeetool.detachFlowHook(opts, function(err, result) { + if (verbose) { + console.log('detachFlowHook result = %j', result); + } + if (err) { + done(err); + } else { + done(); + } + }); + }); - it('undeploySharedflow', function(done) { + it('re-attachFlowHook', function(done) { + if( prevSharedFlow ) { var opts = baseOpts(); - opts.name = SHARED_FLOW_NAME; + opts.flowHookName = "PreProxyFlowHook"; + opts.sharedFlowName = prevSharedFlow; - apigeetool.undeploySharedflow(opts, function(err, result) { + apigeetool.attachFlowHook(opts, function(err, result) { + if (verbose) { + console.log('prevSharedFlow ' + prevSharedFlow ); + console.log('re-attachFlowHook result = %j', result); + } if (err) { done(err); - } else { // If response is non-200 it throws an Error + } else { done(); } }); + } else { + done(); + } + }); + + it('undeploySharedFlow', function(done) { + var opts = baseOpts(); + opts.name = SHARED_FLOW_NAME; + + apigeetool.undeploySharedflow(opts, function(err, result) { + if (err) { + done(err); + } else { // If response is non-200 it throws an Error + done(); + } }); + }); - it('deleteSharedflow', function(done) { - var opts = baseOpts(); - opts.name = SHARED_FLOW_NAME; - apigeetool.deleteSharedflow(opts, done); + it('deleteSharedFlow', function(done) { + var opts = baseOpts(); + opts.name = SHARED_FLOW_NAME; + apigeetool.deleteSharedflow(opts, done); + }); +}); // end shared flow tests + +describe('User Roles and Permissions', function() { // it + this.timeout(REASONABLE_TIMEOUT); + + it('Create Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + + apigeetool.createRole(opts, function (err, result) { + if (verbose) { + console.log('Create Role result = %j', result); + } + if (err) { done(err); } else { done(); } }); - }) -}); + }); + + it('Get Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + + apigeetool.getRole(opts, function (err, result) { + if (verbose) { + console.log('Get Role result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('List Roles', function (done) { + var opts = baseOpts(); + + apigeetool.listRoles(opts, function (err, result) { + if (verbose) { + console.log('List Roles result = %j', result); + } + if (err) { done(err); } else { + assert.equal( result.includes(ROLE_NAME), true ); + done(); + } + }); + }); + + it('Set Role Permissions', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + opts.permissions = '[{"path":"/userroles","permissions":["get"]}]'; + + apigeetool.setRolePermissions(opts, function (err, result) { + if (verbose) { + console.log('Set Role Permissions result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('Get Role Permissions', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + + apigeetool.getRolePermissions(opts, function (err, result) { + if (verbose) { + console.log('Get Role Permissions result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('Assign User to Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + opts.email = config.useremail; + + apigeetool.assignUserRole(opts, function (err, result) { + if (verbose) { + console.log('Assign User to Role result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('Verify User in Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + opts.email = config.useremail; + + apigeetool.verifyUserRole(opts, function (err, result) { + if (verbose) { + console.log('Verify User in Role result = %j', result); + } + if (err) { done(err); } else { + assert.equal( result.emailId, opts.email); + done(); + } + }); + }); + + it('List Users in a Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + opts.email = config.useremail; + + apigeetool.listRoleUsers(opts, function (err, result) { + if (verbose) { + console.log('List Users in a Role result = %j', result); + } + if (err) { done(err); } else { + assert.equal( result.includes(opts.email), true); + done(); + } + }); + }); + + it('Verify access allowed', function (done) { + var opts = baseOpts(); + opts.netrc = false; + opts.username = config.useremail; + opts.password = config.userpassword; + apigeetool.listRoles(opts, function (err, result) { + if (verbose) { + console.log('Verify access allowed for user %s result = %j', config.useremail, result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('Remove User from Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + opts.email = config.useremail; + + apigeetool.removeUserRole(opts, function (err, result) { + if (verbose) { + console.log('Remove User from Role result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); + + it('Delete Role', function (done) { + var opts = baseOpts(); + opts.roleName = ROLE_NAME; + + apigeetool.deleteRole(opts, function (err, result) { + if (verbose) { + console.log('Delete Role result = %j', result); + } + if (err) { done(err); } else { done(); } + }); + }); +}); // End User Roles and Permissions function baseOpts() { var o = { diff --git a/remotetests/testconfig-sample.js b/remotetests/testconfig-sample.js index 3b5b4b4..d967b46 100644 --- a/remotetests/testconfig-sample.js +++ b/remotetests/testconfig-sample.js @@ -10,11 +10,17 @@ module.exports = { // The user name to authenticate with for the Apigee management API username: '', // The password for that user name - password: '' + password: '', // comment the username and password and uncomment the following to use .netrc // netrc: true - + debug: false, + verbose: false, + + // Developer must exist or be added to org before tests + useremail: 'someone+tester@google.com', + userpassword: 'Supersecret123' + // Uncomment for the management API URI of your local Apigee environment // Leave commented to test using the Apigee cloud. //baseuri: 'http://mgmt:8080' diff --git a/test/testoptions.js b/test/testoptions.js index 7c51d22..6ce2a13 100644 --- a/test/testoptions.js +++ b/test/testoptions.js @@ -16,7 +16,7 @@ describe('Options parsing test', function() { fs.readFileSync.calls.reset(); request.defaults.calls.reset(); }); - + it('Test no descriptor', function(done) { var desc = {}; var opts = { foo: 1, bar: 'baz'}; @@ -51,6 +51,20 @@ describe('Options parsing test', function() { done(); }); }); + + it('Test missing option prompt false', function(done) { + var desc = { + foo: {}, + ping: { required: false, prompt: false }, + pong: { required: true, prompt: false } + }; + var opts = { foo: 1, ping: 1}; + options.validate(opts, desc, function(err) { + assert(err); + assert(/Missing required option/.test(err.message)); + done(); + }); + }); it('Test nothing and missing stuff', function(done) { var desc = { foo: {}, @@ -114,13 +128,16 @@ describe('Options parsing test', function() { it('Test command-line help', function() { var desc = { - foo: {}, - bar: { required: false, shortOption: 'b' }, - baz: { required: true } + foo: { }, + ping: { name: 'Ping', required: false, prompt: false }, + pong: { name: 'Pong', required: true, prompt: false } }; + var opts = { ping: 1, pong: 'value'}; var help = options.getHelp(desc); //console.log('Help is:' + help); + assert.notEqual( help, undefined ); }); + it('Test command-line toggle', function() { var desc = { foo: {},