Skip to content

Commit

Permalink
Merge pull request #100 from akamai/develop
Browse files Browse the repository at this point in the history
Release CLI version 1.15.0
  • Loading branch information
herzykj committed Jul 1, 2021
2 parents b3e493d + 5edbba5 commit 26efb40
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 47 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ You can use this example "recipe" to quickly customize the sandbox to your devel
"property":"123456:2",
"requestHostnames":[
"localhost2"
]
],
"cpcode": 1234
},
{
"hostname":"www.example.com",
Expand Down
2 changes: 1 addition & 1 deletion cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"commands": [
{
"name": "sandbox",
"version": "1.4.0",
"version": "1.5.0",
"description": "Manage Akamai Sandbox environments.",
"bin": "https://github.com/akamai/cli-sandbox/releases/download/{{.Version}}/akamai-{{.Name}}-{{.Version}}-{{.OS}}-{{.Arch}}{{.BinSuffix}}"
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "akamai-sandbox-cli",
"version": "1.4.0",
"version": "1.5.0",
"description": "A tool that makes it easier to manage Akamai Sandboxes. Call the Sandbox API from the command line.",
"repository": "https://github.com/akamai/cli-sandbox",
"license": "Apache-2.0",
Expand Down
3 changes: 3 additions & 0 deletions schemas/recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
},
"minItems": 1
},
"cpcode": {
"type": "number"
},
"hostname": {
"type": "string"
},
Expand Down
75 changes: 47 additions & 28 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ async function showSandboxOverview(sandboxId: string) {
console.log(`default: ${localSandbox.isCurrent}\n`);
}
const sandbox = await cliUtils.spinner(sandboxSvc.getSandbox(sandboxId));
const properties = await cliUtils.spinner(Promise.all(
sandbox.properties.map(p => sandboxSvc.getProperty(sandboxId, p.sandboxPropertyId)))
);

cliUtils.logWithBorder('Detailed information for the sandbox');

Expand All @@ -163,8 +166,9 @@ async function showSandboxOverview(sandboxId: string) {

cliUtils.logWithBorder('Sandbox Properties');

sandbox.properties.forEach(p => {
properties.forEach(p => {
console.log('sandbox-property-id: ' + p.sandboxPropertyId);
console.log('cpcode: ' + p.cpcode);
console.log(`request hostname(s): ${p.requestHostnames.join(', ')}\n`);
});
}
Expand Down Expand Up @@ -226,12 +230,20 @@ function parseToBoolean(str: string) {
}
}

async function updateHostnamesAndRules(requestHostnames, rulesFilePath, sandboxId, sandboxPropertyId) {
if (requestHostnames) {
async function updateProperty(sandboxId, sandboxPropertyId, requestHostnames, cpCode, rulesFilePath) {
if (requestHostnames || cpCode) {
const property = await cliUtils.spinner(sandboxSvc.getProperty(sandboxId, sandboxPropertyId), `Loading a property with id: ${sandboxPropertyId}`);
property.requestHostnames = parseHostnameCsv(requestHostnames);

if (requestHostnames) {
property.requestHostnames = parseHostnameCsv(requestHostnames);
}
if (cpCode) {
property.cpcode = cpCode;
}

await cliUtils.spinner(sandboxSvc.updateProperty(sandboxId, property), `Updating sandbox-property-id: ${sandboxPropertyId}`);
}

if (rulesFilePath) {
const rules = getJsonFromFile(rulesFilePath);
await cliUtils.spinner(sandboxSvc.updateRules(sandboxId, sandboxPropertyId, rules), 'Updating rules.');
Expand All @@ -253,12 +265,12 @@ function parseHostnameCsv(csv) {
.filter(hn => hn.length > 0);
}

async function addPropertyFromRules(sandboxId: string, papiFilePath: string, hostnames: Array<string>) {
async function addPropertyFromRules(sandboxId: string, papiFilePath: string, hostnames: Array<string>, cpCode: number) {
if (!fs.existsSync(papiFilePath)) {
cliUtils.logAndExit(1, `File: ${papiFilePath} does not exist.`);
}
const papiJson = getJsonFromFile(papiFilePath);
return await cliUtils.spinner(sandboxSvc.addPropertyFromRules(sandboxId, hostnames, papiJson), `adding sandbox property to ${sandboxId}`);
return await cliUtils.spinner(sandboxSvc.addPropertyFromRules(sandboxId, hostnames, cpCode, papiJson), `adding sandbox property to ${sandboxId}`);
}

async function createFromRules(papiFilePath: string, propForRules: string, hostnames: Array<string>, isClonable: boolean, name: string, cpcode: number) {
Expand Down Expand Up @@ -303,15 +315,15 @@ function parseHostnameSpecifier(hostnameSpecifier) {
return {hostname: hostnameSpecifier};
}

async function addPropertyToSandboxFromProperty(sandboxId: string, hostnames: Array<string>, propertySpecifier: string) {
async function addPropertyToSandboxFromProperty(sandboxId: string, hostnames: Array<string>, cpCode: number, propertySpecifier: string) {
const propertySpecObj = parsePropertySpecifier(propertySpecifier);
const msg = `Adding property from: ${JSON.stringify(propertySpecObj)}`;
return await cliUtils.spinner(sandboxSvc.addPropertyFromProperty(sandboxId, hostnames, propertySpecObj), msg);
return await cliUtils.spinner(sandboxSvc.addPropertyFromProperty(sandboxId, hostnames, cpCode, propertySpecObj), msg);
}

async function addPropertyToSandboxFromHostname(sandboxId: string, hostnames: Array<string>, hostname: string) {
async function addPropertyToSandboxFromHostname(sandboxId: string, hostnames: Array<string>, cpCode: number, hostname: string) {
const msg = `Adding property based on: ${hostname}`;
return await cliUtils.spinner(sandboxSvc.addPropertyFromProperty(sandboxId, hostnames, {hostname}), msg);
return await cliUtils.spinner(sandboxSvc.addPropertyFromProperty(sandboxId, hostnames, cpCode, {hostname}), msg);
}

async function createFromProperty(propertySpecifier: string, hostnames: Array<string>, isClonable: boolean, name: string, cpcode: number) {
Expand Down Expand Up @@ -344,8 +356,9 @@ function resolveRulesPath(recipeFilePath, rulesPath) {
}

async function createFromPropertiesRecipe(recipe, cpcode) {
const sandboxRecipe = recipe.sandbox;
const properties = sandboxRecipe.properties;
const cloneable = recipe.sandbox.clonable
const sandboxName = recipe.sandbox.name
const properties = recipe.sandbox.properties;

const firstProp = properties[0];
let propForRules;
Expand All @@ -360,7 +373,8 @@ async function createFromPropertiesRecipe(recipe, cpcode) {
}

console.log(`Creating sandbox and property 1 from recipe.`);
const r = await cliUtils.spinner(createRecipeSandboxAndProperty(firstProp, propForRules, sandboxRecipe, cpcode));
const r = await cliUtils.spinner(createRecipeSandboxAndProperty(firstProp, propForRules, cloneable, sandboxName,
cpcode || firstProp.cpcode));

for (let i = 1; i < properties.length; i++) {
try {
Expand Down Expand Up @@ -505,23 +519,23 @@ async function createFromRecipe(recipeFilePath, name, clonable, cpcode, originFr

function createRecipeProperty(rp, sandboxId) {
if (rp.property) {
return addPropertyToSandboxFromProperty(sandboxId, rp.requestHostnames, rp.property);
return addPropertyToSandboxFromProperty(sandboxId, rp.requestHostnames, rp.cpcode, rp.property);
} else if (rp.rulesPath) {
return addPropertyFromRules(sandboxId, rp.rulesPath, rp.requestHostnames);
return addPropertyFromRules(sandboxId, rp.rulesPath, rp.requestHostnames, rp.cpcode);
} else if (rp.hostname) {
return addPropertyToSandboxFromHostname(sandboxId, rp.requestHostnames, rp.hostname);
return addPropertyToSandboxFromHostname(sandboxId, rp.requestHostnames, rp.cpcode, rp.hostname);
} else {
cliUtils.logAndExit(1, 'Critical error with recipe property. Define the rulesPath or property.');
}
}

function createRecipeSandboxAndProperty(rp, propertyForRules, recipe, cpcode) {
function createRecipeSandboxAndProperty(rp, propertyForRules, isCloneable, sandboxName, cpcode) {
if (rp.property) {
return createFromProperty(rp.property, rp.requestHostnames, recipe.clonable, recipe.name, cpcode);
return createFromProperty(rp.property, rp.requestHostnames, isCloneable, sandboxName, cpcode);
} else if (rp.hostname) {
return createFromHostname(rp.hostname, rp.requestHostnames, recipe.clonable, recipe.name, cpcode);
return createFromHostname(rp.hostname, rp.requestHostnames, isCloneable, sandboxName, cpcode);
} else if (rp.rulesPath) {
return createFromRules(rp.rulesPath, propertyForRules, rp.requestHostnames, recipe.clonable, recipe.name, cpcode);
return createFromRules(rp.rulesPath, propertyForRules, rp.requestHostnames, isCloneable, sandboxName, cpcode);
} else {
cliUtils.logAndExit(1, 'Critical error with recipe property. Define the rulesPath or property.');
}
Expand Down Expand Up @@ -596,13 +610,13 @@ async function downloadClientIfNecessary() {
}
}

function addPropertyToSandbox(sandboxId, property, rulesPath, hostname, requestHostnames) {
function addPropertyToSandbox(sandboxId, property, rulesPath, hostname, requestHostnames, cpCode) {
if (property) {
return addPropertyToSandboxFromProperty(sandboxId, requestHostnames, property);
return addPropertyToSandboxFromProperty(sandboxId, requestHostnames, cpCode, property);
} else if (rulesPath) {
return addPropertyFromRules(sandboxId, rulesPath, requestHostnames);
return addPropertyFromRules(sandboxId, rulesPath, requestHostnames, cpCode);
} else if (hostname) {
return addPropertyToSandboxFromHostname(sandboxId, requestHostnames, hostname);
return addPropertyToSandboxFromHostname(sandboxId, requestHostnames, cpCode, hostname);
} else {
cliUtils.logAndExit(1, `Critical error while adding property to the sandbox : ${sandboxId} You need to define the rulesPath or property.`);
}
Expand Down Expand Up @@ -837,11 +851,13 @@ program
.description('Updates the sandbox’s property.')
.option('-r, --rules <file>', 'JSON file containing a PAPI rule tree.')
.option('-H, --requesthostnames <string>', 'Comma-delimited list of request hostnames within the sandbox.')
.option('-C, --cpcode <cpcode>', 'Specify an existing cpcode instead of letting the system generate a new one.')
.action(async function(sandboxId, sandboxPropertyId, options) {
const rules = options.rules;
const requestHostnames = options.requesthostnames;
const cpCode = options.cpcode;
try {
await updateHostnamesAndRules(requestHostnames, rules, sandboxId, sandboxPropertyId);
await updateProperty(sandboxId, sandboxPropertyId, requestHostnames, cpCode, rules);
console.log(`Successfully updated sandbox-id: ${sandboxId} sandbox-property-id: ${sandboxPropertyId}`);
} catch (e) {
handleException(e);
Expand All @@ -854,6 +870,7 @@ program
.option('-r, --rules <file>', 'JSON file containing a PAPI rule tree.')
.option('-c, --clonable <boolean>', 'Make this sandbox clonable? (Y/N)')
.option('-n, --name <string>', 'Name of sandbox.')
.option('-C, --cpcode <cpcode>', 'Specify an existing cpcode instead of letting the system generate a new one.')
.option('-H, --requesthostnames <string>', 'Comma-delimited list of request hostnames within the sandbox.')
.option('--recipe <file>', 'Path to `recipe.json` file.')
.action(async function(arg, options) {
Expand All @@ -876,12 +893,12 @@ program

await cliUtils.spinner(sandboxSvc.updateSandbox(sandbox), `updating sandbox-id: ${sandbox.sandboxId}`);

const propertyChange: boolean = !!options.requesthostnames || !!options.rules;
const propertyChange: boolean = !!options.cpcode || !!options.requesthostnames || !!options.rules;
if (propertyChange && sandbox.properties.length > 1) {
cliUtils.logAndExit(1, `Unable to update property as multiple were found (${sandbox.properties.length}). Use update-property to add additional properties to the sandbox.`);
}
const sandboxPropertyId = sandbox.properties[0].sandboxPropertyId;
await updateHostnamesAndRules(options.requesthostnames, options.rules, sandboxId, sandboxPropertyId);
await updateProperty(sandboxId, sandboxPropertyId, options.requesthostnames, options.cpcode, options.rules);
console.log(`Successfully updated sandbox-id: ${sandboxId}`)
} catch (e) {
handleException(e);
Expand Down Expand Up @@ -1003,13 +1020,15 @@ program
.option('-r, --rules <file>', 'JSON file containing a PAPI rule tree.')
.option('-p, --property <property-id | property-name:version>', 'Property to use. If you do not specify a version, the most recent version is used.')
.option('-o, --hostname <hostname>', 'The hostname of your Akamai property, such as www.example.com.')
.option('-C, --cpcode <cpcode>', 'Specify an existing cpcode instead of letting the system generate a new one.')
.option('-H, --requesthostnames <string>', 'Comma separated list of request hostnames.')
.action(async function(arg, options) {
helpExitOnNoArgs(options);
try {
const papiFilePath = options.rules;
const propertySpecifier = options.property;
const hostnameSpecifier = options.hostname;
const cpCode = options.cpcode;
const hostnamesCsv = options.requesthostnames;

let sandboxId;
Expand All @@ -1031,7 +1050,7 @@ program
}
const hostnames = hostnamesCsv ? parseHostnameCsv(hostnamesCsv) : undefined;

await addPropertyToSandbox(sandboxId, propertySpecifier, papiFilePath, hostnameSpecifier, hostnames);
await addPropertyToSandbox(sandboxId, propertySpecifier, papiFilePath, hostnameSpecifier, hostnames, cpCode);
} catch (e) {
handleException(e);
}
Expand Down
15 changes: 2 additions & 13 deletions src/service/sandbox-client-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,6 @@ export async function hasSandboxFolder(sandboxName) {
return files.some(fileItem => fileItem.toLowerCase() === sandboxName.toLowerCase());
}


function versionToNumber(version) {
return version.split('.').map(s => parseInt(s)).reduce((acc, value) => 100 * acc + value);
}

export async function executeSandboxClient(printLogs) {
const loggingPath = getLogPath();
const loggingFilePath = path.join(loggingPath, 'sandbox-client.log');
Expand All @@ -199,14 +194,8 @@ export async function executeSandboxClient(printLogs) {
`"${await envUtils.getJavaExecutablePath()}"`
];

if (versionToNumber(CONNECTOR_VERSION) >= versionToNumber('1.3.1')) {
args.push(`-DLOG_PATH="${loggingPath}"`);
args.push(`-DLOGGING_CONFIG_FILE="${LOG_CONFIG_FILE}"`);
} else {
args.push(`-Dlogging.file.path="${loggingPath}"`);
args.push(`-Dlogging.config="${LOG_CONFIG_FILE}"`);
}

args.push(`-DLOG_PATH="${loggingPath}"`);
args.push(`-DLOGGING_CONFIG_FILE="${LOG_CONFIG_FILE}"`);
args.push(`-jar "${JAR_FILE_PATH}"`);
args.push(`--config="${configPath}"`);

Expand Down
8 changes: 6 additions & 2 deletions src/service/sandbox-svc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,25 @@ export function createFromRules(papiRules, fromPropertyObj, requestHostnames, na
return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body);
}

export function addPropertyFromRules(sandboxId: string, requestHostnames, papiRules) {
export function addPropertyFromRules(sandboxId: string, requestHostnames, cpCode: number, papiRules) {
const bodyObj = {
cpCode: cpCode,
requestHostnames: requestHostnames,
createFromRules: papiRules
};
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body);
}

export function addPropertyFromProperty(sandboxId: string, requestHostnames, fromPropertyObj) {
export function addPropertyFromProperty(sandboxId: string, requestHostnames, cpCode: number, fromPropertyObj) {
const bodyObj = {
createFromProperty: fromPropertyObj,
};
if (requestHostnames) {
bodyObj['requestHostnames'] = requestHostnames;
}
if (cpCode) {
bodyObj['cpcode'] = cpCode;
}
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body);
}

Expand Down

0 comments on commit 26efb40

Please sign in to comment.