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

EW-10370 Adding metrics to the header #64

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/cli-httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import * as envUtils from './utils/env-utils';
import * as cliUtils from './utils/cli-utils';
import { promiseTimeout } from './utils/timeout-promise';
import { EDGEWORKERS_API_BASE, EDGEWORKERS_CLIENT_HEADER } from './edgeworkers/ew-service';
import { EDGEKV_API_BASE } from './edgekv/ekv-service';

export var accountKey: string = null;
export var timeoutVal: number = 120000;
const versionHeader = "X-AK-EDGEKV-CLI-VER"
const ekvcliHeader = "X-AK-EDGEKV-CLI"
let pjson = require('../package.json');

export function setAccountKey(account: string) {
accountKey = account;
Expand All @@ -14,7 +18,7 @@ export function setAccountKey(account: string) {
* This is for non-Tarball gets and all POST/PUT actions that return JSON or string bodies
* for both edge CLI and edge KV CLI. This method authenticates, sends request and returns promise
*/
export function sendEdgeRequest(pth: string, method: string, body, headers, timeout: number) {
export function sendEdgeRequest(pth: string, method: string, body, headers, timeout: number, metricType?: string) {
const edge = envUtils.getEdgeGrid();
var path = pth;
var qs: string = "&";
Expand All @@ -28,6 +32,12 @@ export function sendEdgeRequest(pth: string, method: string, body, headers, time
headers[EDGEWORKERS_CLIENT_HEADER] = "CLI";
}

if (path.includes(EDGEKV_API_BASE)) {
headers[versionHeader] = pjson.version;
headers[ekvcliHeader] = metricType;
}


let servicePromise = function () {
return new Promise<any>(
(resolve, reject) => {
Expand Down Expand Up @@ -68,24 +78,24 @@ export function sendEdgeRequest(pth: string, method: string, body, headers, time
return promiseTimeout(timeout, servicePromise());
}

export function postJson(path: string, body, timeout: number) {
export function postJson(path: string, body, timeout: number, metricType?: string) {
return sendEdgeRequest(path, 'POST', body, {
'Content-Type': 'application/json'
}, timeout);
}, timeout, metricType);
}

export function putJson(path: string, body, timeout: number) {
export function putJson(path: string, body, timeout: number, metricType?: string) {
return sendEdgeRequest(path, 'PUT', body, {
'Content-Type': 'application/json'
}, timeout);
}, timeout, metricType);
}

export function getJson(path: string, timeout: number) {
return sendEdgeRequest(path, 'GET', '', {}, timeout);
export function getJson(path: string, timeout: number, metricType?: string ) {
return sendEdgeRequest(path, 'GET', '', {}, timeout, metricType);
}

export function deleteReq(path: string, timeout: number) {
return sendEdgeRequest(path, 'DELETE', '', {}, timeout);
export function deleteReq(path: string, timeout: number, metricType?: string) {
return sendEdgeRequest(path, 'DELETE', '', {}, timeout, metricType);
}

export function isOkStatus(code) {
Expand Down
21 changes: 21 additions & 0 deletions src/edgekv/ekv-metricFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const ekvMetrics = {
listNameSpaces: "List/ns",
createNamespace: "Create/ns",
showNamespace: "Show/ns",
updateNamespace: "Update/ns",
initialize: "Init/ekv",
showInitStatus: "Show/status",
writeItem: "Write/item",
readItem: "Read/item",
deleteItem: "Delete/item",
readItemsFromGroup: "List/items",
createToken: "Create/token",
readToken: "Download/token",
readTokenList: "List/tokens",
deleteToken: "Delete/token",
listAuthGroup: "List/authgroup",
modifyAuthGroup: "Modify/authgroup"
} as const;



37 changes: 19 additions & 18 deletions src/edgekv/ekv-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ekvMetrics } from './ekv-metricFactory';
import * as httpEdge from '../cli-httpRequest';
import * as error from './ekv-error';
import * as cliUtils from '../utils/cli-utils';
import * as fs from 'fs';

const EDGEKV_API_BASE = '/edgekv/v1';
export const EDGEKV_API_BASE = '/edgekv/v1';
const INIT_EKV_TIMEOUT = 120000;
const DEFAULT_EKV_TIMEOUT = 60000;

Expand All @@ -12,92 +13,92 @@ export function getNameSpaceList(network: string, details: boolean) {
if (details) {
qs += `?details=${details}`
}
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces${qs}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces${qs}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.listNameSpaces).then(r => r.body).catch(err => error.handleError(err));
}

export function createNameSpace(network: string, namespace: string, retention, groupId, geoLocation) {
var body = { "namespace": namespace, "retentionInSeconds": retention, "groupId": groupId,"geoLocation": geoLocation};
return httpEdge.postJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.postJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.createNamespace).then(r => r.body).catch(err => error.handleError(err));
}

export function getNameSpace(network: string, namespace: string) {
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.showNamespace).then(r => r.body).catch(err => error.handleError(err));
}

export function updateNameSpace(network: string, namespace: string, retention, geoLocation) {
var body = { "namespace": namespace, "retentionInSeconds": retention, "geoLocation": geoLocation };
return httpEdge.putJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.putJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.updateNamespace).then(r => r.body).catch(err => error.handleError(err));
}

export function initializeEdgeKV() {
return httpEdge.putJson(`${EDGEKV_API_BASE}/initialize`, "", cliUtils.getTimeout(INIT_EKV_TIMEOUT)).then(r => r.response).catch(err => error.handleError(err));
return httpEdge.putJson(`${EDGEKV_API_BASE}/initialize`, "", cliUtils.getTimeout(INIT_EKV_TIMEOUT), ekvMetrics.initialize).then(r => r.response).catch(err => error.handleError(err));
}

export function getInitializedEdgeKV() {
return httpEdge.getJson(`${EDGEKV_API_BASE}/initialize`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.response).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/initialize`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.showInitStatus).then(r => r.response).catch(err => error.handleError(err));
}

export function writeItems(network: string, namespace: string, groupId: string, itemId: string, itemList) {
let body = itemList;
return httpEdge.putJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.putJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.writeItem).then(r => r.body).catch(err => error.handleError(err));
}

export function writeItemsFromFile(network: string, namespace: string, groupId: string, itemId: string, itemPath: string) {
let path = `${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`;
return httpEdge.sendEdgeRequest(path, 'PUT', fs.readFileSync(itemPath, { encoding: null }).toString('utf8'), {
'Content-Type': 'application/json'
}, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
}, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.writeItem).then(r => r.body).catch(err => error.handleError(err));
}

export function readItem(network: string, namespace: string, groupId: string, itemId: string) {
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.readItem).then(r => r.body).catch(err => error.handleError(err));
}

export function deleteItem(network: string, namespace: string, groupId: string, itemId: string) {
return httpEdge.deleteReq(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.deleteReq(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}/items/${itemId}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.deleteItem).then(r => r.body).catch(err => error.handleError(err));
}

export function getItemsFromGroup(network: string, namespace: string, groupId: string, maxItems: number) {
var qs: string = "";
if (maxItems !== undefined) {
qs += `?maxItems=${maxItems}`;
}
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}${qs}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/networks/${network}/namespaces/${namespace}/groups/${groupId}${qs}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.readItemsFromGroup).then(r => r.body).catch(err => error.handleError(err));
}

export function createEdgeKVToken(tokenName: string, permissionList, allowOnStg: boolean, allowOnProd: boolean, ewids:string, expiry) {

let body = {
"name": tokenName, "allowOnProduction": allowOnProd, "allowOnStaging": allowOnStg, "ewids":ewids ,"expiry": expiry, "namespacePermissions": permissionList
};
return httpEdge.postJson(`${EDGEKV_API_BASE}/tokens`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.postJson(`${EDGEKV_API_BASE}/tokens`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.createToken).then(r => r.body).catch(err => error.handleError(err));
}

export function getSingleToken(tokenName: string) {
return httpEdge.getJson(`${EDGEKV_API_BASE}/tokens/${tokenName}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/tokens/${tokenName}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.readToken).then(r => r.body).catch(err => error.handleError(err));
}

export function getTokenList(incExpired: boolean) {
var qs: string = "";
if (incExpired) {
qs += `?includeExpired=${incExpired}`
}
return httpEdge.getJson(`${EDGEKV_API_BASE}/tokens${qs}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/tokens${qs}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.readTokenList).then(r => r.body).catch(err => error.handleError(err));
}

export function revokeToken(tokenName: string) {
return httpEdge.deleteReq(`${EDGEKV_API_BASE}/tokens/${tokenName}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.deleteReq(`${EDGEKV_API_BASE}/tokens/${tokenName}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.deleteToken).then(r => r.body).catch(err => error.handleError(err));
}

export function modifyAuthGroupPermission(namespace: string, groupId: number) {
let body = { "groupId": groupId };
return httpEdge.putJson(`${EDGEKV_API_BASE}/auth/namespaces/${namespace}`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.putJson(`${EDGEKV_API_BASE}/auth/namespaces/${namespace}`, body, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.modifyAuthGroup).then(r => r.body).catch(err => error.handleError(err));
}

export function listAuthGroups(groupId: number) {
var group: string = "";
if (groupId) {
group = `/${groupId}`;
}
return httpEdge.getJson(`${EDGEKV_API_BASE}/auth/groups${group}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT)).then(r => r.body).catch(err => error.handleError(err));
return httpEdge.getJson(`${EDGEKV_API_BASE}/auth/groups${group}`, cliUtils.getTimeout(DEFAULT_EKV_TIMEOUT), ekvMetrics.listAuthGroup).then(r => r.body).catch(err => error.handleError(err));
}