Skip to content

Commit

Permalink
Refactor validate_template_version_patch method to use parent_service…
Browse files Browse the repository at this point in the history
…_template_name instead of parent_resource_id (#3825)
  • Loading branch information
marrobi authored Jan 12, 2024
1 parent ea119df commit 00db0ee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ ENHANCEMENTS:
BUG FIXES:
* Fix issue with workspace menu not working correctly([#3819](https://github.com/microsoft/AzureTRE/issues/3819))
* Fix issue with connect button showing when no uri([#3820](https://github.com/microsoft/AzureTRE/issues/3820))
* Fix user resource upgrade validation: use the parent_service_template_name instead of the parent_resource_id. ([#3824](https://github.com/microsoft/AzureTRE/issues/3824))

COMPONENTS:


## 0.16.0 (December 1, 2023)

**BREAKING CHANGES & MIGRATIONS**:
Expand Down
2 changes: 1 addition & 1 deletion api_app/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.18.0"
__version__ = "0.18.1"
11 changes: 8 additions & 3 deletions api_app/db/repositories/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ async def get_resource_dependency_list(self, resource: Resource) -> List:
return [resource[0] for resource in sorted_list]

async def validate_template_version_patch(self, resource: Resource, resource_patch: ResourcePatch, resource_template_repo: ResourceTemplateRepository, resource_template: ResourceTemplate, force_version_update: bool = False):
parent_resource_id = None
parent_service_template_name = None
if resource.resourceType == ResourceType.UserResource:
parent_resource_id = resource.parentWorkspaceServiceId
try:
resource_repo = await ResourceRepository.create()
parent_service = await resource_repo.get_resource_by_id(resource.parentWorkspaceServiceId)
parent_service_template_name = parent_service.templateName
except EntityDoesNotExist:
raise ValueError(f'Parent workspace service {resource.parentWorkspaceServiceId} not found')

# validate Major upgrade
try:
Expand All @@ -159,7 +164,7 @@ async def validate_template_version_patch(self, resource: Resource, resource_pat

# validate if target template with desired version is registered
try:
await resource_template_repo.get_template_by_name_and_version(resource.templateName, resource_patch.templateVersion, resource_template.resourceType, parent_resource_id)
await resource_template_repo.get_template_by_name_and_version(resource.templateName, resource_patch.templateVersion, resource_template.resourceType, parent_service_template_name)
except EntityDoesNotExist:
raise TargetTemplateVersionDoesNotExist(f"Template '{resource_template.name}' not found for resource type '{resource_template.resourceType}' with target template version '{resource_patch.templateVersion}'")

Expand Down
17 changes: 11 additions & 6 deletions api_app/tests_ma/test_api/test_routes/test_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,8 @@ async def test_patch_user_resource_patches_user_resource(self, _, update_item_mo
@ patch("api.dependencies.workspaces.UserResourceRepository.get_user_resource_by_id", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.update_item_with_etag", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.get_timestamp", return_value=FAKE_UPDATE_TIMESTAMP)
async def test_patch_user_resource_with_upgrade_major_version_returns_bad_request(self, _, update_item_mock, __, ___, ____, _____, ______, _______, ________, app, client):
@ patch("db.repositories.resources.ResourceRepository.create", return_value=AsyncMock())
async def test_patch_user_resource_with_upgrade_major_version_returns_bad_request(self, _, __, ___, ____, _____, ______, _______, ________, _________, __________, app, client):
user_resource_service_patch = {"templateVersion": "2.0.0"}
etag = "some-etag-value"

Expand All @@ -1060,7 +1061,9 @@ async def test_patch_user_resource_with_upgrade_major_version_returns_bad_reques
@ patch("api.dependencies.workspaces.UserResourceRepository.get_user_resource_by_id", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.update_item_with_etag", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.get_timestamp", return_value=FAKE_UPDATE_TIMESTAMP)
async def test_patch_user_resource_with_upgrade_major_version_and_force_update_returns_patched_user_resource(self, _, update_item_mock, __, ___, ____, _____, ______, _______, ________, app, client):
@ patch("db.repositories.resources.ResourceRepository.create", return_value=AsyncMock())
@ patch("db.repositories.resources.ResourceRepository.get_resource_by_id", return_value=AsyncMock())
async def test_patch_user_resource_with_upgrade_major_version_and_force_update_returns_patched_user_resource(self, _, __, ___, update_item_mock, ____, _____, ______, _______, ________, _________, resource_history_repo_save_item_mock, app, client):
user_resource_service_patch = {"templateVersion": "2.0.0"}
etag = "some-etag-value"

Expand All @@ -1072,7 +1075,7 @@ async def test_patch_user_resource_with_upgrade_major_version_and_force_update_r
modified_user_resource.templateVersion = "2.0.0"

response = await client.patch(app.url_path_for(strings.API_UPDATE_USER_RESOURCE, workspace_id=WORKSPACE_ID, service_id=SERVICE_ID, resource_id=USER_RESOURCE_ID) + "?force_version_update=True", json=user_resource_service_patch, headers={"etag": etag})

resource_history_repo_save_item_mock.assert_called_once()
update_item_mock.assert_called_once_with(modified_user_resource, etag)
assert response.status_code == status.HTTP_202_ACCEPTED

Expand All @@ -1086,7 +1089,9 @@ async def test_patch_user_resource_with_upgrade_major_version_and_force_update_r
@ patch("api.dependencies.workspaces.UserResourceRepository.get_user_resource_by_id", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.update_item_with_etag", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.get_timestamp", return_value=FAKE_UPDATE_TIMESTAMP)
async def test_patch_user_resource_with_downgrade_version_returns_bad_request(self, _, update_item_mock, __, ___, ____, _____, ______, _______, ________, app, client):
@ patch("db.repositories.resources.ResourceRepository.create", return_value=AsyncMock())
@ patch("db.repositories.resources.ResourceRepository.get_resource_by_id", return_value=AsyncMock())
async def test_patch_user_resource_with_downgrade_version_returns_bad_request(self, _, __, ___, update_item_mock, ____, _____, ______, _______, ________, _________, __________, app, client):
user_resource_service_patch = {"templateVersion": "0.0.1"}
etag = "some-etag-value"

Expand All @@ -1111,7 +1116,8 @@ async def test_patch_user_resource_with_downgrade_version_returns_bad_request(se
@ patch("api.dependencies.workspaces.UserResourceRepository.get_user_resource_by_id", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.update_item_with_etag", return_value=sample_user_resource_object())
@ patch("api.routes.workspaces.UserResourceRepository.get_timestamp", return_value=FAKE_UPDATE_TIMESTAMP)
async def test_patch_user_resource_with_upgrade_minor_version_patches_user_resource(self, _, update_item_mock, __, ___, ____, _____, ______, _______, ________, app, client):
@ patch("db.repositories.resources.ResourceRepository.create", return_value=AsyncMock())
async def test_patch_user_resource_with_upgrade_minor_version_patches_user_resource(self, resource_repo_create_mock, ___, update_item_mock, ____, _____, ______, _______, ________, _________, __________, app, client):
user_resource_service_patch = {"templateVersion": "0.2.0"}
etag = "some-etag-value"

Expand All @@ -1123,7 +1129,6 @@ async def test_patch_user_resource_with_upgrade_minor_version_patches_user_resou
modified_user_resource.templateVersion = "0.2.0"

response = await client.patch(app.url_path_for(strings.API_UPDATE_USER_RESOURCE, workspace_id=WORKSPACE_ID, service_id=SERVICE_ID, resource_id=USER_RESOURCE_ID), json=user_resource_service_patch, headers={"etag": etag})

update_item_mock.assert_called_once_with(modified_user_resource, etag)
assert response.status_code == status.HTTP_202_ACCEPTED

Expand Down

0 comments on commit 00db0ee

Please sign in to comment.