/// <summary>
        /// Handles the Click event of the Done control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Done_Click(object sender, RoutedEventArgs e)
        {
            this.progressBar.Visibility = Visibility.Visible;
            this.btnDone.IsEnabled      = false;
            var selectedSubscription = (this.comboSubscriptionList as ComboBox).SelectedItem as RM.Models.SubscriptionInner;
            var selectedServiceBus   = (this.comboServiceBusList as ComboBox).SelectedItem as NamespaceModelInner;

            string newBusName = this.txtServiceBusName.Text;
            string rgName     = null;

            if (!string.IsNullOrEmpty(selectedServiceBus.Id))
            {
                int startIndex = selectedServiceBus.Id.IndexOf("resourceGroups") + 15;
                rgName = selectedServiceBus.Id.Substring(startIndex, selectedServiceBus.Id.IndexOf('/', startIndex) - startIndex);
            }

            TokenCredentials           tokenCredentials           = new TokenCredentials(this.authResult.AccessToken);
            ServiceBusManagementClient serviceBusManagementClient = new ServiceBusManagementClient(tokenCredentials);

            serviceBusManagementClient.SubscriptionId = selectedSubscription.SubscriptionId;

            List <SharedAccessAuthorizationRuleInner> serviceBusList = new List <SharedAccessAuthorizationRuleInner>();

            // Case 1. When user used existing service bus.
            Thread existingServiceBusThread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    var resp = serviceBusManagementClient.Namespaces.ListAuthorizationRules(rgName, selectedServiceBus.Name);
                    serviceBusList.AddRange(resp);

                    while (!string.IsNullOrEmpty(resp.NextPageLink))
                    {
                        resp = serviceBusManagementClient.Namespaces.ListAuthorizationRulesNext(resp.NextPageLink);
                        serviceBusList.AddRange(resp);
                    }

                    var selectedAuthRule = serviceBusList.FirstOrDefault(rule => rule.Rights != null && rule.Rights.Contains(AccessRights.Listen) && rule.Rights.Contains(AccessRights.Manage) && rule.Rights.Contains(AccessRights.Send));

                    if (selectedAuthRule == null)
                    {
                        MessageBox.Show("Failed to find a suitable Authorization rule to use. Please create an Authorization rule with Listen, Manage and Send rights and retry the operation");
                        this.Dispatcher.Invoke(() =>
                        {
                            this.progressBar.Visibility = Visibility.Hidden;
                            this.btnDone.IsEnabled      = true;
                        });
                        return;
                    }
                    else
                    {
                        serviceBusManagementClient.Namespaces.ListKeys(rgName, selectedServiceBus.Name, selectedAuthRule.Name);

                        ApplicationData.Instance.ServiceBusSharedKey = serviceBusManagementClient.Namespaces.ListKeys(rgName, selectedServiceBus.Name, selectedAuthRule.Name).PrimaryKey;
                        ApplicationData.Instance.ServiceBusKeyName   = serviceBusManagementClient.Namespaces.ListKeys(rgName, selectedServiceBus.Name, selectedAuthRule.Name).KeyName;
                        ApplicationData.Instance.ServiceBusUrl       = selectedServiceBus.ServiceBusEndpoint;

                        this.Dispatcher.Invoke(() =>
                        {
                            MainWindow mainWindow = new MainWindow();
                            mainWindow.Show();
                            this.Close();
                        });
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(CallInfo.Site(), ex, "Failed to connect to service bus namespace");

                    this.Dispatcher.Invoke(() => MessageBox.Show("Failed to connect to service bus namespace!!", "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error));
                }
            }));

            // Case 2. When user created a new service bus.
            Thread newServiceBusThread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    RM.ResourceManagementClient resourceManagementClient = new RM.ResourceManagementClient(tokenCredentials);
                    resourceManagementClient.SubscriptionId = selectedSubscription.SubscriptionId;

                    RM.Models.ResourceGroupInner resourceGroup = null;

                    try
                    {
                        resourceGroup = resourceManagementClient.ResourceGroups.Get("TunnelRelay");
                    }
                    catch (Microsoft.Rest.Azure.CloudException httpEx)
                    {
                        if (httpEx.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                        {
                            resourceGroup = resourceManagementClient.ResourceGroups.CreateOrUpdate("TunnelRelay", new RM.Models.ResourceGroupInner
                            {
                                Location = "WestUS",
                                Name     = "TunnelRelay",
                                Tags     = newServiceBus.Tags,
                            });
                        }
                    }

                    rgName = resourceGroup.Name;

                    if (string.IsNullOrEmpty(newBusName))
                    {
                        MessageBox.Show("Please enter the name for service bus.");
                        this.Dispatcher.Invoke(() =>
                        {
                            this.progressBar.Visibility = Visibility.Hidden;
                            this.btnDone.IsEnabled      = true;
                        });
                        return;
                    }

                    if (newBusName.Length < 6)
                    {
                        MessageBox.Show("Name of service bus must be at least 6 characters.");
                        this.Dispatcher.Invoke(() =>
                        {
                            this.progressBar.Visibility = Visibility.Hidden;
                            this.btnDone.IsEnabled      = true;
                        });
                        return;
                    }

                    selectedServiceBus = serviceBusManagementClient.Namespaces.CreateOrUpdate(rgName, newBusName, new NamespaceModelInner
                    {
                        Location = selectedServiceBus.Location,
                        Sku      = selectedServiceBus.Sku,
                        Tags     = selectedServiceBus.Tags,
                    });

                    var resp = serviceBusManagementClient.Namespaces.ListAuthorizationRules(rgName, selectedServiceBus.Name);
                    serviceBusList.AddRange(resp);

                    while (!string.IsNullOrEmpty(resp.NextPageLink))
                    {
                        resp = serviceBusManagementClient.Namespaces.ListAuthorizationRulesNext(resp.NextPageLink);
                        serviceBusList.AddRange(resp);
                    }

                    var selectedAuthRule = serviceBusList.FirstOrDefault(rule => rule.Rights != null && rule.Rights.Contains(AccessRights.Listen) && rule.Rights.Contains(AccessRights.Manage) && rule.Rights.Contains(AccessRights.Send));

                    if (selectedAuthRule == null)
                    {
                        MessageBox.Show("Failed to find a suitable Authorization rule to use. Please create an Authorization rule with Listen, Manage and Send rights and retry the operation");
                        this.Dispatcher.Invoke(() =>
                        {
                            this.progressBar.Visibility = Visibility.Hidden;
                            this.btnDone.IsEnabled      = true;
                        });
                        return;
                    }
                    else
                    {
                        ApplicationData.Instance.ServiceBusSharedKey = serviceBusManagementClient.Namespaces.ListKeys(rgName, selectedServiceBus.Name, selectedAuthRule.Name).PrimaryKey;
                        ApplicationData.Instance.ServiceBusKeyName   = serviceBusManagementClient.Namespaces.ListKeys(rgName, selectedServiceBus.Name, selectedAuthRule.Name).KeyName;
                        ApplicationData.Instance.ServiceBusUrl       = selectedServiceBus.ServiceBusEndpoint;

                        this.Dispatcher.Invoke(() =>
                        {
                            MainWindow mainWindow = new MainWindow();
                            mainWindow.Show();
                            this.Close();
                        });
                    }
                }
                catch (CloudException cloudEx)
                {
                    Logger.LogError(CallInfo.Site(), cloudEx, "Cloud exception while creating service bus namespace.");

                    this.Dispatcher.Invoke(() =>
                    {
                        MessageBox.Show(cloudEx.Message, "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        this.progressBar.Visibility = Visibility.Hidden;
                        this.btnDone.IsEnabled      = true;
                    });
                }
                catch (Exception ex)
                {
                    Logger.LogError(CallInfo.Site(), ex, "Failed to create new service bus namespace");

                    this.Dispatcher.Invoke(() =>
                    {
                        MessageBox.Show("Failed to create new service bus namespace!!", "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        this.progressBar.Visibility = Visibility.Hidden;
                        this.btnDone.IsEnabled      = true;
                    });
                }
            }));

            if (selectedServiceBus.Id == null)
            {
                newServiceBusThread.Start();
            }
            else
            {
                existingServiceBusThread.Start();
            }
        }
Пример #2
0
        /// <summary>
        /// Handles the Click event of the Done control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Done_Click(object sender, RoutedEventArgs e)
        {
            this.progressBar.Visibility = Visibility.Visible;
            this.btnDone.IsEnabled      = false;
            var selectedSubscription = (this.comboSubscriptionList as ComboBox).SelectedItem as RM.Models.SubscriptionInner;
            var selectedServiceBus   = (this.comboServiceBusList as ComboBox).SelectedItem as NamespaceModelInner;

            string newBusName = this.txtServiceBusName.Text;
            string rgName     = null;

            if (!string.IsNullOrEmpty(selectedServiceBus.Id))
            {
                int startIndex = selectedServiceBus.Id.IndexOf("resourceGroups") + 15;
                rgName = selectedServiceBus.Id.Substring(startIndex, selectedServiceBus.Id.IndexOf('/', startIndex) - startIndex);
            }

            TokenCredentials           tokenCredentials           = new TokenCredentials(this.userAuthenticator.GetSubscriptionSpecificUserToken(selectedSubscription).AccessToken);
            ServiceBusManagementClient serviceBusManagementClient = new ServiceBusManagementClient(tokenCredentials);

            serviceBusManagementClient.SubscriptionId = selectedSubscription.SubscriptionId;

            List <SharedAccessAuthorizationRuleInner> serviceBusList = new List <SharedAccessAuthorizationRuleInner>();

            // Case 1. When user used existing service bus.
            Thread existingServiceBusThread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    this.SetApplicationData(rgName, serviceBusManagementClient, selectedServiceBus);
                }
                catch (Exception ex)
                {
                    Logger.LogError(CallInfo.Site(), ex, "Failed to connect to service bus namespace");

                    this.Dispatcher.Invoke(() => MessageBox.Show("Failed to connect to service bus namespace!!", "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error));
                }
            }));

            // Case 2. When user created a new service bus.
            Thread newServiceBusThread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    RM.ResourceManagementClient resourceManagementClient = new RM.ResourceManagementClient(tokenCredentials);
                    resourceManagementClient.SubscriptionId = selectedSubscription.SubscriptionId;

                    RM.Models.ResourceGroupInner resourceGroup = null;

                    try
                    {
                        resourceGroup = resourceManagementClient.ResourceGroups.GetAsync("TunnelRelay").ConfigureAwait(false).GetAwaiter().GetResult();
                    }
                    catch (Microsoft.Rest.Azure.CloudException httpEx)
                    {
                        if (httpEx.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                        {
                            resourceGroup = resourceManagementClient.ResourceGroups.CreateOrUpdateAsync("TunnelRelay", new RM.Models.ResourceGroupInner
                            {
                                Location = "WestUS",
                                Name     = "TunnelRelay",
                                Tags     = newServiceBus.Tags,
                            }).ConfigureAwait(false).GetAwaiter().GetResult();
                        }
                    }

                    rgName = resourceGroup.Name;

                    if (string.IsNullOrEmpty(newBusName))
                    {
                        MessageBox.Show("Please enter the name for service bus.");
                        this.Dispatcher.Invoke(() =>
                        {
                            this.progressBar.Visibility = Visibility.Hidden;
                            this.btnDone.IsEnabled      = true;
                        });
                        return;
                    }

                    if (newBusName.Length < 6)
                    {
                        MessageBox.Show("Name of service bus must be at least 6 characters.");
                        this.Dispatcher.Invoke(() =>
                        {
                            this.progressBar.Visibility = Visibility.Hidden;
                            this.btnDone.IsEnabled      = true;
                        });
                        return;
                    }

                    selectedServiceBus = serviceBusManagementClient.Namespaces.CreateOrUpdateAsync(rgName, newBusName, new NamespaceModelInner
                    {
                        Location = selectedServiceBus.Location,
                        Sku      = selectedServiceBus.Sku,
                        Tags     = selectedServiceBus.Tags,
                    }).ConfigureAwait(false).GetAwaiter().GetResult();

                    this.SetApplicationData(rgName, serviceBusManagementClient, selectedServiceBus);
                }
                catch (CloudException cloudEx)
                {
                    Logger.LogError(CallInfo.Site(), cloudEx, "Cloud exception while creating service bus namespace.");

                    this.Dispatcher.Invoke(() =>
                    {
                        MessageBox.Show(cloudEx.Message, "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        this.progressBar.Visibility = Visibility.Hidden;
                        this.btnDone.IsEnabled      = true;
                    });
                }
                catch (Exception ex)
                {
                    Logger.LogError(CallInfo.Site(), ex, "Failed to create new service bus namespace");

                    this.Dispatcher.Invoke(() =>
                    {
                        MessageBox.Show("Failed to create new service bus namespace!!", "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        this.progressBar.Visibility = Visibility.Hidden;
                        this.btnDone.IsEnabled      = true;
                    });
                }
            }));

            if (selectedServiceBus.Id == null)
            {
                newServiceBusThread.Start();
            }
            else
            {
                existingServiceBusThread.Start();
            }
        }