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
27
28
29
30
31 public final class RundeckConfigurationWizardPanelDescriptor extends WizardPanelDescriptor {
32
33
34 private final Container container = new Container();
35
36
37 private final JTextField rundeckUrlTextField = new JTextField(30);
38
39
40 private final JTextField rundeckAPITokenTextField = new JPasswordField(30);
41
42 private final JTextField rundeckLoginTextField = new JTextField(30);
43
44 private final JTextField rundeckPasswordTextField = new JPasswordField(30);
45
46
47
48
49
50
51
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:");
63 final JLabel rundeckAPITokenlabel = new JLabel("API token:");
64 final JLabel rundeckLoginlabel = new JLabel("Login:");
65 final JLabel rundeckPasswordlabel = new JLabel("Password:");
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"));
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);
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);
134 return false;
135 }
136
137 getRundeckMonitorConfiguration().setRundeckUrl(rundeckUrlTextField.getText().replaceFirst("/+$", ""));
138 getRundeckMonitorConfiguration().setRundeckAPIKey(rundeckAPITokenTextField.getText());
139 getRundeckMonitorConfiguration().setRundeckLogin(rundeckLoginTextField.getText());
140 getRundeckMonitorConfiguration().setRundeckPassword(rundeckPasswordTextField.getText());
141
142
143 final RundeckClient rundeckClient;
144 try {
145
146 rundeckClient = RundeckClientTools.buildMinimalRundeckClient(getRundeckMonitorConfiguration());
147 rundeckClient.ping();
148 rundeckClient.testAuth();
149 }
150
151
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);
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);
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);
166 return false;
167 }
168
169
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);
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);
179 return false;
180 }
181
182 return true;
183 }
184 }