|
8 | 8 | import os |
9 | 9 | import requests |
10 | 10 |
|
11 | | -from azure.cli.core.azclierror import ResourceNotFoundError |
| 11 | +from azure.cli.core.azclierror import ResourceNotFoundError, CLIError |
12 | 12 | from azure.cli.core.util import send_raw_request |
13 | 13 | from azure.cli.core.commands.client_factory import get_subscription_id |
14 | 14 | from azure.cli.command_modules.containerapp._clients import ( |
@@ -303,6 +303,161 @@ def list(cls, cmd, resource_group_name, container_app_name): |
303 | 303 | return policy_list |
304 | 304 |
|
305 | 305 |
|
| 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 | + |
306 | 461 | class DaprComponentResiliencyPreviewClient(): |
307 | 462 | api_version = PREVIEW_API_VERSION |
308 | 463 |
|
|
0 commit comments