Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
refactor: always ignore lock failures on free plans
Browse files Browse the repository at this point in the history
  • Loading branch information
yt-ms authored and Midnighter committed May 3, 2021
1 parent e78ae28 commit d4ab75c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
31 changes: 14 additions & 17 deletions src/structurizr/api/structurizr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class StructurizrClient:
agent (str): A string identifying the agent (e.g. 'structurizr-java/1.2.0').
workspace_archive_location (pathlib.Path): A directory for archiving downloaded
workspaces, or None to suppress archiving.
ignore_free_plan_locking_errors (bool): When True (the default), attempts to
lock a workspace on a free plan licence will succeed even though free plans
don't allow locking.
"""

def __init__(self, *, settings: StructurizrClientSettings, **kwargs):
Expand All @@ -84,7 +81,6 @@ def __init__(self, *, settings: StructurizrClientSettings, **kwargs):
self.agent = settings.agent
self.workspace_archive_location = settings.workspace_archive_location
self.merge_from_remote = True
self.ignore_free_plan_locking_errors = True
self._workspace_url = f"/workspace/{self.workspace_id}"
self._lock_url = f"{self._workspace_url}/lock"
self._params = {
Expand Down Expand Up @@ -138,11 +134,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
def lock(self):
"""Provide a context manager for locking and unlocking a workspace."""
locked, paid_plan = self._lock_workspace()
if not locked:
if paid_plan or not self.ignore_free_plan_locking_errors:
raise StructurizrClientException(
f"Failed to lock the Structurizr workspace {self.workspace_id}."
)
if paid_plan and not locked:
raise StructurizrClientException(
f"Failed to lock the Structurizr workspace {self.workspace_id}."
)
try:
yield self
finally:
Expand Down Expand Up @@ -242,25 +237,27 @@ def _lock_workspace(self) -> Tuple[bool, bool]:
response.raise_for_status()
logger.debug("%r", response.json())
response = APIResponse.parse_raw(response.text)
paid_plan = self._paid_plan(response)
if not response.success:
logger.warning(
f"Failed to lock workspace {self.workspace_id}. {response.message}"
)
return response.success, self._paid_plan(response)
if not paid_plan:
logger.warning("Locking not supoprted on free plan. Ignoring.")
return response.success, paid_plan

def lock_workspace(self) -> bool:
"""Lock the Structurizr workspace.
Returns:
bool: `True` if the workspace could be locked, `False` otherwise.
Note that free plan Structurizr licences do not support locking. By
default this failure will be ignored, however if you do want such locks
to fail then set ignore_free_plan_locking_errors to False.
Note that free plan Structurizr licences do not support locking, so this
will fail but continue anyway.
"""
success, paid_plan = self._lock_workspace()
if not success and not paid_plan and self.ignore_free_plan_locking_errors:
logger.debug("Ignoring lock failure on free plan")
if not success and not paid_plan:
# Free plans can't lock so ignore.
success = True
return success

Expand All @@ -284,8 +281,8 @@ def unlock_workspace(self) -> bool:
logger.warning(
f"Failed to unlock workspace {self.workspace_id}. {response.message}"
)
if self.ignore_free_plan_locking_errors and not self._paid_plan(response):
logger.debug("Ignoring unlock failure on free plan")
if not self._paid_plan(response):
logger.warning("Unlocking not supported on free plan. Ignoring.")
success = True
return success

Expand Down
22 changes: 0 additions & 22 deletions tests/unit/api/test_structurizr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,25 +359,3 @@ def fake_send(request: Request):
pass

assert len(requests) == 1


def test_failed_lock_on_free_plan_with_ignore_off(
client: StructurizrClient, mocker: MockerFixture
):
"""Check that if ignoring free plan lock failures is disabled then it does fail."""

def fake_send(request: Request):
return Response(
200,
content='{"success": false, "message": "Cannot lock on free plan"}'.encode(
"ascii"
),
request=request,
)

mocker.patch.object(client._client, "send", new=fake_send)

client.ignore_free_plan_locking_errors = False
with pytest.raises(StructurizrClientException, match="Failed to lock"):
with client.lock():
pass

0 comments on commit d4ab75c

Please sign in to comment.