diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 1f54cc4..d12db00 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -27,6 +27,10 @@ - [Listing package variables](#listing-package-variables) - [Listing assignments](#listing-assignments) - [Mapping variables](#mapping-variables) + - [Finding staging nodes](#finding-staging-nodes) + - [Find a node](#find-a-node) + - [Find a node with configuration](#find-a-node-with-configuration) + - [Export node as JSON](#export-node-as-json) - [Data Pool export / import commands](#data-pool-export--import-commands) - [Export Data Pool](#export-data-pool) - [Batch Import multiple Data Pools](#batch-import-multiple-data-pools) @@ -634,6 +638,60 @@ This mapping should be saved and then used during import. Since the format of the variables.json file on import is the same JSON structure as the list variables result, you can either map the values to the variables.json file for each variable, or replace the variables.json file with the result of the listing & mapping altogether. If the mapping of variables is skipped, you should delete the variables.json file before importing. +#### Finding nodes + +The **config nodes find** command allows you to retrieve information about a specific node within a package. + +##### Find a staging node +To find a specific node in a package, use the following command: +``` +content-cli config nodes find --packageKey --nodeKey +``` + +The command will display the node information in the console: +``` +info: ID: node-id-123 +info: Key: node-key +info: Name: My Node +info: Type: VIEW +info: Package Node Key: package-node-key +info: Parent Node Key: parent-node-key +info: Created By: user@celonis.com +info: Updated By: user@celonis.com +info: Creation Date: 2025-10-22T10:30:00.000Z +info: Change Date: 2025-10-22T15:45:00.000Z +info: Flavor: STUDIO +``` + +##### Find a staging node with configuration +By default, the node configuration is not included in the response. To include the node's configuration, use the `--withConfiguration` flag: +``` +content-cli config nodes find --packageKey --nodeKey --withConfiguration +``` + +When configuration is included, it will be displayed as a JSON string in the output: +``` +info: Configuration: {"key":"value","nested":{"field":"data"}} +``` + +##### Export staging node as JSON +To export the node information as a JSON file instead of displaying it in the console, use the `--json` option: +``` +content-cli config nodes find --packageKey --nodeKey --json +``` + +This will create a JSON file in the current working directory with a UUID filename: +``` +info: File downloaded successfully. New filename: 9560f81f-f746-4117-83ee-dd1f614ad624.json +``` + +The JSON file contains the complete node information including all fields and, if requested, the configuration. + +You can combine options to export a node with its configuration: +``` +content-cli config nodes find --packageKey --nodeKey --withConfiguration --json +``` + ### Deployment commands (beta) The **deployment** command group allows you to create deployments, list their history, check active deployments, and retrieve deployables and targets. diff --git a/src/commands/configuration-management/api/node-api.ts b/src/commands/configuration-management/api/node-api.ts new file mode 100644 index 0000000..c0e9160 --- /dev/null +++ b/src/commands/configuration-management/api/node-api.ts @@ -0,0 +1,24 @@ +import { HttpClient } from "../../../core/http/http-client"; +import { Context } from "../../../core/command/cli-context"; +import { NodeTransport } from "../interfaces/node.interfaces"; +import { FatalError } from "../../../core/utils/logger"; + +export class NodeApi { + private httpClient: () => HttpClient; + + constructor(context: Context) { + this.httpClient = () => context.httpClient; + } + + public async findStagingNodeByKey(packageKey: string, nodeKey: string, withConfiguration: boolean): Promise { + const queryParams = new URLSearchParams(); + queryParams.set("withConfiguration", withConfiguration.toString()); + + return this.httpClient() + .get(`/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?${queryParams.toString()}`) + .catch((e) => { + throw new FatalError(`Problem finding node ${nodeKey} in package ${packageKey}: ${e}`); + }); + } +} + diff --git a/src/commands/configuration-management/interfaces/node.interfaces.ts b/src/commands/configuration-management/interfaces/node.interfaces.ts new file mode 100644 index 0000000..c191b9f --- /dev/null +++ b/src/commands/configuration-management/interfaces/node.interfaces.ts @@ -0,0 +1,22 @@ +export interface NodeConfiguration { + [key: string]: any; +} + +export interface NodeTransport { + id: string; + key: string; + name: string; + packageNodeKey: string; + parentNodeKey?: string; + packageNodeId: string; + type: string; + configuration?: NodeConfiguration; + invalidConfiguration?: string; + invalidContent: boolean; + creationDate: string; + changeDate: string; + createdBy: string; + updatedBy: string; + schemaVersion: number; + flavor?: string; +} diff --git a/src/commands/configuration-management/module.ts b/src/commands/configuration-management/module.ts index b4a4189..85e68d6 100644 --- a/src/commands/configuration-management/module.ts +++ b/src/commands/configuration-management/module.ts @@ -7,6 +7,7 @@ import { Context } from "../../core/command/cli-context"; import { Command, OptionValues } from "commander"; import { ConfigCommandService } from "./config-command.service"; import { VariableCommandService } from "./variable-command.service"; +import { NodeService } from "./node.service"; class Module extends IModule { @@ -68,6 +69,17 @@ class Module extends IModule { .option("--keysByVersionFile ", "Package keys by version mappings file path.", "") .action(this.listVariables); + const nodesCommand = configCommand.command("nodes") + .description("Commands related to nodes of the package"); + + nodesCommand.command("find") + .description("Find a specific node in a package") + .requiredOption("--packageKey ", "Identifier of the package") + .requiredOption("--nodeKey ", "Identifier of the node") + .option("--withConfiguration", "Include node configuration in the response", false) + .option("--json", "Return the response as a JSON file") + .action(this.findNode); + const listCommand = configurator.command("list"); listCommand.command("assignments") .description("Command to list possible variable assignments for a type") @@ -114,6 +126,10 @@ class Module extends IModule { private async listAssignments(context: Context, command: Command, options: OptionValues): Promise { await new VariableCommandService(context).listAssignments(options.type, options.json, options.params); } + + private async findNode(context: Context, command: Command, options: OptionValues): Promise { + await new NodeService(context).findNode(options.packageKey, options.nodeKey, options.withConfiguration, options.json); + } } export = Module; \ No newline at end of file diff --git a/src/commands/configuration-management/node.service.ts b/src/commands/configuration-management/node.service.ts new file mode 100644 index 0000000..23ed510 --- /dev/null +++ b/src/commands/configuration-management/node.service.ts @@ -0,0 +1,44 @@ +import { NodeApi } from "./api/node-api"; +import { Context } from "../../core/command/cli-context"; +import { fileService, FileService } from "../../core/utils/file-service"; +import { logger } from "../../core/utils/logger"; +import { v4 as uuidv4 } from "uuid"; + +export class NodeService { + private nodeApi: NodeApi; + + constructor(context: Context) { + this.nodeApi = new NodeApi(context); + } + + public async findNode(packageKey: string, nodeKey: string, withConfiguration: boolean, jsonResponse: boolean): Promise { + const node = await this.nodeApi.findStagingNodeByKey(packageKey, nodeKey, withConfiguration); + + if (jsonResponse) { + const filename = uuidv4() + ".json"; + fileService.writeToFileWithGivenName(JSON.stringify(node, null, 2), filename); + logger.info(FileService.fileDownloadedMessage + filename); + } else { + logger.info(`ID: ${node.id}`); + logger.info(`Key: ${node.key}`); + logger.info(`Name: ${node.name}`); + logger.info(`Type: ${node.type}`); + logger.info(`Package Node Key: ${node.packageNodeKey}`); + if (node.parentNodeKey) { + logger.info(`Parent Node Key: ${node.parentNodeKey}`); + } + logger.info(`Created By: ${node.createdBy}`); + logger.info(`Updated By: ${node.updatedBy}`); + logger.info(`Creation Date: ${new Date(node.creationDate).toISOString()}`); + logger.info(`Change Date: ${new Date(node.changeDate).toISOString()}`); + if (node.configuration) { + logger.info(`Configuration: ${JSON.stringify(node.configuration, null, 2)}`); + } + if (node.invalidContent) { + logger.info(`Invalid Configuration: ${node.invalidConfiguration}`); + } + logger.info(`Flavor: ${node.flavor}`); + } + } +} + diff --git a/tests/commands/configuration-management/config-node.spec.ts b/tests/commands/configuration-management/config-node.spec.ts new file mode 100644 index 0000000..75f4fea --- /dev/null +++ b/tests/commands/configuration-management/config-node.spec.ts @@ -0,0 +1,175 @@ +import { NodeTransport } from "../../../src/commands/configuration-management/interfaces/node.interfaces"; +import { mockAxiosGet } from "../../utls/http-requests-mock"; +import { NodeService } from "../../../src/commands/configuration-management/node.service"; +import { testContext } from "../../utls/test-context"; +import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup"; +import { FileService } from "../../../src/core/utils/file-service"; +import * as path from "path"; + +describe("Node find", () => { + const node: NodeTransport = { + id: "node-id", + key: "node-key", + name: "Node Name", + packageNodeKey: "package-node-key", + parentNodeKey: "parent-node-key", + packageNodeId: "package-node-id", + type: "VIEW", + invalidContent: false, + creationDate: new Date().toISOString(), + changeDate: new Date().toISOString(), + createdBy: "user-id", + updatedBy: "user-id", + schemaVersion: 1, + flavor: "STUDIO", + }; + + it("Should find node without configuration", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, node); + + await new NodeService(testContext).findNode(packageKey, nodeKey, false, false); + + expect(loggingTestTransport.logMessages.length).toBe(11); + expect(loggingTestTransport.logMessages[0].message).toContain(`ID: ${node.id}`); + expect(loggingTestTransport.logMessages[1].message).toContain(`Key: ${node.key}`); + expect(loggingTestTransport.logMessages[2].message).toContain(`Name: ${node.name}`); + expect(loggingTestTransport.logMessages[3].message).toContain(`Type: ${node.type}`); + expect(loggingTestTransport.logMessages[4].message).toContain(`Package Node Key: ${node.packageNodeKey}`); + expect(loggingTestTransport.logMessages[5].message).toContain(`Parent Node Key: ${node.parentNodeKey}`); + expect(loggingTestTransport.logMessages[6].message).toContain(`Created By: ${node.createdBy}`); + expect(loggingTestTransport.logMessages[7].message).toContain(`Updated By: ${node.updatedBy}`); + expect(loggingTestTransport.logMessages[8].message).toContain(`Creation Date: ${new Date(node.creationDate).toISOString()}`); + expect(loggingTestTransport.logMessages[9].message).toContain(`Change Date: ${new Date(node.changeDate).toISOString()}`); + expect(loggingTestTransport.logMessages[10].message).toContain(`Flavor: ${node.flavor}`); + }); + + it("Should find node with configuration", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + const nodeWithConfig: NodeTransport = { + ...node, + configuration: { + someKey: "someValue", + anotherKey: 123, + }, + }; + + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=true`, nodeWithConfig); + + await new NodeService(testContext).findNode(packageKey, nodeKey, true, false); + + expect(loggingTestTransport.logMessages.length).toBe(12); + expect(loggingTestTransport.logMessages[0].message).toContain(`ID: ${nodeWithConfig.id}`); + expect(loggingTestTransport.logMessages[10].message).toContain(`Configuration: ${JSON.stringify(nodeWithConfig.configuration, null, 2)}`); + expect(loggingTestTransport.logMessages[11].message).toContain(`Flavor: ${nodeWithConfig.flavor}`); + }); + + it("Should find node without parent node key", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + const nodeWithoutParent: NodeTransport = { + ...node, + parentNodeKey: undefined, + }; + + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, nodeWithoutParent); + + await new NodeService(testContext).findNode(packageKey, nodeKey, false, false); + + expect(loggingTestTransport.logMessages.length).toBe(10); + // Verify that parent node key is not logged + const parentNodeKeyMessage = loggingTestTransport.logMessages.find(log => log.message.includes("Parent Node Key")); + expect(parentNodeKeyMessage).toBeUndefined(); + }); + + it("Should find node and return as JSON", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, node); + + await new NodeService(testContext).findNode(packageKey, nodeKey, false, true); + + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; + + expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), expect.any(String), {encoding: "utf-8"}); + + const nodeTransport = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as NodeTransport; + + expect(nodeTransport).toEqual(node); + }); + + it("Should find node with configuration and return as JSON", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + const nodeWithConfig: NodeTransport = { + ...node, + configuration: { + someKey: "someValue", + nested: { + value: true, + }, + }, + }; + + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=true`, nodeWithConfig); + + await new NodeService(testContext).findNode(packageKey, nodeKey, true, true); + + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; + + expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), expect.any(String), {encoding: "utf-8"}); + + const nodeTransport = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as NodeTransport; + + expect(nodeTransport).toEqual(nodeWithConfig); + expect(nodeTransport.configuration).toEqual(nodeWithConfig.configuration); + }); + + it("Should find node with invalid configuration", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + const invalidConfigMessage = "Invalid JSON: Unexpected token at position 10"; + const nodeWithInvalidConfig: NodeTransport = { + ...node, + invalidContent: true, + invalidConfiguration: invalidConfigMessage, + }; + + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, nodeWithInvalidConfig); + + await new NodeService(testContext).findNode(packageKey, nodeKey, false, false); + + expect(loggingTestTransport.logMessages.length).toBe(12); + expect(loggingTestTransport.logMessages[0].message).toContain(`ID: ${nodeWithInvalidConfig.id}`); + expect(loggingTestTransport.logMessages[10].message).toContain(`Invalid Configuration: ${invalidConfigMessage}`); + expect(loggingTestTransport.logMessages[11].message).toContain(`Flavor: ${nodeWithInvalidConfig.flavor}`); + }); + + it("Should find node with invalid configuration and return as JSON", async () => { + const packageKey = "package-key"; + const nodeKey = "node-key"; + const invalidConfigMessage = "Syntax error in configuration"; + const nodeWithInvalidConfig: NodeTransport = { + ...node, + invalidContent: true, + invalidConfiguration: invalidConfigMessage, + }; + + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, nodeWithInvalidConfig); + + await new NodeService(testContext).findNode(packageKey, nodeKey, false, true); + + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; + + expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), expect.any(String), {encoding: "utf-8"}); + + const nodeTransport = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as NodeTransport; + + expect(nodeTransport).toEqual(nodeWithInvalidConfig); + expect(nodeTransport.invalidConfiguration).toEqual(invalidConfigMessage); + expect(nodeTransport.invalidContent).toBe(true); + }); +}); +