Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scg app level routing #5504

Merged
merged 8 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/spring/azext_spring/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def load_command_table(self, _):

gateway_route_config_cmd_group = CliCommandType(
operations_tmpl='azext_spring.gateway#{}',
client_factory=cf_spring_20220901preview
client_factory=cf_spring_20221101preview
)

api_portal_cmd_group = CliCommandType(
Expand Down
32 changes: 29 additions & 3 deletions src/spring/azext_spring/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# --------------------------------------------------------------------------------------------

import json
import re

from azure.cli.core.azclierror import InvalidArgumentValueError
from azure.cli.core.util import sdk_no_wait
from knack.log import get_logger

from .custom import LOG_RUNNING_PROMPT
from .vendored_sdks.appplatform.v2022_09_01_preview import models
from .vendored_sdks.appplatform.v2022_11_01_preview import models

logger = get_logger(__name__)
DEFAULT_NAME = "default"
Expand Down Expand Up @@ -139,7 +140,7 @@ def gateway_route_config_create(cmd, client, resource_group, service, name,
app_name=None,
routes_json=None,
routes_file=None):
_validate_route_config_exist(client, resource_group, service, name)
_validate_route_config_not_exist(client, resource_group, service, name)
route_properties = models.GatewayRouteConfigProperties()
return _create_or_update_gateway_route_configs(client, resource_group, service, name, route_properties,
app_name, routes_file, routes_json)
Expand All @@ -149,6 +150,7 @@ def gateway_route_config_update(cmd, client, resource_group, service, name,
app_name=None,
routes_json=None,
routes_file=None):
_validate_route_config_exist(client, resource_group, service, name)
route_properties = client.gateway_route_configs.get(
resource_group, service, DEFAULT_NAME, name).properties
return _create_or_update_gateway_route_configs(client, resource_group, service, name, route_properties,
Expand Down Expand Up @@ -195,13 +197,20 @@ def _update_cors(existing, allowed_origins, allowed_methods, allowed_headers, ma
return cors


def _validate_route_config_exist(client, resource_group, service, name):
def _validate_route_config_not_exist(client, resource_group, service, name):
route_configs = client.gateway_route_configs.list(
resource_group, service, DEFAULT_NAME)
if name in (route_config.name for route_config in list(route_configs)):
raise InvalidArgumentValueError("Route config " + name + " already exists")


def _validate_route_config_exist(client, resource_group, service, name):
route_configs = client.gateway_route_configs.list(
resource_group, service, DEFAULT_NAME)
if name not in (route_config.name for route_config in list(route_configs)):
raise InvalidArgumentValueError("Route config " + name + " doesn't exist")


def _create_or_update_gateway_route_configs(client, resource_group, service, name, route_properties,
app_name, routes_file, routes_json):
app_resource_id = _get_app_resource_id_by_name(client, resource_group, service, app_name)
Expand All @@ -225,6 +234,7 @@ def _create_or_update_routes_properties(routes_file, routes_json, route_properti
if routes_file is None and routes_json is None:
return route_properties

route_properties = models.GatewayRouteConfigProperties()
if routes_file is not None:
with open(routes_file, 'r') as json_file:
raw_json = json.load(json_file)
Expand All @@ -235,5 +245,21 @@ def _create_or_update_routes_properties(routes_file, routes_json, route_properti
if isinstance(raw_json, list):
route_properties.routes = raw_json
else:
raw_json = _route_config_property_convert(raw_json)
route_properties = models.GatewayRouteConfigProperties(**raw_json)
return route_properties


# Convert camelCase to snake_case to align with backend
def _route_config_property_convert(raw_json):
if raw_json is None:
return raw_json

convert_raw_json = {}
for key in raw_json:
if key == "routes":
convert_raw_json[key] = list(map(lambda v: _route_config_property_convert(v), raw_json[key]))
else:
replaced_key = re.sub('(?<!^)(?=[A-Z])', '_', key).lower()
convert_raw_json[replaced_key] = raw_json[key]
return convert_raw_json
29 changes: 29 additions & 0 deletions src/spring/azext_spring/tests/latest/files/gateway_routes_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"filters": [
"StripPrefix=1"
],
"predicates": [
"Path=/api/echo2"
],
"ssoEnabled": true,
"openApi": {
"uri": "https://raw.githubusercontent.com/fake.json"
},
"protocol": "HTTPS",
"routes": [
{
"title": "Customers service",
"description": "Route to customer service",
"predicates": [
"Path=/api/customers-service/owners"
],
"ssoEnabled": false,
"filters": [
"StripPrefix=2"
],
"tags": [
"pet clinic"
]
}
]
}
Loading