Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EDB-2660: Implement EdgeKV CLI Freeflow support #133

Merged
merged 10 commits into from
Jul 24, 2023
Merged
4 changes: 2 additions & 2 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.7.3",
"version": "1.7.4",
"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
57 changes: 43 additions & 14 deletions src/edgekv/ekv-cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
import * as envUtils from '../utils/env-utils';
import * as cliUtils from '../utils/cli-utils';
import * as configUtils from '../utils/config-utils';
import {
ORDER_BY,
MAX_ITEMS,
GROUP_ID,
RETENTION,
GEO_LOCATION,
STAGING,
PRODUCTION,
EW_IDS,
EXPIRY,
NAMESPACE,
import {
ORDER_BY,
MAX_ITEMS,
GROUP_ID,
RETENTION,
GEO_LOCATION,
STAGING,
PRODUCTION,
EW_IDS,
EXPIRY,
NAMESPACE,
SAVE_PATH } from './../utils/constants';
import { SANDBOX_ID } from '../utils/constants';
import * as kvCliHandler from './ekv-handler';
import { ekvJsonOutput } from './client-manager';
import * as httpEdge from '../cli-httpRequest';
import * as pkginfo from '../../package.json';
import { Command } from 'commander';
import {validateNamespaceDataAccessPolicy} from './ekv-helper';

const program = new Command();
const currentYear = new Date().getFullYear();
Expand Down Expand Up @@ -118,10 +119,14 @@ program
program
.command('initialize')
.description('Initialize EdgeKV for the first time')
.option(
'--dataAccessPolicy <database_data_access_policy>',
'`dataAccessPolicy` option must be of the form `restrictDataAccess=<bool>,allowNamespacePolicyOverride=<bool>` where <bool> can be true or false.'
)
.alias('init')
.action(async function () {
.action(async function (options) {
try {
await kvCliHandler.initializeEdgeKv();
await kvCliHandler.initializeEdgeKv(options['dataAccessPolicy']);
} catch (e) {
cliUtils.logAndExit(1, e);
}
Expand Down Expand Up @@ -413,19 +418,25 @@ create
'--geoLocation <geolocation>',
'Specifies the persistent storage location for data when creating a namespace on the production network. This can help optimize performance by storing data where most or all of your users are located. The value defaults to `US` on the `STAGING` and `PRODUCTION` networks.'
)
.option(
'--dataAccessPolicy <namespace_data_access_policy>',
'`dataAccessPolicy` option must be of the form `restrictDataAccess=<bool>` where <bool> can be true or false.'
)
.description('Creates an EdgeKV namespace')
.action(async function (environment, namespace, options) {
options['retention'] = options.retention || configUtils.searchProperty(RETENTION);
options['groupId'] = options.groupId || configUtils.searchProperty(GROUP_ID);
options['geolocation'] = options.geolocation || configUtils.searchProperty(GEO_LOCATION);
options['dataAccessPolicy'] = options.dataAccessPolicy ? validateNamespaceDataAccessPolicy(options.dataAccessPolicy) : undefined;

try {
await kvCliHandler.createNamespace(
environment,
namespace,
options.retention,
options.groupId,
options.geoLocation
options.geoLocation,
options.dataAccessPolicy
);
} catch (e) {
cliUtils.logAndExit(1, e);
Expand Down Expand Up @@ -559,6 +570,24 @@ modify
cliUtils.logAndExit(0, copywrite);
});

modify
.command('db')
.requiredOption(
'--dataAccessPolicy <database_data_access_policy>',
'`dataAccessPolicy` option must be of the form `restrictDataAccess=<bool>,allowNamespacePolicyOverride=<bool>` where <bool> can be true or false.'
)
.description('Modify the database data access policy')
.action(async function (options) {
try {
await kvCliHandler.updateDatabase(options['dataAccessPolicy']);
} catch (e) {
cliUtils.logAndExit(1, e);
}
})
.on('--help', function () {
cliUtils.logAndExit(0, copywrite);
});

const download = program
.command('download')
.alias('dnld')
Expand Down
53 changes: 48 additions & 5 deletions src/edgekv/ekv-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as response from './ekv-response';
import * as ekvhelper from './ekv-helper';
import * as edgeWorkersSvc from '../edgeworkers/ew-service';
import { ekvJsonOutput } from './client-manager';
import {validateDataAccessPolicy} from './ekv-helper';

export async function listNameSpaces(
environment: string,
Expand Down Expand Up @@ -33,6 +34,7 @@ export async function listNameSpaces(
RetentionPeriod: retentionPeriod,
GeoLocation: value['geoLocation'],
AccessGroupId: groupId,
'Namespace dataAccessPolicy': value['dataAccessPolicy'] ? 'restrictDataAccess=' + value['dataAccessPolicy']['restrictDataAccess'] + ', policyType=' + value['dataAccessPolicy']['policyType'] : 'N/A',
});
} else {
nsListResp.push({ NamespaceId: value['namespace'] });
Expand Down Expand Up @@ -84,7 +86,8 @@ export async function createNamespace(
nameSpace: string,
retention: number,
groupId: number,
geoLocation: string
geoLocation: string,
dataAccessPolicy: object = undefined
) {
if (!groupId) {
cliUtils.logAndExit(
Expand All @@ -102,7 +105,8 @@ export async function createNamespace(
nameSpace,
retentionPeriod,
groupId,
geoLocation
geoLocation,
dataAccessPolicy
),
`Creating namespace for environment ${environment}`
);
Expand Down Expand Up @@ -196,9 +200,13 @@ export async function updateNameSpace(
}
}

export async function initializeEdgeKv() {
export async function initializeEdgeKv(dataAccessPolicyStr: string) {
let dataAccessPolicy;
if (dataAccessPolicyStr) {
dataAccessPolicy = validateDataAccessPolicy(dataAccessPolicyStr);
}
const initializedEdgeKv = await cliUtils.spinner(
edgekvSvc.initializeEdgeKV(),
edgekvSvc.initializeEdgeKV(dataAccessPolicy),
'Initializing EdgeKV...'
);

Expand Down Expand Up @@ -289,6 +297,41 @@ export async function getInitializationStatus() {
}
}

export async function updateDatabase(dataAccessPolicyStr: string) {
const dataAccessPolicy = validateDataAccessPolicy(dataAccessPolicyStr);
const updateDataAccessPolicy = await cliUtils.spinner(
edgekvSvc.updateDatabase(dataAccessPolicy),
'Updating database data access policy...'
);

if (updateDataAccessPolicy.data != undefined && !updateDataAccessPolicy.isError) {
const updateRespBody = updateDataAccessPolicy.data;

const status = updateDataAccessPolicy.status;
let msg;
if (Object.prototype.hasOwnProperty.call(updateRespBody, 'accountStatus')) {
const accountStatus = updateRespBody['accountStatus'];
if (status == 200 && accountStatus == 'INITIALIZED') {
msg = 'EdgeKV database data access policy successfully modified';
} else {
msg = 'EdgeKV database data access policy was not modified';
}
}
if (ekvJsonOutput.isJSONOutputMode()) {
ekvJsonOutput.writeJSONOutput(0, msg, updateRespBody);
} else {
cliUtils.logWithBorder(msg);
response.logInitialize(updateRespBody);
}
} else {
const errorReason = `${updateDataAccessPolicy.error_reason}`;
response.logError(
updateDataAccessPolicy,
`ERROR: EdgeKV database update failed (${errorReason}) [TraceId: ${updateDataAccessPolicy.traceId}]`
);
}
}

export async function writeItemToEdgeKV(
environment: string,
nameSpace: string,
Expand Down Expand Up @@ -652,7 +695,7 @@ export async function listAuthGroups(options: {
ewGroups = await getEwGroups(options.groupIds);
}
const msg = `User has the following permission access for group: ${groupId}`;

if (ekvJsonOutput.isJSONOutputMode()) {
const obj = {
authGroups,
Expand Down
Loading