# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


@register_command(
    "networkcloud kubernetescluster agentpool update",
)
class Update(AAZCommand):
    """Update the properties of the provided Kubernetes cluster agent pool, or update the tags associated with the Kubernetes cluster agent pool. Properties and tag updates can be done independently.

    :example: Patch agent pool of the Kubernetes cluster
        az networkcloud kubernetescluster agentpool update --name "poolName" --kubernetes-cluster-name "kubernetesClusterName" --resource-group "resourceGroupName" --count 3 --upgrade-settings max-surge="1"
    """

    _aaz_info = {
        "version": "2023-07-01",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.networkcloud/kubernetesclusters/{}/agentpools/{}", "2023-07-01"],
        ]
    }

    AZ_SUPPORT_NO_WAIT = True

    def _handler(self, command_args):
        super()._handler(command_args)
        return self.build_lro_poller(self._execute_operations, self._output)

    _args_schema = None

    @classmethod
    def _build_arguments_schema(cls, *args, **kwargs):
        if cls._args_schema is not None:
            return cls._args_schema
        cls._args_schema = super()._build_arguments_schema(*args, **kwargs)

        # define Arg Group ""

        _args_schema = cls._args_schema
        _args_schema.agent_pool_name = AAZStrArg(
            options=["-n", "--name", "--agent-pool-name"],
            help="The name of the Kubernetes cluster agent pool.",
            required=True,
            id_part="child_name_1",
            fmt=AAZStrArgFormat(
                pattern="^([a-zA-Z0-9][a-zA-Z0-9-_]{0,28}[a-zA-Z0-9])$",
            ),
        )
        _args_schema.kubernetes_cluster_name = AAZStrArg(
            options=["--kubernetes-cluster-name"],
            help="The name of the Kubernetes cluster.",
            required=True,
            id_part="name",
            fmt=AAZStrArgFormat(
                pattern="^([a-zA-Z0-9][a-zA-Z0-9-_]{0,28}[a-zA-Z0-9])$",
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "AgentPoolUpdateParameters"

        _args_schema = cls._args_schema
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="AgentPoolUpdateParameters",
            help="The Azure resource tags that will replace the existing ones.",
        )

        tags = cls._args_schema.tags
        tags.Element = AAZStrArg()

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.count = AAZIntArg(
            options=["--count"],
            arg_group="Properties",
            help="The number of virtual machines that use this configuration.",
        )
        _args_schema.upgrade_settings = AAZObjectArg(
            options=["--upgrade-settings"],
            arg_group="Properties",
            help="The configuration of the agent pool.",
        )

        upgrade_settings = cls._args_schema.upgrade_settings
        upgrade_settings.max_surge = AAZStrArg(
            options=["max-surge"],
            help="The maximum number or percentage of nodes that are surged during upgrade. This can either be set to an integer (e.g. '5') or a percentage (e.g. '50%'). If a percentage is specified, it is the percentage of the total agent pool size at the time of the upgrade. For percentages, fractional nodes are rounded up. If not specified, the default is 1.",
            default="1",
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.AgentPoolsUpdate(ctx=self.ctx)()
        self.post_operations()

    @register_callback
    def pre_operations(self):
        pass

    @register_callback
    def post_operations(self):
        pass

    def _output(self, *args, **kwargs):
        result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
        return result

    class AgentPoolsUpdate(AAZHttpOperation):
        CLIENT_TYPE = "MgmtClient"

        def __call__(self, *args, **kwargs):
            request = self.make_request()
            session = self.client.send_request(request=request, stream=False, **kwargs)
            if session.http_response.status_code in [202]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200,
                    self.on_error,
                    lro_options={"final-state-via": "azure-async-operation"},
                    path_format_arguments=self.url_parameters,
                )
            if session.http_response.status_code in [200]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200,
                    self.on_error,
                    lro_options={"final-state-via": "azure-async-operation"},
                    path_format_arguments=self.url_parameters,
                )

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.NetworkCloud/kubernetesClusters/{kubernetesClusterName}/agentPools/{agentPoolName}",
                **self.url_parameters
            )

        @property
        def method(self):
            return "PATCH"

        @property
        def error_format(self):
            return "MgmtErrorFormat"

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "agentPoolName", self.ctx.args.agent_pool_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "kubernetesClusterName", self.ctx.args.kubernetes_cluster_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2023-07-01",
                    required=True,
                ),
            }
            return parameters

        @property
        def header_parameters(self):
            parameters = {
                **self.serialize_header_param(
                    "Content-Type", "application/json",
                ),
                **self.serialize_header_param(
                    "Accept", "application/json",
                ),
            }
            return parameters

        @property
        def content(self):
            _content_value, _builder = self.new_content_builder(
                self.ctx.args,
                typ=AAZObjectType,
                typ_kwargs={"flags": {"client_flatten": True}}
            )
            _builder.set_prop("properties", AAZObjectType, typ_kwargs={"flags": {"client_flatten": True}})
            _builder.set_prop("tags", AAZDictType, ".tags")

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("count", AAZIntType, ".count")
                properties.set_prop("upgradeSettings", AAZObjectType, ".upgrade_settings")

            upgrade_settings = _builder.get(".properties.upgradeSettings")
            if upgrade_settings is not None:
                upgrade_settings.set_prop("maxSurge", AAZStrType, ".max_surge")

            tags = _builder.get(".tags")
            if tags is not None:
                tags.set_elements(AAZStrType, ".")

            return self.serialize_content(_content_value)

        def on_200(self, session):
            data = self.deserialize_http_content(session)
            self.ctx.set_var(
                "instance",
                data,
                schema_builder=self._build_schema_on_200
            )

        _schema_on_200 = None

        @classmethod
        def _build_schema_on_200(cls):
            if cls._schema_on_200 is not None:
                return cls._schema_on_200

            cls._schema_on_200 = AAZObjectType()
            _UpdateHelper._build_schema_agent_pool_read(cls._schema_on_200)

            return cls._schema_on_200


class _UpdateHelper:
    """Helper class for Update"""

    _schema_agent_pool_read = None

    @classmethod
    def _build_schema_agent_pool_read(cls, _schema):
        if cls._schema_agent_pool_read is not None:
            _schema.extended_location = cls._schema_agent_pool_read.extended_location
            _schema.id = cls._schema_agent_pool_read.id
            _schema.location = cls._schema_agent_pool_read.location
            _schema.name = cls._schema_agent_pool_read.name
            _schema.properties = cls._schema_agent_pool_read.properties
            _schema.system_data = cls._schema_agent_pool_read.system_data
            _schema.tags = cls._schema_agent_pool_read.tags
            _schema.type = cls._schema_agent_pool_read.type
            return

        cls._schema_agent_pool_read = _schema_agent_pool_read = AAZObjectType()

        agent_pool_read = _schema_agent_pool_read
        agent_pool_read.extended_location = AAZObjectType(
            serialized_name="extendedLocation",
        )
        agent_pool_read.id = AAZStrType(
            flags={"read_only": True},
        )
        agent_pool_read.location = AAZStrType(
            flags={"required": True},
        )
        agent_pool_read.name = AAZStrType(
            flags={"read_only": True},
        )
        agent_pool_read.properties = AAZObjectType(
            flags={"required": True, "client_flatten": True},
        )
        agent_pool_read.system_data = AAZObjectType(
            serialized_name="systemData",
            flags={"read_only": True},
        )
        agent_pool_read.tags = AAZDictType()
        agent_pool_read.type = AAZStrType(
            flags={"read_only": True},
        )

        extended_location = _schema_agent_pool_read.extended_location
        extended_location.name = AAZStrType(
            flags={"required": True},
        )
        extended_location.type = AAZStrType(
            flags={"required": True},
        )

        properties = _schema_agent_pool_read.properties
        properties.administrator_configuration = AAZObjectType(
            serialized_name="administratorConfiguration",
        )
        properties.agent_options = AAZObjectType(
            serialized_name="agentOptions",
        )
        properties.attached_network_configuration = AAZObjectType(
            serialized_name="attachedNetworkConfiguration",
        )
        properties.availability_zones = AAZListType(
            serialized_name="availabilityZones",
        )
        properties.count = AAZIntType(
            flags={"required": True},
        )
        properties.detailed_status = AAZStrType(
            serialized_name="detailedStatus",
            flags={"read_only": True},
        )
        properties.detailed_status_message = AAZStrType(
            serialized_name="detailedStatusMessage",
            flags={"read_only": True},
        )
        properties.kubernetes_version = AAZStrType(
            serialized_name="kubernetesVersion",
            flags={"read_only": True},
        )
        properties.labels = AAZListType()
        properties.mode = AAZStrType(
            flags={"required": True},
        )
        properties.provisioning_state = AAZStrType(
            serialized_name="provisioningState",
            flags={"read_only": True},
        )
        properties.taints = AAZListType()
        properties.upgrade_settings = AAZObjectType(
            serialized_name="upgradeSettings",
        )
        properties.vm_sku_name = AAZStrType(
            serialized_name="vmSkuName",
            flags={"required": True},
        )

        administrator_configuration = _schema_agent_pool_read.properties.administrator_configuration
        administrator_configuration.admin_username = AAZStrType(
            serialized_name="adminUsername",
        )
        administrator_configuration.ssh_public_keys = AAZListType(
            serialized_name="sshPublicKeys",
        )

        ssh_public_keys = _schema_agent_pool_read.properties.administrator_configuration.ssh_public_keys
        ssh_public_keys.Element = AAZObjectType()

        _element = _schema_agent_pool_read.properties.administrator_configuration.ssh_public_keys.Element
        _element.key_data = AAZStrType(
            serialized_name="keyData",
            flags={"required": True},
        )

        agent_options = _schema_agent_pool_read.properties.agent_options
        agent_options.hugepages_count = AAZIntType(
            serialized_name="hugepagesCount",
            flags={"required": True},
        )
        agent_options.hugepages_size = AAZStrType(
            serialized_name="hugepagesSize",
        )

        attached_network_configuration = _schema_agent_pool_read.properties.attached_network_configuration
        attached_network_configuration.l2_networks = AAZListType(
            serialized_name="l2Networks",
        )
        attached_network_configuration.l3_networks = AAZListType(
            serialized_name="l3Networks",
        )
        attached_network_configuration.trunked_networks = AAZListType(
            serialized_name="trunkedNetworks",
        )

        l2_networks = _schema_agent_pool_read.properties.attached_network_configuration.l2_networks
        l2_networks.Element = AAZObjectType()

        _element = _schema_agent_pool_read.properties.attached_network_configuration.l2_networks.Element
        _element.network_id = AAZStrType(
            serialized_name="networkId",
            flags={"required": True},
        )
        _element.plugin_type = AAZStrType(
            serialized_name="pluginType",
        )

        l3_networks = _schema_agent_pool_read.properties.attached_network_configuration.l3_networks
        l3_networks.Element = AAZObjectType()

        _element = _schema_agent_pool_read.properties.attached_network_configuration.l3_networks.Element
        _element.ipam_enabled = AAZStrType(
            serialized_name="ipamEnabled",
        )
        _element.network_id = AAZStrType(
            serialized_name="networkId",
            flags={"required": True},
        )
        _element.plugin_type = AAZStrType(
            serialized_name="pluginType",
        )

        trunked_networks = _schema_agent_pool_read.properties.attached_network_configuration.trunked_networks
        trunked_networks.Element = AAZObjectType()

        _element = _schema_agent_pool_read.properties.attached_network_configuration.trunked_networks.Element
        _element.network_id = AAZStrType(
            serialized_name="networkId",
            flags={"required": True},
        )
        _element.plugin_type = AAZStrType(
            serialized_name="pluginType",
        )

        availability_zones = _schema_agent_pool_read.properties.availability_zones
        availability_zones.Element = AAZStrType()

        labels = _schema_agent_pool_read.properties.labels
        labels.Element = AAZObjectType()
        cls._build_schema_kubernetes_label_read(labels.Element)

        taints = _schema_agent_pool_read.properties.taints
        taints.Element = AAZObjectType()
        cls._build_schema_kubernetes_label_read(taints.Element)

        upgrade_settings = _schema_agent_pool_read.properties.upgrade_settings
        upgrade_settings.max_surge = AAZStrType(
            serialized_name="maxSurge",
        )

        system_data = _schema_agent_pool_read.system_data
        system_data.created_at = AAZStrType(
            serialized_name="createdAt",
        )
        system_data.created_by = AAZStrType(
            serialized_name="createdBy",
        )
        system_data.created_by_type = AAZStrType(
            serialized_name="createdByType",
        )
        system_data.last_modified_at = AAZStrType(
            serialized_name="lastModifiedAt",
        )
        system_data.last_modified_by = AAZStrType(
            serialized_name="lastModifiedBy",
        )
        system_data.last_modified_by_type = AAZStrType(
            serialized_name="lastModifiedByType",
        )

        tags = _schema_agent_pool_read.tags
        tags.Element = AAZStrType()

        _schema.extended_location = cls._schema_agent_pool_read.extended_location
        _schema.id = cls._schema_agent_pool_read.id
        _schema.location = cls._schema_agent_pool_read.location
        _schema.name = cls._schema_agent_pool_read.name
        _schema.properties = cls._schema_agent_pool_read.properties
        _schema.system_data = cls._schema_agent_pool_read.system_data
        _schema.tags = cls._schema_agent_pool_read.tags
        _schema.type = cls._schema_agent_pool_read.type

    _schema_kubernetes_label_read = None

    @classmethod
    def _build_schema_kubernetes_label_read(cls, _schema):
        if cls._schema_kubernetes_label_read is not None:
            _schema.key = cls._schema_kubernetes_label_read.key
            _schema.value = cls._schema_kubernetes_label_read.value
            return

        cls._schema_kubernetes_label_read = _schema_kubernetes_label_read = AAZObjectType()

        kubernetes_label_read = _schema_kubernetes_label_read
        kubernetes_label_read.key = AAZStrType(
            flags={"required": True},
        )
        kubernetes_label_read.value = AAZStrType(
            flags={"required": True},
        )

        _schema.key = cls._schema_kubernetes_label_read.key
        _schema.value = cls._schema_kubernetes_label_read.value


__all__ = ["Update"]
