Skip to content

Commit 7127bb4

Browse files
Rework validate infrastructure code
1 parent 7d00456 commit 7127bb4

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

shared/python/utils.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,6 @@ def _print_log(message: str, prefix: str = '', color: str = '', output: str = ''
230230
print_warning = lambda msg, output = '', duration = '' : _print_log(msg, '⚠️ ', BOLD_Y, output, duration, True)
231231
print_val = lambda name, value, val_below = False : _print_log(f"{name:<25}:{'\n' if val_below else ' '}{value}", '👉🏽 ', BOLD_B)
232232

233-
# Validation functions will raise ValueError if the value is not valid
234-
235-
validate_http_verb = lambda val: HTTP_VERB(val)
236-
validate_infrastructure = lambda val: INFRASTRUCTURE(val)
237-
validate_sku = lambda val: APIM_SKU(val)
238-
239233
def create_bicep_deployment_group(rg_name: str, rg_location: str, deployment: str | INFRASTRUCTURE, bicep_parameters: dict, bicep_parameters_file: str = 'params.json') -> Output:
240234
"""
241235
Create a Bicep deployment in a resource group, writing parameters to a file and running the deployment.
@@ -355,7 +349,6 @@ def cleanup_infra_deployments(deployment: INFRASTRUCTURE, indexes: int | list[in
355349
deployment (INFRASTRUCTURE): The infrastructure deployment enum value.
356350
indexes (int | list[int] | None): A single index, a list of indexes, or None for no index.
357351
"""
358-
validate_infrastructure(deployment)
359352

360353
if indexes is None:
361354
indexes_list = [None]
@@ -540,8 +533,6 @@ def get_infra_rg_name(deployment_name: INFRASTRUCTURE, index: int | None = None)
540533
str: The generated resource group name.
541534
"""
542535

543-
validate_infrastructure(deployment_name)
544-
545536
rg_name = f"apim-infra-{deployment_name.value}"
546537

547538
if index is not None:
@@ -633,3 +624,23 @@ def run(command: str, ok_message: str = '', error_message: str = '', print_outpu
633624
print_message(ok_message if success else error_message, output_text if not success or print_output else "", f"[{int(minutes)}m:{int(seconds)}s]")
634625

635626
return Output(success, output_text)
627+
628+
# Validation functions will raise ValueError if the value is not valid
629+
630+
validate_http_verb = lambda val: HTTP_VERB(val)
631+
validate_sku = lambda val: APIM_SKU(val)
632+
633+
def validate_infrastructure(infra: INFRASTRUCTURE, supported_infras: list[INFRASTRUCTURE]) -> bool:
634+
"""
635+
Validate that the provided infrastructure is a supported infrastructure.
636+
637+
Args:
638+
infra (INFRASTRUCTURE): The infrastructure deployment enum value.
639+
supported_infras (list[INFRASTRUCTURE]): List of supported infrastructures.
640+
641+
Raises:
642+
ValueError: If the infrastructure is not supported.
643+
"""
644+
645+
if infra not in supported_infras:
646+
raise ValueError(f"Unsupported infrastructure: {infra}. Supported infrastructures are: {', '.join([i.value for i in supported_infras])}")

tests/python/test_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import pytest
2+
from apimtypes import INFRASTRUCTURE
13
import os
24
import builtins
3-
import pytest
45
from io import StringIO
56
from unittest.mock import patch, MagicMock, mock_open
67
from shared.python import utils
@@ -252,3 +253,22 @@ def test_extract_json_multiple_json_types():
252253
assert utils.extract_json(s) == [1, 2, 3]
253254
s2 = '{"a": 1}[1,2,3]'
254255
assert utils.extract_json(s2) == {"a": 1}
256+
257+
# ------------------------------
258+
# validate_infrastructure
259+
# ------------------------------
260+
261+
def test_validate_infrastructure_supported():
262+
# Should return True for supported infra
263+
assert utils.validate_infrastructure(INFRASTRUCTURE.SIMPLE_APIM, [INFRASTRUCTURE.SIMPLE_APIM]) is None
264+
265+
def test_validate_infrastructure_unsupported():
266+
# Should raise ValueError for unsupported infra
267+
with pytest.raises(ValueError) as exc:
268+
utils.validate_infrastructure(INFRASTRUCTURE.SIMPLE_APIM, [INFRASTRUCTURE.APIM_ACA])
269+
assert "Unsupported infrastructure" in str(exc.value)
270+
271+
def test_validate_infrastructure_multiple_supported():
272+
# Should return True if infra is in the supported list
273+
supported = [INFRASTRUCTURE.SIMPLE_APIM, INFRASTRUCTURE.APIM_ACA]
274+
assert utils.validate_infrastructure(INFRASTRUCTURE.APIM_ACA, supported) is None

0 commit comments

Comments
 (0)