|
| 1 | +# coding=utf-8 |
| 2 | +# -------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | +# -------------------------------------------------------------------------------------------- |
| 6 | +# pylint: disable=line-too-long, broad-except, logging-format-interpolation, too-many-public-methods, too-many-boolean-expressions, logging-fstring-interpolation |
| 7 | + |
| 8 | +from knack.log import get_logger |
| 9 | +from typing import Any, Dict |
| 10 | + |
| 11 | +from azure.cli.core.commands import AzCliCommand |
| 12 | +from azure.cli.core.azclierror import (ValidationError, ResourceNotFoundError) |
| 13 | +from azure.cli.command_modules.containerapp.base_resource import BaseResource |
| 14 | + |
| 15 | +from ._clients import ContainerAppFunctionsPreviewClient |
| 16 | +from ._client_factory import handle_raw_exception |
| 17 | + |
| 18 | +logger = get_logger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class ContainerAppFunctionsDecorator(BaseResource): |
| 22 | + """Base decorator for Container App Functions operations""" |
| 23 | + |
| 24 | + def __init__(self, cmd: AzCliCommand, client: Any, raw_parameters: Dict, models: str): |
| 25 | + super().__init__(cmd, client, raw_parameters, models) |
| 26 | + |
| 27 | + def get_argument_container_app_name(self): |
| 28 | + return self.get_param("container_app_name") |
| 29 | + |
| 30 | + def get_argument_revision_name(self): |
| 31 | + return self.get_param("revision_name") |
| 32 | + |
| 33 | + def get_argument_function_name(self): |
| 34 | + return self.get_param("function_name") |
| 35 | + |
| 36 | + def set_argument_container_app_name(self, container_app_name): |
| 37 | + self.set_param("container_app_name", container_app_name) |
| 38 | + |
| 39 | + def set_argument_revision_name(self, revision_name): |
| 40 | + self.set_param("revision_name", revision_name) |
| 41 | + |
| 42 | + def set_argument_function_name(self, function_name): |
| 43 | + self.set_param("function_name", function_name) |
| 44 | + |
| 45 | + |
| 46 | +class ContainerAppFunctionsListDecorator(ContainerAppFunctionsDecorator): |
| 47 | + """Decorator for listing functions""" |
| 48 | + |
| 49 | + def __init__(self, cmd: AzCliCommand, client: Any, raw_parameters: Dict, models: str): |
| 50 | + super().__init__(cmd, client, raw_parameters, models) |
| 51 | + |
| 52 | + def list(self): |
| 53 | + """List functions for a container app or revision""" |
| 54 | + try: |
| 55 | + revision_name = self.get_argument_revision_name() |
| 56 | + container_app_name = self.get_argument_container_app_name() |
| 57 | + resource_group_name = self.get_argument_resource_group_name() |
| 58 | + |
| 59 | + if not resource_group_name: |
| 60 | + raise ValidationError("Resource group name is required.") |
| 61 | + |
| 62 | + if not container_app_name: |
| 63 | + raise ValidationError("Container app name is required.") |
| 64 | + |
| 65 | + if revision_name: |
| 66 | + # List functions for a specific revision |
| 67 | + return self.client.list_functions_by_revision( |
| 68 | + cmd=self.cmd, |
| 69 | + resource_group_name=resource_group_name, |
| 70 | + container_app_name=container_app_name, |
| 71 | + revision_name=revision_name |
| 72 | + ) |
| 73 | + else: |
| 74 | + # List functions for the entire container app |
| 75 | + return self.client.list_functions( |
| 76 | + cmd=self.cmd, |
| 77 | + resource_group_name=resource_group_name, |
| 78 | + container_app_name=container_app_name |
| 79 | + ) |
| 80 | + except Exception as e: |
| 81 | + handle_raw_exception(e) |
| 82 | + |
| 83 | + |
| 84 | +class ContainerAppFunctionsShowDecorator(ContainerAppFunctionsDecorator): |
| 85 | + """Decorator for showing a specific function""" |
| 86 | + |
| 87 | + def __init__(self, cmd: AzCliCommand, client: Any, raw_parameters: Dict, models: str): |
| 88 | + super().__init__(cmd, client, raw_parameters, models) |
| 89 | + |
| 90 | + def show(self): |
| 91 | + """Show details of a specific function""" |
| 92 | + try: |
| 93 | + revision_name = self.get_argument_revision_name() |
| 94 | + container_app_name = self.get_argument_container_app_name() |
| 95 | + function_name = self.get_argument_function_name() |
| 96 | + resource_group_name = self.get_argument_resource_group_name() |
| 97 | + |
| 98 | + if not resource_group_name: |
| 99 | + raise ValidationError("Resource group name is required.") |
| 100 | + |
| 101 | + if not container_app_name: |
| 102 | + raise ValidationError("Container app name is required.") |
| 103 | + |
| 104 | + if not function_name: |
| 105 | + raise ValidationError("Function name is required.") |
| 106 | + |
| 107 | + if revision_name: |
| 108 | + # Get function for a specific revision |
| 109 | + return self.client.get_function_by_revision( |
| 110 | + cmd=self.cmd, |
| 111 | + resource_group_name=resource_group_name, |
| 112 | + container_app_name=container_app_name, |
| 113 | + revision_name=revision_name, |
| 114 | + function_name=function_name |
| 115 | + ) |
| 116 | + else: |
| 117 | + # Get function for the entire container app |
| 118 | + return self.client.get_function( |
| 119 | + cmd=self.cmd, |
| 120 | + resource_group_name=resource_group_name, |
| 121 | + container_app_name=container_app_name, |
| 122 | + function_name=function_name |
| 123 | + ) |
| 124 | + except Exception as e: |
| 125 | + handle_raw_exception(e) |
0 commit comments