11package com .microsoft .azuretools .azureexplorer .forms ;
22
3+ import java .net .URL ;
4+ import java .util .Arrays ;
5+
36import org .apache .commons .lang3 .StringUtils ;
47import org .eclipse .swt .SWT ;
58import org .eclipse .swt .events .FocusAdapter ;
1114import org .eclipse .swt .widgets .Combo ;
1215import org .eclipse .swt .widgets .Composite ;
1316import org .eclipse .swt .widgets .Control ;
14- import org .eclipse .swt .widgets .Display ;
1517import org .eclipse .swt .widgets .Group ;
1618import org .eclipse .swt .widgets .Label ;
1719import org .eclipse .swt .widgets .Shell ;
1820import org .eclipse .swt .widgets .Text ;
1921import org .eclipse .ui .PlatformUI ;
2022import org .eclipse .ui .browser .IWebBrowser ;
2123
22- import java .net .URL ;
23- import java .util .Optional ;
24-
25- import com .microsoft .azure .hdinsight .common .ClusterManagerEx ;
26- import com .microsoft .azure .hdinsight .sdk .cluster .HDInsightAdditionalClusterDetail ;
27- import com .microsoft .azure .hdinsight .sdk .common .AuthenticationException ;
28- import com .microsoft .azure .hdinsight .sdk .storage .HDStorageAccount ;
24+ import com .microsoft .azure .hdinsight .common .mvc .SettableControl ;
25+ import com .microsoft .azure .hdinsight .serverexplore .AddNewClusterCtrlProvider ;
26+ import com .microsoft .azure .hdinsight .serverexplore .AddNewClusterModel ;
2927import com .microsoft .azure .hdinsight .serverexplore .hdinsightnode .HDInsightRootModule ;
30- import com .microsoft .azure .hdinsight .spark .jobs .JobUtils ;
31- import com .microsoft .azuretools .azurecommons .helpers .AzureCmdException ;
32- import com .microsoft .azuretools .azurecommons .helpers .NotNull ;
33- import com .microsoft .azuretools .azurecommons .helpers .StringHelper ;
3428import com .microsoft .azuretools .core .components .AzureTitleAreaDialogWrapper ;
35- import com .microsoft .azuretools .core .utils . PluginUtil ;
29+ import com .microsoft .azuretools .core .rxjava . EclipseSchedulers ;
3630import com .microsoft .azuretools .core .utils .Messages ;
3731import com .microsoft .azuretools .telemetry .AppInsightsClient ;
38- import com .microsoft .tooling .msservices .helpers .azure .sdk .StorageClientSDKManager ;
39- import com .microsoft .tooling .msservices .model .storage .ClientStorageAccount ;
40-
41- public class AddNewClusterForm extends AzureTitleAreaDialogWrapper {
42-
43- private String clusterName ;
44- private String userName ;
45- private String password ;
46-
47- private String storageName ;
48- private String storageKey ;
49- private String storageContainer ;
50-
51- private HDStorageAccount storageAccount ;
52-
53- private boolean isCarryOnNextStep ;
5432
33+ public class AddNewClusterForm extends AzureTitleAreaDialogWrapper implements SettableControl <AddNewClusterModel > {
5534 private Text clusterNameField ;
5635 private Text userNameField ;
5736 private Text storageNameField ;
@@ -61,7 +40,9 @@ public class AddNewClusterForm extends AzureTitleAreaDialogWrapper {
6140
6241 private HDInsightRootModule hdInsightModule ;
6342
64- private static final String URL_PREFIX = "https://" ;
43+ private Label clusterNameLabel ;
44+ private Label userNameLabel ;
45+ private Label passwordLabel ;
6546
6647 public AddNewClusterForm (Shell parentShell , HDInsightRootModule module ) {
6748 super (parentShell );
@@ -76,17 +57,20 @@ protected void configureShell(Shell newShell) {
7657
7758 }
7859
79- private void refreshContainers (@ NotNull ClientStorageAccount storageAccount ) {
80- try {
81- containersComboBox .removeAll ();
60+ private AddNewClusterCtrlProvider prepareCtrl () {
61+ AddNewClusterModel current = new AddNewClusterModel ();
62+
63+ getData (current );
64+
65+ return new AddNewClusterCtrlProvider (current );
66+ }
8267
83- StorageClientSDKManager .getManager ().getBlobContainers (storageAccount .getConnectionString ())
84- .forEach (blob -> containersComboBox .add (blob .getName ()));
85-
86- // not find setMaximumRowCount(*) method in SWT
87- } catch (AzureCmdException e ) {
88- setErrorMessage (e .getMessage ());
89- }
68+ private void refreshContainers () {
69+ prepareCtrl ()
70+ .refreshContainers ()
71+ .subscribeOn (EclipseSchedulers .processBarVisibleAsync ("Getting storage account containers..." ))
72+ .observeOn (EclipseSchedulers .dispatchThread ())
73+ .subscribe (this ::setData );
9074 }
9175
9276 @ Override
@@ -106,7 +90,7 @@ protected Control createDialogArea(Composite parent) {
10690 gridData .grabExcessHorizontalSpace = true ;
10791 container .setLayoutData (gridData );
10892
109- Label clusterNameLabel = new Label (container , SWT .LEFT );
93+ clusterNameLabel = new Label (container , SWT .LEFT );
11094 clusterNameLabel .setText ("Cluster Name:" );
11195 gridData = new GridData ();
11296 gridData .horizontalIndent = 30 ;
@@ -159,28 +143,14 @@ protected Control createDialogArea(Composite parent) {
159143 storageNameField .addFocusListener (new FocusAdapter () {
160144 @ Override
161145 public void focusLost (FocusEvent e ) {
162- super .focusLost (e );
163-
164- if (StringUtils .isNotBlank (storageNameField .getText ()) && StringUtils .isNotBlank (storageKeyField .getText ())) {
165- ClientStorageAccount storageAccount = new ClientStorageAccount (storageNameField .getText ());
166- storageAccount .setPrimaryKey (storageKeyField .getText ());
167-
168- refreshContainers (storageAccount );
169- }
146+ refreshContainers ();
170147 }
171148 });
172149
173150 storageKeyField .addFocusListener (new FocusAdapter () {
174151 @ Override
175152 public void focusLost (FocusEvent e ) {
176- super .focusLost (e );
177-
178- if (StringUtils .isNotBlank (storageNameField .getText ()) && StringUtils .isNotBlank (storageKeyField .getText ())) {
179- ClientStorageAccount storageAccount = new ClientStorageAccount (storageNameField .getText ());
180- storageAccount .setPrimaryKey (storageKeyField .getText ());
181-
182- refreshContainers (storageAccount );
183- }
153+ refreshContainers ();
184154 }
185155 });
186156
@@ -207,7 +177,7 @@ public void focusLost(FocusEvent e) {
207177 gridData .grabExcessHorizontalSpace = true ;
208178 clusterAccountGroup .setLayoutData (gridData );
209179
210- Label userNameLabel = new Label (clusterAccountGroup , SWT .LEFT );
180+ userNameLabel = new Label (clusterAccountGroup , SWT .LEFT );
211181 userNameLabel .setText ("User Name:" );
212182 gridData = new GridData ();
213183 gridData .horizontalIndent = 38 ;
@@ -220,7 +190,7 @@ public void focusLost(FocusEvent e) {
220190 userNameField .setLayoutData (gridData );
221191 userNameField .setToolTipText ("The user name of the HDInsight cluster.\n \n Press the F1 key or click the '?'(Help) button to get more details." );
222192
223- Label passwordLabel = new Label (clusterAccountGroup , SWT .LEFT );
193+ passwordLabel = new Label (clusterAccountGroup , SWT .LEFT );
224194 passwordLabel .setText ("Password:" );
225195 gridData = new GridData ();
226196 gridData .horizontalIndent = 38 ;
@@ -249,106 +219,54 @@ public void focusLost(FocusEvent e) {
249219
250220 @ Override
251221 protected void okPressed () {
252- synchronized (AddNewClusterForm .class ) {
253- isCarryOnNextStep = true ;
254- setErrorMessage (null );
255-
256- AppInsightsClient .create (Messages .HDInsightAddNewClusterAction , null );
257-
258- String clusterNameOrUrl = clusterNameField .getText ().trim ();
259- userName = userNameField .getText ().trim ();
260- storageName = storageNameField .getText ().trim ();
261-
262- storageKey = storageKeyField .getText ().trim ();
263-
264- password = passwordField .getText ();
265-
266- if (StringHelper .isNullOrWhiteSpace (clusterNameOrUrl ) || StringHelper .isNullOrWhiteSpace (storageName )
267- || StringHelper .isNullOrWhiteSpace (storageKey ) || StringHelper .isNullOrWhiteSpace (userName )
268- || StringHelper .isNullOrWhiteSpace (password )) {
269- setErrorMessage ("Cluster Name, Storage Name, Storage Key, User Name, or Password shouldn't be empty" );
270- isCarryOnNextStep = false ;
271- } else {
272- clusterName = getClusterName (clusterNameOrUrl );
273-
274- if (clusterName == null ) {
275- setErrorMessage ("Wrong cluster name or endpoint" );
276- isCarryOnNextStep = false ;
277- } else {
278- int status = ClusterManagerEx .getInstance ().isHDInsightAdditionalStorageExist (clusterName ,
279- storageName );
280- if (status == 1 ) {
281- setErrorMessage ("Cluster already exist in current list" );
282- isCarryOnNextStep = false ;
283- } else if (status == 2 ) {
284- setErrorMessage ("Default storage account is required" );
285- isCarryOnNextStep = false ;
286- }
287- }
288-
289- if (containersComboBox .getSelectionIndex () == -1 ) {
290- setErrorMessage ("The storage container isn't selected" );
291- isCarryOnNextStep = false ;
292- } else {
293- storageContainer = containersComboBox .getItem (containersComboBox .getSelectionIndex ());
294- }
295- }
296-
297- if (isCarryOnNextStep ) {
298- getStorageAccount ();
299- }
300-
301- if (isCarryOnNextStep ) {
302- if (storageAccount == null ) {
303- isCarryOnNextStep = false ;
304- } else {
305- HDInsightAdditionalClusterDetail hdInsightAdditionalClusterDetail = new HDInsightAdditionalClusterDetail (
306- clusterName , userName , password , storageAccount );
307- try {
308- JobUtils .authenticate (hdInsightAdditionalClusterDetail );
309-
310- ClusterManagerEx .getInstance ().addHDInsightAdditionalCluster (hdInsightAdditionalClusterDetail );
311- hdInsightModule .refreshWithoutAsync ();
312- } catch (AuthenticationException authErr ) {
313- isCarryOnNextStep = false ;
314- String errorMessage = "Authentication Error: " + Optional .ofNullable (authErr .getMessage ())
315- .filter (msg -> !msg .isEmpty ())
316- .orElse ("Wrong username/password" )
317- + " (" + authErr .getErrorCode () + ")" ;
318- setErrorMessage (errorMessage );
319- } catch (Exception ex ) {
320- isCarryOnNextStep = false ;
321- setErrorMessage ("Authentication Error: " + ex .getMessage ());
322- }
323- }
324- }
325-
326- if (isCarryOnNextStep ) {
327- super .okPressed ();
328- }
329- }
222+ prepareCtrl ().validateAndAdd ()
223+ .subscribeOn (EclipseSchedulers .processBarVisibleAsync ("Validating the cluster settings..." ))
224+ .observeOn (EclipseSchedulers .dispatchThread ())
225+ .doOnNext (this ::setData )
226+ .map (AddNewClusterModel ::getErrorMessage )
227+ .filter (StringUtils ::isEmpty )
228+ .subscribe (toUpdate -> {
229+ hdInsightModule .refreshWithoutAsync ();
230+ AppInsightsClient .create (Messages .HDInsightAddNewClusterAction , null );
231+
232+ super .okPressed ();
233+ },
234+ err -> {
235+ setErrorMessage (err .getMessage ());
236+ });
330237 }
331238
332- // format input string
333- private static String getClusterName (String userNameOrUrl ) {
334- if (userNameOrUrl .startsWith (URL_PREFIX )) {
335- return StringHelper .getClusterNameFromEndPoint (userNameOrUrl );
336- } else {
337- return userNameOrUrl ;
338- }
239+ @ Override
240+ public void getData (AddNewClusterModel data ) {
241+ // Components -> Data
242+ data .setClusterName (clusterNameField .getText ()).setClusterNameLabelTitle (clusterNameLabel .getText ())
243+ .setUserName (userNameField .getText ()).setUserNameLabelTitle (userNameLabel .getText ())
244+ .setPassword (passwordField .getText ()).setPasswordLabelTitle (passwordLabel .getText ())
245+ .setStorageName (storageNameField .getText ()).setStorageKey (storageKeyField .getText ())
246+ .setErrorMessage (getErrorMessage ()).setSelectedContainerIndex (containersComboBox .getSelectionIndex ())
247+ .setContainers (Arrays .asList (containersComboBox .getItems ()));
339248 }
340249
341- private void getStorageAccount () {
342- PluginUtil .showBusy (true , getShell ());
343- Display .getDefault ().syncExec (new Runnable () {
344- @ Override
345- public void run () {
346- storageAccount = new HDStorageAccount (
347- null , ClusterManagerEx .getInstance ().getBlobFullName (storageName ), storageKey , false , storageContainer );
348- isCarryOnNextStep = true ;
349- }
350- });
351- PluginUtil .showBusy (false , getShell ());
250+ @ Override
251+ public void setData (AddNewClusterModel data ) {
252+ // Data -> Components
253+
254+ // Text fields
255+ clusterNameField .setText (data .getClusterName ());
256+ clusterNameLabel .setText (data .getClusterNameLabelTitle ());
257+ userNameField .setText (data .getUserName ());
258+ userNameLabel .setText (data .getUserNameLabelTitle ());
259+ passwordField .setText (data .getPassword ());
260+ passwordLabel .setText (data .getPasswordLabelTitle ());
261+ storageNameField .setText (data .getStorageName ());
262+ storageKeyField .setText (data .getStorageKey ());
263+ setErrorMessage (data .getErrorMessage ());
264+
265+ // Combo box
266+ containersComboBox .removeAll ();
267+ data .getContainers ().forEach (containersComboBox ::add );
268+ containersComboBox .select (data .getSelectedContainerIndex ());
269+
270+ getShell ().layout (true , true );
352271 }
353-
354272}
0 commit comments