# --------------------------------------------------------------------------------------------
# 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 baremetalmachine update",
)
class Update(AAZCommand):
    """Update properties of the provided bare metal machine, or update tags associated with the bare metal machine. Properties and tag updates can be done independently.

    :example: Patch bare metal machine
        az networkcloud baremetalmachine update --bare-metal-machine-name "bareMetalMachineName" --machine-details "machinedetails" --tags key1="myvalue1" key2="myvalue2" --resource-group "resourceGroupName"
    """

    _aaz_info = {
        "version": "2023-07-01",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.networkcloud/baremetalmachines/{}", "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.bare_metal_machine_name = AAZStrArg(
            options=["-n", "--name", "--bare-metal-machine-name"],
            help="The name of the bare metal machine.",
            required=True,
            id_part="name",
            fmt=AAZStrArgFormat(
                pattern="^([a-zA-Z0-9][a-zA-Z0-9]{0,62}[a-zA-Z0-9])$",
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "BareMetalMachineUpdateParameters"

        _args_schema = cls._args_schema
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="BareMetalMachineUpdateParameters",
            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.machine_details = AAZStrArg(
            options=["--machine-details"],
            arg_group="Properties",
            help="The details provided by the customer during the creation of rack manifests that allows for custom data to be associated with this machine.",
            fmt=AAZStrArgFormat(
                max_length=256,
            ),
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.BareMetalMachinesUpdate(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 BareMetalMachinesUpdate(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/bareMetalMachines/{bareMetalMachineName}",
                **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(
                    "bareMetalMachineName", self.ctx.args.bare_metal_machine_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("machineDetails", AAZStrType, ".machine_details")

            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_bare_metal_machine_read(cls._schema_on_200)

            return cls._schema_on_200


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

    _schema_bare_metal_machine_read = None

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

        cls._schema_bare_metal_machine_read = _schema_bare_metal_machine_read = AAZObjectType()

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

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

        properties = _schema_bare_metal_machine_read.properties
        properties.associated_resource_ids = AAZListType(
            serialized_name="associatedResourceIds",
            flags={"read_only": True},
        )
        properties.bmc_connection_string = AAZStrType(
            serialized_name="bmcConnectionString",
            flags={"required": True},
        )
        properties.bmc_credentials = AAZObjectType(
            serialized_name="bmcCredentials",
            flags={"required": True},
        )
        properties.bmc_mac_address = AAZStrType(
            serialized_name="bmcMacAddress",
            flags={"required": True},
        )
        properties.boot_mac_address = AAZStrType(
            serialized_name="bootMacAddress",
            flags={"required": True},
        )
        properties.cluster_id = AAZStrType(
            serialized_name="clusterId",
            flags={"read_only": True},
        )
        properties.cordon_status = AAZStrType(
            serialized_name="cordonStatus",
            flags={"read_only": 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.hardware_inventory = AAZObjectType(
            serialized_name="hardwareInventory",
        )
        properties.hardware_validation_status = AAZObjectType(
            serialized_name="hardwareValidationStatus",
        )
        properties.hybrid_aks_clusters_associated_ids = AAZListType(
            serialized_name="hybridAksClustersAssociatedIds",
            flags={"read_only": True},
        )
        properties.kubernetes_node_name = AAZStrType(
            serialized_name="kubernetesNodeName",
            flags={"read_only": True},
        )
        properties.kubernetes_version = AAZStrType(
            serialized_name="kubernetesVersion",
            flags={"read_only": True},
        )
        properties.machine_details = AAZStrType(
            serialized_name="machineDetails",
            flags={"required": True},
        )
        properties.machine_name = AAZStrType(
            serialized_name="machineName",
            flags={"required": True},
        )
        properties.machine_sku_id = AAZStrType(
            serialized_name="machineSkuId",
            flags={"required": True},
        )
        properties.oam_ipv4_address = AAZStrType(
            serialized_name="oamIpv4Address",
            flags={"read_only": True},
        )
        properties.oam_ipv6_address = AAZStrType(
            serialized_name="oamIpv6Address",
            flags={"read_only": True},
        )
        properties.os_image = AAZStrType(
            serialized_name="osImage",
            flags={"read_only": True},
        )
        properties.power_state = AAZStrType(
            serialized_name="powerState",
            flags={"read_only": True},
        )
        properties.provisioning_state = AAZStrType(
            serialized_name="provisioningState",
            flags={"read_only": True},
        )
        properties.rack_id = AAZStrType(
            serialized_name="rackId",
            flags={"required": True},
        )
        properties.rack_slot = AAZIntType(
            serialized_name="rackSlot",
            flags={"required": True},
        )
        properties.ready_state = AAZStrType(
            serialized_name="readyState",
            flags={"read_only": True},
        )
        properties.serial_number = AAZStrType(
            serialized_name="serialNumber",
            flags={"required": True},
        )
        properties.service_tag = AAZStrType(
            serialized_name="serviceTag",
            flags={"read_only": True},
        )
        properties.virtual_machines_associated_ids = AAZListType(
            serialized_name="virtualMachinesAssociatedIds",
            flags={"read_only": True},
        )

        associated_resource_ids = _schema_bare_metal_machine_read.properties.associated_resource_ids
        associated_resource_ids.Element = AAZStrType()

        bmc_credentials = _schema_bare_metal_machine_read.properties.bmc_credentials
        bmc_credentials.password = AAZStrType(
            flags={"required": True, "secret": True},
        )
        bmc_credentials.username = AAZStrType(
            flags={"required": True},
        )

        hardware_inventory = _schema_bare_metal_machine_read.properties.hardware_inventory
        hardware_inventory.additional_host_information = AAZStrType(
            serialized_name="additionalHostInformation",
            flags={"read_only": True},
        )
        hardware_inventory.interfaces = AAZListType(
            flags={"read_only": True},
        )
        hardware_inventory.nics = AAZListType(
            flags={"read_only": True},
        )

        interfaces = _schema_bare_metal_machine_read.properties.hardware_inventory.interfaces
        interfaces.Element = AAZObjectType()

        _element = _schema_bare_metal_machine_read.properties.hardware_inventory.interfaces.Element
        _element.link_status = AAZStrType(
            serialized_name="linkStatus",
            flags={"read_only": True},
        )
        _element.mac_address = AAZStrType(
            serialized_name="macAddress",
            flags={"read_only": True},
        )
        _element.name = AAZStrType(
            flags={"read_only": True},
        )
        _element.network_interface_id = AAZStrType(
            serialized_name="networkInterfaceId",
            flags={"read_only": True},
        )

        nics = _schema_bare_metal_machine_read.properties.hardware_inventory.nics
        nics.Element = AAZObjectType()

        _element = _schema_bare_metal_machine_read.properties.hardware_inventory.nics.Element
        _element.lldp_neighbor = AAZObjectType(
            serialized_name="lldpNeighbor",
        )
        _element.mac_address = AAZStrType(
            serialized_name="macAddress",
            flags={"read_only": True},
        )
        _element.name = AAZStrType(
            flags={"read_only": True},
        )

        lldp_neighbor = _schema_bare_metal_machine_read.properties.hardware_inventory.nics.Element.lldp_neighbor
        lldp_neighbor.port_description = AAZStrType(
            serialized_name="portDescription",
            flags={"read_only": True},
        )
        lldp_neighbor.port_name = AAZStrType(
            serialized_name="portName",
            flags={"read_only": True},
        )
        lldp_neighbor.system_description = AAZStrType(
            serialized_name="systemDescription",
            flags={"read_only": True},
        )
        lldp_neighbor.system_name = AAZStrType(
            serialized_name="systemName",
            flags={"read_only": True},
        )

        hardware_validation_status = _schema_bare_metal_machine_read.properties.hardware_validation_status
        hardware_validation_status.last_validation_time = AAZStrType(
            serialized_name="lastValidationTime",
            flags={"read_only": True},
        )
        hardware_validation_status.result = AAZStrType(
            flags={"read_only": True},
        )

        hybrid_aks_clusters_associated_ids = _schema_bare_metal_machine_read.properties.hybrid_aks_clusters_associated_ids
        hybrid_aks_clusters_associated_ids.Element = AAZStrType()

        virtual_machines_associated_ids = _schema_bare_metal_machine_read.properties.virtual_machines_associated_ids
        virtual_machines_associated_ids.Element = AAZStrType()

        system_data = _schema_bare_metal_machine_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_bare_metal_machine_read.tags
        tags.Element = AAZStrType()

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


__all__ = ["Update"]
