Skip to content

Commit

Permalink
Merge pull request actions#25 from akamai/feature/DEVPOPS-788
Browse files Browse the repository at this point in the history
Feature/DEVPOPS-788
  • Loading branch information
bradforj287 authored Feb 22, 2019
2 parents 771003e + 0715d9e commit 5531616
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
38 changes: 20 additions & 18 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, isClonable: boolean, name: string) {
async function createFromRules(papiFilePath: string, hostnames: Array<string>, 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) {
Expand Down Expand Up @@ -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<string>, isClonable: boolean, name: string) {
async function createFromProperty(propertySpecifier: string, hostnames: Array<string>, 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<string>, isClonable: boolean, name: string) {
async function createFromHostname(hostname: string, hostnames: Array<string>, 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<Array<string>> {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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.");
}
Expand Down Expand Up @@ -733,12 +733,14 @@ program
.option('-n, --name <string>', 'name of sandbox')
.option('-H, --requesthostnames <string>', 'comma separated list of request hostnames')
.option('--recipe <path>', 'path to recipe json file')
.option('-C, --cpcode <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;
try {
const recipePath = options.recipe;
if (recipePath) {
await createFromRecipe(recipePath, options.name, options.clonable);
await createFromRecipe(recipePath, options.name, options.clonable, cpcode);
return;
}

Expand All @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/service/sandbox-svc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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,
Expand All @@ -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);
}

Expand Down

0 comments on commit 5531616

Please sign in to comment.