Skip to content

Commit 4f163c1

Browse files
authored
Merge pull request #44 from aws-solutions/feature/v2.6.1
updated to version v2.6.1
2 parents 943cec4 + d7076c3 commit 4f163c1

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6-
7-
6+
7+
## [2.6.1] - 2023-04
8+
### Added
9+
- Added support to block customer misconfiguration for 'Terminate Unused Workspaces' feature. The feature will terminate workspaces only on the last day of the month to avoid accidental termination due to misconfiguration.
10+
811
## [2.6.0] - 2023-03
912
### Added
1013
- Updated the solution to use CDK V2 to generate CloudFormation templates and support CDK deployments.

source/workspaces_app/workspaces_app/__tests__/test_workspaces_helper.py

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,27 @@ def test_check_if_workspace_needs_to_be_terminated_returns_yes_is_dry_run_false(
566566
# 'terminateUnusedWorkspaces': 'Yes'
567567
# 'isDryRun': False
568568

569+
start_time = time.strftime("%Y-%m") + '-01T00:00:00Z'
570+
end_time = time.strftime("%Y-%m") + '-02T00:00:00Z'
569571
settings = {
570572
'region': 'us-east-1',
571573
'hourlyLimits': 10,
572574
'testEndOfMonth': 'yes',
573575
'isDryRun': False,
574576
'startTime': 1,
575577
'endTime': 2,
576-
'terminateUnusedWorkspaces': 'Yes'
578+
'terminateUnusedWorkspaces': 'Yes',
579+
'dateTimeValues': {
580+
"start_time_for_current_month": '',
581+
"end_time_for_current_month": '',
582+
"last_day_current_month": '',
583+
"first_day_selected_month": '',
584+
"start_time_selected_date": start_time,
585+
"end_time_selected_date": end_time,
586+
"current_month_last_day": True,
587+
"date_today": '',
588+
"date_for_s3_key": ''
589+
}
577590
}
578591
workspace_id = "123qwe123qwe"
579592
workspace_helper = workspaces_helper.WorkspacesHelper(session, settings)
@@ -652,6 +665,9 @@ def test_get_last_known_user_connection_timestamp_returns_resource_unavailable_f
652665

653666

654667
def test_get_termination_status_returns_empty_string_for_terminate_workspaces_no(session):
668+
start_time = time.strftime("%Y-%m") + '-01T00:00:00Z'
669+
end_time = time.strftime("%Y-%m") + '-02T00:00:00Z'
670+
655671
workspace_utils.TERMINATE_UNUSED_WORKSPACES = 'No'
656672
settings = {
657673
'region': 'us-east-1',
@@ -660,7 +676,18 @@ def test_get_termination_status_returns_empty_string_for_terminate_workspaces_no
660676
'isDryRun': True,
661677
'startTime': 1,
662678
'endTime': 2,
663-
'TerminateUnusedWorkspaces': 'No'
679+
'TerminateUnusedWorkspaces': 'No',
680+
'dateTimeValues': {
681+
"start_time_for_current_month": '',
682+
"end_time_for_current_month": '',
683+
"last_day_current_month": '',
684+
"first_day_selected_month": '',
685+
"start_time_selected_date": start_time,
686+
"end_time_selected_date": end_time,
687+
"current_month_last_day": True,
688+
"date_today": '',
689+
"date_for_s3_key": ''
690+
}
664691
}
665692
workspace_id = "123qwe123qwe"
666693
billable_time = 0
@@ -755,14 +782,27 @@ def test_get_termination_status_returns_dry_run_for_terminate_workspaces_dry_run
755782

756783

757784
def test_get_termination_status_returns_empty_string_when_workspace_used(mocker, session):
785+
start_time = time.strftime("%Y-%m") + '-01T00:00:00Z'
786+
end_time = time.strftime("%Y-%m") + '-02T00:00:00Z'
758787
settings = {
759788
'region': 'us-east-1',
760789
'hourlyLimits': 10,
761790
'testEndOfMonth': 'yes',
762791
'isDryRun': True,
763792
'startTime': 1,
764793
'endTime': 2,
765-
'TerminateUnusedWorkspaces': 'Dry Run'
794+
'TerminateUnusedWorkspaces': 'Dry Run',
795+
'dateTimeValues': {
796+
"start_time_for_current_month": '',
797+
"end_time_for_current_month": '',
798+
"last_day_current_month": '',
799+
"first_day_selected_month": '',
800+
"start_time_selected_date": start_time,
801+
"end_time_selected_date": end_time,
802+
"current_month_last_day": True,
803+
"date_today": '',
804+
"date_for_s3_key": ''
805+
}
766806
}
767807
workspace_id = "123qwe123qwe"
768808
billable_time = 0
@@ -784,14 +824,27 @@ def test_get_termination_status_returns_empty_string_when_workspace_used(mocker,
784824

785825

786826
def test_get_termination_status_returns_empty_string_when_workspace_not_available_first_day(mocker, session):
827+
start_time = time.strftime("%Y-%m") + '-01T00:00:00Z'
828+
end_time = time.strftime("%Y-%m") + '-02T00:00:00Z'
787829
settings = {
788830
'region': 'us-east-1',
789831
'hourlyLimits': 10,
790832
'testEndOfMonth': True,
791833
'isDryRun': True,
792834
'startTime': 1,
793835
'endTime': 2,
794-
'terminateUnusedWorkspaces': 'Dry Run'
836+
'terminateUnusedWorkspaces': 'Dry Run',
837+
'dateTimeValues': {
838+
"start_time_for_current_month": '',
839+
"end_time_for_current_month": '',
840+
"last_day_current_month": '',
841+
"first_day_selected_month": '',
842+
"start_time_selected_date": start_time,
843+
"end_time_selected_date": end_time,
844+
"current_month_last_day": True,
845+
"date_today": '',
846+
"date_for_s3_key": ''
847+
}
795848
}
796849
workspace_id = "123qwe123qwe"
797850
billable_time = 0
@@ -1203,3 +1256,36 @@ def test_compare_usage_metrics_for_always_on_returns_no_change_for_end_of_month_
12031256
workspace_helper.modify_workspace_properties.return_value = '-N-'
12041257

12051258
assert workspace_helper.compare_usage_metrics_for_always_on('ws-112d', 10, 85, "ALWAYS_ON") == expected
1259+
1260+
1261+
def test_check_if_workspace_needs_to_be_terminated_returns_empty_string_for_last_day_month_false(session):
1262+
# 'terminateUnusedWorkspaces': 'Dry Run'
1263+
# 'isDryRun': True
1264+
1265+
start_time = time.strftime("%Y-%m") + '-01T00:00:00Z'
1266+
end_time = time.strftime("%Y-%m") + '-02T00:00:00Z'
1267+
settings = {
1268+
'region': 'us-east-1',
1269+
'hourlyLimits': 10,
1270+
'testEndOfMonth': 'yes',
1271+
'isDryRun': False,
1272+
'startTime': 1,
1273+
'endTime': 2,
1274+
'terminateUnusedWorkspaces': 'Yes',
1275+
'dateTimeValues': {
1276+
"start_time_for_current_month": '',
1277+
"end_time_for_current_month": '',
1278+
"last_day_current_month": '',
1279+
"first_day_selected_month": '',
1280+
"start_time_selected_date": start_time,
1281+
"end_time_selected_date": end_time,
1282+
"current_month_last_day": False,
1283+
"date_today": '',
1284+
"date_for_s3_key": ''
1285+
}
1286+
}
1287+
workspace_id = "123qwe123qwe"
1288+
workspace_helper = workspaces_helper.WorkspacesHelper(session, settings)
1289+
result = workspace_helper.check_if_workspace_needs_to_be_terminated(workspace_id)
1290+
assert result == ''
1291+

source/workspaces_app/workspaces_app/workspaces_helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def get_termination_status(self, workspace_id, billable_time, tags):
175175
workspace_terminated = ''
176176
log.debug(f'Getting the termination status for workspace: '
177177
f'{workspace_id}, billable time: {billable_time} and tags {tags}')
178+
log.debug(f"The value for last month check is {self.settings.get('dateTimeValues').get('current_month_last_day')}")
178179
try:
179180
if workspace_utils.is_terminate_workspace_enabled() and (
180181
self.settings.get('dateTimeValues').get('current_month_last_day') or
@@ -255,7 +256,7 @@ def check_if_workspace_needs_to_be_terminated(self, workspace_id):
255256
log.debug(f'Termination option for workspace {workspace_id} is set to DryRun. The report was updated but the'
256257
' terminate action was not called')
257258
workspace_terminated = 'Yes - Dry Run'
258-
elif self.settings.get('terminateUnusedWorkspaces') == 'Yes' and not self.settings.get('isDryRun'):
259+
elif self.settings.get('terminateUnusedWorkspaces') == 'Yes' and not self.settings.get('isDryRun') and self.settings.get('dateTimeValues').get('current_month_last_day'):
259260
log.debug(f'All the criteria for termination of workspace {workspace_id} are met. '
260261
f'Calling the terminate action.')
261262
workspace_terminated = self.terminate_unused_workspace(workspace_id)

0 commit comments

Comments
 (0)