View Javadoc
1   package com.github.sbugat.rundeckmonitor.wizard;
2   
3   import java.awt.Component;
4   import java.awt.Container;
5   import java.awt.GridBagConstraints;
6   import java.awt.GridBagLayout;
7   import java.awt.Insets;
8   
9   import javax.swing.BoxLayout;
10  import javax.swing.JLabel;
11  import javax.swing.JOptionPane;
12  import javax.swing.JPanel;
13  import javax.swing.JPasswordField;
14  import javax.swing.JSeparator;
15  import javax.swing.JTextField;
16  
17  import org.rundeck.api.RundeckApiException;
18  import org.rundeck.api.RundeckApiException.RundeckApiLoginException;
19  import org.rundeck.api.RundeckApiException.RundeckApiTokenException;
20  import org.rundeck.api.RundeckClient;
21  
22  import com.github.sbugat.rundeckmonitor.configuration.RundeckMonitorConfiguration;
23  import com.github.sbugat.rundeckmonitor.tools.RundeckClientTools;
24  
25  /**
26   * RunDeck configuration wizard panel.
27   * 
28   * @author Sylvain Bugat
29   * 
30   */
31  public final class RundeckConfigurationWizardPanelDescriptor extends WizardPanelDescriptor {
32  
33  	/** Main container of the panel. */
34  	private final Container container = new Container();
35  
36  	/** RunDeck URL input. */
37  	private final JTextField rundeckUrlTextField = new JTextField(30);
38  
39  	/** Password input. */
40  	private final JTextField rundeckAPITokenTextField = new JPasswordField(30);
41  	/** Login input. */
42  	private final JTextField rundeckLoginTextField = new JTextField(30);
43  	/** RunDeck API key input. */
44  	private final JTextField rundeckPasswordTextField = new JPasswordField(30);
45  
46  	/**
47  	 * Copy arguments and initialize the RunDeck configuration wizard panel.
48  	 * 
49  	 * @param backArg previous panel
50  	 * @param nextArg next panel
51  	 * @param rundeckMonitorConfigurationArg RunDeck monitor common configuration
52  	 */
53  	public RundeckConfigurationWizardPanelDescriptor(final ConfigurationWizardStep backArg, final ConfigurationWizardStep nextArg, final RundeckMonitorConfiguration rundeckMonitorConfigurationArg) {
54  		super(ConfigurationWizardStep.RUNDECK_STEP, backArg, nextArg, rundeckMonitorConfigurationArg);
55  
56  		rundeckUrlTextField.setText(rundeckMonitorConfigurationArg.getRundeckUrl());
57  		rundeckAPITokenTextField.setText(rundeckMonitorConfigurationArg.getRundeckAPIKey());
58  		rundeckLoginTextField.setText(rundeckMonitorConfigurationArg.getRundeckLogin());
59  		rundeckPasswordTextField.setText(rundeckMonitorConfigurationArg.getRundeckPassword());
60  
61  		container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
62  		final JLabel rundeckUrllabel = new JLabel("Rundeck URL:"); //$NON-NLS-1$
63  		final JLabel rundeckAPITokenlabel = new JLabel("API token:"); //$NON-NLS-1$
64  		final JLabel rundeckLoginlabel = new JLabel("Login:"); //$NON-NLS-1$
65  		final JLabel rundeckPasswordlabel = new JLabel("Password:"); //$NON-NLS-1$
66  
67  		final JPanel topPanel = new JPanel();
68  		topPanel.setLayout(new GridBagLayout());
69  		final GridBagConstraints gridBagConstraits = new GridBagConstraints();
70  		gridBagConstraits.insets = new Insets(2, 2, 2, 2);
71  		gridBagConstraits.fill = GridBagConstraints.HORIZONTAL;
72  		gridBagConstraits.gridwidth = 1;
73  
74  		gridBagConstraits.gridx = 0;
75  		gridBagConstraits.gridy = 0;
76  		topPanel.add(rundeckUrllabel, gridBagConstraits);
77  		gridBagConstraits.gridx = 1;
78  		topPanel.add(rundeckUrlTextField, gridBagConstraits);
79  
80  		container.add(topPanel);
81  
82  		container.add(new JSeparator());
83  
84  		final JPanel aPIKeyPanel = new JPanel();
85  		aPIKeyPanel.setLayout(new GridBagLayout());
86  
87  		gridBagConstraits.gridx = 0;
88  		gridBagConstraits.gridy = 0;
89  		aPIKeyPanel.add(rundeckAPITokenlabel, gridBagConstraits);
90  		gridBagConstraits.gridx = 1;
91  		aPIKeyPanel.add(rundeckAPITokenTextField, gridBagConstraits);
92  
93  		container.add(aPIKeyPanel);
94  
95  		container.add(new JLabel("OR")); //$NON-NLS-1$
96  
97  		final JPanel loginPasswordPanel = new JPanel();
98  		loginPasswordPanel.setLayout(new GridBagLayout());
99  
100 		gridBagConstraits.gridx = 0;
101 		gridBagConstraits.gridy = 0;
102 		loginPasswordPanel.add(rundeckLoginlabel, gridBagConstraits);
103 		gridBagConstraits.gridx = 1;
104 		loginPasswordPanel.add(rundeckLoginTextField, gridBagConstraits);
105 
106 		gridBagConstraits.gridx = 0;
107 		gridBagConstraits.gridy = 1;
108 		loginPasswordPanel.add(rundeckPasswordlabel, gridBagConstraits);
109 		gridBagConstraits.gridx = 1;
110 		loginPasswordPanel.add(rundeckPasswordTextField, gridBagConstraits);
111 
112 		container.add(loginPasswordPanel);
113 
114 		container.add(new JSeparator());
115 	}
116 
117 	@Override
118 	public Component getPanelComponent() {
119 
120 		return container;
121 	}
122 
123 	@Override
124 	public boolean validate() {
125 
126 		if (rundeckUrlTextField.getText().isEmpty()) {
127 			JOptionPane.showMessageDialog(null, "Missing rundeck URL parameter.", "Missing parameter", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
128 			return false;
129 		}
130 
131 		if (rundeckAPITokenTextField.getText().isEmpty() && (rundeckLoginTextField.getText().isEmpty() || rundeckPasswordTextField.getText().isEmpty())) {
132 
133 			JOptionPane.showMessageDialog(null, "Missing rundeck API token," + System.lineSeparator() + "or rundeck login/password parameters", "Missing parameters", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
134 			return false;
135 		}
136 
137 		getRundeckMonitorConfiguration().setRundeckUrl(rundeckUrlTextField.getText().replaceFirst("/+$", "")); //$NON-NLS-1$ //$NON-NLS-2$
138 		getRundeckMonitorConfiguration().setRundeckAPIKey(rundeckAPITokenTextField.getText());
139 		getRundeckMonitorConfiguration().setRundeckLogin(rundeckLoginTextField.getText());
140 		getRundeckMonitorConfiguration().setRundeckPassword(rundeckPasswordTextField.getText());
141 
142 		// Test authentication credentials
143 		final RundeckClient rundeckClient;
144 		try {
145 			// Initialize the rundeck client with minimal rundeck version (1)
146 			rundeckClient = RundeckClientTools.buildMinimalRundeckClient(getRundeckMonitorConfiguration());
147 			rundeckClient.ping();
148 			rundeckClient.testAuth();
149 		}
150 		/*
151 		 * catch( final RundeckApiException e ) { JOptionPane.showMessageDialog( null, "Unable to connect to the project URL," + System.lineSeparator() + "check and change this parameter.", "Configuration error", JOptionPane.ERROR_MESSAGE ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ return false; }
152 		 */
153 		catch (final RundeckApiTokenException e) {
154 
155 			JOptionPane.showMessageDialog(null, "Invalid authentication token," + System.lineSeparator() + "check and change this parameter.", "Configuration error", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
156 			return false;
157 		}
158 		catch (final RundeckApiLoginException e) {
159 
160 			JOptionPane.showMessageDialog(null, "Invalid login/password," + System.lineSeparator() + "check and change these parameters.", "Configuration error", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
161 			return false;
162 		}
163 		catch (final Exception e) {
164 
165 			JOptionPane.showMessageDialog(null, "Invalid project URL," + System.lineSeparator() + "check and change this parameter.", "Configuration error", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
166 			return false;
167 		}
168 
169 		// Test existing projects
170 		try {
171 			if (rundeckClient.getProjects().isEmpty()) {
172 				JOptionPane.showMessageDialog(null, "Configured rundeck has no project," + System.lineSeparator() + "check and change this rundeck instance.", "Configuration error", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
173 				return false;
174 			}
175 		}
176 		catch (final RundeckApiException e) {
177 
178 			JOptionPane.showMessageDialog(null, "Configured rundeck has no project," + System.lineSeparator() + "check and change this rundeck instance.", "Configuration error", JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
179 			return false;
180 		}
181 
182 		return true;
183 	}
184 }