Skip to content

Commit b5363d8

Browse files
committed
refactor: read x-cli-entity-* extensions from OpenApi spec
Replace hardcoded lookup_map/entity_map in templates/lookups.jinja with spec-driven logic that reads x-cli-entity-lookup (on parameters), x-cli-entity-list (on response schemas), and x-cli-entity-ref (on entity schemas) directly from the OpenAPI specification.
1 parent 7d8e433 commit b5363d8

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-34
lines changed

lib/binarylane/api/server_actions/post_v_2_servers_server_id_actions_add_disk.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def sync_detailed(
8686
This is used to add an additional disk in unallocated storage space.
8787
This does not alter the total disk space available to your server: to add additional disk space for
8888
your server use the 'Resize' action.
89+
**This action may require the server to be rebooted.**
8990
9091
9192
Args:
@@ -125,6 +126,7 @@ def sync(
125126
This is used to add an additional disk in unallocated storage space.
126127
This does not alter the total disk space available to your server: to add additional disk space for
127128
your server use the 'Resize' action.
129+
**This action may require the server to be rebooted.**
128130
129131
130132
Args:
@@ -157,6 +159,7 @@ async def asyncio_detailed(
157159
This is used to add an additional disk in unallocated storage space.
158160
This does not alter the total disk space available to your server: to add additional disk space for
159161
your server use the 'Resize' action.
162+
**This action may require the server to be rebooted.**
160163
161164
162165
Args:
@@ -194,6 +197,7 @@ async def asyncio(
194197
This is used to add an additional disk in unallocated storage space.
195198
This does not alter the total disk space available to your server: to add additional disk space for
196199
your server use the 'Resize' action.
200+
**This action may require the server to be rebooted.**
197201
198202
199203
Args:

lib/binarylane/api/server_actions/post_v_2_servers_server_id_actions_delete_disk.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def sync_detailed(
8585
8686
This is used to delete a disk added using the 'AddDisk' action.
8787
**NB: This is a destructive operation and no further confirmation will be requested.**
88+
**This action may require the server to be rebooted.**
8889
8990
9091
Args:
@@ -123,6 +124,7 @@ def sync(
123124
124125
This is used to delete a disk added using the 'AddDisk' action.
125126
**NB: This is a destructive operation and no further confirmation will be requested.**
127+
**This action may require the server to be rebooted.**
126128
127129
128130
Args:
@@ -154,6 +156,7 @@ async def asyncio_detailed(
154156
155157
This is used to delete a disk added using the 'AddDisk' action.
156158
**NB: This is a destructive operation and no further confirmation will be requested.**
159+
**This action may require the server to be rebooted.**
157160
158161
159162
Args:
@@ -190,6 +193,7 @@ async def asyncio(
190193
191194
This is used to delete a disk added using the 'AddDisk' action.
192195
**NB: This is a destructive operation and no further confirmation will be requested.**
196+
**This action may require the server to be rebooted.**
193197
194198
195199
Args:

lib/binarylane/models/size_options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class SizeOptions:
2121
disk_cost_per_additional_gigabyte (float): The additional cost per GB per month for additional storage space.
2222
memory_max (int): The maximum memory in MB permitted on this size.
2323
memory_cost_per_additional_megabyte (float): The additional cost per MB per month for additional memory.
24-
transfer_max (float): The maximum transfer in TB permitted for this size.
24+
transfer_max (float): The maximum transfer in TB permitted for this size. If this is the same as Size.Transfer
25+
no additional transfer is supported.
2526
transfer_cost_per_additional_gigabyte (float): The additional cost per GB per month for additional included
2627
transfer.
2728
ipv4_addresses_max (int): The maximum number of IPv4 addresses permitted on this size.

templates/lookups.jinja

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
{% filter trim %}{# Discard whitespace #}
22

33
{# ---
4-
This template synthesises "x-cli-lookup" and "x-cli-entity" extension properties
5-
on path parameter and request schema definitions as the required data is not
6-
available in OpenAPI Specification published by BinaryLane.
4+
This template reads x-cli-entity-lookup, x-cli-entity-list, and x-cli-entity-ref
5+
extension properties from the OpenAPI specification and adapts them to the
6+
"x-cli-lookup" and "x-cli-entity" format used by downstream templates.
77
--- #}
88

9-
{# Map of attribute name to command that can resolve attribute reference. #}
10-
{% set lookup_map = {
11-
"load_balancer_id": "load-balancer list",
12-
"server_id": "server list",
13-
"vpc_id": "vpc list",
14-
} %}
15-
16-
{# Map of command to {id,ref} obect specifying the attributes within
17-
x-cli-command's response that provide the reference (input) and id (output). #}
18-
{% set entity_map = {
19-
"load-balancer list": {"id": "id", "ref": "name"},
20-
"server list": {"id": "id", "ref": "name"},
21-
"vpc list": {"id": "id", "ref": "name"},
22-
} %}
9+
{# Build global lookup map on first invocation by scanning all endpoints. #}
10+
{% if openapi.cli_entity_lookup_map is not defined %}
11+
{% set _ = openapi.__setattr__("cli_entity_lookup_map", {}) %}
12+
{% for tag_key, tag_value in endpoint_collections_by_tag | dictsort %}
13+
{% for ep in tag_value.endpoints if ep.data["x-cli-command"] %}
14+
{% for param in (ep.data.parameters or []) if param.param_in == "path" and param["x-cli-entity-lookup"] %}
15+
{% set _ = openapi.cli_entity_lookup_map.__setitem__(param.name, param["x-cli-entity-lookup"]) %}
16+
{% endfor %}
17+
{% endfor %}
18+
{% endfor %}
19+
{% endif %}
2320

24-
{# Add x-cli-lookup to attributes that support lookup. #}
25-
{% for parameter_name in lookup_map %}
21+
{# Copy x-cli-entity-lookup to x-cli-lookup on path parameters. #}
22+
{% for param in (endpoint.data.parameters or []) if param.param_in == "path" and param["x-cli-entity-lookup"] %}
23+
{% set _ = param.__setattr__("x-cli-lookup", param["x-cli-entity-lookup"]) %}
24+
{% endfor %}
2625

27-
{# Examine path parameters #}
28-
{% for param in (endpoint.data.parameters or []) if param.param_in == "path" and param.name == parameter_name %}
29-
{% set _ = param.__setattr__("x-cli-lookup", lookup_map[parameter_name]) %}
26+
{# Copy x-cli-entity-lookup to x-cli-lookup on matching request body properties. #}
27+
{% if endpoint.json_body and endpoint.json_body.get_instance_type_string() != 'list' %}
28+
{% for name, prop in openapi.models_by_class[endpoint.json_body.class_info].data.properties.items() %}
29+
{% for parameter_name, lookup_command in openapi.cli_entity_lookup_map.items() if name in ["%s","%ss"] | map("format", parameter_name) %}
30+
{% set _ = prop.__setattr__("x-cli-lookup", lookup_command) %}
31+
{% endfor %}
3032
{% endfor %}
31-
32-
{# Examine request body schema #}
33-
{% if endpoint.json_body and endpoint.json_body.get_instance_type_string() != 'list' %}
34-
{% for name, prop in openapi.models_by_class[endpoint.json_body.class_info].data.properties.items() if name in ["%s","%ss"] | map("format", parameter_name) %}
35-
{% set _ = prop.__setattr__("x-cli-lookup", lookup_map[parameter_name]) %}
33+
{% endif %}
34+
35+
{# Build x-cli-entity on list endpoints from x-cli-entity-list and x-cli-entity-ref. #}
36+
{% for response_type in endpoint.responses if response_type.status_code == 200 and response_type.prop.class_info %}
37+
{% set response_model = openapi.models_by_class[response_type.prop.class_info] %}
38+
{% if response_model.data["x-cli-entity-list"] %}
39+
{% set list_prop_name = response_model.data["x-cli-entity-list"] %}
40+
{% for prop in response_type.prop.required_properties if prop.name == list_prop_name and prop.inner_property.class_info %}
41+
{% set entity_model = openapi.models_by_class[prop.inner_property.class_info] %}
42+
{% if entity_model.data["x-cli-entity-ref"] %}
43+
{% set _ = endpoint.data.__setattr__("x-cli-entity", {"id": "id", "ref": entity_model.data["x-cli-entity-ref"]}) %}
44+
{% endif %}
3645
{% endfor %}
3746
{% endif %}
38-
39-
{% endfor %}
40-
41-
{# Add x-cli-entity if endpoint is is used to perform lookups. #}
42-
{% for command_name in entity_map if endpoint.data["x-cli-command"] == command_name %}
43-
{% set _ = endpoint.data.__setattr__("x-cli-entity", entity_map[command_name]) %}
4447
{% endfor %}
4548

4649
{% endfilter %}

0 commit comments

Comments
 (0)