Skip to content

Commit

Permalink
code to enable the vscode and intelliJ plugins headers to track pendo…
Browse files Browse the repository at this point in the history
… events in portal-api (#119)

* added IDE extension header
* validate EwId in command update-id (#115)
* Bump json5 from 2.2.1 to 2.2.3
* update year in copyright text
  • Loading branch information
hkambham authored Jan 31, 2023
1 parent c3771f2 commit a1b193d
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"commands": [
{
"name": "edgeworkers",
"version": "1.5.2",
"version": "1.6.2",
"aliases": ["ew", "edgeworkers"],
"description": "Manage Akamai EdgeWorkers code bundles."
},
{
"name": "edgekv",
"version": "1.5.2",
"version": "1.6.2",
"aliases": ["ekv", "edgekv"],
"description": "Manage Akamai EdgeKV database."
}
Expand Down
16 changes: 8 additions & 8 deletions 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-edgeworkers-cli",
"version": "1.6.1",
"version": "1.6.2",
"description": "A tool that makes it easier to manage Akamai EdgeWorkers code bundles and EdgeKV databases. Call the EdgeWorkers and EdgeKV API from the command line.",
"repository": "https://github.com/akamai/cli-edgeworkers",
"scripts": {
Expand Down
10 changes: 9 additions & 1 deletion src/cli-httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { promiseTimeout } from './utils/timeout-promise';
import {
EDGEWORKERS_API_BASE,
EDGEWORKERS_CLIENT_HEADER,
EDGEWORKERS_IDE_HEADER,
} from './edgeworkers/ew-service';
import { EDGEKV_API_BASE } from './edgekv/ekv-service';

export let accountKey = null;
export let ideExtensionType = null;
export const timeoutVal = 120000;
const versionHeader = 'X-AK-EDGEKV-CLI-VER';
const ekvcliHeader = 'X-AK-EDGEKV-CLI';
Expand All @@ -16,6 +18,9 @@ import * as pjson from '../package.json';
export function setAccountKey(account: string) {
accountKey = account;
}
export function setIdeExtension(ideExtension: string) {
ideExtensionType = ideExtension;
}

/**
* This is for non-Tarball gets and all POST/PUT actions that return JSON or string bodies
Expand All @@ -42,7 +47,10 @@ export function sendEdgeRequest(
if (path.includes(EDGEWORKERS_API_BASE)) {
headers[EDGEWORKERS_CLIENT_HEADER] = 'CLI';
}

if (ideExtensionType) {
// Check if the ide extension is calling the CLi then add to header
headers[EDGEWORKERS_IDE_HEADER] = ideExtensionType;
}
if (path.includes(EDGEKV_API_BASE)) {
headers[versionHeader] = pjson.version;
headers[ekvcliHeader] = metricType;
Expand Down
11 changes: 6 additions & 5 deletions src/edgekv/ekv-cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import * as httpEdge from '../cli-httpRequest';
import * as pkginfo from '../../package.json';
import { Command } from 'commander';
const program = new Command();
const currentYear = new Date().getFullYear();
const copywrite =
'\nCopyright (c) 2019-2021 Akamai Technologies, Inc. Licensed under Apache 2 license.\nYour use of Akamai\'s products and services is subject to the terms and provisions outlined in Akamai\'s legal policies.\nVisit http://github.com/akamai/cli-edgeworkers for detailed documentation';
'\nCopyright (c) 2019-' + currentYear + ' Akamai Technologies, Inc. Licensed under Apache 2 license.\nYour use of Akamai\'s products and services is subject to the terms and provisions outlined in Akamai\'s legal policies.\nVisit http://github.com/akamai/cli-edgeworkers for detailed documentation';
program
.version(pkginfo.version)
.description(pkginfo.description)
Expand Down Expand Up @@ -65,11 +66,11 @@ program
const helper = program.createHelp();
program.configureHelp({
optionDescription: () => '',
optionTerm: (cmd) =>
optionTerm: (cmd) =>
helper.optionTerm(cmd) + '\n\t' + helper.optionDescription(cmd),

subcommandDescription: () => '' ,
subcommandTerm: (cmd) =>
subcommandTerm: (cmd) =>
helper.subcommandTerm(cmd) + '\n\t' + helper.subcommandDescription(cmd),
});

Expand Down Expand Up @@ -424,7 +425,7 @@ create
)
.option(
'--save_path <save_path>',
'Path specifying where to save the edgekv_tokens.js token file.'
'Path specifying where to save the edgekv_tokens.js token file.'
)
.option(
'-o, --overwrite',
Expand Down
22 changes: 15 additions & 7 deletions src/edgeworkers/ew-cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import * as httpEdge from '../cli-httpRequest';
import { ewJsonOutput } from './client-manager';
import * as pkginfo from '../../package.json';
import { Command } from 'commander';
import {exec} from 'child_process';
const program = new Command();
import * as path from 'path';
import * as os from 'os';
import isValidDomain from 'is-valid-domain';
const copywrite = '\nCopyright (c) 2019-2021 Akamai Technologies, Inc. Licensed under Apache 2 license.\nYour use of Akamai\'s products and services is subject to the terms and provisions outlined in Akamai\'s legal policies.\nVisit http://github.com/akamai/cli-edgeworkers for detailed documentation';
const currentYear = new Date().getFullYear();
const copywrite = '\nCopyright (c) 2019-' + currentYear + ' Akamai Technologies, Inc. Licensed under Apache 2 license.\nYour use of Akamai\'s products and services is subject to the terms and provisions outlined in Akamai\'s legal policies.\nVisit http://github.com/akamai/cli-edgeworkers for detailed documentation';

/* ========== EdgeWorkers CLI Program Commands ========== */
program
Expand All @@ -20,6 +24,7 @@ program
.option('--json [path]', 'Write command output to JSON file at given path, otherwise written to CLI cache directory')
.option('--jsonout', 'Write command output as JSON to stdout')
.option('--accountkey <account-id>', 'internal parameter')
.option('--ideExtension <ideExtensionTYpe>', 'extension parameter')
.option('--timeout <timeout>', 'Use this for custom timeout')
.on('option:debug', function () {
envUtils.setDebugMode(true);
Expand All @@ -41,6 +46,9 @@ program
.on('option:accountkey', function (key) {
httpEdge.setAccountKey(key);
})
.on('option:ideExtension', function (ideExtensionType) {
httpEdge.setIdeExtension(ideExtensionType);
})
.on('option:timeout', function (timeout) {
envUtils.setTimeout(timeout);
})
Expand All @@ -58,11 +66,11 @@ program
const helper = program.createHelp();
program.configureHelp({
optionDescription: () => '',
optionTerm: (cmd) =>
optionTerm: (cmd) =>
helper.optionTerm(cmd) + '\n\t' + helper.optionDescription(cmd),

subcommandDescription: () => '' ,
subcommandTerm: (cmd) =>
subcommandTerm: (cmd) =>
helper.subcommandTerm(cmd) + '\n\t' + helper.subcommandDescription(cmd),
});

Expand All @@ -75,7 +83,7 @@ program
cliUtils.logAndExit(0, copywrite);
}
else {
const command = (program.commands.find(c => c.name() == arg))
const command = (program.commands.find(c => c.name() == arg))
? program.commands.find(c => c.name() == arg)
: program.commands.find(c => c.aliases()[0] == arg);
if (!command) {
Expand Down Expand Up @@ -113,7 +121,7 @@ program
.option('-restier, --resourceTierId <resourceTierId>', 'Filter EdgeWorkers by resource tiers')
.action(async function (ewId, options) {
try {
await cliHandler.showEdgeWorkerIdOverview(ewId, options.groupId, options.resourceTierId);
cliHandler.showEdgeWorkerIdOverview(ewId, options.groupId, options.resourceTierId);
} catch (e) {
cliUtils.logAndExit(1, e);
}
Expand Down Expand Up @@ -384,7 +392,7 @@ program
.description('De-activate a version for a given EdgeWorker ID on an Akamai Network')
.alias('deact')
.action(async function (ewId, network, versionId) {

// Network must use correct keyword STAGING|PRODUCTION
if (network.toUpperCase() !== cliUtils.staging && network.toUpperCase() !== cliUtils.production)
cliUtils.logAndExit(1, `ERROR: Network parameter must be either staging or production - was: ${network}`);
Expand Down Expand Up @@ -447,7 +455,7 @@ exclusive to the --acl option; only use one or the other.')
// deprecation msg for people using older CLI. This will be removed eventually.
if (!isValidDomain(hostName)) {
cliUtils.logAndExit(1, 'ERROR: Creating auth token with secret is deprecated with version CLI 1.1.0. Please use your host name to create an authentication token.');
}
}

await cliHandler.createAuthToken(hostName, options);
} catch (e) {
Expand Down
5 changes: 5 additions & 0 deletions src/edgeworkers/ew-error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cliUtils from '../utils/cli-utils';
import {ErrorMessage} from '../utils/http-error-message';

export function handleError(err, commandId) {
Expand Down Expand Up @@ -59,4 +60,8 @@ export function handleError(err, commandId) {

}
}
}

export function invalidParameterError(commandId, errDetail=null) {
return handleError(cliUtils.toJsonPretty({status: 400, detail: errDetail}), commandId);
}
6 changes: 6 additions & 0 deletions src/edgeworkers/ew-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from 'fs';

export const EDGEWORKERS_API_BASE = '/edgeworkers/v1';
export const EDGEWORKERS_CLIENT_HEADER = 'X-EW-CLIENT';
export const EDGEWORKERS_IDE_HEADER = 'X-EW-IDE';
const DEFAULT_EW_TIMEOUT = 120000;

// This is only for fetching tarball bodies
Expand All @@ -26,6 +27,7 @@ function fetchTarball(
path += `${qs}accountSwitchKey=${accountKey}`;
}
headers[EDGEWORKERS_CLIENT_HEADER] = 'CLI';
// headers[EDGEWORKERS_IDE_HEADER] = 'VSCODE';
headers['Accept'] = 'application/gzip';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -191,6 +193,10 @@ export function updateEdgeWorkerId(
name: string,
resourceTierId: string
) {
if(!cliUtils.isValidEwId(ewId)) {
return error.invalidParameterError('UPDATE_EW');
}

const body = { groupId: groupId, name: name };
if (resourceTierId != undefined && resourceTierId != null) {
body['resourceTierId'] = resourceTierId;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/cli-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,8 @@ export function sortObjectArray(
}
});
}

export function isValidEwId(edgeworkerId: string) {
const ewIdFormat = new RegExp('^[1-9][0-9]+');
return ewIdFormat.test(edgeworkerId);
}
16 changes: 16 additions & 0 deletions tests/cli-httpRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as envUtils from '../src/utils/env-utils';

import {
accountKey,
ideExtensionType,
setAccountKey,
setIdeExtension,
sendEdgeRequest,
postJson,
putJson,
Expand Down Expand Up @@ -64,7 +66,21 @@ describe('cli-httpRequest tests', () => {
setAccountKey('');
});
});
describe('testing setIDEExtension', () => {
test('ide extension should be null by default', () => {
expect(ideExtensionType).toBeNull();
});

test('string should be set successfully', () => {
const ide = 'vscode';
setIdeExtension(ide);
expect(ideExtensionType).toBe('vscode');
});

afterAll(() => {
setIdeExtension('');
});
});
describe('testing sendEdgeRequest', () => {
test('successful response should return 200 series status', async () => {
const authSpy = edgeGrid.auth;
Expand Down
17 changes: 17 additions & 0 deletions tests/utils/cli-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as cliUtils from '../../src/utils/cli-utils';

describe('cli utils tests', () => {
describe('isValidEwId', () => {
it('should return true for valid ewId', () => {
expect(cliUtils.isValidEwId('123456')).toBe(true);
});

it('should return false for invalid ewId', () => {
expect(cliUtils.isValidEwId('wrongEwId')).toBe(false);
});

it('should return false for ewId starting with zero', () => {
expect(cliUtils.isValidEwId('0123456')).toBe(false);
});
});
});

0 comments on commit a1b193d

Please sign in to comment.