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

[containerapp] Add new commands for httprouteconfig #8302

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/containerapp/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Release History
===============
upcoming
++++++
* 'az containerapp env httprouteconfig' Add comands for the httprouteconfig feature area.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 'az containerapp env httprouteconfig' Add comands for the httprouteconfig feature area.
* 'az containerapp env httprouteconfig': Add comands for the httprouteconfig feature area.


1.1.0b1
++++++
Expand All @@ -12,7 +13,7 @@ upcoming
* 'az containerapp create/update': `--yaml` support property pollingInterval and cooldownPeriod
* 'az containerapp session code-interpreter upload-file/list-files/show-file-content/show-file-metadata/delete-file': Support `--path` to specify the path of code interpreter session file resource
* 'az containerapp session code-interpreter': Update response payload format for api-version 2024-10-02-preview
* 'az containerapp env maintenance-config add/update/list/remove': Support environment maintenance config management
* 'az containerapp env maintenance-config add/update/list/remove': Support environment maintenance config management
* 'az containerapp sessionpool create': Support managed identity when create session pool with --mi-system-assigned --mi-user-assigned

1.0.0b4
Expand Down
69 changes: 69 additions & 0 deletions src/containerapp/azext_containerapp/_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,75 @@ def list_usages(cls, cmd, resource_group_name, name):
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
return r.json()

@classmethod
def update_httprouteconfig(cls, cmd, resource_group_name, name, httprouteconfig_name, httprouteconfig_envelope):
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
sub_id = get_subscription_id(cmd.cli_ctx)
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/managedEnvironments/{}/httpRouteConfigs/{}?api-version={}"
request_url = url_fmt.format(
management_hostname.strip('/'),
sub_id,
resource_group_name,
name,
httprouteconfig_name,
cls.api_version)

r = send_raw_request(cmd.cli_ctx, "PUT", request_url, body=json.dumps(httprouteconfig_envelope))
return r.json()

@classmethod
def list_httprouteconfigs(cls, cmd, resource_group_name, name, formatter=lambda x: x):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def list_httprouteconfigs(cls, cmd, resource_group_name, name, formatter=lambda x: x):
def list(cls, cmd, resource_group_name, name, formatter=lambda x: x):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you need the formatter here? Seems lambda x: x do nothing.

route_list = []
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
sub_id = get_subscription_id(cmd.cli_ctx)
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/managedEnvironments/{}/httpRouteConfigs?api-version={}"
request_url = url_fmt.format(
management_hostname.strip('/'),
sub_id,
resource_group_name,
name,
cls.api_version)

r = send_raw_request(cmd.cli_ctx, "GET", request_url, body=None)
j = r.json()
for route in j["value"]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may throw None Error if j["value"] is None.

formatted = formatter(route)
route_list.append(formatted)
return route_list

@classmethod
def show_httprouteconfig(cls, cmd, resource_group_name, name, httprouteconfig_name):
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
sub_id = get_subscription_id(cmd.cli_ctx)
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/managedEnvironments/{}/httpRouteConfigs/{}?api-version={}"
request_url = url_fmt.format(
management_hostname.strip('/'),
sub_id,
resource_group_name,
name,
httprouteconfig_name,
cls.api_version)

r = send_raw_request(cmd.cli_ctx, "GET", request_url, body=None)
return r.json()

@classmethod
def delete_httprouteconfig(cls, cmd, resource_group_name, name, httprouteconfig_name):
Copy link
Contributor

@Greedygre Greedygre Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def delete_httprouteconfig(cls, cmd, resource_group_name, name, httprouteconfig_name):
def delete(cls, cmd, resource_group_name, name, httprouteconfig_name):

management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
sub_id = get_subscription_id(cmd.cli_ctx)
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/managedEnvironments/{}/httpRouteConfigs/{}?api-version={}"
request_url = url_fmt.format(
management_hostname.strip('/'),
sub_id,
resource_group_name,
name,
httprouteconfig_name,
cls.api_version)

send_raw_request(cmd.cli_ctx, "DELETE", request_url, body=None)
# API doesn't return JSON (it returns no content)
return

Comment on lines +362 to +430
Copy link
Contributor

@Greedygre Greedygre Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend using a separate Client for /httpRouteConfigs, you can refer class StorageClient()


class AuthPreviewClient(AuthClient):
api_version = PREVIEW_API_VERSION
Expand Down
50 changes: 50 additions & 0 deletions src/containerapp/azext_containerapp/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,3 +2189,53 @@
text: |
az containerapp env maintenance-config remove --environment myEnv -g MyResourceGroup
"""

helps['containerapp env httprouteconfig'] = """
type: group
short-summary: Commands to manage environment level http routing.
"""

helps['containerapp env httprouteconfig list'] = """
type: command
short-summary: List the http route configs in the environment.
examples:
- name: List the http route configs in the environment.
text: |
az containerapp env httprouteconfig list -g MyResourceGroup -n MyEnvironment
"""

helps['containerapp env httprouteconfig create'] = """
type: command
short-summary: Create a new http route config.
examples:
- name: Create a new route from a yaml file.
text: |
az containerapp env httprouteconfig create -g MyResourceGroup -n MyEnvironment --httprouteconfig-name configname --yaml config.yaml
"""

helps['containerapp env httprouteconfig update'] = """
type: command
short-summary: Update a http route config.
examples:
- name: Updates a route in the environment from a yaml file.
text: |
az containerapp env httprouteconfig update -g MyResourceGroup -n MyEnvironment --httprouteconfig-name configname --yaml config.yaml
"""

helps['containerapp env httprouteconfig show'] = """
type: command
short-summary: Show a http route config.
examples:
- name: Shows a route from the environment.
text: |
az containerapp env httprouteconfig show -g MyResourceGroup -n MyEnvironment --httprouteconfig-name configname
"""

helps['containerapp env httprouteconfig delete'] = """
type: command
short-summary: Delete a http route config.
examples:
- name: Deletes a route from the environment.
text: |
az containerapp env httprouteconfig delete -g MyResourceGroup -n MyEnvironment --httprouteconfig-name configname
"""
7 changes: 7 additions & 0 deletions src/containerapp/azext_containerapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def load_command_table(self, args):
g.custom_command('delete', 'delete_managed_environment', supports_no_wait=True, confirmation=True, exception_handler=ex_handler_factory())
g.custom_command('update', 'update_managed_environment', supports_no_wait=True, exception_handler=ex_handler_factory())

with self.command_group('containerapp env httprouteconfig') as g:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.command_group('containerapp env httprouteconfig') as g:
with self.command_group('containerapp env http-route-config') as g:
Suggested change
with self.command_group('containerapp env httprouteconfig') as g:
with self.command_group('containerapp env httprouteconfig') as g:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.command_group('containerapp env httprouteconfig') as g:
with self.command_group('containerapp env httprouteconfig', is_preview=True) as g:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For new commands, we always recommend to support in preview mode first

g.custom_show_command('show', 'show_httprouteconfig')
g.custom_command('list', 'list_httprouteconfigs')
g.custom_command('create', 'update_httprouteconfig', exception_handler=ex_handler_factory())
g.custom_command('update', 'update_httprouteconfig', exception_handler=ex_handler_factory())
g.custom_command('delete', 'delete_httprouteconfig', confirmation=True, exception_handler=ex_handler_factory())

with self.command_group('containerapp job') as g:
g.custom_show_command('show', 'show_containerappsjob')
g.custom_command('list', 'list_containerappsjob')
Expand Down
40 changes: 40 additions & 0 deletions src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3322,3 +3322,43 @@ def list_maintenance_config(cmd, resource_group_name, env_name):
)
r = maintenance_config_decorator.list()
return r


def update_httprouteconfig(cmd, resource_group_name, name, httprouteconfig_name, yaml):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaml_httprouteconfig = load_yaml_file(yaml)
# check if the type is dict
if not isinstance(yaml_httprouteconfig, dict):
raise ValidationError('Invalid YAML provided. Please see https://aka.ms/azure-container-apps-yaml for a valid YAML spec.')

httprouteconfig_envelope = {}

httprouteconfig_envelope["properties"] = yaml_httprouteconfig

try:
return ManagedEnvironmentPreviewClient.update_httprouteconfig(cmd, resource_group_name, name, httprouteconfig_name, httprouteconfig_envelope)
except Exception as e:
handle_raw_exception(e)


def list_httprouteconfigs(cmd, resource_group_name, name):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)
try:
return ManagedEnvironmentPreviewClient.list_httprouteconfigs(cmd, resource_group_name, name)
except Exception as e:
handle_raw_exception(e)


def show_httprouteconfig(cmd, resource_group_name, name, httprouteconfig_name):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)
try:
return ManagedEnvironmentPreviewClient.show_httprouteconfig(cmd, resource_group_name, name, httprouteconfig_name)
except Exception as e:
handle_raw_exception(e)


def delete_httprouteconfig(cmd, resource_group_name, name, httprouteconfig_name):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)
try:
return ManagedEnvironmentPreviewClient.delete_httprouteconfig(cmd, resource_group_name, name, httprouteconfig_name)
except Exception as e:
handle_raw_exception(e)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rules:
- description: "rule 1"
routes:
- match:
prefix: "/1"
action:
PrefixRewrite: "/"
targets:
- ContainerApp: "app1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rules:
- description: "rule 2"
routes:
- match:
prefix: "/2"
action:
PrefixRewrite: "/"
targets:
- ContainerApp: "app2"
Loading
Loading