Skip to content

Commit d95b9b5

Browse files
committed
Update to version 2.4.1
1 parent 7b7d155 commit d95b9b5

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

.DS_Store

-6 KB
Binary file not shown.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
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+
## [2.4.1] - 2021-10
7+
### Fixed
8+
- Fixed the bug to get all the workspaces in a directory
9+
610
## [2.4] - 2021-09
711
### Fixed
812
- Fixed the bug to correctly calculate billable hours if user disconnects workspace within autostop timeout

source/.coverage

-52 KB
Binary file not shown.

source/ecs/workspaces_helper.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def process_workspace(self, workspace):
9696
workspace_terminated = self.get_termination_status(workspace_id, billable_time, tags)
9797
optimization_result = self.compare_usage_metrics(workspace_id, billable_time, hourly_threshold,
9898
workspace_running_mode)
99+
99100
return {
100101
'workspaceID': workspace_id,
101102
'billableTime': billable_time,
@@ -104,9 +105,9 @@ def process_workspace(self, workspace):
104105
'newMode': optimization_result['newMode'],
105106
'bundleType': workspace_bundle_type,
106107
'initialMode': workspace_running_mode,
107-
'userName': workspace['UserName'],
108-
'computerName': workspace['ComputerName'],
109-
'directoryId': workspace['DirectoryId'],
108+
'userName': workspace.get('UserName', ''),
109+
'computerName': workspace.get('ComputerName', ''),
110+
'directoryId': workspace.get('DirectoryId', ''),
110111
'tags': tags,
111112
'workspaceTerminated': workspace_terminated
112113
}
@@ -200,7 +201,7 @@ def get_workspaces_for_directory(self, directory_id):
200201
DirectoryId=directory_id,
201202
NextToken=next_token
202203
)
203-
list_workspaces.extend(response.get('Directories', []))
204+
list_workspaces.extend(response.get('Workspaces', []))
204205
next_token = response.get('NextToken', None)
205206
except botocore.exceptions.ClientError as e:
206207
log.error(

source/tests/test_workspaces_helper.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,102 @@ def test_get_termination_status_5(mocker):
878878
workspace_helper.check_if_workspace_needs_to_be_terminated.return_value = 'Yes - Dry Run'
879879
result = workspace_helper.get_termination_status(workspace_id, billable_time, tags)
880880
assert result == ''
881+
882+
883+
def test_get_workspaces_for_directory_use_next_token():
884+
settings = {
885+
'region': 'us-east-1',
886+
'hourlyLimits': 10,
887+
'testEndOfMonth': 'yes',
888+
'isDryRun': True,
889+
'startTime': 1,
890+
'endTime': 2,
891+
'TerminateUnusedWorkspaces': 'Dry Run'
892+
}
893+
directory_id = "123qwe123qwe"
894+
workspace_helper = WorkspacesHelper(settings)
895+
client_stubber = Stubber(workspace_helper.workspaces_client)
896+
897+
expected_params_1 = {
898+
'DirectoryId': directory_id
899+
}
900+
901+
response_1 = {
902+
'Workspaces': [{'WorkspaceId':'id_1'}],
903+
'NextToken': 's223123jj32'
904+
}
905+
906+
expected_params_2 = {
907+
'DirectoryId': directory_id,
908+
'NextToken': 's223123jj32'
909+
}
910+
911+
response_2 = {
912+
'Workspaces': [{'WorkspaceId':'id_2'}]
913+
}
914+
915+
client_stubber.add_response('describe_workspaces', response_1, expected_params_1)
916+
client_stubber.add_response('describe_workspaces', response_2, expected_params_2)
917+
client_stubber.activate()
918+
response = workspace_helper.get_workspaces_for_directory(directory_id)
919+
client_stubber.activate()
920+
assert response == [{'WorkspaceId': 'id_1'}, {'WorkspaceId': 'id_2'}]
921+
922+
923+
def test_get_workspaces_for_directory_no_next_token():
924+
settings = {
925+
'region': 'us-east-1',
926+
'hourlyLimits': 10,
927+
'testEndOfMonth': 'yes',
928+
'isDryRun': True,
929+
'startTime': 1,
930+
'endTime': 2,
931+
'TerminateUnusedWorkspaces': 'Dry Run'
932+
}
933+
directory_id = "123qwe123qwe"
934+
workspace_helper = WorkspacesHelper(settings)
935+
client_stubber = Stubber(workspace_helper.workspaces_client)
936+
937+
expected_params_1 = {
938+
'DirectoryId': directory_id
939+
}
940+
941+
response_1 = {
942+
'Workspaces': [{'WorkspaceId':'id_1'}]
943+
}
944+
945+
expected_params_2 = {
946+
'DirectoryId': directory_id,
947+
'NextToken': 's223123jj32'
948+
}
949+
950+
response_2 = {
951+
'Workspaces': [{'WorkspaceId':'id_2'}]
952+
}
953+
954+
client_stubber.add_response('describe_workspaces', response_1, expected_params_1)
955+
client_stubber.add_response('describe_workspaces', response_2, expected_params_2)
956+
client_stubber.activate()
957+
response = workspace_helper.get_workspaces_for_directory(directory_id)
958+
client_stubber.activate()
959+
assert response == [{'WorkspaceId': 'id_1'}]
960+
961+
962+
def test_get_workspaces_for_directory_return_exception():
963+
settings = {
964+
'region': 'us-east-1',
965+
'hourlyLimits': 10,
966+
'testEndOfMonth': 'yes',
967+
'isDryRun': True,
968+
'startTime': 1,
969+
'endTime': 2,
970+
'TerminateUnusedWorkspaces': 'Dry Run'
971+
}
972+
directory_id = "123qwe123qwe"
973+
workspace_helper = WorkspacesHelper(settings)
974+
client_stubber = Stubber(workspace_helper.workspaces_client)
975+
client_stubber.add_client_error('describe_workspaces', "Invalid_request")
976+
client_stubber.activate()
977+
response = workspace_helper.get_workspaces_for_directory(directory_id)
978+
client_stubber.activate()
979+
assert response == []

0 commit comments

Comments
 (0)