Skip to content

Commit 8a268c2

Browse files
committed
Allow to create components when logged off from cluster
Signed-off-by: Denis Golovin dgolovin@redhat.com
1 parent 8bf8243 commit 8a268c2

File tree

10 files changed

+50
-17
lines changed

10 files changed

+50
-17
lines changed

src/odo.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,12 +863,15 @@ export class OdoImpl implements Odo {
863863

864864
public async createComponentFromFolder(application: OpenShiftObject, type: string, version: string, name: string, location: Uri, starter: string = undefined, useExistingDevfile = false): Promise<OpenShiftObject> {
865865
await this.execute(Command.createLocalComponent(application.getParent().getName(), application.getName(), type, version, name, location.fsPath, starter, useExistingDevfile), location.fsPath);
866-
if (workspace.workspaceFolders) {
866+
if (workspace.workspaceFolders && application.getParent().getParent()) { // if there are workspace folders and cluster is acvessible
867867
const targetApplication = (await this.getApplications(application.getParent())).find((value) => value === application);
868868
if (!targetApplication) {
869869
await this.insertAndReveal(application);
870870
}
871871
await this.insertAndReveal(new OpenShiftComponent(application, name, ContextType.COMPONENT, location, 'local', version ? ComponentKind.S2I : ComponentKind.DEVFILE, {name: type? type : name , tag: version}));
872+
} else {
873+
OdoImpl.data.delete(application);
874+
OdoImpl.data.delete(application.getParent());
872875
}
873876
let wsFolder: WorkspaceFolder;
874877
if (workspace.workspaceFolders) {

src/openshift/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ export class Component extends OpenShiftItem {
616616

617617
const componentName = await Component.getName(
618618
'Component name',
619-
Component.odo.getComponents(application),
619+
application.getParent().getParent() ? Component.odo.getComponents(application): Promise.resolve([]),
620620
application.getName(),
621621
initialNameValue
622622
);

src/openshift/openshiftItem.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,19 @@ export default class OpenShiftItem {
9090
}
9191

9292
static async getApplicationNames(project: OpenShiftObject, createCommand = false): Promise<Array<OpenShiftObject | QuickPickCommand>> {
93-
return OpenShiftItem.odo.getApplications(project).then((applicationList) => {
94-
if (applicationList.length === 0 && !createCommand) throw new VsCommandError(errorMessage.Component);
95-
return createCommand ? [new QuickPickCommand('$(plus) Create new Application...', async () => {
96-
return OpenShiftItem.getName('Application name', Promise.resolve(applicationList));
97-
}), ...applicationList] : applicationList;
98-
});
93+
if (project.getParent()) {
94+
return OpenShiftItem.odo.getApplications(project).then((applicationList) => {
95+
if (applicationList.length === 0 && !createCommand) throw new VsCommandError(errorMessage.Component);
96+
return createCommand ? [new QuickPickCommand('$(plus) Create new Application...', async () => {
97+
return OpenShiftItem.getName('Application name', Promise.resolve(applicationList));
98+
}), ...applicationList] : applicationList;
99+
});
100+
}
101+
return [
102+
new QuickPickCommand('$(plus) Create new Application...', async () => {
103+
return OpenShiftItem.getName('Application name', Promise.resolve([]));
104+
})
105+
];
99106
}
100107

101108
static async getComponentNames(application: OpenShiftObject, condition?: (value: OpenShiftObject) => boolean): Promise<OpenShiftObject[]> {
@@ -126,12 +133,23 @@ export default class OpenShiftItem {
126133
let context: OpenShiftObject | QuickPickCommand = treeItem;
127134
let project: OpenShiftObject;
128135
if (!context) {
129-
context = (await this.odo.getProjects()).find((prj:OpenShiftProject)=>prj.active);
130-
if (!context) {
131-
throw new VsCommandError(errorMessage.Project);
136+
const clusters = await this.odo.getClusters();
137+
if (clusters.length) { // connected to cluster because odo version printed out server url
138+
const projects = await this.odo.getProjects();
139+
context = projects.find((prj:OpenShiftProject)=>prj.active);
140+
if (!context) {
141+
throw new VsCommandError(errorMessage.Project)
142+
}
143+
} else { // cluster is not accessible or user not logged in
144+
const projectName = await OpenShiftItem.getName('Project Name', Promise.resolve([]))
145+
if (projectName) {
146+
context = new OpenShiftProject(undefined, projectName, true);
147+
} else {
148+
context = null;
149+
}
132150
}
133151
}
134-
if (context && context.contextValue === ContextType.PROJECT && appPlaceholder ) {
152+
if (context && !isCommand(context) && context.contextValue === ContextType.PROJECT && appPlaceholder ) {
135153
project = context;
136154
const applicationList = await OpenShiftItem.getApplicationNames(project, appPlaceholder.includes('create') && compPlaceholder === undefined);
137155
if ( applicationList.length === 1 && isCommand(applicationList[0])) {

test/unit/oc.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ suite('Oc', () => {
4848
execStub = sandbox.stub(CliChannel.prototype, 'execute');
4949
quickPickStub = sandbox.stub(window, 'showQuickPick');
5050
detectOrDownloadStub = sandbox.stub(ToolsConfig, 'detect').resolves('path');
51+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
5152
sandbox.stub(OdoImpl.prototype, 'getProjects').resolves([projectItem]);
5253
});
5354

test/unit/openshift/application.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ suite('OpenShift/Application', () => {
2020
let quickPickStub: sinon.SinonStub;
2121
let sandbox: sinon.SinonSandbox;
2222
let execStub: sinon.SinonStub;
23-
const projectItem = new TestItem(null, 'project', ContextType.PROJECT);
23+
const clusterItem = new TestItem(null, 'cluster', ContextType.CLUSTER);
24+
const projectItem = new TestItem(clusterItem, 'project', ContextType.PROJECT);
2425
const appItem = new TestItem(projectItem, 'app', ContextType.APPLICATION);
2526
const compItem = new TestItem(appItem, 'app', ContextType.COMPONENT_NO_CONTEXT, [], null);
2627
compItem.path = 'path/to/component';
@@ -29,6 +30,7 @@ suite('OpenShift/Application', () => {
2930
setup(() => {
3031
sandbox = sinon.createSandbox();
3132
execStub = sandbox.stub(OdoImpl.prototype, 'execute').resolves({error: null, stdout: '', stderr: ''});
33+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
3234
sandbox.stub(OdoImpl.prototype, 'getApplications').resolves([appItem]);
3335
sandbox.stub(OpenShiftItem, 'getApplicationNames').resolves([appItem]);
3436
sandbox.stub(vscode.window, 'showInputBox');

test/unit/openshift/component.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ suite('OpenShift/Component', () => {
6262
execStub = sandbox.stub(OdoImpl.prototype, 'execute').resolves({ stdout: '' });
6363
spawnStub = sandbox.stub(OdoImpl.prototype, 'spawn');
6464
sandbox.stub(OdoImpl.prototype, 'getServices');
65+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
6566
sandbox.stub(OdoImpl.prototype, 'getProjects').resolves([projectItem]);
6667
sandbox.stub(OdoImpl.prototype, 'getApplications').resolves([]);
6768
getComponentsStub = sandbox.stub(OdoImpl.prototype, 'getComponents').resolves([]);

test/unit/openshift/openshiftitem.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ chai.use(sinonChai);
1818
suite('OpenShiftItem', () => {
1919

2020
let sandbox: sinon.SinonSandbox;
21-
const projectItem = new TestItem(null, 'project', ContextType.PROJECT);
21+
const clusterItem = new TestItem(null, 'cluster', ContextType.CLUSTER);
22+
const projectItem = new TestItem(clusterItem, 'project', ContextType.PROJECT);
2223
const appItem = new TestItem(projectItem, 'application', ContextType.APPLICATION);
2324
const componentItem = new TestItem(appItem, 'component', ContextType.COMPONENT);
2425
const serviceItem = new TestItem(appItem, 'service', ContextType.SERVICE);
@@ -27,6 +28,7 @@ suite('OpenShiftItem', () => {
2728

2829
setup(() => {
2930
sandbox = sinon.createSandbox();
31+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
3032
});
3133

3234
teardown(() => {

test/unit/openshift/service.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ suite('OpenShift/Service', () => {
2525
let getProjectsStub: sinon.SinonStub;
2626
let getApplicationsStub: sinon.SinonStub;
2727
let execStub: sinon.SinonStub;
28-
const projectItem = new TestItem(null, 'project', ContextType.PROJECT);
28+
const clusterItem = new TestItem(null, 'cluster', ContextType.CLUSTER);
29+
const projectItem = new TestItem(clusterItem, 'project', ContextType.PROJECT);
2930
const appItem = new TestItem(projectItem, 'application', ContextType.APPLICATION);
3031
const serviceItem = new TestItem(appItem, 'service', ContextType.SERVICE);
3132
const templateName = 'template';
@@ -34,6 +35,7 @@ suite('OpenShift/Service', () => {
3435

3536
setup(() => {
3637
sandbox = sinon.createSandbox();
38+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
3739
getProjectsStub = sandbox.stub(OdoImpl.prototype, 'getProjects');
3840
getApplicationsStub = sandbox.stub(OdoImpl.prototype, 'getApplications');
3941
getServicesStub = sandbox.stub(OdoImpl.prototype, 'getServices').resolves([serviceItem]);

test/unit/openshift/storage.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ suite('OpenShift/Storage', () => {
2323
let inputStub: sinon.SinonStub;
2424
let getProjectsStub: sinon.SinonStub;
2525
let getStorageNamesStub: sinon.SinonStub;
26-
const projectItem = new TestItem(null, 'project', ContextType.PROJECT);
26+
const clusterItem = new TestItem(null, 'cluster', ContextType.CLUSTER);
27+
const projectItem = new TestItem(clusterItem, 'project', ContextType.PROJECT);
2728
const appItem = new TestItem(projectItem, 'app', ContextType.APPLICATION);
2829
const componentItem = new TestItem(appItem, 'component', ContextType.COMPONENT);
2930
const storageItem = new TestItem(componentItem, 'storage', ContextType.STORAGE);
@@ -33,6 +34,7 @@ suite('OpenShift/Storage', () => {
3334

3435
setup(() => {
3536
sandbox = sinon.createSandbox();
37+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
3638
getStorageNamesStub = sandbox.stub(OdoImpl.prototype, 'getStorageNames').resolves([storageItem]);
3739
execStub = sandbox.stub(OdoImpl.prototype, 'execute').resolves({error: '', stdout: '', stderr: ''});
3840
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');

test/unit/openshift/url.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ suite('OpenShift/URL', () => {
2727
let getApplicationNamesStub: sinon.SinonStub;
2828
let getComponentsNameStub: sinon.SinonStub;
2929
let getRouteNameStub: sinon.SinonStub;
30-
const projectItem = new TestItem(null, 'project', ContextType.PROJECT);
30+
const clusterItem = new TestItem(null, 'cluster', ContextType.CLUSTER);
31+
const projectItem = new TestItem(clusterItem, 'project', ContextType.PROJECT);
3132
const appItem = new TestItem(projectItem, 'app', ContextType.APPLICATION);
3233
const componentItem = new TestItem(appItem, 'component', ContextType.COMPONENT_PUSHED);
3334
const routeItem = new TestItem(componentItem, 'route', ContextType.COMPONENT_ROUTE);
@@ -180,6 +181,7 @@ suite('OpenShift/URL', () => {
180181
sandbox = sinon.createSandbox();
181182
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
182183
inputStub = sandbox.stub(vscode.window, 'showInputBox');
184+
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([clusterItem]);
183185
getRouteNameStub = sandbox.stub(OpenShiftItem, 'getRoutes').resolves([routeItem]);
184186
getApplicationNamesStub = sandbox.stub(OpenShiftItem, 'getApplicationNames').resolves([appItem]);
185187
getComponentsNameStub = sandbox.stub(OpenShiftItem, 'getComponentNames').resolves([componentItem]);

0 commit comments

Comments
 (0)