using Microsoft.Azure.Management.AppService.Fluent; using Microsoft.Azure.Management.Fluent; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using Tango.AzureUtils.Deployment; using Tango.AzureUtils.UI.Managers; using Tango.Core.Commands; using Tango.Core.Cryptography; using Tango.Core.DI; using Tango.SharedUI; namespace Tango.AzureUtils.UI.ViewModels { public class MainViewVM : AzureDashboardViewModel { private IAzure _azure; #region Properties private bool _isInitialized; public bool IsInitialized { get { return _isInitialized; } set { _isInitialized = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private AzureUtilsProgressEventArgs _progress; public AzureUtilsProgressEventArgs Progress { get { return _progress; } set { _progress = value; RaisePropertyChangedAuto(); } } private String _email; public String Email { get { return _email; } set { _email = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _password; public String Password { get { return _password; } set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } #endregion #region Commands public RelayCommand InitCommand { get; set; } #endregion public MainViewVM() { InitCommand = new RelayCommand(Init, () => !IsInitialized && !String.IsNullOrWhiteSpace(Email) && !String.IsNullOrWhiteSpace(Password)); try { Email = Settings.Email; Password = Settings.Password; } catch { } } public override void OnApplicationReady() { StatusManager.StatusUpdated += StatusManager_StatusUpdated; StatusManager.UpdateStatus(AzureUtilsStage.Ready, "Waiting for authentication...", false); } private void StatusManager_StatusUpdated(object sender, AzureUtilsProgressEventArgs e) { Progress = e; } public async void Init() { try { IsFree = false; StatusManager.UpdateStatus(AzureUtilsStage.Initializing, "Authenticating...", true); _azure = await AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync(ConfigurationManager.AppSettings.Get("GatewayUrl"), Email, Password); Settings.Email = Email; Settings.Password = Password; Settings.Save(); List allApps = new List(); StatusManager.UpdateStatus(AzureUtilsStage.Initializing, "Retrieving machine service deployment slots...", true); var apps = await _azure.WebApps.ListAsync(); foreach (var app in apps) { if (app.Name.Contains("Machine")) { allApps.Add(app); } var slots = await app.DeploymentSlots.ListAsync(); foreach (var slot in slots) { if (slot.Name.Contains("Machine")) { allApps.Add(slot); } } } IsInitialized = true; StatusManager.UpdateStatus(AzureUtilsStage.Ready, "Authenticated successfully.", false); TangoIOC.Default.GetAllInstancesByBase().Where(x => x != this).ToList().ForEach(x => x.OnAuthenticated(_azure, allApps.ToList())); } catch (Exception ex) { StatusManager.UpdateStatus(ex); } finally { IsFree = true; } } } }