Skip to content

Commit

Permalink
Fix: AWS Session Manager sync on first load (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
BornToBeRoot authored Oct 5, 2022
1 parent 8c8a8fb commit 4d24159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
7 changes: 3 additions & 4 deletions Source/NETworkManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ protected override async void OnContentRendered(EventArgs e)

private void AfterContentRendered()
{
// Load the profiles before the applications are loaded so that we can use them (e.g. for synchronization)
LoadProfiles();

// Load application list, filter, sort, etc.
LoadApplicationList();

// Load profiles
LoadProfiles();

// Hide to tray after the window shows up... not nice, but otherwise the hotkeys do not work
if (CommandLineManager.Current.Autostart && SettingsManager.Current.Autostart_StartMinimizedInTray)
HideWindowToTray();
Expand Down Expand Up @@ -1200,7 +1200,6 @@ protected override void OnSourceInitialized(EventArgs e)
base.OnSourceInitialized(e);

_hwndSoure = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);

_hwndSoure?.AddHook(HwndHook);

RegisterHotKeys();
Expand Down
51 changes: 32 additions & 19 deletions Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ public AWSSessionManagerHostViewModel(IDialogCoordinator instance)
SettingsManager.Current.PropertyChanged += Current_PropertyChanged;
SettingsManager.Current.AWSSessionManager_AWSProfiles.CollectionChanged += AWSSessionManager_AWSProfiles_CollectionChanged;

SyncAllInstanceIDsFromAWS();

_isLoading = false;
}

Expand Down Expand Up @@ -446,7 +448,7 @@ private void EditGroupAction(object group)
ProfileDialogManager.ShowEditGroupDialog(this, _dialogCoordinator, ProfileManager.GetGroup(group.ToString()));
}

private bool SyncInstanceIDsFromAWS_CanExecute(object obj) => !IsSyncing && SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS;
private bool SyncInstanceIDsFromAWS_CanExecute(object obj) => !IsSyncing && IsSyncEnabled;

public ICommand SyncAllInstanceIDsFromAWSCommand => new RelayCommand(p => SyncAllInstanceIDsFromAWSAction(), SyncInstanceIDsFromAWS_CanExecute);

Expand Down Expand Up @@ -523,14 +525,27 @@ private void CheckSettings()
IsPowerShellConfigured = !string.IsNullOrEmpty(SettingsManager.Current.AWSSessionManager_ApplicationFilePath) && File.Exists(SettingsManager.Current.AWSSessionManager_ApplicationFilePath);
}

private bool IsConfigured => IsAWSCLIInstalled && IsAWSSessionManagerPluginInstalled && IsPowerShellConfigured;

private async Task SyncAllInstanceIDsFromAWS()
{
_log.Info("Sync all EC2 Instance(s) from AWS...");
{
if (!IsSyncEnabled)
{
_log.Info("Sync all EC2 instances from AWS is disabled in the settings.");
return;
}

_log.Info("Sync all EC2 instance(s) from AWS...");

// Check if prerequisites are met
if (!IsAWSCLIInstalled || !IsAWSSessionManagerPluginInstalled)
if (!IsConfigured)
{
_log.Warn($"Prerequisites not met! AWS CLI installed {IsAWSCLIInstalled}. AWS Session Manager plugin installed {IsAWSSessionManagerPluginInstalled}");
_log.Warn($"Preconditions not met! AWS CLI installed {IsAWSCLIInstalled}. AWS Session Manager plugin installed {IsAWSSessionManagerPluginInstalled}. PowerShell configured {IsPowerShellConfigured}.");
return;
}

if (IsSyncing)
{
_log.Info("Skip... Sync is already running!");
return;
}

Expand All @@ -545,7 +560,7 @@ private async Task SyncAllInstanceIDsFromAWS()
}
else
{
_log.Warn("MainWindow is null!");
_log.Warn("Cannot find MainWindow because it is null!");
return;
}

Expand All @@ -555,7 +570,7 @@ private async Task SyncAllInstanceIDsFromAWS()
{
if (!profile.IsEnabled)
{
_log.Info($"Sync EC2 Instance(s) for AWS profile \"[{profile.Profile}\\{profile.Region}]\" is disabled! Skip...");
_log.Info($"Skip AWS profile \"[{profile.Profile}\\{profile.Region}]\" because it is disabled!");
continue;
}

Expand Down Expand Up @@ -669,7 +684,7 @@ private async Task SyncInstanceIDsFromAWS(string profile, string region)
if (ProfileManager.GroupExists(groupName))
ProfileManager.RemoveGroup(ProfileManager.GetGroup(groupName));

_log.Info("No EC2 Instance(s) found!");
_log.Info("No EC2 Instance(s) found!");
}
else
{
Expand Down Expand Up @@ -873,7 +888,9 @@ public void OnViewVisible(bool fromSettings)

RefreshProfiles();

if (!fromSettings && SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
// Do not synchronize If the view becomes visible again
// after the settings have been opened
if (!fromSettings)
SyncAllInstanceIDsFromAWS();
}

Expand All @@ -884,8 +901,7 @@ public void OnViewHide()

public void OnProfileLoaded()
{
if (SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
SyncAllInstanceIDsFromAWS();
SyncAllInstanceIDsFromAWS();
}

public void RefreshProfiles()
Expand Down Expand Up @@ -915,19 +931,16 @@ private void Current_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SettingsInfo.AWSSessionManager_EnableSyncInstanceIDsFromAWS))
{
if (SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
IsSyncEnabled = SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS;

if (IsSyncEnabled)
SyncAllInstanceIDsFromAWS();
else
RemoveDynamicGroups();

IsSyncEnabled = SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS;
}

if (e.PropertyName == nameof(SettingsInfo.AWSSessionManager_SyncOnlyRunningInstancesFromAWS))
{
if (SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
SyncAllInstanceIDsFromAWS();
}
SyncAllInstanceIDsFromAWS();

if (e.PropertyName == nameof(SettingsInfo.AWSSessionManager_ApplicationFilePath))
CheckSettings();
Expand Down

0 comments on commit 4d24159

Please sign in to comment.