2727import com .intellij .openapi .application .ModalityState ;
2828import com .intellij .openapi .project .Project ;
2929import com .intellij .openapi .ui .DialogWrapper ;
30- import com .intellij .openapi .ui .popup .IconButton ;
3130import com .microsoft .azure .hdinsight .common .ClusterManagerEx ;
3231import com .microsoft .azure .hdinsight .sdk .cluster .HDInsightAdditionalClusterDetail ;
33- import com .microsoft .azure .hdinsight .sdk .common .HDIException ;
32+ import com .microsoft .azure .hdinsight .sdk .common .AuthenticationException ;
3433import com .microsoft .azure .hdinsight .sdk .storage .HDStorageAccount ;
35- import com .microsoft .azure .hdinsight .serverexplore .AddHDInsightAdditionalClusterImpl ;
3634import com .microsoft .azure .hdinsight .serverexplore .hdinsightnode .HDInsightRootModule ;
35+ import com .microsoft .azure .hdinsight .spark .jobs .JobUtils ;
3736import com .microsoft .azuretools .azurecommons .helpers .AzureCmdException ;
3837import com .microsoft .azuretools .azurecommons .helpers .StringHelper ;
3938import com .microsoft .azuretools .telemetry .AppInsightsClient ;
4039import com .microsoft .intellij .hdinsight .messages .HDInsightBundle ;
40+ import com .microsoft .tooling .msservices .helpers .azure .sdk .StorageClientSDKManager ;
41+ import com .microsoft .tooling .msservices .model .storage .BlobContainer ;
42+ import com .microsoft .tooling .msservices .model .storage .ClientStorageAccount ;
43+ import org .apache .commons .lang .StringUtils ;
4144import org .jetbrains .annotations .NotNull ;
4245import org .jetbrains .annotations .Nullable ;
4346
4447import javax .swing .*;
4548import java .awt .*;
4649import java .awt .event .ActionEvent ;
50+ import java .awt .event .FocusAdapter ;
51+ import java .awt .event .FocusEvent ;
52+ import java .util .Optional ;
4753import java .util .stream .Stream ;
4854
4955public class AddNewClusterFrom extends DialogWrapper {
@@ -55,6 +61,7 @@ public class AddNewClusterFrom extends DialogWrapper {
5561
5662 private String storageName ;
5763 private String storageKey ;
64+ private String storageContainer ;
5865
5966 private HDStorageAccount storageAccount ;
6067
@@ -73,6 +80,8 @@ public class AddNewClusterFrom extends DialogWrapper {
7380 private JLabel storageKeyLabel ;
7481 private JLabel userNameLabel ;
7582 private JLabel passwordLabel ;
83+ private JComboBox <BlobContainer > containersComboBox ;
84+ private JLabel storageContainerLabel ;
7685
7786 private HDInsightRootModule hdInsightModule ;
7887
@@ -93,6 +102,47 @@ public AddNewClusterFrom(final Project project, HDInsightRootModule hdInsightMod
93102 errorMessageField .setBorder (BorderFactory .createEmptyBorder ());
94103
95104 this .setModal (true );
105+
106+ storageKeyTextField .addFocusListener (new FocusAdapter () {
107+ @ Override
108+ public void focusLost (FocusEvent e ) {
109+ super .focusLost (e );
110+
111+ if (StringUtils .isNotBlank (storageNameField .getText ()) && StringUtils .isNotBlank (storageKeyTextField .getText ())) {
112+ ClientStorageAccount storageAccount = new ClientStorageAccount (storageNameField .getText ());
113+ storageAccount .setPrimaryKey (storageKeyTextField .getText ());
114+
115+ refreshContainers (storageAccount );
116+ }
117+ }
118+ });
119+
120+ storageNameField .addFocusListener (new FocusAdapter () {
121+ @ Override
122+ public void focusLost (FocusEvent e ) {
123+ super .focusLost (e );
124+
125+ if (StringUtils .isNotBlank (storageNameField .getText ()) && StringUtils .isNotBlank (storageKeyTextField .getText ())) {
126+ ClientStorageAccount storageAccount = new ClientStorageAccount (storageNameField .getText ());
127+ storageAccount .setPrimaryKey (storageKeyTextField .getText ());
128+
129+ refreshContainers (storageAccount );
130+ }
131+ }
132+ });
133+ }
134+
135+ private void refreshContainers (@ NotNull ClientStorageAccount storageAccount ) {
136+ try {
137+ containersComboBox .removeAllItems ();
138+
139+ StorageClientSDKManager .getManager ().getBlobContainers (storageAccount .getConnectionString ())
140+ .forEach (containersComboBox ::addItem );
141+
142+ containersComboBox .setMaximumRowCount (6 );
143+ } catch (AzureCmdException e ) {
144+ containersComboBox .removeAllItems ();
145+ }
96146 }
97147
98148 private class HelpAction extends AbstractAction {
@@ -132,6 +182,7 @@ protected void doOKAction() {
132182 clusterNameLabel ,
133183 storageNameLabel ,
134184 storageKeyLabel ,
185+ storageContainerLabel ,
135186 userNameLabel ,
136187 passwordLabel );
137188
@@ -157,19 +208,41 @@ protected void doOKAction() {
157208 isCarryOnNextStep = false ;
158209 }
159210 }
211+
212+ if (containersComboBox .getSelectedItem () == null ) {
213+ errorMessage = "The storage container isn't selected" ;
214+ isCarryOnNextStep = false ;
215+ } else {
216+ storageContainer = ((BlobContainer ) containersComboBox .getSelectedItem ()).getName ();
217+ }
160218 }
161219
162220 if (isCarryOnNextStep ) {
163221 getStorageAccount ();
164- }
165222
166- if (isCarryOnNextStep ) {
167- if (storageAccount != null ) {
223+ if (storageAccount == null ) {
224+ isCarryOnNextStep = false ;
225+ } else {
168226 HDInsightAdditionalClusterDetail hdInsightAdditionalClusterDetail = new HDInsightAdditionalClusterDetail (clusterName , userName , password , storageAccount );
169- ClusterManagerEx .getInstance ().addHDInsightAdditionalCluster (hdInsightAdditionalClusterDetail );
170- hdInsightModule .refreshWithoutAsync ();
227+ try {
228+ JobUtils .authenticate (hdInsightAdditionalClusterDetail );
229+
230+ ClusterManagerEx .getInstance ().addHDInsightAdditionalCluster (hdInsightAdditionalClusterDetail );
231+ hdInsightModule .refreshWithoutAsync ();
232+ } catch (AuthenticationException authErr ) {
233+ isCarryOnNextStep = false ;
234+ errorMessage = "Authentication Error: " + Optional .ofNullable (authErr .getMessage ())
235+ .filter (msg -> !msg .isEmpty ())
236+ .orElse ("Wrong username/password" ) +
237+ " (" + authErr .getErrorCode () + ")" ;
238+ } catch (Exception ex ) {
239+ isCarryOnNextStep = false ;
240+ errorMessage = "Authentication Error: " + ex .getMessage ();
241+ }
171242 }
243+ }
172244
245+ if (isCarryOnNextStep ) {
173246 super .doOKAction ();
174247 } else {
175248 errorMessageField .setText (errorMessage );
@@ -190,17 +263,10 @@ private static String getClusterName(String userNameOrUrl) {
190263 private void getStorageAccount () {
191264 addNewClusterPanel .setCursor (Cursor .getPredefinedCursor (Cursor .WAIT_CURSOR ));
192265
193- ApplicationManager .getApplication ().invokeAndWait (new Runnable () {
194- @ Override
195- public void run () {
196- try {
197- storageAccount = AddHDInsightAdditionalClusterImpl .getStorageAccount (clusterName , storageName , storageKey , userName , password );
198- isCarryOnNextStep = true ;
199- } catch (AzureCmdException | HDIException e ) {
200- isCarryOnNextStep = false ;
201- errorMessage = e .getMessage ();
202- }
203- }
266+ ApplicationManager .getApplication ().invokeAndWait (() -> {
267+ storageAccount = new HDStorageAccount (
268+ null , ClusterManagerEx .getInstance ().getBlobFullName (storageName ), storageKey , false , storageContainer );
269+ isCarryOnNextStep = true ;
204270 }, ModalityState .NON_MODAL );
205271
206272 addNewClusterPanel .setCursor (Cursor .getDefaultCursor ());
0 commit comments