Skip to content

Commit

Permalink
[JBIDE-13020] prompting for ssh keys if none uploaded yet
Browse files Browse the repository at this point in the history
  • Loading branch information
adietish committed May 17, 2013
1 parent 66b949e commit e41a618
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 108 deletions.
Expand Up @@ -48,7 +48,7 @@ public class Connection {
private String username;
private String password;
private String host;
private IUser user;
private IUser connection;
private boolean isDomainLoaded;
private boolean rememberPassword;
private boolean didPromptForPassword;
Expand Down Expand Up @@ -105,11 +105,11 @@ private String getHost(String scheme, String host) {
}

protected void setUser(IUser user) {
this.user = user;
this.connection = user;
}

private IUser getUser() {
return user;
return connection;
}

public String getUsername() {
Expand Down Expand Up @@ -205,10 +205,10 @@ public boolean connect() throws OpenShiftException {
}

/**
* Creates an OpenShift user instance for this connection. Prompts for
* Creates an OpenShift connection instance for this connection. Prompts for
* credentials if needed.
*
* @return <code>true</code> if user could get created, <code>false</code>
* @return <code>true</code> if connection could get created, <code>false</code>
* otherwise.
*/
protected boolean createUser() {
Expand Down Expand Up @@ -241,11 +241,11 @@ private void loadPassword() {
}

private boolean hasUser() {
return user != null;
return connection != null;
}

protected void clearUser() {
this.user = null;
this.connection = null;
}

public void update(Connection connection) {
Expand Down Expand Up @@ -280,7 +280,7 @@ public IApplication createApplication(final String applicationName, final IStand
final ApplicationScale scale, final IGearProfile gearProfile)
throws OpenShiftException {
if (connect()) {
return user.getDefaultDomain().createApplication(applicationName, applicationType, scale, gearProfile);
return connection.getDefaultDomain().createApplication(applicationName, applicationType, scale, gearProfile);
}
return null;
}
Expand All @@ -296,30 +296,30 @@ public IApplication createApplication(final String applicationName, final IStand
*/
public IDomain createDomain(String id) throws OpenShiftException {
if (connect()) {
return user.createDomain(id);
return connection.createDomain(id);
}
return null;
}

public IApplication getApplicationByName(String name) throws OpenShiftException {
if (connect()
&& user.hasDomain()) {
return user.getDefaultDomain().getApplicationByName(name);
&& connection.hasDomain()) {
return connection.getDefaultDomain().getApplicationByName(name);
}
return null;
}

public List<IApplication> getApplications() throws OpenShiftException {
if (connect()
&& user.hasDomain()) {
return user.getDefaultDomain().getApplications();
&& connection.hasDomain()) {
return connection.getDefaultDomain().getApplications();
}
return null;
}

public List<IStandaloneCartridge> getStandaloneCartridgeNames() throws OpenShiftException {
if (connect()) {
return user.getConnection().getStandaloneCartridges();
return connection.getConnection().getStandaloneCartridges();
}
return null;
}
Expand All @@ -330,7 +330,7 @@ public void load() {

public IDomain getDefaultDomain() throws OpenShiftException {
if (connect()) {
IDomain domain = user.getDefaultDomain();
IDomain domain = connection.getDefaultDomain();
isDomainLoaded = true;
return domain;
}
Expand All @@ -343,36 +343,43 @@ public boolean isLoaded() throws OpenShiftException {

public List<IEmbeddableCartridge> getEmbeddableCartridges() throws OpenShiftException {
if (connect()) {
return user.getConnection().getEmbeddableCartridges();
return connection.getConnection().getEmbeddableCartridges();
}
return null;
}

public boolean hasApplication(String name) throws OpenShiftException {
if (connect()) {
return user.getDefaultDomain().hasApplicationByName(name);
return connection.getDefaultDomain().hasApplicationByName(name);
}
return false;
}

public boolean hasApplicationOfType(IStandaloneCartridge type) throws OpenShiftException {
if (hasDomain()) {
return user.getDefaultDomain().hasApplicationByCartridge(type);
return connection.getDefaultDomain().hasApplicationByCartridge(type);
}
return false;
}

public boolean hasDomain() throws OpenShiftException {
if (connect()) {
return user.hasDomain();
return connection.hasDomain();
}
return false;
}

public boolean hasSSHKeys() throws OpenShiftException {
if (connect()) {
return !connection.getSSHKeys().isEmpty();
}
return false;
}

public void refresh() throws OpenShiftException {
isDomainLoaded = false;
if (connect()) {
user.refresh();
connection.refresh();
}
}

Expand All @@ -382,36 +389,36 @@ public boolean isConnected() {

public List<IOpenShiftSSHKey> getSSHKeys() {
if (connect()) {
return user.getSSHKeys();
return connection.getSSHKeys();
}
return null;
}

public IOpenShiftSSHKey getSSHKeyByPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException,
OpenShiftException {
if (connect()) {
return user.getSSHKeyByPublicKey(publicKey);
return connection.getSSHKeyByPublicKey(publicKey);
}
return null;
}

public IOpenShiftSSHKey putSSHKey(String name, ISSHPublicKey key) throws OpenShiftException {
if (connect()) {
return user.putSSHKey(name, key);
return connection.putSSHKey(name, key);
}
return null;
}

public boolean hasSSHKeyName(String name) {
if (connect()) {
return user.hasSSHKeyName(name);
return connection.hasSSHKeyName(name);
}
return false;
}

public boolean hasSSHPublicKey(String publicKey) {
if (connect()) {
return user.hasSSHPublicKey(publicKey);
return connection.hasSSHPublicKey(publicKey);
}
return false;
}
Expand Down
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2012 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.openshift.express.internal.ui.utils;

import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.WizardDialog;

/**
* @author Andre Dietisheim
*/
public class WizardUtils {

private WizardUtils() {
}

public static void close(IWizard wizard) {
IWizardContainer container = wizard.getContainer();
if (container instanceof WizardDialog) {
((WizardDialog) container).close();
}
}


}
Expand Up @@ -55,7 +55,6 @@
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
Expand Down Expand Up @@ -84,6 +83,7 @@
import org.jboss.tools.openshift.express.internal.ui.wizard.AbstractOpenShiftWizardPage;
import org.jboss.tools.openshift.express.internal.ui.wizard.domain.NewDomainDialog;
import org.jboss.tools.openshift.express.internal.ui.wizard.embed.EmbedCartridgeStrategyAdapter;
import org.jboss.tools.openshift.express.internal.ui.wizard.ssh.NoSSHKeysWizard;

import com.openshift.client.ApplicationScale;
import com.openshift.client.IApplication;
Expand Down Expand Up @@ -673,20 +673,23 @@ public int hashCode(Object element) {

@Override
protected void onPageActivated(final DataBindingContext dbc) {
if (ensureHasDomain()) {
try {
pageModel.reset();
// needs to be done before loading resources, otherwise:
// dbc.updateModels() will be called and old data could be
// restored
loadOpenshiftResources(dbc);
dbc.updateTargets();
enableApplicationWidgets(pageModel.isUseExistingApplication());
createExistingAppNameContentAssist(existingAppNameText, pageModel.getApplicationNames());
this.newAppNameText.setFocus();
} catch (OpenShiftException e) {
Logger.error("Failed to reset page fields", e);
}
if (!ensureHasDomain()
|| !ensureHasSSHKeys()) {
org.jboss.tools.openshift.express.internal.ui.utils.WizardUtils.close(getWizard());
return;
}
try {
pageModel.reset();
// needs to be done before loading resources, otherwise:
// dbc.updateModels() will be called and old data could be
// restored
loadOpenshiftResources(dbc);
dbc.updateTargets();
enableApplicationWidgets(pageModel.isUseExistingApplication());
createExistingAppNameContentAssist(existingAppNameText, pageModel.getApplicationNames());
this.newAppNameText.setFocus();
} catch (OpenShiftException e) {
Logger.error("Failed to reset page fields", e);
}
}

Expand All @@ -697,26 +700,38 @@ protected void onPageActivated(final DataBindingContext dbc) {
*/
private boolean ensureHasDomain() {
try {
final Connection user = this.pageModel.getConnection();
if (user != null && !user.hasDomain()) {
IWizard domainDialog = new NewDomainDialog(user);
WizardDialog dialog = new WizardDialog(Display.getCurrent().getActiveShell(), domainDialog);
dialog.create();
dialog.setBlockOnOpen(true);
int result = dialog.open();
if (result != Dialog.OK) {
final IWizardContainer container = getWizard().getContainer();
if (container instanceof WizardDialog) {
((WizardDialog) container).close();
return false;
}
}
final Connection connection = pageModel.getConnection();
if (connection == null
|| connection.hasDomain()) {
return true;
}
WizardDialog dialog = new WizardDialog(
Display.getCurrent().getActiveShell(), new NewDomainDialog(connection));
dialog.create();
dialog.setBlockOnOpen(true);
return dialog.open() != Dialog.OK;
} catch (OpenShiftException e) {
Logger.error("Failed to refresh OpenShift account info", e);
return false;
}
}

private boolean ensureHasSSHKeys() {
try {
final Connection connection = pageModel.getConnection();
if (connection == null
|| connection.hasSSHKeys()) {
return true;
}
WizardDialog dialog = new WizardDialog(
Display.getCurrent().getActiveShell(), new NoSSHKeysWizard(connection));
dialog.create();
dialog.setBlockOnOpen(true);
return dialog.open() == Dialog.OK;
} catch (OpenShiftException e) {
Logger.error("Failed to refresh OpenShift account info", e);
return false;
}
return true;
}

protected void loadOpenshiftResources(final DataBindingContext dbc) {
Expand Down
Expand Up @@ -22,10 +22,10 @@
public abstract class AbstractSSHKeyWizardPageModel extends ObservableUIPojo implements ISSHKeyWizardPageModel {

private String name;
private Connection user;
private Connection connection;

public AbstractSSHKeyWizardPageModel(Connection user) {
this.user = user;
this.connection = user;
}

@Override
Expand All @@ -40,16 +40,16 @@ public void setName(String name) {

@Override
public boolean hasKeyName(String name) {
return user.hasSSHKeyName(name);
return connection.hasSSHKeyName(name);
}

@Override
public boolean hasPublicKey(String publicKeyContent) {
return user.hasSSHPublicKey(publicKeyContent);
return connection.hasSSHPublicKey(publicKeyContent);
}

protected Connection getUser() {
return user;
protected Connection getConnection() {
return connection;
}

protected String checkedGetSSH2Home() throws OpenShiftException {
Expand Down
Expand Up @@ -14,6 +14,8 @@
import org.eclipse.jface.wizard.Wizard;
import org.jboss.tools.openshift.express.internal.core.connection.Connection;

import com.openshift.client.IOpenShiftSSHKey;

/**
* @author André Dietisheim
*/
Expand All @@ -38,4 +40,9 @@ public boolean performFinish() {
public void addPages() {
addPage(this.addSSHKeyWizardPage = new AddSSHKeyWizardPage(user, this));
}

public IOpenShiftSSHKey getSSHKey() {
return addSSHKeyWizardPage.getSSHKey();
}

}

0 comments on commit e41a618

Please sign in to comment.