|
| 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 | +from copy import deepcopy |
| 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 | + |
| 16 | +from ._models import MaintenanceConfiguration as MaintenanceConfigurationModel |
| 17 | +from ._client_factory import handle_raw_exception, handle_non_404_status_code_exception |
| 18 | + |
| 19 | +logger = get_logger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class ContainerappEnvMaintenanceConfigDecorator(BaseResource): |
| 23 | + def __init__(self, cmd: AzCliCommand, client: Any, raw_parameters: Dict, models: str): |
| 24 | + super().__init__(cmd, client, raw_parameters, models) |
| 25 | + self.maintenance_config_def = deepcopy(MaintenanceConfigurationModel) |
| 26 | + self.existing_maintenance_config_def = None |
| 27 | + |
| 28 | + def get_argument_environment_name(self): |
| 29 | + return self.get_param('env_name') |
| 30 | + |
| 31 | + def get_argument_resource_group_name(self): |
| 32 | + return self.get_param('resource_group_name') |
| 33 | + |
| 34 | + def get_argument_weekday(self): |
| 35 | + return self.get_param('weekday') |
| 36 | + |
| 37 | + def get_argument_start_hour_utc(self): |
| 38 | + return self.get_param('start_hour_utc') |
| 39 | + |
| 40 | + def get_argument_duration(self): |
| 41 | + return self.get_param('duration') |
| 42 | + |
| 43 | + |
| 44 | +class ContainerAppEnvMaintenanceConfigPreviewDecorator(ContainerappEnvMaintenanceConfigDecorator): |
| 45 | + def validate_arguments(self): |
| 46 | + if self.get_argument_start_hour_utc() is not None: |
| 47 | + if not (0 <= int(self.get_argument_start_hour_utc()) <= 23): |
| 48 | + raise ValidationError("Start hour must be an integer from 0 to 23") |
| 49 | + |
| 50 | + if self.get_argument_duration() is not None: |
| 51 | + if not (8 <= int(self.get_argument_duration()) <= 24): |
| 52 | + raise ValidationError("Duration must be an integer from 8 to 24") |
| 53 | + |
| 54 | + if self.get_argument_weekday() is not None: |
| 55 | + if self.get_argument_weekday().lower() not in ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]: |
| 56 | + raise ValidationError("Weekday must be a day of the week") |
| 57 | + |
| 58 | + def construct_payload(self, forUpdate=False): |
| 59 | + if forUpdate: |
| 60 | + self.existing_maintenance_config_def = self.client.list( |
| 61 | + cmd=self.cmd, |
| 62 | + resource_group_name=self.get_argument_resource_group_name(), |
| 63 | + environment_name=self.get_argument_environment_name()) |
| 64 | + |
| 65 | + self.maintenance_config_def = deepcopy(self.existing_maintenance_config_def) |
| 66 | + |
| 67 | + if self.get_argument_start_hour_utc() is not None: |
| 68 | + self.maintenance_config_def["properties"]["scheduledEntries"][0]["startHourUtc"] = self.get_argument_start_hour_utc() |
| 69 | + if self.get_argument_duration() is not None: |
| 70 | + self.maintenance_config_def["properties"]["scheduledEntries"][0]["durationHours"] = self.get_argument_duration() |
| 71 | + if self.get_argument_weekday() is not None: |
| 72 | + self.maintenance_config_def["properties"]["scheduledEntries"][0]["weekDay"] = self.get_argument_weekday() |
| 73 | + |
| 74 | + def create_or_update(self): |
| 75 | + try: |
| 76 | + return self.client.create_or_update( |
| 77 | + cmd=self.cmd, |
| 78 | + resource_group_name=self.get_argument_resource_group_name(), |
| 79 | + environment_name=self.get_argument_environment_name(), |
| 80 | + maintenance_config_envelope=self.maintenance_config_def) |
| 81 | + except Exception as e: |
| 82 | + handle_raw_exception(e) |
| 83 | + |
| 84 | + def remove(self): |
| 85 | + try: |
| 86 | + return self.client.remove( |
| 87 | + cmd=self.cmd, |
| 88 | + resource_group_name=self.get_argument_resource_group_name(), |
| 89 | + environment_name=self.get_argument_environment_name()) |
| 90 | + except Exception as e: |
| 91 | + handle_raw_exception(e) |
| 92 | + |
| 93 | + def list(self): |
| 94 | + try: |
| 95 | + return self.client.list( |
| 96 | + cmd=self.cmd, |
| 97 | + resource_group_name=self.get_argument_resource_group_name(), |
| 98 | + environment_name=self.get_argument_environment_name()) |
| 99 | + except Exception as e: |
| 100 | + handle_non_404_status_code_exception(e) |
| 101 | + return "" |
0 commit comments