-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathStorageAccountForModulesDialog.xaml.cs
158 lines (147 loc) · 8.11 KB
/
StorageAccountForModulesDialog.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// ----------------------------------------------------------------------------------
//
// 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 AutomationISE.Model;
using Microsoft.Azure.Management.Storage.Models;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure;
namespace AutomationISE
{
/// <summary>
/// Interaction logic for StorageAccountForModulesDialog.xaml
/// </summary>
public partial class StorageAccountForModulesDialog : Window
{
IEnumerable<StorageAccount> storageAccounts;
public String storageAccountName;
public String storageSubID;
public String storageResourceGroupName;
public String region;
public String authority = null;
public bool createNewStorageAccount = false;
public StorageAccountForModulesDialog()
{
InitializeComponent();
}
private async void subscriptionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var subObject = (AutomationISEClient.SubscriptionObject) subscriptionComboBox.SelectedItem;
var azureARMAuthResult = AuthenticateHelper.RefreshTokenByAuthority(subObject.Authority, Properties.Settings.Default.appIdURI);
authority = subObject.Authority;
var authToken = azureARMAuthResult.AccessToken;
var token = new Microsoft.Rest.TokenCredentials(authToken);
var storageManagementClient = new Microsoft.Azure.Management.Storage.StorageManagementClient(new Uri(Properties.Settings.Default.appIdURI), token);
storageManagementClient.SubscriptionId = subObject.SubscriptionId;
resourceGroupcomboBox.Items.Clear();
try
{
storageAccounts = storageManagementClient.StorageAccounts.List();
foreach (var storageAccount in storageAccounts)
{
var startPosition = storageAccount.Id.IndexOf("/resourceGroups/");
var endPosition = storageAccount.Id.IndexOf("/", startPosition + 16);
var resourceGroup = storageAccount.Id.Substring(startPosition + 16, endPosition - startPosition - 16);
if (resourceGroupcomboBox.Items.IndexOf(resourceGroup) == -1)
{
resourceGroupcomboBox.Items.Add(resourceGroup);
}
}
var cloudtoken = AuthenticateHelper.RefreshTokenByAuthority(authority, Properties.Settings.Default.appIdURI);
var subscriptionCreds = new TokenCloudCredentials(((AutomationISEClient.SubscriptionObject)subscriptionComboBox.SelectedItem).SubscriptionId, cloudtoken.AccessToken);
var resourceManagementClient = new Microsoft.Azure.Subscriptions.SubscriptionClient(subscriptionCreds, new Uri(Properties.Settings.Default.appIdURI));
CancellationToken cancelToken = new CancellationToken();
var subscriptionRegions = await resourceManagementClient.Subscriptions.ListLocationsAsync(((AutomationISEClient.SubscriptionObject)subscriptionComboBox.SelectedItem).SubscriptionId, cancelToken);
regionComboBox.ItemsSource = subscriptionRegions.Locations;
regionComboBox.DisplayMemberPath = "Name";
}
catch (Exception Ex)
{
throw Ex;
}
}
private void resourceGroupcomboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CollectionView storageAccountcomboBoxView = (CollectionView)CollectionViewSource.GetDefaultView(storageAccounts);
storageAccountcomboBox.ItemsSource = storageAccounts;
storageAccountcomboBox.DisplayMemberPath = "Name";
storageAccountcomboBoxView.Filter = item =>
{
StorageAccount storageItem = item as StorageAccount;
if (storageItem == null || resourceGroupcomboBox.SelectedItem == null) return false;
return storageItem.Id.Contains("/" + resourceGroupcomboBox.SelectedItem.ToString() + "/");
};
}
private async void OKbutton_Click(object sender, RoutedEventArgs e)
{
try
{
if (!String.IsNullOrEmpty(subscriptionComboBox.Text) && !String.IsNullOrEmpty(resourceGroupcomboBox.Text) && !String.IsNullOrEmpty(storageAccountcomboBox.Text))
{
if (storageAccountcomboBox.SelectedItem == null)
{
var subObject = (AutomationISEClient.SubscriptionObject)subscriptionComboBox.SelectedItem;
var azureARMAuthResult = AuthenticateHelper.RefreshTokenByAuthority(subObject.Authority, Properties.Settings.Default.appIdURI);
var authToken = azureARMAuthResult.AccessToken;
var token = new Microsoft.Rest.TokenCredentials(authToken);
var storageManagementClient = new Microsoft.Azure.Management.Storage.StorageManagementClient(new Uri(Properties.Settings.Default.appIdURI), token);
storageManagementClient.SubscriptionId = subObject.SubscriptionId;
var result = await storageManagementClient.StorageAccounts.CheckNameAvailabilityAsync(storageAccountcomboBox.Text);
if (!(result.NameAvailable.Value))
{
var messageBoxResult = System.Windows.Forms.MessageBox.Show(
"Storage account name is not available in this subscription. Please choose another name", "Name not available",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
createNewStorageAccount = true;
storageSubID = ((AutomationISEClient.SubscriptionObject)subscriptionComboBox.SelectedItem).SubscriptionId;
storageResourceGroupName = resourceGroupcomboBox.Text;
storageAccountName = storageAccountcomboBox.Text;
region = regionComboBox.Text;
this.DialogResult = true;
this.Close();
}
}
else
{
storageSubID = ((AutomationISEClient.SubscriptionObject)subscriptionComboBox.SelectedItem).SubscriptionId;
storageResourceGroupName = resourceGroupcomboBox.SelectedItem.ToString();
storageAccountName = storageAccountcomboBox.Text;
this.DialogResult = true;
this.Close();
}
}
}
catch (Exception Ex)
{
throw Ex;
}
}
private void storageAccountcomboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
regionComboBox.IsEnabled = true;
if (storageAccountcomboBox.SelectedItem != null)
{
regionComboBox.Text = ((Microsoft.Azure.Management.Storage.Models.StorageAccount)storageAccountcomboBox.SelectedItem).PrimaryLocation;
regionComboBox.IsEnabled = false;
}
}
}
}