Skip to content

Commit 9815ad2

Browse files
JessicaJHeedatho7561
authored andcommitted
Replace @clusterRequired
Signed-off-by: Jessica He <jhe@redhat.com>
1 parent 835d437 commit 9815ad2

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

src/odo.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface Odo {
7171
removeRegistry(name: string): Promise<void>;
7272
describeComponent(contextPath: string, experimental?: boolean): Promise<ComponentDescription | undefined>;
7373
analyze(contextPath: string): Promise<AnalyzeResponse[]>;
74+
canCreatePod(): Promise<boolean>;
7475

7576
/**
7677
* Returns the URL of the API of the current active cluster,
@@ -97,7 +98,6 @@ export interface Odo {
9798
}
9899

99100
export class OdoImpl implements Odo {
100-
101101
private static cli: cliInstance.Cli = cliInstance.CliChannel.getInstance();
102102

103103
private static instance: Odo;
@@ -341,6 +341,18 @@ export class OdoImpl implements Odo {
341341
public async deleteComponentConfiguration(componentPath: string): Promise<void> {
342342
await this.execute(Command.deleteComponentConfiguration(), componentPath);
343343
}
344+
345+
public async canCreatePod(): Promise<boolean> {
346+
try {
347+
const result: cliInstance.CliExitData = await this.execute(Command.canCreatePod());
348+
if (result.stdout === 'yes') {
349+
return true;
350+
}
351+
} catch {
352+
//ignore
353+
}
354+
return false;
355+
}
344356
}
345357

346358
export function getInstance(): Odo {

src/odo/command.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,8 @@ export class Command {
311311
new CommandOption('-f'),
312312
]);
313313
}
314+
315+
static canCreatePod(): CommandText {
316+
return new CommandText('oc auth can-i create pod');
317+
}
314318
}

src/openshift/component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { selectWorkspaceFolder } from '../util/workspace';
2121
import { VsCommandError, vsCommand } from '../vscommand';
2222
import GitImportLoader from '../webview/git-import/gitImportLoader';
2323
import LogViewLoader from '../webview/log/LogViewLoader';
24-
import OpenShiftItem from './openshiftItem';
24+
import OpenShiftItem, { clusterRequired } from './openshiftItem';
2525
import DescribeViewLoader from '../webview/describe/describeViewLoader';
2626

2727
function createCancelledResult(stepName: string): any {
@@ -204,7 +204,7 @@ export class Component extends OpenShiftItem {
204204
}
205205

206206
@vsCommand('openshift.component.dev')
207-
//@clusterRequired() check for user is logged in should be implemented from scratch
207+
@clusterRequired()
208208
static async dev(component: ComponentWorkspaceFolder, runOn?: undefined | 'podman') {
209209
const cs = Component.getComponentDevState(component);
210210
cs.devStatus = ComponentContextState.DEV_STARTING;
@@ -294,7 +294,7 @@ export class Component extends OpenShiftItem {
294294
}
295295

296296
@vsCommand('openshift.component.exitDevMode')
297-
// @clusterRequired()
297+
@clusterRequired()
298298
static async exitDevMode(component: ComponentWorkspaceFolder): Promise<void> {
299299
const componentState = Component.componentStates.get(component.contextPath)
300300
if (componentState) {
@@ -304,7 +304,7 @@ export class Component extends OpenShiftItem {
304304
}
305305

306306
@vsCommand('openshift.component.forceExitDevMode')
307-
// @clusterRequired()
307+
@clusterRequired()
308308
static forceExitDevMode(component: ComponentWorkspaceFolder): Promise<void> {
309309
const componentState = Component.componentStates.get(component.contextPath)
310310
if (componentState.devProcess && componentState.devProcess.exitCode === null) {
@@ -314,7 +314,7 @@ export class Component extends OpenShiftItem {
314314
}
315315

316316
@vsCommand('openshift.component.openInBrowser')
317-
// @clusterRequired()
317+
@clusterRequired()
318318
static async openInBrowser(component: ComponentWorkspaceFolder): Promise<string | null | undefined> {
319319
const componentDescription = await Component.odo.describeComponent(component.contextPath, !!Component.getComponentDevState(component).runOn);
320320
if (componentDescription.devForwardedPorts?.length === 1) {

src/openshift/openshiftItem.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ export function clusterRequired() {
132132
}
133133

134134
descriptor[fnKey] = async function (...args: any[]): Promise<any> {
135-
let activeCluster = await getInstance().getActiveCluster();
136-
if (!activeCluster) {
135+
let hasActiveCluster = await getInstance().canCreatePod();
136+
if (!hasActiveCluster) {
137137
const lOrC = await window.showInformationMessage('Login in to a Cluster to run this command.', 'Login', 'Cancel');
138138
if (lOrC === 'Login') {
139139
const loginResult = await commands.executeCommand('openshift.explorer.login');
140140
if (typeof loginResult === 'string') {
141141
window.showInformationMessage(loginResult);
142142
}
143-
activeCluster = await getInstance().getActiveCluster();
143+
hasActiveCluster = await getInstance().canCreatePod();
144144
}
145145
}
146-
if (activeCluster) {
146+
if (hasActiveCluster) {
147147
return fn.apply(this, args);
148148
}
149149
};

test/integration/odo.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ suite('odo integration', function () {
131131
});
132132

133133
suiteTeardown(async function () {
134+
await odo.execute(Command.odoLoginWithUsernamePassword(clusterUrl, username, password));
134135
const newWorkspaceFolders = workspace.workspaceFolders.filter((workspaceFolder) => {
135136
const fsPath = workspaceFolder.uri.fsPath;
136137
return (fsPath !== tmpFolder1.fsPath && fsPath !== tmpFolder2.fsPath);
@@ -158,6 +159,16 @@ suite('odo integration', function () {
158159
expect(analysis2).to.exist;
159160
expect(analysis2[0].devfile).to.equal('go');
160161
});
162+
163+
test('canCreatePod()', async function () {
164+
const canCreatePod1 = await odo.canCreatePod();
165+
expect(canCreatePod1).to.exist;
166+
expect(canCreatePod1).to.equal(true);
167+
await odo.execute(Command.odoLogout());
168+
const canCreatePod2 = await odo.canCreatePod();
169+
expect(canCreatePod2).to.exist;
170+
expect(canCreatePod2).to.equal(false);
171+
});
161172
});
162173

163174
suite('services', function () {

0 commit comments

Comments
 (0)