Skip to content

Commit d4ab75c

Browse files
yt-msMidnighter
authored andcommitted
refactor: always ignore lock failures on free plans
1 parent e78ae28 commit d4ab75c

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

src/structurizr/api/structurizr_client.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ class StructurizrClient:
5858
agent (str): A string identifying the agent (e.g. 'structurizr-java/1.2.0').
5959
workspace_archive_location (pathlib.Path): A directory for archiving downloaded
6060
workspaces, or None to suppress archiving.
61-
ignore_free_plan_locking_errors (bool): When True (the default), attempts to
62-
lock a workspace on a free plan licence will succeed even though free plans
63-
don't allow locking.
6461
"""
6562

6663
def __init__(self, *, settings: StructurizrClientSettings, **kwargs):
@@ -84,7 +81,6 @@ def __init__(self, *, settings: StructurizrClientSettings, **kwargs):
8481
self.agent = settings.agent
8582
self.workspace_archive_location = settings.workspace_archive_location
8683
self.merge_from_remote = True
87-
self.ignore_free_plan_locking_errors = True
8884
self._workspace_url = f"/workspace/{self.workspace_id}"
8985
self._lock_url = f"{self._workspace_url}/lock"
9086
self._params = {
@@ -138,11 +134,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
138134
def lock(self):
139135
"""Provide a context manager for locking and unlocking a workspace."""
140136
locked, paid_plan = self._lock_workspace()
141-
if not locked:
142-
if paid_plan or not self.ignore_free_plan_locking_errors:
143-
raise StructurizrClientException(
144-
f"Failed to lock the Structurizr workspace {self.workspace_id}."
145-
)
137+
if paid_plan and not locked:
138+
raise StructurizrClientException(
139+
f"Failed to lock the Structurizr workspace {self.workspace_id}."
140+
)
146141
try:
147142
yield self
148143
finally:
@@ -242,25 +237,27 @@ def _lock_workspace(self) -> Tuple[bool, bool]:
242237
response.raise_for_status()
243238
logger.debug("%r", response.json())
244239
response = APIResponse.parse_raw(response.text)
240+
paid_plan = self._paid_plan(response)
245241
if not response.success:
246242
logger.warning(
247243
f"Failed to lock workspace {self.workspace_id}. {response.message}"
248244
)
249-
return response.success, self._paid_plan(response)
245+
if not paid_plan:
246+
logger.warning("Locking not supoprted on free plan. Ignoring.")
247+
return response.success, paid_plan
250248

251249
def lock_workspace(self) -> bool:
252250
"""Lock the Structurizr workspace.
253251
254252
Returns:
255253
bool: `True` if the workspace could be locked, `False` otherwise.
256254
257-
Note that free plan Structurizr licences do not support locking. By
258-
default this failure will be ignored, however if you do want such locks
259-
to fail then set ignore_free_plan_locking_errors to False.
255+
Note that free plan Structurizr licences do not support locking, so this
256+
will fail but continue anyway.
260257
"""
261258
success, paid_plan = self._lock_workspace()
262-
if not success and not paid_plan and self.ignore_free_plan_locking_errors:
263-
logger.debug("Ignoring lock failure on free plan")
259+
if not success and not paid_plan:
260+
# Free plans can't lock so ignore.
264261
success = True
265262
return success
266263

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

tests/unit/api/test_structurizr_client.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -359,25 +359,3 @@ def fake_send(request: Request):
359359
pass
360360

361361
assert len(requests) == 1
362-
363-
364-
def test_failed_lock_on_free_plan_with_ignore_off(
365-
client: StructurizrClient, mocker: MockerFixture
366-
):
367-
"""Check that if ignoring free plan lock failures is disabled then it does fail."""
368-
369-
def fake_send(request: Request):
370-
return Response(
371-
200,
372-
content='{"success": false, "message": "Cannot lock on free plan"}'.encode(
373-
"ascii"
374-
),
375-
request=request,
376-
)
377-
378-
mocker.patch.object(client._client, "send", new=fake_send)
379-
380-
client.ignore_free_plan_locking_errors = False
381-
with pytest.raises(StructurizrClientException, match="Failed to lock"):
382-
with client.lock():
383-
pass

0 commit comments

Comments
 (0)