Skip to content

Commit 557bdfb

Browse files
committed
Merge pull request #940 from nityasharma/dev
Added storage account information to Get-AzureHDInsightCluster's response
2 parents 5d7c796 + b2a0601 commit 557bdfb

File tree

7 files changed

+182
-6
lines changed

7 files changed

+182
-6
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Copyright Microsoft Corporation
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ----------------------------------------------------------------------------------
13+
14+
using System;
15+
using System.Collections.Generic;
16+
using System.Linq;
17+
18+
namespace Microsoft.Azure.Commands.HDInsight.Models
19+
{
20+
internal class ClusterConfigurationUtils
21+
{
22+
public static string GetResourceGroupFromClusterId(string clusterId)
23+
{
24+
// Parse resource group from cluster Id
25+
// The code expects Id to be of the format \
26+
// /subscriptions/<subscription ID>/resourceGroups/<Resource Group Id>/providers/Microsoft.HDInsight/clusters/<cluster name>
27+
28+
string resourceGroup = null;
29+
int index = clusterId.IndexOf("resourceGroups", StringComparison.OrdinalIgnoreCase);
30+
31+
if (index >= 0)
32+
{
33+
index += "resourceGroups".Length;
34+
string[] parts = clusterId.Substring(index).Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
35+
36+
if (parts.Length > 0)
37+
{
38+
resourceGroup = parts[0];
39+
}
40+
}
41+
42+
return resourceGroup;
43+
}
44+
45+
public static AzureHDInsightDefaultStorageAccount GetDefaultStorageAccountDetails(
46+
IDictionary<string, string> configuration,
47+
string version)
48+
{
49+
string key = version.Equals("2.1") ? Constants.ClusterConfiguration.DefaultStorageAccountNameKeyOld
50+
: Constants.ClusterConfiguration.DefaultStorageAccountNameKey;
51+
52+
string accountAndContainerStr;
53+
54+
if (configuration.TryGetValue(key, out accountAndContainerStr))
55+
{
56+
string[] accountAndContainer = accountAndContainerStr.Substring("wasb://".Length).Split('@');
57+
58+
return new AzureHDInsightDefaultStorageAccount
59+
{
60+
StorageContainerName = accountAndContainer[0],
61+
StorageAccountName = accountAndContainer[1],
62+
StorageAccountKey = configuration[Constants.ClusterConfiguration.StorageAccountKeyPrefix + accountAndContainer[1]]
63+
};
64+
}
65+
66+
return null;
67+
}
68+
69+
public static List<string> GetAdditionStorageAccounts(IDictionary<string, string> configuration, string defaultAccount)
70+
{
71+
// Parse the storage account names from the key and exclude the default one
72+
return (from key in configuration.Keys
73+
where key.StartsWith(Constants.ClusterConfiguration.StorageAccountKeyPrefix, StringComparison.OrdinalIgnoreCase) &&
74+
!key.EndsWith(defaultAccount, StringComparison.OrdinalIgnoreCase)
75+
select key.Remove(0, Constants.ClusterConfiguration.StorageAccountKeyPrefix.Length)).ToList();
76+
}
77+
}
78+
}

src/ResourceManager/HDInsight/Commands.HDInsight/Commands.HDInsight.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@
7575
<Compile Include="Models\Management\AzureHDInsightCluster.cs" />
7676
<Compile Include="Models\Management\AzureHDInsightConfig.cs" />
7777
<Compile Include="Models\Job\AzureHdInsightJobManagementClient.cs" />
78+
<Compile Include="Models\Management\AzureHDInsightDefaultStorageAccount.cs" />
7879
<Compile Include="Models\Management\AzureHdInsightManagementClient.cs" />
7980
<Compile Include="Models\Management\AzureHDInsightMetastoreType.cs" />
8081
<Compile Include="Models\Management\AzureHDInsightMetastore.cs" />
8182
<Compile Include="ManagementCommands\NewAzureHDInsightClusterCommand.cs" />
83+
<Compile Include="ClusterConfigurationUtils.cs" />
8284
<Compile Include="Properties\AssemblyInfo.cs" />
8385
</ItemGroup>
8486
<ItemGroup>
@@ -90,7 +92,9 @@
9092
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
9193
</None>
9294
<None Include="MSSharedLibKey.snk" />
93-
<None Include="packages.config" />
95+
<None Include="packages.config">
96+
<SubType>Designer</SubType>
97+
</None>
9498
</ItemGroup>
9599
<ItemGroup>
96100
<Reference Include="Hyak.Common">

src/ResourceManager/HDInsight/Commands.HDInsight/Constants.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,12 @@ public static class JobDefinitions
4040
public const string AzureHDInsightMapReduceJobDefinition = "AzureRmHDInsightMapReduceJobDefinition";
4141
public const string AzureHDInsightStreamingMapReduceJobDefinition = "AzureRmHDInsightStreamingMapReduceJobDefinition";
4242
}
43+
44+
public static class ClusterConfiguration
45+
{
46+
public const string DefaultStorageAccountNameKey = "fs.defaultFS";
47+
public const string DefaultStorageAccountNameKeyOld = "fs.default.name";
48+
public const string StorageAccountKeyPrefix = "fs.azure.account.key.";
49+
}
4350
}
4451
}

src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/GetAzureHDInsightClusterCommand.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using System.Collections.Generic;
1617
using System.Linq;
1718
using System.Management.Automation;
@@ -44,7 +45,14 @@ public class GetAzureHDInsightCommand : HDInsightCmdletBase
4445
protected override void ProcessRecord()
4546
{
4647
var result = HDInsightManagementClient.GetCluster(ResourceGroupName, ClusterName);
47-
var output = result.Select(cluster => new AzureHDInsightCluster(cluster)).ToList();
48+
49+
var output = result.Select(entry =>
50+
{
51+
string resourceGroupName = ClusterConfigurationUtils.GetResourceGroupFromClusterId(entry.Id);
52+
var configuration = HDInsightManagementClient.GetClusterConfigurations(resourceGroupName, entry.Name, "core-site");
53+
return new AzureHDInsightCluster(entry, configuration);
54+
}).ToList();
55+
4856
WriteObject(output, true);
4957
}
5058
}

src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHDInsightCluster.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections.Generic;
1617
using System.Linq;
1718
using Microsoft.Azure.Management.HDInsight.Models;
1819

@@ -34,18 +35,35 @@ public AzureHDInsightCluster(Cluster cluster)
3435
cluster.Properties.ConnectivityEndpoints.FirstOrDefault(c => c.Name.Equals("HTTPS", StringComparison.OrdinalIgnoreCase));
3536
HttpEndpoint = httpEndpoint != null ? httpEndpoint.Location : null;
3637

38+
ResourceGroup = ClusterConfigurationUtils.GetResourceGroupFromClusterId(cluster.Id);
39+
}
40+
41+
public AzureHDInsightCluster(Cluster cluster, IDictionary<string, string> clusterConfiguration)
42+
: this(cluster)
43+
{
44+
if (clusterConfiguration != null)
45+
{
46+
var defaultAccount = ClusterConfigurationUtils.GetDefaultStorageAccountDetails(
47+
clusterConfiguration,
48+
cluster.Properties.ClusterVersion);
49+
50+
DefaultStorageAccount = defaultAccount.StorageAccountName;
51+
DefaultStorageContainer = defaultAccount.StorageContainerName;
52+
53+
AdditionalStorageAccounts = ClusterConfigurationUtils.GetAdditionStorageAccounts(clusterConfiguration, DefaultStorageAccount);
54+
}
3755
}
3856

3957
/// <summary>
4058
/// The name of the resource.
4159
/// </summary>
4260
public string Name { get; set; }
43-
61+
4462
/// <summary>
4563
/// The ID of the resource.
4664
/// </summary>
4765
public string Id { get; set; }
48-
66+
4967
/// <summary>
5068
/// The location of the resource.
5169
/// </summary>
@@ -80,5 +98,25 @@ public AzureHDInsightCluster(Cluster cluster)
8098
/// The endpoint with which to connect to the cluster.
8199
/// </summary>
82100
public string HttpEndpoint { get; set; }
101+
102+
/// <summary>
103+
/// Default storage account for this cluster.
104+
/// </summary>
105+
public string DefaultStorageAccount { get; set; }
106+
107+
/// <summary>
108+
/// Default storage container for this cluster.
109+
/// </summary>
110+
public string DefaultStorageContainer { get; set; }
111+
112+
/// <summary>
113+
/// Default storage container for this cluster.
114+
/// </summary>
115+
public string ResourceGroup { get; set; }
116+
117+
/// <summary>
118+
/// Additional storage accounts for this cluster
119+
/// </summary>
120+
public List<string> AdditionalStorageAccounts { get; set; }
83121
}
84122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Copyright Microsoft Corporation
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ----------------------------------------------------------------------------------
13+
14+
namespace Microsoft.Azure.Commands.HDInsight.Models
15+
{
16+
public class AzureHDInsightDefaultStorageAccount
17+
{
18+
public string StorageAccountName { get; set; }
19+
20+
public string StorageAccountKey { get; set; }
21+
22+
public string StorageContainerName { get; set; }
23+
}
24+
}

src/ResourceManager/HDInsight/Commands.HDInsight/Models/Management/AzureHdInsightManagementClient.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public virtual List<Cluster> GetCluster(string resourceGroupName, string cluster
5959
var getresponse = Get(resourceGroupName, clusterName);
6060
if (getresponse != null)
6161
{
62-
result.Add(getresponse.Cluster);
62+
result.Add(getresponse.Cluster);
6363
}
6464
}
6565
return result;
@@ -72,7 +72,7 @@ public virtual ClusterListResponse ListClusters()
7272

7373
public virtual ClusterListResponse ListClusters(string resourceGroupName)
7474
{
75-
return HdInsightManagementClient.Clusters.ListByResourceGroup(resourceGroupName);
75+
return HdInsightManagementClient.Clusters.ListByResourceGroup(resourceGroupName);
7676
}
7777

7878
public virtual ClusterGetResponse Get(string resourceGroupName, string clusterName)
@@ -109,5 +109,22 @@ public virtual CapabilitiesResponse GetCapabilities(string location)
109109
{
110110
return HdInsightManagementClient.Clusters.GetCapabilities(location);
111111
}
112+
113+
public virtual IDictionary<string, string> GetClusterConfigurations(string resourceGroupName, string clusterName, string configurationName)
114+
{
115+
Dictionary<string, string> properties = new Dictionary<string, string>();
116+
117+
if (string.IsNullOrWhiteSpace(resourceGroupName) ||
118+
string.IsNullOrWhiteSpace(clusterName) ||
119+
string.IsNullOrWhiteSpace(configurationName))
120+
{
121+
return properties;
122+
}
123+
124+
return HdInsightManagementClient.Clusters.GetClusterConfigurations(
125+
resourceGroupName,
126+
clusterName,
127+
configurationName).Configuration;
128+
}
112129
}
113130
}

0 commit comments

Comments
 (0)