Skip to content

Commit f4985c9

Browse files
[Containerapp] az containerapp function : list, show, keys list/show/set, invocations summary/traces commands (#9185)
* Az function cli changes * adding all the missing files * fixing style errors * resolving some of the comments * resolving comments * resolving comments * fixing style error * tests * adding app insight extension * test extension add app-insights * adding quotes with *** * version update * updates to tests due to version update * removing mistakenly added file * addressing comment about history.rst
1 parent 1ae78b4 commit f4985c9

20 files changed

+26919
-18
lines changed

src/containerapp/HISTORY.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Release History
55
upcoming
66
++++++
77
* Upgrade api-version to 2025-10-02-preview
8+
* 'az containerapp function list': List functions in a container app
9+
* 'az containerapp function show': Show specific function in a container app
10+
* 'az containerapp function keys show': Show specific function key in a container app
11+
* 'az containerapp function keys list': List function keys in a container app
12+
* 'az containerapp function keys set': Create a new or update an existing function key in a container app
13+
* 'az containerapp function invocations summary': Get function invocation summary from Application Insights
14+
* 'az containerapp function invocations traces': Get function invocation traces from Application Insights
15+
* 'az containerapp debug': Support `--command` to run a command inside the container and exit
816

917
1.2.0b5
1018
++++++

src/containerapp/azext_containerapp/_clients.py

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import requests
1010

11-
from azure.cli.core.azclierror import ResourceNotFoundError
11+
from azure.cli.core.azclierror import ResourceNotFoundError, CLIError
1212
from azure.cli.core.util import send_raw_request
1313
from azure.cli.core.commands.client_factory import get_subscription_id
1414
from azure.cli.command_modules.containerapp._clients import (
@@ -303,6 +303,161 @@ def list(cls, cmd, resource_group_name, container_app_name):
303303
return policy_list
304304

305305

306+
class ContainerAppFunctionsPreviewClient():
307+
api_version = PREVIEW_API_VERSION
308+
309+
@classmethod
310+
def list_functions_by_revision(cls, cmd, resource_group_name, container_app_name, revision_name):
311+
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
312+
sub_id = get_subscription_id(cmd.cli_ctx)
313+
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/containerApps/{}/revisions/{}/functions?api-version={}"
314+
request_url = url_fmt.format(
315+
management_hostname.strip('/'),
316+
sub_id,
317+
resource_group_name,
318+
container_app_name,
319+
revision_name,
320+
cls.api_version)
321+
322+
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
323+
if not r:
324+
raise CLIError(f"Error retrieving functions for revision '{revision_name}' of container app '{container_app_name}'.")
325+
return r.json()
326+
327+
@classmethod
328+
def get_function_by_revision(cls, cmd, resource_group_name, container_app_name, revision_name, function_name):
329+
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
330+
sub_id = get_subscription_id(cmd.cli_ctx)
331+
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/containerApps/{}/revisions/{}/functions/{}?api-version={}"
332+
request_url = url_fmt.format(
333+
management_hostname.strip('/'),
334+
sub_id,
335+
resource_group_name,
336+
container_app_name,
337+
revision_name,
338+
function_name,
339+
cls.api_version)
340+
341+
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
342+
if not r:
343+
raise CLIError(f"Error retrieving function '{function_name}' for revision '{revision_name}' of container app '{container_app_name}'.")
344+
return r.json()
345+
346+
@classmethod
347+
def list_functions(cls, cmd, resource_group_name, container_app_name):
348+
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
349+
sub_id = get_subscription_id(cmd.cli_ctx)
350+
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/containerApps/{}/functions?api-version={}"
351+
request_url = url_fmt.format(
352+
management_hostname.strip('/'),
353+
sub_id,
354+
resource_group_name,
355+
container_app_name,
356+
cls.api_version)
357+
358+
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
359+
if not r:
360+
raise CLIError(f"Error retrieving functions for container app '{container_app_name}'.")
361+
return r.json()
362+
363+
@classmethod
364+
def get_function(cls, cmd, resource_group_name, container_app_name, function_name):
365+
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
366+
sub_id = get_subscription_id(cmd.cli_ctx)
367+
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/containerApps/{}/functions/{}?api-version={}"
368+
request_url = url_fmt.format(
369+
management_hostname.strip('/'),
370+
sub_id,
371+
resource_group_name,
372+
container_app_name,
373+
function_name,
374+
cls.api_version)
375+
376+
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
377+
if not r:
378+
raise CLIError(f"Error retrieving function '{function_name}' for container app '{container_app_name}'.")
379+
return r.json()
380+
381+
@classmethod
382+
def show_function_keys(cls, cmd, resource_group_name, name, key_type, key_name, function_name=None, revision_name=None, replica_name=None, container_name=None):
383+
from ._utils import execute_function_admin_command
384+
385+
command_fmt = ""
386+
if key_type != "functionKey":
387+
command_fmt = "/bin/azure-functions-admin keys show --key-type {} --key-name {}"
388+
command = command_fmt.format(key_type, key_name)
389+
else:
390+
command_fmt = "/bin/azure-functions-admin keys show --key-type {} --key-name {} --function-name {}"
391+
command = command_fmt.format(key_type, key_name, function_name)
392+
393+
r = execute_function_admin_command(
394+
cmd=cmd,
395+
resource_group_name=resource_group_name,
396+
name=name,
397+
command=command,
398+
revision_name=revision_name,
399+
replica_name=replica_name,
400+
container_name=container_name
401+
)
402+
if not r:
403+
raise CLIError(f"Error retrieving function key '{key_name}' of type '{key_type}'.")
404+
return r
405+
406+
@classmethod
407+
def list_function_keys(cls, cmd, resource_group_name, name, key_type, function_name=None, revision_name=None, replica_name=None, container_name=None):
408+
from ._utils import execute_function_admin_command
409+
410+
command_fmt = ""
411+
if key_type != "functionKey":
412+
command_fmt = "/bin/azure-functions-admin keys list --key-type {}"
413+
command = command_fmt.format(key_type)
414+
else:
415+
command_fmt = "/bin/azure-functions-admin keys list --key-type {} --function-name {}"
416+
command = command_fmt.format(key_type, function_name)
417+
418+
r = execute_function_admin_command(
419+
cmd=cmd,
420+
resource_group_name=resource_group_name,
421+
name=name,
422+
command=command,
423+
revision_name=revision_name,
424+
replica_name=replica_name,
425+
container_name=container_name
426+
)
427+
if not r:
428+
raise CLIError(f"Error retrieving function keys of type '{key_type}'.")
429+
return r
430+
431+
@classmethod
432+
def set_function_keys(cls, cmd, resource_group_name, name, key_type, key_name, key_value, function_name=None, revision_name=None, replica_name=None, container_name=None):
433+
"""Set/Update function keys based on key type"""
434+
from ._utils import execute_function_admin_command
435+
436+
command_fmt = ""
437+
if key_type != "functionKey":
438+
command_fmt = "/bin/azure-functions-admin keys set --key-type {} --key-name {}"
439+
command = command_fmt.format(key_type, key_name)
440+
else:
441+
command_fmt = "/bin/azure-functions-admin keys set --key-type {} --key-name {} --function-name {}"
442+
command = command_fmt.format(key_type, key_name, function_name)
443+
444+
if key_value is not None:
445+
command += " --key-value {}".format(key_value)
446+
447+
r = execute_function_admin_command(
448+
cmd=cmd,
449+
resource_group_name=resource_group_name,
450+
name=name,
451+
command=command,
452+
revision_name=revision_name,
453+
replica_name=replica_name,
454+
container_name=container_name
455+
)
456+
if not r:
457+
raise CLIError(f"Error setting function key '{key_name}' of type '{key_type}'.")
458+
return r
459+
460+
306461
class DaprComponentResiliencyPreviewClient():
307462
api_version = PREVIEW_API_VERSION
308463

src/containerapp/azext_containerapp/_help.py

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@
161161
- name: Create a container app and deploy a model from Azure AI Foundry
162162
text: |
163163
az containerapp up -n my-containerapp -l westus3 --model-registry azureml --model-name Phi-4 --model-version 7
164-
- name: Create an Azure Functions on Azure Container Apps (kind=functionapp)
164+
- name: Create an Azure Functions on Container Apps (kind=functionapp)
165165
text: |
166166
az containerapp up -n my-containerapp --image my-app:v1.0 --kind functionapp
167167
"""
168168

169-
170169
helps['containerapp replica count'] = """
171170
type: command
172171
short-summary: Count of a container app's replica(s)
@@ -179,6 +178,135 @@
179178
az containerapp replica count -n my-containerapp -g MyResourceGroup
180179
"""
181180

181+
helps['containerapp function'] = """
182+
type: group
183+
short-summary: Commands related to Azure Functions on Container Apps.
184+
"""
185+
186+
helps['containerapp function list'] = """
187+
type: command
188+
short-summary: List all functions in an Azure Functions on Container Apps.
189+
long-summary: |
190+
--revision is required only if the app is not in single revision mode.
191+
Run to check activerevisionmode: az containerapp show -n my-containerapp -g MyResourceGroup --query properties.configuration.activeRevisionsMode
192+
examples:
193+
- name: List all functions in an Azure Functions on Container Apps. (single active revision mode)
194+
text: |
195+
az containerapp function list -n my-containerapp -g MyResourceGroup
196+
- name: List all functions in an Azure Functions on Container Apps for a specific revision.
197+
text: |
198+
az containerapp function list -n my-containerapp -g MyResourceGroup --revision MyRevision
199+
"""
200+
201+
helps['containerapp function show'] = """
202+
type: command
203+
short-summary: Get details of a function in an Azure Functions on Container Apps.
204+
long-summary: |
205+
--revision is required only if the app is not in single revision mode.
206+
Run to check activerevisionmode: az containerapp show -n my-containerapp -g MyResourceGroup --query properties.configuration.activeRevisionsMode
207+
examples:
208+
- name: Show details of a function in an Azure Functions on Container Apps. (single active revision mode)
209+
text: |
210+
az containerapp function show -n my-containerapp -g MyResourceGroup --function-name MyFunction
211+
- name: Show details of a function in an Azure Functions on Container Apps for a specific revision.
212+
text: |
213+
az containerapp function show -n my-containerapp -g MyResourceGroup --function-name MyFunction --revision MyRevision
214+
"""
215+
216+
helps['containerapp function keys'] = """
217+
type: group
218+
short-summary: Commands for keys management in an Azure Functions on Container Apps.
219+
"""
220+
221+
helps['containerapp function keys show'] = """
222+
type: command
223+
short-summary: Show specific function key in an Azure Functions on Container Apps.
224+
examples:
225+
- name: Show a function key for a specific function in an Azure Functions on Container Apps.
226+
text: |
227+
az containerapp function keys show -n my-containerapp -g MyResourceGroup --key-type functionKey --key-name default --function-name MyFunctionName
228+
- name: Show a host key for an Azure Functions on Container Apps.
229+
text: |
230+
az containerapp function keys show -n my-containerapp -g MyResourceGroup --key-type hostKey --key-name default
231+
- name: Show a master key for an Azure Functions on Container Apps.
232+
text: |
233+
az containerapp function keys show -n my-containerapp -g MyResourceGroup --key-type masterKey --key-name _master
234+
- name: Show a system key for an Azure Functions on Container Apps.
235+
text: |
236+
az containerapp function keys show -n my-containerapp -g MyResourceGroup --key-type systemKey --key-name MyKeyName
237+
"""
238+
239+
helps['containerapp function keys list'] = """
240+
type: command
241+
short-summary: List function keys in an Azure Functions on Container Apps.
242+
examples:
243+
- name: List function keys for a specific function in an Azure Functions on Container Apps.
244+
text: |
245+
az containerapp function keys list -n my-containerapp -g MyResourceGroup --key-type functionKey --function-name MyFunctionName
246+
- name: List host keys for an Azure Functions on Container Apps.
247+
text: |
248+
az containerapp function keys list -n my-containerapp -g MyResourceGroup --key-type hostKey
249+
- name: List master keys for an Azure Functions on Container Apps.
250+
text: |
251+
az containerapp function keys list -n my-containerapp -g MyResourceGroup --key-type masterKey
252+
- name: List system keys for an Azure Functions on Container Apps.
253+
text: |
254+
az containerapp function keys list -n my-containerapp -g MyResourceGroup --key-type systemKey
255+
"""
256+
257+
helps['containerapp function keys set'] = """
258+
type: command
259+
short-summary: Create or update specific function key in an Azure Functions on Container Apps.
260+
examples:
261+
- name: Create or update a function key for a specific function in an Azure Functions on Container Apps.
262+
text: |
263+
az containerapp function keys set -n my-containerapp -g MyResourceGroup --key-type functionKey --key-name default --key-value MyKeyValue --function-name MyFunctionName
264+
- name: Create or update a host key for an Azure Functions on Container Apps.
265+
text: |
266+
az containerapp function keys set -n my-containerapp -g MyResourceGroup --key-type hostKey --key-name default --key-value MyKeyValue
267+
- name: Create or update the master key for an Azure Functions on Container Apps.
268+
text: |
269+
az containerapp function keys set -n my-containerapp -g MyResourceGroup --key-type masterKey --key-name _master --key-value MyKeyValue
270+
- name: Create or update a system key for an Azure Functions on Container Apps.
271+
text: |
272+
az containerapp function keys set -n my-containerapp -g MyResourceGroup --key-type systemKey --key-name MyKeyName --key-value MyKeyValue
273+
"""
274+
275+
helps['containerapp function invocations'] = """
276+
type: group
277+
short-summary: Commands to get function invocation data and traces from Application Insights.
278+
"""
279+
280+
helps['containerapp function invocations summary'] = """
281+
type: command
282+
short-summary: Get function invocation summary from Application Insights.
283+
examples:
284+
- name: Get invocation summary for a function with default timespan (30 days)
285+
text: |
286+
az containerapp function invocations summary -n my-containerapp -g MyResourceGroup --function-name MyFunction
287+
- name: Get invocation summary for a function with specific timespan
288+
text: |
289+
az containerapp function invocations summary -n my-containerapp -g MyResourceGroup --function-name MyFunction --timespan 7d
290+
- name: Get invocation summary for a function in a specific revision
291+
text: |
292+
az containerapp function invocations summary -n my-containerapp -g MyResourceGroup --function-name MyFunction --revision MyRevision
293+
"""
294+
295+
helps['containerapp function invocations traces'] = """
296+
type: command
297+
short-summary: Get function invocation traces from Application Insights.
298+
examples:
299+
- name: Get invocation traces for a function with default timespan (30 days)
300+
text: |
301+
az containerapp function invocations traces -n my-containerapp -g MyResourceGroup --function-name MyFunction
302+
- name: Get invocation traces for a function with specific timespan
303+
text: |
304+
az containerapp function invocations traces -n my-containerapp -g MyResourceGroup --function-name MyFunction --timespan 24h
305+
- name: Get invocation traces for a function in a specific revision
306+
text: |
307+
az containerapp function invocations traces -n my-containerapp -g MyResourceGroup --function-name MyFunction --revision MyRevision
308+
"""
309+
182310
# Environment Commands
183311
helps['containerapp env'] = """
184312
type: group
@@ -920,7 +1048,7 @@
9201048
az containerapp create -n my-containerapp -g MyResourceGroup \\
9211049
--image my-app:v1.0 --environment MyContainerappEnv \\
9221050
--enable-java-agent
923-
- name: Create an Azure Functions on Azure Container Apps (kind=functionapp)
1051+
- name: Create an Azure Functions on Container Apps (kind=functionapp)
9241052
text: |
9251053
az containerapp create -n my-containerapp -g MyResourceGroup \\
9261054
--image my-app:v1.0 --environment MyContainerappEnv \\
@@ -2308,11 +2436,14 @@
23082436

23092437
helps['containerapp debug'] = """
23102438
type: command
2311-
short-summary: Open an SSH-like interactive shell within a container app debug console.
2439+
short-summary: Open an SSH-like interactive shell within a container app debug console or execute a command inside the container and exit.
23122440
examples:
23132441
- name: Debug by connecting to a container app's debug console by replica, revision and container
23142442
text: |
23152443
az containerapp debug -n MyContainerapp -g MyResourceGroup --revision MyRevision --replica MyReplica --container MyContainer
2444+
- name: Debug by executing a command inside a container app and exit
2445+
text: |
2446+
az containerapp debug -n MyContainerapp -g MyResourceGroup --revision MyRevision --replica MyReplica --container MyContainer --command "echo Hello World"
23162447
"""
23172448

23182449
helps['containerapp label-history'] = """

0 commit comments

Comments
 (0)