Skip to content

fix: Refactor RemoteConfigTasks class #1185

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

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
4 changes: 2 additions & 2 deletions src/commands/cluster/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import * as helpers from '../../core/helpers.js';
import * as constants from '../../core/constants.js';
import * as ContextFlags from './flags.js';
import {RemoteConfigTasks} from '../../core/config/remote/remote_config_tasks.js';
import {ListrRemoteConfig} from '../../core/config/remote/listr_config_tasks.js';
import type {RemoteConfigManager} from '../../core/config/remote/remote_config_manager.js';
import {connectConfigBuilder, resetConfigBuilder, setupConfigBuilder} from './configs.js';
import {SoloError} from '../../core/errors.js';
Expand All @@ -46,7 +46,7 @@
this.parent.setupHomeDirectoryTask(),
this.parent.getLocalConfig().promptLocalConfigTask(this.parent.getK8()),
this.tasks.selectContext(),
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
ListrRemoteConfig.loadRemoteConfig(this.parent, argv),

Check warning on line 49 in src/commands/cluster/handlers.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster/handlers.ts#L49

Added line #L49 was not covered by tests
this.tasks.readClustersFromRemoteConfig(argv),
this.tasks.updateLocalConfig(),
],
Expand Down
1 change: 1 addition & 0 deletions src/commands/cluster/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export class ClusterCommandTasks {
throw new SoloError(ErrorMessages.INVALID_CONTEXT_FOR_CLUSTER(selectedContext));
}
this.parent.getK8().setCurrentContext(selectedContext);
this.parent.getConfigManager().setFlag(flags.context, selectedContext);
},
};
}
Expand Down
19 changes: 2 additions & 17 deletions src/commands/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {Flags as flags} from './flags.js';
import * as constants from '../core/constants.js';
import {Templates} from '../core/templates.js';
import chalk from 'chalk';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {ListrRemoteConfig} from '../core/config/remote/listr_config_tasks.js';
import {ClusterCommandTasks} from './cluster/tasks.js';
import type {Namespace} from '../core/config/remote/types.js';
import type {ContextClusterStructure} from '../types/config_types.js';
Expand Down Expand Up @@ -131,22 +131,7 @@ export class DeploymentCommand extends BaseCommand {
});
},
},
{
title: 'Create remoteConfig in clusters',
task: async (ctx, task) => {
const subTasks: SoloListrTask<Context>[] = [];

for (const context of Object.keys(ctx.config.contextCluster)) {
const cluster = ctx.config.contextCluster[context];
subTasks.push(RemoteConfigTasks.createRemoteConfig.bind(this)(cluster, context, ctx.config.namespace));
}

return task.newListr(subTasks, {
concurrent: false,
rendererOptions: {collapseSubtasks: false},
});
},
},
ListrRemoteConfig.createRemoteConfigInMultipleClusters(this),
],
{
concurrent: false,
Expand Down
1 change: 0 additions & 1 deletion src/commands/mirror_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {type ProfileManager} from '../core/profile_manager.js';
import {BaseCommand} from './base.js';
import {Flags as flags} from './flags.js';
import {getEnvValue} from '../core/helpers.js';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {type CommandBuilder, type PodName} from '../types/aliases.js';
import {type Opts} from '../types/command_types.js';
import {ListrLease} from '../core/lease/listr_lease.js';
Expand Down
1 change: 0 additions & 1 deletion src/commands/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import * as helpers from '../core/helpers.js';
import {addDebugOptions, resolveValidJsonFilePath, validatePath} from '../core/helpers.js';
import path from 'path';
import fs from 'fs';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {type KeyManager} from '../core/key_manager.js';
import {type PlatformInstaller} from '../core/platform_installer.js';
import {type ProfileManager} from '../core/profile_manager.js';
Expand Down
1 change: 0 additions & 1 deletion src/commands/node/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {type LeaseManager} from '../../core/lease/lease_manager.js';
import {type RemoteConfigManager} from '../../core/config/remote/remote_config_manager.js';
import {IllegalArgumentError, SoloError} from '../../core/errors.js';
import {ComponentType, ConsensusNodeStates} from '../../core/config/remote/enumerations.js';
import {RemoteConfigTasks} from '../../core/config/remote/remote_config_tasks.js';
import type {SoloLogger} from '../../core/logging.js';
import type {NodeCommandTasks} from './tasks.js';
import {type Lease} from '../../core/lease/lease.js';
Expand Down
1 change: 0 additions & 1 deletion src/commands/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {type AccountManager} from '../core/account_manager.js';
import {BaseCommand} from './base.js';
import {Flags as flags} from './flags.js';
import {getNodeAccountMap} from '../core/helpers.js';
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
import {type CommandBuilder, type NodeAliases} from '../types/aliases.js';
import {type Opts} from '../types/command_types.js';
import {ListrLease} from '../core/lease/listr_lease.js';
Expand Down
94 changes: 94 additions & 0 deletions src/core/config/remote/listr_config_tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import type {ListrTask} from 'listr2';
import type {BaseCommand} from '../../../commands/base.js';
import {type Cluster, type Context, type Namespace} from './types.js';
import type {SoloListrTask} from '../../../types/index.js';

/**
* Static class that handles all tasks related to remote config used by other commands.
*/
export class ListrRemoteConfig {
/**
* Prevents instantiation of this utility class.
*/
private constructor() {
throw new Error('This class cannot be instantiated');
}

Check warning on line 31 in src/core/config/remote/listr_config_tasks.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/listr_config_tasks.ts#L30-L31

Added lines #L30 - L31 were not covered by tests

/* ----------- Create and Load ----------- */

/**
* Loads the remote config from the config class and performs component validation.
*
* @param command - the BaseCommand object on which an action will be performed
* @param argv - used to update the last executed command and command history
*/
public static loadRemoteConfig(command: BaseCommand, argv: any): ListrTask<any, any, any> {
return {
title: 'Load remote config',
task: async (_, task): Promise<void> => {
await command.getRemoteConfigManager().loadAndValidate(argv);
},
};
}

Check warning on line 48 in src/core/config/remote/listr_config_tasks.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/listr_config_tasks.ts#L42-L48

Added lines #L42 - L48 were not covered by tests

/**
* Create remoteConfig and save it to the provided cluster.
* @param command
* @param cluster
* @param context
* @param namespace
*/
public static createRemoteConfig(
command: BaseCommand,
cluster: Cluster,
context: Context,
namespace: Namespace,
): ListrTask<any, any, any> {
return {
title: `Create remote config in cluster: ${cluster}`,
task: async (_, task): Promise<void> => {
await command.getRemoteConfigManager().createAndValidate(cluster, context, namespace);
},
};
}

Check warning on line 69 in src/core/config/remote/listr_config_tasks.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/listr_config_tasks.ts#L58-L69

Added lines #L58 - L69 were not covered by tests

/**
* Create a remoteConfig object and save it to multiple clusters, read from ctx config
*
* @param command - the BaseCommand object on which an action will be performed
*/
public static createRemoteConfigInMultipleClusters(command: BaseCommand): ListrTask<any, any, any> {
return {
title: 'Create remoteConfig in clusters',
task: async (ctx, task) => {
const subTasks: SoloListrTask<Context>[] = [];

for (const context of Object.keys(ctx.config.contextCluster)) {
const cluster = ctx.config.contextCluster[context];
subTasks.push(ListrRemoteConfig.createRemoteConfig(command, cluster, context, ctx.config.namespace));
}

return task.newListr(subTasks, {
concurrent: false,
rendererOptions: {collapseSubtasks: false},
});
},
};
}

Check warning on line 93 in src/core/config/remote/listr_config_tasks.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/listr_config_tasks.ts#L77-L93

Added lines #L77 - L93 were not covered by tests
}
59 changes: 15 additions & 44 deletions src/core/config/remote/remote_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,55 +215,26 @@
await self.save();
}

/**
* Builds a listr task for loading the remote configuration.
*
* @param argv - arguments containing command input for historical reference.
* @returns a Listr task which loads the remote configuration.
*/
public buildLoadTask(argv: {_: string[]}): SoloListrTask<EmptyContextConfig> {
public async createAndValidate(cluster: Cluster, context: Context, namespace: Namespace) {
const self = this;
self.k8.setCurrentContext(context);

Check warning on line 220 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L220

Added line #L220 was not covered by tests

return {
title: 'Load remote config',
task: async (_, task): Promise<void> => {
await self.loadAndValidate(argv);
},
};
}

/**
* Builds a task for creating a new remote configuration, intended for use with Listr task management.
* Merges cluster mappings from the provided context into the local configuration, then creates the remote config.
*
* @returns a Listr task which creates the remote configuration.
*/
public buildCreateTask(cluster: Cluster, context: Context, namespace: Namespace): SoloListrTask<ListrContext> {
const self = this;

return {
title: `Create remote config in cluster: ${cluster}`,
task: async (_, task): Promise<void> => {
self.k8.setCurrentContext(context);

if (!(await self.k8.hasNamespace(namespace))) {
await self.k8.createNamespace(namespace);
}
if (!(await self.k8.hasNamespace(namespace))) {
await self.k8.createNamespace(namespace);
}

Check warning on line 224 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L222-L224

Added lines #L222 - L224 were not covered by tests

const localConfigExists = this.localConfig.configFileExists();
if (!localConfigExists) {
throw new SoloError("Local config doesn't exist");
}
const localConfigExists = this.localConfig.configFileExists();
if (!localConfigExists) {
throw new SoloError("Local config doesn't exist");
}

Check warning on line 229 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L226-L229

Added lines #L226 - L229 were not covered by tests

self.unload();
if (await self.load()) {
task.title = `${task.title} - ${chalk.red('Remote config already exists')}}`;
throw new SoloError('Remote config already exists');
}
self.unload();
if (await self.load()) {
self.logger.showUser(chalk.red('Remote config already exists'));
throw new SoloError('Remote config already exists');
}

Check warning on line 235 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L231-L235

Added lines #L231 - L235 were not covered by tests

await self.create();
},
};
await self.create();

Check warning on line 237 in src/core/config/remote/remote_config_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/config/remote/remote_config_manager.ts#L237

Added line #L237 was not covered by tests
}

/* ---------- Utilities ---------- */
Expand Down
45 changes: 0 additions & 45 deletions src/core/config/remote/remote_config_tasks.ts

This file was deleted.

Loading