Files
threshold-ui/e2e-tests/helpers/boundary-cli/targets.js
T
Zhihe Li 7c1814118d refactor: 💡 Move e2e tests to new folder (#2546)
* refactor: 💡 Move e2e tests to new folder

* refactor: 💡 Convert desktop tests to mjs and use es6 imports

* refactor: 💡 Use module setting for e2e directory and remove mjs

* refactor: 💡 Remove "e2e" in all scripts in package.json
2024-11-12 11:21:37 -05:00

111 lines
2.8 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { execSync } from 'node:child_process';
import { nanoid } from 'nanoid';
/**
* Creates a TCP target.
* @param {string} projectId ID of the project under which the target will be created.
* @returns {Promise<string>} new target's ID
*/
export async function createTcpTarget(projectId) {
const targetName = 'target-' + nanoid();
const defaultPort = 22;
let target;
try {
target = JSON.parse(
execSync(
`boundary targets create tcp \
-default-port ${defaultPort} \
-name ${targetName} \
-scope-id ${projectId} \
-format json`,
),
).item;
} catch (e) {
console.log(`${e.stderr}`);
}
return target.id;
}
/**
* Creates a SSH target
* @param {string} projectId ID of the project under which the target will be created.
* @returns {Promise<string>} new target's ID
*/
export async function createSshTargetCli(projectId) {
const targetName = 'target-' + nanoid();
const defaultPort = 22;
let target;
try {
target = JSON.parse(
execSync(
`boundary targets create ssh \
-default-port ${defaultPort} \
-name ${targetName} \
-scope-id ${projectId} \
-format json`,
),
).item;
} catch (e) {
console.log(`${e.stderr}`);
}
return target.id;
}
/**
* Authorizes a session given an alias
* @param {string} alias alias of the target to be connected to
* @returns Session information
*/
export async function authorizeSessionByAliasCli(alias) {
let session;
try {
session = JSON.parse(
execSync(`boundary targets authorize-session ${alias} -format json`),
);
} catch (e) {
throw new Error(`Failed to connect to alias: ${e.stderr}`);
}
return session;
}
/**
* Authorizes a session given a target id
* @param {string} targetId Id of the target to be connected to
* @returns Session information
*/
export async function authorizeSessionByTargetIdCli(targetId) {
let session;
try {
session = JSON.parse(
execSync(
`boundary targets authorize-session -id ${targetId} -format json`,
),
);
} catch (e) {
throw new Error(`Failed to connect to target: ${e.stderr}`);
}
return session;
}
/**
* Gets Target ID from Target Name
* @param {string} projectId ID of the project under which the target exists
* @param {string} targetName Name of the target
* @returns {Promise<string>} ID of the target
*/
export async function getTargetIdFromNameCli(projectId, targetName) {
const targets = JSON.parse(
execSync(`boundary targets list -scope-id ${projectId} -format json`),
);
const target = targets.items.filter((obj) => obj.name == targetName)[0];
if (target) {
return target.id;
}
return null;
}