|
| 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 |
| 7 | + |
| 8 | + |
| 9 | +from knack.log import get_logger |
| 10 | +from typing import Any, Dict |
| 11 | + |
| 12 | +from azure.cli.core.azclierror import ValidationError |
| 13 | +from azure.cli.core.commands import AzCliCommand |
| 14 | +from azure.cli.command_modules.containerapp.base_resource import BaseResource |
| 15 | +from azure.cli.command_modules.containerapp._utils import safe_get |
| 16 | +from ._clients import SessionPoolPreviewClient |
| 17 | + |
| 18 | +from ._models import SessionCodeInterpreterExecution as SessionCodeInterpreterExecutionPreviewModel |
| 19 | +from ._client_factory import handle_raw_exception |
| 20 | +from .containerapp_sessionpool_decorator import ContainerType |
| 21 | + |
| 22 | +logger = get_logger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +class SessionCustomContainerPreviewDecorator(BaseResource): |
| 26 | + def __init__(self, cmd: AzCliCommand, client: Any, raw_parameters: Dict, models: str): |
| 27 | + super().__init__(cmd, client, raw_parameters, models) |
| 28 | + self.session_code_interpreter_def = SessionCodeInterpreterExecutionPreviewModel |
| 29 | + self.session_pool_client = SessionPoolPreviewClient |
| 30 | + |
| 31 | + def get_argument_name(self): |
| 32 | + return self.get_param('name') |
| 33 | + |
| 34 | + def get_argument_resource_group_name(self): |
| 35 | + return self.get_param('resource_group_name') |
| 36 | + |
| 37 | + def get_argument_identifier(self): |
| 38 | + return self.get_param('identifier') |
| 39 | + |
| 40 | + def get_sessionpool(self): |
| 41 | + return self.session_pool_client.show(cmd=self.cmd, |
| 42 | + resource_group_name=self.get_argument_resource_group_name(), |
| 43 | + name=self.get_argument_name()) |
| 44 | + |
| 45 | + |
| 46 | +class SessionCustomContainerCommandsPreviewDecorator(SessionCustomContainerPreviewDecorator): |
| 47 | + def stop_session(self): |
| 48 | + try: |
| 49 | + existing_pool_def = self.get_sessionpool() |
| 50 | + if safe_get(existing_pool_def, "properties", "containerType").lower() != ContainerType.CustomContainer.name.lower(): |
| 51 | + raise ValidationError("Stop session operation is only supported for session pools with type 'CustomContainer'.") |
| 52 | + |
| 53 | + return self.client.stop_session( |
| 54 | + cmd=self.cmd, |
| 55 | + identifier=self.get_argument_identifier(), |
| 56 | + session_pool_endpoint=existing_pool_def["properties"]["poolManagementEndpoint"]) |
| 57 | + except Exception as e: |
| 58 | + handle_raw_exception(e) |
0 commit comments