Skip to content

Commit 4d24159

Browse files
authored
Fix: AWS Session Manager sync on first load (#1591)
1 parent 8c8a8fb commit 4d24159

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

Source/NETworkManager/MainWindow.xaml.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,12 @@ protected override async void OnContentRendered(EventArgs e)
445445

446446
private void AfterContentRendered()
447447
{
448+
// Load the profiles before the applications are loaded so that we can use them (e.g. for synchronization)
449+
LoadProfiles();
450+
448451
// Load application list, filter, sort, etc.
449452
LoadApplicationList();
450453

451-
// Load profiles
452-
LoadProfiles();
453-
454454
// Hide to tray after the window shows up... not nice, but otherwise the hotkeys do not work
455455
if (CommandLineManager.Current.Autostart && SettingsManager.Current.Autostart_StartMinimizedInTray)
456456
HideWindowToTray();
@@ -1200,7 +1200,6 @@ protected override void OnSourceInitialized(EventArgs e)
12001200
base.OnSourceInitialized(e);
12011201

12021202
_hwndSoure = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
1203-
12041203
_hwndSoure?.AddHook(HwndHook);
12051204

12061205
RegisterHotKeys();

Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs

+32-19
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ public AWSSessionManagerHostViewModel(IDialogCoordinator instance)
324324
SettingsManager.Current.PropertyChanged += Current_PropertyChanged;
325325
SettingsManager.Current.AWSSessionManager_AWSProfiles.CollectionChanged += AWSSessionManager_AWSProfiles_CollectionChanged;
326326

327+
SyncAllInstanceIDsFromAWS();
328+
327329
_isLoading = false;
328330
}
329331

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

449-
private bool SyncInstanceIDsFromAWS_CanExecute(object obj) => !IsSyncing && SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS;
451+
private bool SyncInstanceIDsFromAWS_CanExecute(object obj) => !IsSyncing && IsSyncEnabled;
450452

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

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

528+
private bool IsConfigured => IsAWSCLIInstalled && IsAWSSessionManagerPluginInstalled && IsPowerShellConfigured;
529+
526530
private async Task SyncAllInstanceIDsFromAWS()
527-
{
528-
_log.Info("Sync all EC2 Instance(s) from AWS...");
531+
{
532+
if (!IsSyncEnabled)
533+
{
534+
_log.Info("Sync all EC2 instances from AWS is disabled in the settings.");
535+
return;
536+
}
537+
538+
_log.Info("Sync all EC2 instance(s) from AWS...");
529539

530-
// Check if prerequisites are met
531-
if (!IsAWSCLIInstalled || !IsAWSSessionManagerPluginInstalled)
540+
if (!IsConfigured)
532541
{
533-
_log.Warn($"Prerequisites not met! AWS CLI installed {IsAWSCLIInstalled}. AWS Session Manager plugin installed {IsAWSSessionManagerPluginInstalled}");
542+
_log.Warn($"Preconditions not met! AWS CLI installed {IsAWSCLIInstalled}. AWS Session Manager plugin installed {IsAWSSessionManagerPluginInstalled}. PowerShell configured {IsPowerShellConfigured}.");
543+
return;
544+
}
545+
546+
if (IsSyncing)
547+
{
548+
_log.Info("Skip... Sync is already running!");
534549
return;
535550
}
536551

@@ -545,7 +560,7 @@ private async Task SyncAllInstanceIDsFromAWS()
545560
}
546561
else
547562
{
548-
_log.Warn("MainWindow is null!");
563+
_log.Warn("Cannot find MainWindow because it is null!");
549564
return;
550565
}
551566

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

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

672-
_log.Info("No EC2 Instance(s) found!");
687+
_log.Info("No EC2 Instance(s) found!");
673688
}
674689
else
675690
{
@@ -873,7 +888,9 @@ public void OnViewVisible(bool fromSettings)
873888

874889
RefreshProfiles();
875890

876-
if (!fromSettings && SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
891+
// Do not synchronize If the view becomes visible again
892+
// after the settings have been opened
893+
if (!fromSettings)
877894
SyncAllInstanceIDsFromAWS();
878895
}
879896

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

885902
public void OnProfileLoaded()
886903
{
887-
if (SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
888-
SyncAllInstanceIDsFromAWS();
904+
SyncAllInstanceIDsFromAWS();
889905
}
890906

891907
public void RefreshProfiles()
@@ -915,19 +931,16 @@ private void Current_PropertyChanged(object sender, PropertyChangedEventArgs e)
915931
{
916932
if (e.PropertyName == nameof(SettingsInfo.AWSSessionManager_EnableSyncInstanceIDsFromAWS))
917933
{
918-
if (SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
934+
IsSyncEnabled = SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS;
935+
936+
if (IsSyncEnabled)
919937
SyncAllInstanceIDsFromAWS();
920938
else
921939
RemoveDynamicGroups();
922-
923-
IsSyncEnabled = SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS;
924940
}
925941

926942
if (e.PropertyName == nameof(SettingsInfo.AWSSessionManager_SyncOnlyRunningInstancesFromAWS))
927-
{
928-
if (SettingsManager.Current.AWSSessionManager_EnableSyncInstanceIDsFromAWS)
929-
SyncAllInstanceIDsFromAWS();
930-
}
943+
SyncAllInstanceIDsFromAWS();
931944

932945
if (e.PropertyName == nameof(SettingsInfo.AWSSessionManager_ApplicationFilePath))
933946
CheckSettings();

0 commit comments

Comments
 (0)