|
23 | 23 |
|
24 | 24 | import com.microsoft.azure.hdinsight.common.CommonConst; |
25 | 25 | import com.microsoft.azure.hdinsight.common.logger.ILogger; |
26 | | -import com.microsoft.azure.hdinsight.sdk.storage.HDStorageAccount; |
| 26 | +import com.microsoft.azure.hdinsight.sdk.cluster.ClusterDetail; |
| 27 | +import com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail; |
27 | 28 | import com.microsoft.azure.hdinsight.sdk.storage.IHDIStorageAccount; |
28 | 29 | import com.microsoft.azure.hdinsight.sdk.storage.StorageAccountType; |
29 | | -import com.microsoft.azure.storage.CloudStorageAccount; |
30 | | -import com.microsoft.azure.storage.StorageException; |
31 | | -import com.microsoft.azure.storage.blob.BlobContainerPermissions; |
32 | | -import com.microsoft.azure.storage.blob.BlobContainerProperties; |
33 | | -import com.microsoft.azure.storage.blob.CloudBlobContainer; |
| 30 | +import com.microsoft.azuretools.authmanage.AuthMethodManager; |
34 | 31 | import com.microsoft.azuretools.azurecommons.helpers.AzureCmdException; |
35 | 32 | import com.microsoft.azuretools.azurecommons.helpers.NotNull; |
36 | | -import com.microsoft.azuretools.azurecommons.helpers.StringHelper; |
| 33 | +import com.microsoft.azuretools.sdkmanage.AzureManager; |
37 | 34 | import com.microsoft.azuretools.telemetry.AppInsightsConstants; |
38 | 35 | import com.microsoft.azuretools.telemetry.TelemetryConstants; |
39 | 36 | import com.microsoft.azuretools.telemetry.TelemetryProperties; |
40 | 37 | import com.microsoft.tooling.msservices.components.DefaultLoader; |
41 | | -import com.microsoft.tooling.msservices.model.storage.BlobContainer; |
42 | 38 | import com.microsoft.tooling.msservices.serviceexplorer.Node; |
43 | | -import com.microsoft.tooling.msservices.serviceexplorer.RefreshableNode; |
44 | | -import org.apache.commons.lang3.exception.ExceptionUtils; |
| 39 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent; |
| 40 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener; |
45 | 41 |
|
46 | | -import java.net.URISyntaxException; |
47 | | -import java.security.InvalidKeyException; |
48 | | -import java.util.Calendar; |
49 | | -import java.util.GregorianCalendar; |
| 42 | +import java.io.IOException; |
50 | 43 | import java.util.HashMap; |
51 | 44 | import java.util.Map; |
52 | | -import java.util.stream.Stream; |
53 | | -import java.util.stream.StreamSupport; |
54 | 45 |
|
55 | | -public class StorageAccountNode extends RefreshableNode implements TelemetryProperties, ILogger { |
| 46 | +public class StorageAccountNode extends Node implements TelemetryProperties, ILogger { |
56 | 47 | private static final String STORAGE_ACCOUNT_MODULE_ID = StorageAccountNode.class.getName(); |
57 | 48 | private static final String ICON_PATH = CommonConst.StorageAccountIConPath; |
58 | 49 | private static final String ADLS_ICON_PATH = CommonConst.ADLS_STORAGE_ACCOUNT_ICON_PATH; |
59 | 50 | private static final String DEFAULT_STORAGE_FLAG = "(default)"; |
| 51 | + private static final String REST_SEGMENT_STORAGE_ACCOUNT = "/storageaccounts"; |
60 | 52 |
|
61 | 53 | private IHDIStorageAccount storageAccount; |
| 54 | + @NotNull |
| 55 | + private IClusterDetail clusterDetail; |
62 | 56 |
|
63 | | - public StorageAccountNode(Node parent, @NotNull IHDIStorageAccount storageAccount, boolean isDefaultStorageAccount) { |
| 57 | + public StorageAccountNode(Node parent, @NotNull IHDIStorageAccount storageAccount, @NotNull IClusterDetail clusterDetail, boolean isDefaultStorageAccount) { |
64 | 58 | super(STORAGE_ACCOUNT_MODULE_ID, isDefaultStorageAccount ? storageAccount.getName() + DEFAULT_STORAGE_FLAG : storageAccount.getName(), parent, getIconPath(storageAccount)); |
65 | 59 | this.storageAccount = storageAccount; |
| 60 | + this.clusterDetail = clusterDetail; |
| 61 | + loadAdditionalActions(); |
66 | 62 | } |
67 | 63 |
|
68 | | - private Stream<BlobContainer> getBlobContainers(String connectionString) throws AzureCmdException { |
69 | | - CloudStorageAccount cloudStorageAccount; |
70 | | - try { |
71 | | - cloudStorageAccount = CloudStorageAccount.parse(connectionString); |
72 | | - } catch (URISyntaxException | InvalidKeyException e) { |
73 | | - throw new AzureCmdException(e.getMessage()); |
74 | | - } |
75 | | - |
76 | | - Iterable<CloudBlobContainer> containers = cloudStorageAccount.createCloudBlobClient().listContainers(); |
77 | | - return StreamSupport.stream(containers.spliterator(), false).map((container) -> { |
78 | | - BlobContainerPermissions permissions = null ; |
79 | | - String access = null; |
80 | | - try { |
81 | | - permissions = container.downloadPermissions(); |
82 | | - } catch (StorageException e) { |
83 | | - // ignore the exception |
84 | | - // We need not to know the permission since the HDInsight cluster itself do have 'write' access to storage |
85 | | - } |
86 | | - if (permissions != null) { |
87 | | - access = permissions.getPublicAccess().toString(); |
88 | | - } |
89 | | - String name = container.getName(); |
90 | | - String eTag = null; |
91 | | - |
92 | | - String uri = container.getUri().toString(); |
93 | | - Calendar lastModified = new GregorianCalendar(); |
94 | | - BlobContainerProperties properties = container.getProperties(); |
95 | | - |
96 | | - if (properties != null) { |
97 | | - eTag = properties.getEtag(); |
98 | | - lastModified.setTime(properties.getLastModified()); |
99 | | - } |
100 | | - return new BlobContainer(name, uri, eTag, lastModified, access); |
101 | | - }); |
102 | | - } |
103 | | - |
104 | | - @Override |
105 | | - protected void refreshItems() { |
106 | | - try { |
107 | | - if (storageAccount.getAccountType() == StorageAccountType.BLOB) { |
108 | | - HDStorageAccount blobStorageAccount = (HDStorageAccount) storageAccount; |
109 | | - String defaultContainer = blobStorageAccount.getDefaultContainer(); |
110 | | - final String connectionString = ((HDStorageAccount) storageAccount).getConnectionString(); |
111 | | - getBlobContainers(connectionString).forEach(blobContainer -> { |
112 | | - addChildNode(new BlobContainerNode(this, blobStorageAccount, blobContainer, !StringHelper.isNullOrWhiteSpace(defaultContainer) && defaultContainer.equals(blobContainer.getName()))); |
113 | | - }); |
114 | | - } else { |
115 | | - StringBuilder sb = new StringBuilder(); |
116 | | - sb.append("Can't refresh the storage account since unsupported storage account type: " + storageAccount.getAccountType() + "\n"); |
117 | | - sb.append("Account name: " + storageAccount.getName() + "\n"); |
118 | | - sb.append("Subscription ID: " + storageAccount.getSubscriptionId() + "\n"); |
119 | | - sb.append("Default storage schema: " + storageAccount.getDefaultStorageSchema() + "\n"); |
120 | | - sb.append("Default container or root path: " + storageAccount.getDefaultContainerOrRootPath() + "\n"); |
121 | | - log().warn(sb.toString()); |
122 | | - } |
123 | | - } catch (Exception ex) { |
124 | | - String exceptionMsg = ex.getCause() == null ? "" : ex.getCause().getMessage(); |
125 | | - String errorHint = String.format("Failed to load storage account %s. ", storageAccount.getName()); |
126 | | - log().warn(errorHint + ExceptionUtils.getStackTrace(ex)); |
127 | | - |
128 | | - DefaultLoader.getUIHelper().showError(errorHint + exceptionMsg, "HDInsight Explorer"); |
| 64 | + protected void loadAdditionalActions() { |
| 65 | + if (clusterDetail instanceof ClusterDetail) { |
| 66 | + addAction("Open Storage in Azure Management Portal", new NodeActionListener() { |
| 67 | + @Override |
| 68 | + protected void actionPerformed(NodeActionEvent e) throws AzureCmdException { |
| 69 | + try { |
| 70 | + final AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager(); |
| 71 | + // not signed in |
| 72 | + if (azureManager == null) { |
| 73 | + return; |
| 74 | + } |
| 75 | + final String portalUrl = azureManager.getPortalUrl(); |
| 76 | + final String tenantId = clusterDetail.getSubscription().getTenantId(); |
| 77 | + String url = portalUrl |
| 78 | + + REST_SEGMENT_JOB_MANAGEMENT_TENANTID |
| 79 | + + tenantId |
| 80 | + + REST_SEGMENT_JOB_MANAGEMENT_RESOURCE |
| 81 | + + ((ClusterDetail) clusterDetail).getId() |
| 82 | + + REST_SEGMENT_STORAGE_ACCOUNT; |
| 83 | + DefaultLoader.getIdeHelper().openLinkInBrowser(url); |
| 84 | + } catch (IOException ex) { |
| 85 | + throw new AzureCmdException(OPEN_RESOURCES_IN_PORTAL_FAILED, ex); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
129 | 89 | } |
130 | 90 | } |
131 | 91 |
|
|
0 commit comments