Skip to content

Commit

Permalink
Merge pull request #287 from vscheuber/main
Browse files Browse the repository at this point in the history
update log list/tail/fetch commands
  • Loading branch information
vscheuber committed Oct 2, 2023
2 parents 64b897e + bb4ae75 commit d196ac3
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 715 deletions.
12 changes: 6 additions & 6 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
Expand Up @@ -104,7 +104,7 @@
]
},
"dependencies": {
"@rockcarver/frodo-lib": "2.0.0-32",
"@rockcarver/frodo-lib": "2.0.0-34",
"chokidar": "^3.5.3",
"cli-progress": "^3.11.2",
"cli-table3": "^0.6.3",
Expand Down
81 changes: 53 additions & 28 deletions src/cli/log/log-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Option } from 'commander';

import { fetchLogs, provisionCreds } from '../../ops/LogOps';
import * as config from '../../utils/Config';
import { printMessage } from '../../utils/Console';
import { printMessage, verboseMessage } from '../../utils/Console';
import { FrodoCommand } from '../FrodoCommand';
import { sourcesOptionM } from './log';

Expand Down Expand Up @@ -64,34 +64,52 @@ Cannot be more than 30 days in the past. If not specified, logs from one hour ag
)
.action(async (host, user, password, options, command) => {
command.handleDefaultArgsAndOpts(host, user, password, options, command);
let credsFromParameters = true;

let foundCredentials = false;

const conn = await getConnectionProfile();
if (conn) {
state.setHost(conn.tenant);
if (conn.logApiKey != null && conn.logApiSecret != null) {
credsFromParameters = false;
state.setLogApiKey(conn.logApiKey);
state.setLogApiSecret(conn.logApiSecret);
} else {
if (conn.username == null && conn.password == null) {
if (!state.getUsername() && !state.getPassword()) {
credsFromParameters = false;
printMessage(
'User credentials not specified as parameters and no saved API key and secret found!',
'warn'
);
return;
}
} else {
state.setUsername(conn.username);
state.setPassword(conn.password);
}
if (await getTokens(true)) {
const creds = await provisionCreds();
state.setLogApiKey(creds.api_key_id as string);
state.setLogApiSecret(creds.api_key_secret as string);
}
if (conn) state.setHost(conn.tenant);

// log api creds have been supplied as username and password arguments
if (state.getUsername() && state.getPassword()) {
verboseMessage(`Using log api credentials from command line.`);
state.setLogApiKey(state.getUsername());
state.setLogApiSecret(state.getPassword());
foundCredentials = true;
}
// log api creds from connection profile
else if (conn && conn.logApiKey != null && conn.logApiSecret != null) {
verboseMessage(`Using log api credentials from connection profile.`);
state.setLogApiKey(conn.logApiKey);
state.setLogApiSecret(conn.logApiSecret);
foundCredentials = true;
}
// log api creds have been supplied via env variables
else if (state.getLogApiKey() && state.getLogApiSecret()) {
verboseMessage(`Using log api credentials from environment variables.`);
foundCredentials = true;
}
// no log api creds but got username and password, so can try to create them
else if (conn && conn.username && conn.password) {
printMessage(
`Found admin credentials in connection profile, attempting to create log api credentials...`
);
state.setUsername(conn.username);
state.setPassword(conn.password);
if (await getTokens(true)) {
const creds = await provisionCreds();
state.setLogApiKey(creds.api_key_id as string);
state.setLogApiSecret(creds.api_key_secret as string);
await saveConnectionProfile(state.getHost());
foundCredentials = true;
}
// unable to create credentials
else {
printMessage(`Unable to create log api credentials.`);
}
}

if (foundCredentials) {
const now = Date.now() / 1000;
const nowString = new Date(now * 1000).toISOString();
if (
Expand Down Expand Up @@ -129,13 +147,15 @@ Cannot be more than 30 days in the past. If not specified, logs from one hour ag
'End timestamp can not be before begin timestamp',
'error'
);
process.exitCode = 1;
return;
}
if (now - beginTs > LOG_TIME_WINDOW_MAX) {
printMessage(
'Begin timestamp can not be more than 30 days in the past',
'error'
);
process.exitCode = 1;
return;
}
let intermediateEndTs = 0;
Expand All @@ -146,7 +166,6 @@ Cannot be more than 30 days in the past. If not specified, logs from one hour ag
conn.tenant
}...`
);
if (credsFromParameters) await saveConnectionProfile(host); // save new values if they were specified on CLI

let timeIncrement = LOG_TIME_WINDOW_INCREMENT;
if (endTs - beginTs > 30) {
Expand All @@ -167,6 +186,12 @@ Cannot be more than 30 days in the past. If not specified, logs from one hour ag
beginTs = intermediateEndTs;
} while (intermediateEndTs < endTs);
}
// no log api credentials
else {
printMessage('No log api credentials found!');
program.help();
process.exitCode = 1;
}
});

program.parse();
8 changes: 4 additions & 4 deletions src/cli/log/log-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ program
.description('List available ID Cloud log sources.')
.action(async (host, user, password, options, command) => {
command.handleDefaultArgsAndOpts(host, user, password, options, command);
let saveCredentials = false;
let foundCredentials = false;

verboseMessage('Listing available ID Cloud log sources...');

let foundCredentials = false;

const conn = await getConnectionProfile();
if (conn) state.setHost(conn.tenant);

Expand Down Expand Up @@ -50,8 +51,8 @@ program
const creds = await provisionCreds();
state.setLogApiKey(creds.api_key_id as string);
state.setLogApiSecret(creds.api_key_secret as string);
await saveConnectionProfile(state.getHost());
foundCredentials = true;
saveCredentials = true;
}
// unable to create credentials
else {
Expand All @@ -67,7 +68,6 @@ program
'error'
);
} else {
if (saveCredentials) await saveConnectionProfile(state.getHost()); // save new values if they were specified on CLI
printMessage(`Log sources from ${state.getHost()}`);
for (const source of sources) {
printMessage(`${source}`, 'data');
Expand Down
85 changes: 53 additions & 32 deletions src/cli/log/log-tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Option } from 'commander';

import { provisionCreds, tailLogs } from '../../ops/LogOps';
import * as config from '../../utils/Config';
import { printMessage } from '../../utils/Console';
import { printMessage, verboseMessage } from '../../utils/Console';
import { FrodoCommand } from '../FrodoCommand';
import { sourcesOptionM } from './log';

Expand Down Expand Up @@ -36,42 +36,57 @@ Following values are possible (values on the same line are equivalent): \
)
.action(async (host, user, password, options, command) => {
command.handleDefaultArgsAndOpts(host, user, password, options, command);
let credsFromParameters = true;

let foundCredentials = false;

const conn = await getConnectionProfile();
if (conn) {
state.setHost(conn.tenant);
if (conn.logApiKey != null && conn.logApiSecret != null) {
credsFromParameters = false;
state.setLogApiKey(conn.logApiKey);
state.setLogApiSecret(conn.logApiSecret);
} else {
if (conn.username == null && conn.password == null) {
if (!state.getUsername() && !state.getPassword()) {
credsFromParameters = false;
printMessage(
'User credentials not specified as parameters and no saved API key and secret found!',
'warn'
);
return;
}
} else {
state.setUsername(conn.username);
state.setPassword(conn.password);
}
if (await getTokens(true)) {
const creds = await provisionCreds();
state.setLogApiKey(creds.api_key_id as string);
state.setLogApiSecret(creds.api_key_secret as string);
}
if (conn) state.setHost(conn.tenant);

// log api creds have been supplied as username and password arguments
if (state.getUsername() && state.getPassword()) {
verboseMessage(`Using log api credentials from command line.`);
state.setLogApiKey(state.getUsername());
state.setLogApiSecret(state.getPassword());
foundCredentials = true;
}
// log api creds from connection profile
else if (conn && conn.logApiKey != null && conn.logApiSecret != null) {
verboseMessage(`Using log api credentials from connection profile.`);
state.setLogApiKey(conn.logApiKey);
state.setLogApiSecret(conn.logApiSecret);
foundCredentials = true;
}
// log api creds have been supplied via env variables
else if (state.getLogApiKey() && state.getLogApiSecret()) {
verboseMessage(`Using log api credentials from environment variables.`);
foundCredentials = true;
}
// no log api creds but got username and password, so can try to create them
else if (conn && conn.username && conn.password) {
printMessage(
`Found admin credentials in connection profile, attempting to create log api credentials...`
);
state.setUsername(conn.username);
state.setPassword(conn.password);
if (await getTokens(true)) {
const creds = await provisionCreds();
state.setLogApiKey(creds.api_key_id as string);
state.setLogApiSecret(creds.api_key_secret as string);
await saveConnectionProfile(state.getHost());
foundCredentials = true;
}
// unable to create credentials
else {
printMessage(`Unable to create log api credentials.`);
}
}

if (foundCredentials) {
printMessage(
`Tailing ID Cloud logs from the following sources: ${
command.opts().sources
} and levels [${resolveLevel(command.opts().level)}] of ${
conn.tenant
}...`
options.sources
} and levels [${resolveLevel(options.level)}] of ${state.getHost()}...`
);
if (credsFromParameters) await saveConnectionProfile(host); // save new values if they were specified on CLI
await tailLogs(
command.opts().sources,
resolveLevel(command.opts().level),
Expand All @@ -80,6 +95,12 @@ Following values are possible (values on the same line are equivalent): \
config.getNoiseFilters(options.defaults)
);
}
// no log api credentials
else {
printMessage('No log api credentials found!');
program.help();
process.exitCode = 1;
}
});

program.parse();
Loading

0 comments on commit d196ac3

Please sign in to comment.