diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/ClusterConfigurationUtils.cs b/src/ResourceManager/HDInsight/Commands.HDInsight/ClusterConfigurationUtils.cs new file mode 100644 index 000000000000..4c9216626e28 --- /dev/null +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/ClusterConfigurationUtils.cs @@ -0,0 +1,78 @@ +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.Azure.Commands.HDInsight.Models +{ + internal class ClusterConfigurationUtils + { + public static string GetResourceGroupFromClusterId(string clusterId) + { + // Parse resource group from cluster Id + // The code expects Id to be of the format \ + // /subscriptions//resourceGroups//providers/Microsoft.HDInsight/clusters/ + + string resourceGroup = null; + int index = clusterId.IndexOf("resourceGroups", StringComparison.OrdinalIgnoreCase); + + if (index >= 0) + { + index += "resourceGroups".Length; + string[] parts = clusterId.Substring(index).Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length > 0) + { + resourceGroup = parts[0]; + } + } + + return resourceGroup; + } + + public static AzureHDInsightDefaultStorageAccount GetDefaultStorageAccountDetails( + IDictionary configuration, + string version) + { + string key = version.Equals("2.1") ? Constants.ClusterConfiguration.DefaultStorageAccountNameKeyOld + : Constants.ClusterConfiguration.DefaultStorageAccountNameKey; + + string accountAndContainerStr; + + if (configuration.TryGetValue(key, out accountAndContainerStr)) + { + string[] accountAndContainer = accountAndContainerStr.Substring("wasb://".Length).Split('@'); + + return new AzureHDInsightDefaultStorageAccount + { + StorageContainerName = accountAndContainer[0], + StorageAccountName = accountAndContainer[1], + StorageAccountKey = configuration[Constants.ClusterConfiguration.StorageAccountKeyPrefix + accountAndContainer[1]] + }; + } + + return null; + } + + public static List GetAdditionStorageAccounts(IDictionary configuration, string defaultAccount) + { + // Parse the storage account names from the key and exclude the default one + return (from key in configuration.Keys + where key.StartsWith(Constants.ClusterConfiguration.StorageAccountKeyPrefix, StringComparison.OrdinalIgnoreCase) && + !key.EndsWith(defaultAccount, StringComparison.OrdinalIgnoreCase) + select key.Remove(0, Constants.ClusterConfiguration.StorageAccountKeyPrefix.Length)).ToList(); + } + } +} diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/Commands.HDInsight.csproj b/src/ResourceManager/HDInsight/Commands.HDInsight/Commands.HDInsight.csproj index 97b2eba84866..8bac39dcbcdd 100644 --- a/src/ResourceManager/HDInsight/Commands.HDInsight/Commands.HDInsight.csproj +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/Commands.HDInsight.csproj @@ -75,10 +75,12 @@ + + @@ -86,7 +88,9 @@ Always - + + Designer + diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/Constants.cs b/src/ResourceManager/HDInsight/Commands.HDInsight/Constants.cs index a63f650f4b67..9b9da5a4923a 100644 --- a/src/ResourceManager/HDInsight/Commands.HDInsight/Constants.cs +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/Constants.cs @@ -40,5 +40,12 @@ public static class JobDefinitions public const string AzureHDInsightMapReduceJobDefinition = "AzureRMHDInsightMapReduceJobDefinition"; public const string AzureHDInsightStreamingMapReduceJobDefinition = "AzureRMHDInsightStreamingMapReduceJobDefinition"; } + + public static class ClusterConfiguration + { + public const string DefaultStorageAccountNameKey = "fs.defaultFS"; + public const string DefaultStorageAccountNameKeyOld = "fs.default.name"; + public const string StorageAccountKeyPrefix = "fs.azure.account.key."; + } } } diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/GetAzureHDInsightClusterCommand.cs b/src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/GetAzureHDInsightClusterCommand.cs index 853f577fea62..dfab29435d2f 100644 --- a/src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/GetAzureHDInsightClusterCommand.cs +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/GetAzureHDInsightClusterCommand.cs @@ -12,6 +12,7 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using System; using System.Collections.Generic; using System.Linq; using System.Management.Automation; @@ -44,7 +45,14 @@ public class GetAzureHDInsightCommand : HDInsightCmdletBase protected override void ProcessRecord() { var result = HDInsightManagementClient.GetCluster(ResourceGroupName, ClusterName); - var output = result.Select(cluster => new AzureHDInsightCluster(cluster)).ToList(); + + var output = result.Select(entry => + { + string resourceGroupName = ClusterConfigurationUtils.GetResourceGroupFromClusterId(entry.Id); + var configuration = HDInsightManagementClient.GetClusterConfigurations(resourceGroupName, entry.Name, "core-site"); + return new AzureHDInsightCluster(entry, configuration); + }).ToList(); + WriteObject(output, true); } } diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightCluster.cs b/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightCluster.cs index 8a03fd720ee0..5b1584ea6a72 100644 --- a/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightCluster.cs +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightCluster.cs @@ -13,6 +13,7 @@ // ---------------------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Linq; using Microsoft.Azure.Management.HDInsight.Models; @@ -34,18 +35,35 @@ public AzureHDInsightCluster(Cluster cluster) cluster.Properties.ConnectivityEndpoints.FirstOrDefault(c => c.Name.Equals("HTTPS", StringComparison.OrdinalIgnoreCase)); HttpEndpoint = httpEndpoint != null ? httpEndpoint.Location : null; + ResourceGroup = ClusterConfigurationUtils.GetResourceGroupFromClusterId(cluster.Id); + } + + public AzureHDInsightCluster(Cluster cluster, IDictionary clusterConfiguration) + : this(cluster) + { + if (clusterConfiguration != null) + { + var defaultAccount = ClusterConfigurationUtils.GetDefaultStorageAccountDetails( + clusterConfiguration, + cluster.Properties.ClusterVersion); + + DefaultStorageAccount = defaultAccount.StorageAccountName; + DefaultStorageContainer = defaultAccount.StorageContainerName; + + AdditionalStorageAccounts = ClusterConfigurationUtils.GetAdditionStorageAccounts(clusterConfiguration, DefaultStorageAccount); + } } /// /// The name of the resource. /// public string Name { get; set; } - + /// /// The ID of the resource. /// public string Id { get; set; } - + /// /// The location of the resource. /// @@ -80,5 +98,25 @@ public AzureHDInsightCluster(Cluster cluster) /// The endpoint with which to connect to the cluster. /// public string HttpEndpoint { get; set; } + + /// + /// Default storage account for this cluster. + /// + public string DefaultStorageAccount { get; set; } + + /// + /// Default storage container for this cluster. + /// + public string DefaultStorageContainer { get; set; } + + /// + /// Default storage container for this cluster. + /// + public string ResourceGroup { get; set; } + + /// + /// Additional storage accounts for this cluster + /// + public List AdditionalStorageAccounts { get; set; } } } diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightDefaultStorageAccount.cs b/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightDefaultStorageAccount.cs new file mode 100644 index 000000000000..9a6dca3733b2 --- /dev/null +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightDefaultStorageAccount.cs @@ -0,0 +1,24 @@ +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.Commands.HDInsight.Models +{ + public class AzureHDInsightDefaultStorageAccount + { + public string StorageAccountName { get; set; } + + public string StorageAccountKey { get; set; } + + public string StorageContainerName { get; set; } + } +} diff --git a/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHdInsightManagementClient.cs b/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHdInsightManagementClient.cs index abacaac0d61c..b1a8582480e2 100644 --- a/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHdInsightManagementClient.cs +++ b/src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHdInsightManagementClient.cs @@ -59,7 +59,7 @@ public virtual List GetCluster(string resourceGroupName, string cluster var getresponse = Get(resourceGroupName, clusterName); if (getresponse != null) { - result.Add(getresponse.Cluster); + result.Add(getresponse.Cluster); } } return result; @@ -72,7 +72,7 @@ public virtual ClusterListResponse ListClusters() public virtual ClusterListResponse ListClusters(string resourceGroupName) { - return HdInsightManagementClient.Clusters.ListByResourceGroup(resourceGroupName); + return HdInsightManagementClient.Clusters.ListByResourceGroup(resourceGroupName); } public virtual ClusterGetResponse Get(string resourceGroupName, string clusterName) @@ -109,5 +109,22 @@ public virtual CapabilitiesResponse GetCapabilities(string location) { return HdInsightManagementClient.Clusters.GetCapabilities(location); } + + public virtual IDictionary GetClusterConfigurations(string resourceGroupName, string clusterName, string configurationName) + { + Dictionary properties = new Dictionary(); + + if (string.IsNullOrWhiteSpace(resourceGroupName) || + string.IsNullOrWhiteSpace(clusterName) || + string.IsNullOrWhiteSpace(configurationName)) + { + return properties; + } + + return HdInsightManagementClient.Clusters.GetClusterConfigurations( + resourceGroupName, + clusterName, + configurationName).Configuration; + } } }