|
1 | 1 | {% filter trim %}{# Discard whitespace #} |
2 | 2 |
|
3 | 3 | {# --- |
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. |
7 | 7 | --- #} |
8 | 8 |
|
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 %} |
23 | 20 |
|
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 %} |
26 | 25 |
|
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 %} |
30 | 32 | {% 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 %} |
36 | 45 | {% endfor %} |
37 | 46 | {% 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]) %} |
44 | 47 | {% endfor %} |
45 | 48 |
|
46 | 49 | {% endfilter %} |
0 commit comments