From 3f5a8e279b3c7fa65b0bb87bdce7ee9924b5dc95 Mon Sep 17 00:00:00 2001 From: Nasir Ahmed Raffick Date: Thu, 21 Feb 2019 16:40:50 -0500 Subject: [PATCH 1/4] Added command argument usecpcode in create functionality --- src/cli-main.ts | 38 ++++++++++++++++++++------------------ src/service/sandbox-svc.ts | 13 +++++++++++-- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index b57df96e1..2c12d99ed 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -464,12 +464,12 @@ async function addPropertyFromRules(sandboxId: string, papiFilePath: string, hos return await cliUtils.spinner(sandboxSvc.addPropertyFromRules(sandboxId, hostnames, papiJson), `adding sandbox property to ${sandboxId}`); } -async function createFromRules(papiFilePath: string, hostnames: Array, isClonable: boolean, name: string) { +async function createFromRules(papiFilePath: string, hostnames: Array, isClonable: boolean, name: string, cpcode: number) { if (!fs.existsSync(papiFilePath)) { logAndExit(`file: ${papiFilePath} does not exist`); } const papiJson = getJsonFromFile(papiFilePath); - return await cliUtils.spinner(sandboxSvc.createFromRules(papiJson, hostnames, name, isClonable), "creating new sandbox"); + return await cliUtils.spinner(sandboxSvc.createFromRules(papiJson, hostnames, name, isClonable, cpcode), "creating new sandbox"); } function parsePropertySpecifier(propertySpecifier) { @@ -513,15 +513,15 @@ async function addPropertyToSandboxFromHostname(sandboxId: string, hostnames: Ar return await cliUtils.spinner(sandboxSvc.addPropertyFromProperty(sandboxId, hostnames, {hostname}), msg); } -async function createFromProperty(propertySpecifier: string, hostnames: Array, isClonable: boolean, name: string) { +async function createFromProperty(propertySpecifier: string, hostnames: Array, isClonable: boolean, name: string, cpcode: number) { const propertySpecObj = parsePropertySpecifier(propertySpecifier); const msg = `Creating from: ${JSON.stringify(propertySpecObj)}`; - return await cliUtils.spinner(sandboxSvc.createFromProperty(hostnames, name, isClonable, propertySpecObj), msg); + return await cliUtils.spinner(sandboxSvc.createFromProperty(hostnames, name, isClonable, propertySpecObj, cpcode), msg); } -async function createFromHostname(hostname: string, hostnames: Array, isClonable: boolean, name: string) { +async function createFromHostname(hostname: string, hostnames: Array, isClonable: boolean, name: string, cpcode: number) { const msg = `Creating from: ${hostname}`; - return await cliUtils.spinner(sandboxSvc.createFromProperty(hostnames, name, isClonable, {hostname}), msg); + return await cliUtils.spinner(sandboxSvc.createFromProperty(hostnames, name, isClonable, {hostname}, cpcode), msg); } async function getOriginListForSandboxId(sandboxId: string): Promise> { @@ -542,13 +542,13 @@ function resolveRulesPath(recipeFilePath, rulesPath) { return path.join(path.dirname(recipeFilePath), rulesPath); } -async function createFromPropertiesRecipe(recipe) { +async function createFromPropertiesRecipe(recipe, cpcode) { const sandboxRecipe = recipe.sandbox; const properties = sandboxRecipe.properties; const firstProp = properties[0]; console.log(`creating sandbox & property 1 from recipe`); - const r = await cliUtils.spinner(createRecipeSandboxAndProperty(firstProp, sandboxRecipe)); + const r = await cliUtils.spinner(createRecipeSandboxAndProperty(firstProp, sandboxRecipe, cpcode)); for (var i = 1; i < properties.length; i++) { try { @@ -659,14 +659,14 @@ async function updateFromRecipe(sandboxId, recipeFilePath, name, clonable) { } -async function createFromRecipe(recipeFilePath, name, clonable) { +async function createFromRecipe(recipeFilePath, name, clonable, cpcode) { const recipe = validateAndBuildRecipe(recipeFilePath, name, clonable); const sandboxRecipe = recipe.sandbox; var res = null; if (sandboxRecipe.properties) { - res = await createFromPropertiesRecipe(recipe); + res = await createFromPropertiesRecipe(recipe, cpcode); } else if (sandboxRecipe.cloneFrom) { res = await createFromCloneRecipe(recipe); } else { @@ -691,13 +691,13 @@ function createRecipeProperty(rp, sandboxId) { } } -function createRecipeSandboxAndProperty(rp, recipe) { +function createRecipeSandboxAndProperty(rp, recipe, cpcode) { if (rp.property) { - return createFromProperty(rp.property, rp.requestHostnames, recipe.clonable, recipe.name); + return createFromProperty(rp.property, rp.requestHostnames, recipe.clonable, recipe.name, cpcode); } else if (rp.hostname) { - return createFromHostname(rp.hostname, rp.requestHostnames, recipe.clonable, recipe.name); + return createFromHostname(rp.hostname, rp.requestHostnames, recipe.clonable, recipe.name, cpcode); } else if (rp.rulesPath) { - return createFromRules(rp.rulesPath, rp.requestHostnames, recipe.clonable, recipe.name); + return createFromRules(rp.rulesPath, rp.requestHostnames, recipe.clonable, recipe.name, cpcode); } else { logAndExit("critical error with recipe property. rulesPath or property needs to be defined."); } @@ -733,12 +733,14 @@ program .option('-n, --name ', 'name of sandbox') .option('-H, --requesthostnames ', 'comma separated list of request hostnames') .option('--recipe ', 'path to recipe json file') + .option('C, --usecpcode ', 'top-level cpcode to use and bypass during create') .action(async function (options) { helpExitOnNoArgs(options); + const cpcode = options.usecpcode; try { const recipePath = options.recipe; if (recipePath) { - await createFromRecipe(recipePath, options.name, options.clonable); + await createFromRecipe(recipePath, options.name, options.clonable, cpcode); return; } @@ -765,11 +767,11 @@ program var r = null; if (papiFilePath) { - r = await createFromRules(papiFilePath, hostnames, isClonable, name); + r = await createFromRules(papiFilePath, hostnames, isClonable, name, cpcode); } else if (propertySpecifier) { - r = await createFromProperty(propertySpecifier, hostnames, isClonable, name); + r = await createFromProperty(propertySpecifier, hostnames, isClonable, name, cpcode); } else if (hostnameSpecifier) { - r = await createFromHostname(hostnameSpecifier, hostnames, isClonable, name); + r = await createFromHostname(hostnameSpecifier, hostnames, isClonable, name, cpcode); } await registerSandbox(r.sandboxId, r.jwtToken, name); diff --git a/src/service/sandbox-svc.ts b/src/service/sandbox-svc.ts index 3a81dd236..0d184535a 100644 --- a/src/service/sandbox-svc.ts +++ b/src/service/sandbox-svc.ts @@ -104,13 +104,18 @@ export function updateSandbox(sandbox) { return putJson(`${SANDBOX_API_BASE}/sandboxes/${sandbox.sandboxId}`, sandbox).then(r => r.body); } -export function createFromRules(papiRules, requestHostnames, name, isClonable) { +export function createFromRules(papiRules, requestHostnames, name, isClonable, cpcode) { var bodyObj = { name: name, requestHostnames: requestHostnames, createFromRules: papiRules, isClonable: isClonable }; + + if(cpcode) { + bodyObj['cpcode'] = cpcode; + } + return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body); } @@ -132,7 +137,7 @@ export function addPropertyFromProperty(sandboxId: string, requestHostnames, fro return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body); } -export function createFromProperty(requestHostnames, name, isClonable, fromPropertyObj) { +export function createFromProperty(requestHostnames, name, isClonable, fromPropertyObj, cpcode) { var bodyObj = { name: name, createFromProperty: fromPropertyObj, @@ -141,6 +146,10 @@ export function createFromProperty(requestHostnames, name, isClonable, fromPrope if (requestHostnames) { bodyObj['requestHostnames'] = requestHostnames; } + + if(cpcode) { + bodyObj['cpcode'] = cpcode; + } return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body); } From 72a1b22fb2c443748c9dd3b28fc2d632c2ac3df2 Mon Sep 17 00:00:00 2001 From: Nasir Ahmed Raffick Date: Thu, 21 Feb 2019 16:43:32 -0500 Subject: [PATCH 2/4] Updated CLI create sandbox command with optional usecpcode argument. --- src/cli-main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index 2c12d99ed..d8f45171d 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -733,7 +733,7 @@ program .option('-n, --name ', 'name of sandbox') .option('-H, --requesthostnames ', 'comma separated list of request hostnames') .option('--recipe ', 'path to recipe json file') - .option('C, --usecpcode ', 'top-level cpcode to use and bypass during create') + .option('-C, --usecpcode ', 'top-level cpcode to use and bypass during create') .action(async function (options) { helpExitOnNoArgs(options); const cpcode = options.usecpcode; From 657823a6d6f872f36d46e673389246f2c04c8066 Mon Sep 17 00:00:00 2001 From: Nasir Ahmed Raffick Date: Thu, 21 Feb 2019 16:50:35 -0500 Subject: [PATCH 3/4] Changed the description text for the usecpcode argument --- src/cli-main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index d8f45171d..7fb75e01a 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -733,7 +733,7 @@ program .option('-n, --name ', 'name of sandbox') .option('-H, --requesthostnames ', 'comma separated list of request hostnames') .option('--recipe ', 'path to recipe json file') - .option('-C, --usecpcode ', 'top-level cpcode to use and bypass during create') + .option('-C, --usecpcode ', 'specify a pre-existing cpcode instead of letting the system generate a new one') .action(async function (options) { helpExitOnNoArgs(options); const cpcode = options.usecpcode; From 0715d9ea5f6778d3cd1221a6528d5a750ae114cd Mon Sep 17 00:00:00 2001 From: Nasir Ahmed Raffick Date: Thu, 21 Feb 2019 17:38:51 -0500 Subject: [PATCH 4/4] Changed --usecpcode to --cpcode --- src/cli-main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index 7fb75e01a..8c4febad9 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -733,7 +733,7 @@ program .option('-n, --name ', 'name of sandbox') .option('-H, --requesthostnames ', 'comma separated list of request hostnames') .option('--recipe ', 'path to recipe json file') - .option('-C, --usecpcode ', 'specify a pre-existing cpcode instead of letting the system generate a new one') + .option('-C, --cpcode ', 'specify a pre-existing cpcode instead of letting the system generate a new one') .action(async function (options) { helpExitOnNoArgs(options); const cpcode = options.usecpcode;