Skip to content

Commit

Permalink
Merge pull request #21 from akamai/apiPathChange
Browse files Browse the repository at this point in the history
sandbox-api path change
  • Loading branch information
bradforj287 committed Jan 23, 2019
2 parents d3158e2 + 146f9b4 commit ccc15f4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 55 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ Options:
## Customizable Template
You can use this example "recipe" to quickly customize the sandbox to your development environment. Copy the code below and paste it into a text editor.

```{
```
{
"sandbox":{
"clonable":true,
"properties":[
Expand Down Expand Up @@ -189,7 +190,7 @@ You can use this example "recipe" to quickly customize the sandbox to your devel
```
1. Edit the information according to your development environment and property specifications.
1. Save the file with a `.json` extension (e.g., `example_recipe.json`)
1. Run this command on your file ` ./akamai-sandbox create --recipe=./example/example_recipe.json ` to instantiate the sandbox client according to the defined specifications.
1. Run this command on your file `akamai sandbox create --recipe=./example/example_recipe.json ` to instantiate the sandbox client according to the defined specifications.

## Resources
For more information on Sandbox, refer to the [User Guide](https://learn.akamai.com/en-us/webhelp/sandbox/sandbox-user-guide/).
For more information on Sandbox, refer to the [User Guide](https://learn.akamai.com/en-us/webhelp/sandbox/sandbox-user-guide/).
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.1.6",
"version": "1.1.7",
"description": "A tool to help manage Akamai Sandboxes. Uses the sandbox {OPEN} API.",
"repository": "https://github.com/akamai/cli-sandbox",
"scripts": {
Expand Down
41 changes: 16 additions & 25 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ async function showRemoteSandboxes() {
console.log("Loading sandboxes (via OPEN): \n");
var localIds = new Set();
sandboxClientManager.getAllSandboxes().forEach(sb => localIds.add(sb.sandboxId));
var result = await cliUtils.spinner(sandboxSvc.getAllSandboxes());
var sandboxes = result.sandboxes.map(sb => {
const allSandboxesResult = await cliUtils.spinner(sandboxSvc.getAllSandboxes());
const quota = allSandboxesResult.quota;
var sandboxes = allSandboxesResult.result.sandboxes.map(sb => {
return {
has_local: localIds.has(sb.sandboxId) ? "Y" : "N",
name: sb.name,
Expand All @@ -142,6 +143,7 @@ async function showRemoteSandboxes() {
}
});
showSandboxesTable(sandboxes);
console.log(`${quota.used}/${quota.max} sandboxes used`)
}

program
Expand Down Expand Up @@ -313,29 +315,18 @@ program
});

program
.command('delete <sandbox-identifier>')
.command('delete <sandbox-id>')
.description('deletes this sandbox')
.action(async function (arg, options) {
.action(async function (sandboxId, options) {
try {
var results = sandboxClientManager.searchLocalSandboxes(arg);
if (results.length > 1) {
logAndExit(`${results.length} match input. Please be more specific.`);
} else {
if (!await cliUtils.confirm('are you sure you want to delete this sandbox?')) {
return;
}

if (results.length == 1) {
var sb = results[0];
var progressMsg = `deleting sandboxId: ${sb.sandboxId} name: ${sb.name}`;
await cliUtils.spinner(sandboxSvc.deleteSandbox(sb.sandboxId), progressMsg);
console.log("removing local files");
sandboxClientManager.flushLocalSandbox(sb.sandboxId);
} else {
var msg = `deleting sandboxId: ${arg}`;
await cliUtils.spinner(sandboxSvc.deleteSandbox(arg), msg);
}
if (!await cliUtils.confirm('are you sure you want to delete this sandbox?')) {
return;
}

var progressMsg = `deleting sandboxId: ${sandboxId}`;
await cliUtils.spinner(sandboxSvc.deleteSandbox(sandboxId), progressMsg);

sandboxClientManager.flushLocalSandbox(sandboxId);
} catch (e) {
console.error(e);
}
Expand All @@ -345,7 +336,7 @@ function parseToBoolean(str: string) {
if (!str) {
return false;
}
var r = str.trim().toLowerCase();
const parsedInput = str.trim().toLowerCase();
var strToBool = new Map([
['true', true],
['t', true],
Expand All @@ -356,10 +347,10 @@ function parseToBoolean(str: string) {
['n', false],
['no', false],
]);
if (!strToBool.has(str)) {
if (!strToBool.has(parsedInput)) {
logAndExit(`unable to determine boolean from input: ${str} please use y/n`)
} else {
return strToBool.get(str);
return strToBool.get(parsedInput);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/service/sandbox-client-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ function getLogPath() {
}

export function flushLocalSandbox(sandboxId: string) {
if (!datastore.hasRecord(sandboxId)) {
return;
}

console.log("removing local files");
var sb = datastore.getRecord(sandboxId);
var folderPath = path.join(SANDBOXES_DIR, sb.folder);
fsExtra.removeSync(folderPath);
Expand Down
4 changes: 4 additions & 0 deletions src/service/sandbox-datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class SandboxDatastore {
return this.data[sandboxId];
}

hasRecord(sandboxId: string): boolean {
return !!this.getRecord(sandboxId);
}

deleteRecord(sandboxId: string) {
delete this.data[sandboxId];
this.flushToFile();
Expand Down
63 changes: 37 additions & 26 deletions src/service/sandbox-svc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as cliUtils from '../utils/cli-utils';

var accountKey: string = null;

const SANDBOX_API_BASE = '/sandbox-api/v1';

export function setAccountKey(account: string) {
accountKey = account;
}
Expand All @@ -17,7 +19,7 @@ function sendEdgeRequest(pth: string, method: string, body, headers) {
if (accountKey) {
path += `?accountSwitchKey=${accountKey}`;
}
return new Promise(
return new Promise<any>(
(resolve, reject) => {
edge.auth({
path,
Expand All @@ -30,12 +32,11 @@ function sendEdgeRequest(pth: string, method: string, body, headers) {
if (error) {
reject(error);
} else if (isOkStatus(response.statusCode)) {
if (!body) {
resolve();
} else {
var responseObject = JSON.parse(body);
resolve(responseObject);
}
var obj: any = {
response,
body: !!body ? JSON.parse(body) : undefined
};
resolve(obj);
} else {
try {
var errorObj = JSON.parse(body);
Expand Down Expand Up @@ -70,27 +71,37 @@ function del(path: string) {
}

export function deleteSandbox(sandboxId: string) {
return sendEdgeRequest(`/devpops-api/v1/sandboxes/${sandboxId}`, 'DELETE', '', {});
return sendEdgeRequest(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}`, 'DELETE', '', {}).then(r => r.body);
}

export function cloneSandbox(sandboxId: string, name: string, clonable = false) {
const body = {
name,
isClonable: clonable
};
return postJson(`/devpops-api/v1/sandboxes/${sandboxId}/clone`, body);
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/clone`, body).then(r => r.body);
}

export function getAllSandboxes() {
return getJson(`/devpops-api/v1/sandboxes`);
return getJson(`${SANDBOX_API_BASE}/sandboxes`).then(r => {
const limit = parseInt(r.response.headers['x-limit-sandboxes-limit']);
const remaining = parseInt(r.response.headers['x-limit-sandboxes-remaining']);
return {
quota: {
max: limit,
used: limit - remaining
},
result: r.body
}
});
}

export function getSandbox(sandboxId: string) {
return getJson(`/devpops-api/v1/sandboxes/${sandboxId}`);
return getJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}`).then(r => r.body);
}

export function updateSandbox(sandbox) {
return putJson(`/devpops-api/v1/sandboxes/${sandbox.sandboxId}`, sandbox);
return putJson(`${SANDBOX_API_BASE}/sandboxes/${sandbox.sandboxId}`, sandbox).then(r => r.body);
}

export function createFromRules(papiRules, requestHostnames, name, isClonable) {
Expand All @@ -100,15 +111,15 @@ export function createFromRules(papiRules, requestHostnames, name, isClonable) {
createFromRules: papiRules,
isClonable: isClonable
};
return postJson('/devpops-api/v1/sandboxes', bodyObj);
return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body);
}

export function addPropertyFromRules(sandboxId: string, requestHostnames, papiRules) {
var bodyObj = {
requestHostnames: requestHostnames,
createFromRules: papiRules
};
return postJson(`/devpops-api/v1/sandboxes/${sandboxId}/properties`, bodyObj);
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body);
}

export function addPropertyFromProperty(sandboxId: string, requestHostnames, fromPropertyObj) {
Expand All @@ -118,7 +129,7 @@ export function addPropertyFromProperty(sandboxId: string, requestHostnames, fro
if (requestHostnames) {
bodyObj['requestHostnames'] = requestHostnames;
}
return postJson(`/devpops-api/v1/sandboxes/${sandboxId}/properties`, bodyObj);
return postJson(`${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties`, bodyObj).then(r => r.body);
}

export function createFromProperty(requestHostnames, name, isClonable, fromPropertyObj) {
Expand All @@ -130,33 +141,33 @@ export function createFromProperty(requestHostnames, name, isClonable, fromPrope
if (requestHostnames) {
bodyObj['requestHostnames'] = requestHostnames;
}
return postJson('/devpops-api/v1/sandboxes', bodyObj);
return postJson(`${SANDBOX_API_BASE}/sandboxes`, bodyObj).then(r => r.body);
}

export function getRules(sandboxId: string, sandboxPropertyId: string) {
var endpoint = `/devpops-api/v1/sandboxes/${sandboxId}/properties/${sandboxPropertyId}/rules`;
return getJson(endpoint);
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}/rules`;
return getJson(endpoint).then(r => r.body);
}

export function updateRules(sandboxId: string, sandboxPropertyId: string, rules) {
var endpoint = `/devpops-api/v1/sandboxes/${sandboxId}/properties/${sandboxPropertyId}/rules`;
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}/rules`;
var body = {
rules: rules.rules ? rules.rules : rules
};
return putJson(endpoint, body);
return putJson(endpoint, body).then(r => r.body);
}

export function getProperty(sandboxId: string, sandboxPropertyId: string) {
var endpoint = `/devpops-api/v1/sandboxes/${sandboxId}/properties/${sandboxPropertyId}`;
return getJson(endpoint);
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}`;
return getJson(endpoint).then(r => r.body);
}

export function updateProperty(sandboxId, propertyObj) {
var endpoint = `/devpops-api/v1/sandboxes/${sandboxId}/properties/${propertyObj.sandboxPropertyId}`;
return putJson(endpoint, propertyObj);
var endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${propertyObj.sandboxPropertyId}`;
return putJson(endpoint, propertyObj).then(r => r.body);
}

export function deleteProperty(sandboxId, sandboxPropertyId) {
const endpoint = `/devpops-api/v1/sandboxes/${sandboxId}/properties/${sandboxPropertyId}`;
return del(endpoint);
const endpoint = `${SANDBOX_API_BASE}/sandboxes/${sandboxId}/properties/${sandboxPropertyId}`;
return del(endpoint).then(r => r.body);
}

0 comments on commit ccc15f4

Please sign in to comment.