Skip to content

Commit 91ca4a2

Browse files
committed
fix: refactor remote config tasks
Signed-off-by: Ivo Yankov <[email protected]>
1 parent 473a650 commit 91ca4a2

File tree

10 files changed

+114
-112
lines changed

10 files changed

+114
-112
lines changed

src/commands/cluster/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {type ClusterCommandTasks} from './tasks.js';
1919
import * as helpers from '../../core/helpers.js';
2020
import * as constants from '../../core/constants.js';
2121
import * as ContextFlags from './flags.js';
22-
import {RemoteConfigTasks} from '../../core/config/remote/remote_config_tasks.js';
22+
import {ListrRemoteConfig} from '../../core/config/remote/listr_config_tasks.js';
2323
import type {RemoteConfigManager} from '../../core/config/remote/remote_config_manager.js';
2424
import {connectConfigBuilder, resetConfigBuilder, setupConfigBuilder} from './configs.js';
2525
import {SoloError} from '../../core/errors.js';
@@ -46,7 +46,7 @@ export class ClusterCommandHandlers implements CommandHandlers {
4646
this.parent.setupHomeDirectoryTask(),
4747
this.parent.getLocalConfig().promptLocalConfigTask(this.parent.getK8()),
4848
this.tasks.selectContext(),
49-
RemoteConfigTasks.loadRemoteConfig.bind(this)(argv),
49+
ListrRemoteConfig.loadRemoteConfig(this.parent, argv),
5050
this.tasks.readClustersFromRemoteConfig(argv),
5151
this.tasks.updateLocalConfig(),
5252
],

src/commands/cluster/tasks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export class ClusterCommandTasks {
337337
throw new SoloError(ErrorMessages.INVALID_CONTEXT_FOR_CLUSTER(selectedContext));
338338
}
339339
this.parent.getK8().setCurrentContext(selectedContext);
340+
this.parent.getConfigManager().setFlag(flags.context, selectedContext);
340341
},
341342
};
342343
}

src/commands/deployment.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {Flags as flags} from './flags.js';
2121
import * as constants from '../core/constants.js';
2222
import {Templates} from '../core/templates.js';
2323
import chalk from 'chalk';
24-
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
24+
import {ListrRemoteConfig} from '../core/config/remote/listr_config_tasks.js';
2525
import {ClusterCommandTasks} from './cluster/tasks.js';
2626
import type {Namespace} from '../core/config/remote/types.js';
2727
import type {ContextClusterStructure} from '../types/config_types.js';
@@ -131,22 +131,7 @@ export class DeploymentCommand extends BaseCommand {
131131
});
132132
},
133133
},
134-
{
135-
title: 'Create remoteConfig in clusters',
136-
task: async (ctx, task) => {
137-
const subTasks: SoloListrTask<Context>[] = [];
138-
139-
for (const context of Object.keys(ctx.config.contextCluster)) {
140-
const cluster = ctx.config.contextCluster[context];
141-
subTasks.push(RemoteConfigTasks.createRemoteConfig.bind(this)(cluster, context, ctx.config.namespace));
142-
}
143-
144-
return task.newListr(subTasks, {
145-
concurrent: false,
146-
rendererOptions: {collapseSubtasks: false},
147-
});
148-
},
149-
},
134+
ListrRemoteConfig.createRemoteConfigInMultipleClusters(this),
150135
],
151136
{
152137
concurrent: false,

src/commands/mirror_node.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {type ProfileManager} from '../core/profile_manager.js';
2323
import {BaseCommand} from './base.js';
2424
import {Flags as flags} from './flags.js';
2525
import {getEnvValue} from '../core/helpers.js';
26-
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
2726
import {type CommandBuilder, type PodName} from '../types/aliases.js';
2827
import {type Opts} from '../types/command_types.js';
2928
import {ListrLease} from '../core/lease/listr_lease.js';

src/commands/network.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import * as helpers from '../core/helpers.js';
2626
import {addDebugOptions, resolveValidJsonFilePath, validatePath} from '../core/helpers.js';
2727
import path from 'path';
2828
import fs from 'fs';
29-
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
3029
import {type KeyManager} from '../core/key_manager.js';
3130
import {type PlatformInstaller} from '../core/platform_installer.js';
3231
import {type ProfileManager} from '../core/profile_manager.js';

src/commands/node/handlers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {type LeaseManager} from '../../core/lease/lease_manager.js';
4141
import {type RemoteConfigManager} from '../../core/config/remote/remote_config_manager.js';
4242
import {IllegalArgumentError, SoloError} from '../../core/errors.js';
4343
import {ComponentType, ConsensusNodeStates} from '../../core/config/remote/enumerations.js';
44-
import {RemoteConfigTasks} from '../../core/config/remote/remote_config_tasks.js';
4544
import type {SoloLogger} from '../../core/logging.js';
4645
import type {NodeCommandTasks} from './tasks.js';
4746
import {type Lease} from '../../core/lease/lease.js';

src/commands/relay.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {type AccountManager} from '../core/account_manager.js';
2323
import {BaseCommand} from './base.js';
2424
import {Flags as flags} from './flags.js';
2525
import {getNodeAccountMap} from '../core/helpers.js';
26-
import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js';
2726
import {type CommandBuilder, type NodeAliases} from '../types/aliases.js';
2827
import {type Opts} from '../types/command_types.js';
2928
import {ListrLease} from '../core/lease/listr_lease.js';
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the ""License"");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an ""AS IS"" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import type {ListrTask} from 'listr2';
18+
import type {BaseCommand} from '../../../commands/base.js';
19+
import {type Cluster, type Context, type Namespace} from './types.js';
20+
import type {SoloListrTask} from '../../../types/index.js';
21+
22+
/**
23+
* Static class that handles all tasks related to remote config used by other commands.
24+
*/
25+
export class ListrRemoteConfig {
26+
/**
27+
* Prevents instantiation of this utility class.
28+
*/
29+
private constructor() {
30+
throw new Error('This class cannot be instantiated');
31+
}
32+
33+
/* ----------- Create and Load ----------- */
34+
35+
/**
36+
* Loads the remote config from the config class and performs component validation.
37+
*
38+
* @param command - the BaseCommand object on which an action will be performed
39+
* @param argv - used to update the last executed command and command history
40+
*/
41+
public static loadRemoteConfig(command: BaseCommand, argv: any): ListrTask<any, any, any> {
42+
return {
43+
title: 'Load remote config',
44+
task: async (_, task): Promise<void> => {
45+
await command.getRemoteConfigManager().loadAndValidate(argv);
46+
},
47+
};
48+
}
49+
50+
/**
51+
* Create remoteConfig and save it to the provided cluster.
52+
* @param command
53+
* @param cluster
54+
* @param context
55+
* @param namespace
56+
*/
57+
public static createRemoteConfig(
58+
command: BaseCommand,
59+
cluster: Cluster,
60+
context: Context,
61+
namespace: Namespace,
62+
): ListrTask<any, any, any> {
63+
return {
64+
title: `Create remote config in cluster: ${cluster}`,
65+
task: async (_, task): Promise<void> => {
66+
await command.getRemoteConfigManager().createAndValidate(cluster, context, namespace);
67+
},
68+
};
69+
}
70+
71+
/**
72+
* Create a remoteConfig object and save it to multiple clusters, read from ctx config
73+
*
74+
* @param command - the BaseCommand object on which an action will be performed
75+
*/
76+
public static createRemoteConfigInMultipleClusters(command: BaseCommand): ListrTask<any, any, any> {
77+
return {
78+
title: 'Create remoteConfig in clusters',
79+
task: async (ctx, task) => {
80+
const subTasks: SoloListrTask<Context>[] = [];
81+
82+
for (const context of Object.keys(ctx.config.contextCluster)) {
83+
const cluster = ctx.config.contextCluster[context];
84+
subTasks.push(ListrRemoteConfig.createRemoteConfig(command, cluster, context, ctx.config.namespace));
85+
}
86+
87+
return task.newListr(subTasks, {
88+
concurrent: false,
89+
rendererOptions: {collapseSubtasks: false},
90+
});
91+
},
92+
};
93+
}
94+
}

src/core/config/remote/remote_config_manager.ts

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -215,55 +215,26 @@ export class RemoteConfigManager {
215215
await self.save();
216216
}
217217

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

227-
return {
228-
title: 'Load remote config',
229-
task: async (_, task): Promise<void> => {
230-
await self.loadAndValidate(argv);
231-
},
232-
};
233-
}
234-
235-
/**
236-
* Builds a task for creating a new remote configuration, intended for use with Listr task management.
237-
* Merges cluster mappings from the provided context into the local configuration, then creates the remote config.
238-
*
239-
* @returns a Listr task which creates the remote configuration.
240-
*/
241-
public buildCreateTask(cluster: Cluster, context: Context, namespace: Namespace): SoloListrTask<ListrContext> {
242-
const self = this;
243-
244-
return {
245-
title: `Create remote config in cluster: ${cluster}`,
246-
task: async (_, task): Promise<void> => {
247-
self.k8.setCurrentContext(context);
248-
249-
if (!(await self.k8.hasNamespace(namespace))) {
250-
await self.k8.createNamespace(namespace);
251-
}
222+
if (!(await self.k8.hasNamespace(namespace))) {
223+
await self.k8.createNamespace(namespace);
224+
}
252225

253-
const localConfigExists = this.localConfig.configFileExists();
254-
if (!localConfigExists) {
255-
throw new SoloError("Local config doesn't exist");
256-
}
226+
const localConfigExists = this.localConfig.configFileExists();
227+
if (!localConfigExists) {
228+
throw new SoloError("Local config doesn't exist");
229+
}
257230

258-
self.unload();
259-
if (await self.load()) {
260-
task.title = `${task.title} - ${chalk.red('Remote config already exists')}}`;
261-
throw new SoloError('Remote config already exists');
262-
}
231+
self.unload();
232+
if (await self.load()) {
233+
self.logger.showUser(chalk.red('Remote config already exists'));
234+
throw new SoloError('Remote config already exists');
235+
}
263236

264-
await self.create();
265-
},
266-
};
237+
await self.create();
267238
}
268239

269240
/* ---------- Utilities ---------- */

src/core/config/remote/remote_config_tasks.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)