# --------------------------------------------------------------------------------------------
# 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(
    "nginx deployment update",
)
class Update(AAZCommand):
    """Update an Nginx deployment

    :example: Update tags and enable diagnostics support for a deployment
        az nginx deployment update --name myDeployment --resource-group myResourceGroup --location eastus2 --tags tag1="value1" tag2="value2" --enable-diagnostics
    """

    _aaz_info = {
        "version": "2022-08-01",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/nginx.nginxplus/nginxdeployments/{}", "2022-08-01"],
        ]
    }

    AZ_SUPPORT_NO_WAIT = True

    AZ_SUPPORT_GENERIC_UPDATE = 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.deployment_name = AAZStrArg(
            options=["-n", "--name", "--deployment-name"],
            help="The name of targeted Nginx deployment",
            required=True,
            id_part="name",
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "Body"

        _args_schema = cls._args_schema
        _args_schema.identity = AAZObjectArg(
            options=["--identity"],
            arg_group="Body",
            nullable=True,
        )
        _args_schema.location = AAZResourceLocationArg(
            arg_group="Body",
            nullable=True,
            fmt=AAZResourceLocationArgFormat(
                resource_group_arg="resource_group",
            ),
        )
        _args_schema.sku = AAZObjectArg(
            options=["--sku"],
            arg_group="Body",
            nullable=True,
        )
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="Body",
            nullable=True,
        )

        identity = cls._args_schema.identity
        identity.type = AAZStrArg(
            options=["type"],
            nullable=True,
            enum={"None": "None", "SystemAssigned": "SystemAssigned", "SystemAssigned, UserAssigned": "SystemAssigned, UserAssigned", "UserAssigned": "UserAssigned"},
        )
        identity.user_assigned_identities = AAZObjectArg(
            options=["user-assigned-identities"],
            nullable=True,
        )

        sku = cls._args_schema.sku
        sku.name = AAZStrArg(
            options=["name"],
            help="Name of the SKU.",
        )

        tags = cls._args_schema.tags
        tags.Element = AAZStrArg(
            nullable=True,
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.enable_diagnostics = AAZBoolArg(
            options=["--enable-diagnostics"],
            help="Boolean to enable or disable diagnostics on your deployment",
            arg_group="Properties",
            nullable=True,
        )
        _args_schema.logging = AAZObjectArg(
            options=["--logging"],
            arg_group="Properties",
            nullable=True,
        )
        _args_schema.managed_resource_group = AAZStrArg(
            options=["--managed-resource-group"],
            arg_group="Properties",
            help="The managed resource group to deploy VNet injection related network resources.",
            nullable=True,
        )
        _args_schema.network_profile = AAZObjectArg(
            options=["--network-profile"],
            arg_group="Properties",
            nullable=True,
        )
        _args_schema.provisioning_state = AAZStrArg(
            options=["--provisioning-state"],
            help="State of the deployment",
            arg_group="Properties",
            nullable=True,
            enum={"Accepted": "Accepted", "Canceled": "Canceled", "Creating": "Creating", "Deleted": "Deleted", "Deleting": "Deleting", "Failed": "Failed", "NotSpecified": "NotSpecified", "Succeeded": "Succeeded", "Updating": "Updating"},
        )

        logging = cls._args_schema.logging
        logging.storage_account = AAZObjectArg(
            options=["storage-account"],
            nullable=True,
        )

        storage_account = cls._args_schema.logging.storage_account
        storage_account.account_name = AAZStrArg(
            options=["account-name"],
            nullable=True,
        )
        storage_account.container_name = AAZStrArg(
            options=["container-name"],
            nullable=True,
        )

        network_profile = cls._args_schema.network_profile
        network_profile.front_end_ip_configuration = AAZObjectArg(
            options=["front-end-ip-configuration"],
            nullable=True,
        )
        network_profile.network_interface_configuration = AAZObjectArg(
            options=["network-interface-configuration"],
            nullable=True,
        )

        front_end_ip_configuration = cls._args_schema.network_profile.front_end_ip_configuration
        front_end_ip_configuration.private_ip_addresses = AAZListArg(
            options=["private-ip-addresses"],
            nullable=True,
        )
        front_end_ip_configuration.public_ip_addresses = AAZListArg(
            options=["public-ip-addresses"],
            nullable=True,
        )

        private_ip_addresses = cls._args_schema.network_profile.front_end_ip_configuration.private_ip_addresses
        private_ip_addresses.Element = AAZObjectArg(
            nullable=True,
        )

        _element = cls._args_schema.network_profile.front_end_ip_configuration.private_ip_addresses.Element
        _element.private_ip_address = AAZStrArg(
            options=["private-ip-address"],
            nullable=True,
        )
        _element.private_ip_allocation_method = AAZStrArg(
            options=["private-ip-allocation-method"],
            nullable=True,
            enum={"Dynamic": "Dynamic", "Static": "Static"},
        )
        _element.subnet_id = AAZStrArg(
            options=["subnet-id"],
            nullable=True,
        )

        public_ip_addresses = cls._args_schema.network_profile.front_end_ip_configuration.public_ip_addresses
        public_ip_addresses.Element = AAZObjectArg(
            nullable=True,
        )

        _element = cls._args_schema.network_profile.front_end_ip_configuration.public_ip_addresses.Element
        _element.id = AAZStrArg(
            options=["id"],
            nullable=True,
        )

        network_interface_configuration = cls._args_schema.network_profile.network_interface_configuration
        network_interface_configuration.subnet_id = AAZStrArg(
            options=["subnet-id"],
            nullable=True,
        )
        return cls._args_schema

    def _execute_operations(self):
        self.DeploymentsGet(ctx=self.ctx)()
        self.InstanceUpdateByJson(ctx=self.ctx)()
        self.InstanceUpdateByGeneric(ctx=self.ctx)()
        yield self.DeploymentsCreate(ctx=self.ctx)()

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

    class DeploymentsGet(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 [200]:
                return self.on_200(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Nginx.NginxPlus/nginxDeployments/{deploymentName}",
                **self.url_parameters
            )

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

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "deploymentName", self.ctx.args.deployment_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 header_parameters(self):
            parameters = {
                **self.serialize_header_param(
                    "Accept", "application/json",
                ),
            }
            return parameters

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

            return cls._schema_on_200

    class DeploymentsCreate(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_201,
                    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, 201]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200_201,
                    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/Nginx.NginxPlus/nginxDeployments/{deploymentName}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "deploymentName", self.ctx.args.deployment_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 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,
                value=self.ctx.vars.instance,
            )

            return self.serialize_content(_content_value)

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

        _schema_on_200_201 = None

        @classmethod
        def _build_schema_on_200_201(cls):
            if cls._schema_on_200_201 is not None:
                return cls._schema_on_200_201

            cls._schema_on_200_201 = AAZObjectType()
            _build_schema_nginx_deployment_read(cls._schema_on_200_201)

            return cls._schema_on_200_201

    class InstanceUpdateByJson(AAZJsonInstanceUpdateOperation):

        def __call__(self, *args, **kwargs):
            self._update_instance(self.ctx.vars.instance)

        def _update_instance(self, instance):
            _instance_value, _builder = self.new_content_builder(
                self.ctx.args,
                value=instance,
                typ=AAZObjectType
            )
            _builder.set_prop("identity", AAZObjectType, ".identity")
            _builder.set_prop("location", AAZStrType, ".location")
            _builder.set_prop("properties", AAZObjectType)
            _builder.set_prop("sku", AAZObjectType, ".sku")
            _builder.set_prop("tags", AAZDictType, ".tags")

            identity = _builder.get(".identity")
            if identity is not None:
                identity.set_prop("type", AAZStrType, ".type")
                identity.set_prop("userAssignedIdentities", AAZObjectType, ".user_assigned_identities")

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("enableDiagnosticsSupport", AAZBoolType, ".enable_diagnostics")
                properties.set_prop("logging", AAZObjectType, ".logging")
                properties.set_prop("managedResourceGroup", AAZStrType, ".managed_resource_group")
                properties.set_prop("networkProfile", AAZObjectType, ".network_profile")
                properties.set_prop("provisioningState", AAZStrType, ".provisioning_state")

            logging = _builder.get(".properties.logging")
            if logging is not None:
                logging.set_prop("storageAccount", AAZObjectType, ".storage_account")

            storage_account = _builder.get(".properties.logging.storageAccount")
            if storage_account is not None:
                storage_account.set_prop("accountName", AAZStrType, ".account_name")
                storage_account.set_prop("containerName", AAZStrType, ".container_name")

            network_profile = _builder.get(".properties.networkProfile")
            if network_profile is not None:
                network_profile.set_prop("frontEndIPConfiguration", AAZObjectType, ".front_end_ip_configuration")
                network_profile.set_prop("networkInterfaceConfiguration", AAZObjectType, ".network_interface_configuration")

            front_end_ip_configuration = _builder.get(".properties.networkProfile.frontEndIPConfiguration")
            if front_end_ip_configuration is not None:
                front_end_ip_configuration.set_prop("privateIPAddresses", AAZListType, ".private_ip_addresses")
                front_end_ip_configuration.set_prop("publicIPAddresses", AAZListType, ".public_ip_addresses")

            private_ip_addresses = _builder.get(".properties.networkProfile.frontEndIPConfiguration.privateIPAddresses")
            if private_ip_addresses is not None:
                private_ip_addresses.set_elements(AAZObjectType, ".")

            _elements = _builder.get(".properties.networkProfile.frontEndIPConfiguration.privateIPAddresses[]")
            if _elements is not None:
                _elements.set_prop("privateIPAddress", AAZStrType, ".private_ip_address")
                _elements.set_prop("privateIPAllocationMethod", AAZStrType, ".private_ip_allocation_method")
                _elements.set_prop("subnetId", AAZStrType, ".subnet_id")

            public_ip_addresses = _builder.get(".properties.networkProfile.frontEndIPConfiguration.publicIPAddresses")
            if public_ip_addresses is not None:
                public_ip_addresses.set_elements(AAZObjectType, ".")

            _elements = _builder.get(".properties.networkProfile.frontEndIPConfiguration.publicIPAddresses[]")
            if _elements is not None:
                _elements.set_prop("id", AAZStrType, ".id")

            network_interface_configuration = _builder.get(".properties.networkProfile.networkInterfaceConfiguration")
            if network_interface_configuration is not None:
                network_interface_configuration.set_prop("subnetId", AAZStrType, ".subnet_id")

            sku = _builder.get(".sku")
            if sku is not None:
                sku.set_prop("name", AAZStrType, ".name", typ_kwargs={"flags": {"required": True}})

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

            return _instance_value

    class InstanceUpdateByGeneric(AAZGenericInstanceUpdateOperation):

        def __call__(self, *args, **kwargs):
            self._update_instance_by_generic(
                self.ctx.vars.instance,
                self.ctx.generic_update_args
            )


_schema_nginx_deployment_read = None


def _build_schema_nginx_deployment_read(_schema):
    global _schema_nginx_deployment_read
    if _schema_nginx_deployment_read is not None:
        _schema.id = _schema_nginx_deployment_read.id
        _schema.identity = _schema_nginx_deployment_read.identity
        _schema.location = _schema_nginx_deployment_read.location
        _schema.name = _schema_nginx_deployment_read.name
        _schema.properties = _schema_nginx_deployment_read.properties
        _schema.sku = _schema_nginx_deployment_read.sku
        _schema.system_data = _schema_nginx_deployment_read.system_data
        _schema.tags = _schema_nginx_deployment_read.tags
        _schema.type = _schema_nginx_deployment_read.type
        return

    _schema_nginx_deployment_read = AAZObjectType()

    nginx_deployment_read = _schema_nginx_deployment_read
    nginx_deployment_read.id = AAZStrType(
        flags={"read_only": True},
    )
    nginx_deployment_read.identity = AAZObjectType()
    nginx_deployment_read.location = AAZStrType()
    nginx_deployment_read.name = AAZStrType(
        flags={"read_only": True},
    )
    nginx_deployment_read.properties = AAZObjectType()
    nginx_deployment_read.sku = AAZObjectType()
    nginx_deployment_read.system_data = AAZObjectType(
        serialized_name="systemData",
        flags={"read_only": True},
    )
    nginx_deployment_read.tags = AAZDictType()
    nginx_deployment_read.type = AAZStrType(
        flags={"read_only": True},
    )

    identity = _schema_nginx_deployment_read.identity
    identity.principal_id = AAZStrType(
        serialized_name="principalId",
        flags={"read_only": True},
    )
    identity.tenant_id = AAZStrType(
        serialized_name="tenantId",
        flags={"read_only": True},
    )
    identity.type = AAZStrType()
    identity.user_assigned_identities = AAZDictType(
        serialized_name="userAssignedIdentities",
    )

    user_assigned_identities = _schema_nginx_deployment_read.identity.user_assigned_identities
    user_assigned_identities.Element = AAZObjectType()

    _element = _schema_nginx_deployment_read.identity.user_assigned_identities.Element
    _element.client_id = AAZStrType(
        serialized_name="clientId",
        flags={"read_only": True},
    )
    _element.principal_id = AAZStrType(
        serialized_name="principalId",
        flags={"read_only": True},
    )

    properties = _schema_nginx_deployment_read.properties
    properties.enable_diagnostics_support = AAZBoolType(
        serialized_name="enableDiagnosticsSupport",
    )
    properties.ip_address = AAZStrType(
        serialized_name="ipAddress",
        flags={"read_only": True},
    )
    properties.logging = AAZObjectType()
    properties.managed_resource_group = AAZStrType(
        serialized_name="managedResourceGroup",
    )
    properties.network_profile = AAZObjectType(
        serialized_name="networkProfile",
    )
    properties.nginx_version = AAZStrType(
        serialized_name="nginxVersion",
        flags={"read_only": True},
    )
    properties.provisioning_state = AAZStrType(
        serialized_name="provisioningState",
    )

    logging = _schema_nginx_deployment_read.properties.logging
    logging.storage_account = AAZObjectType(
        serialized_name="storageAccount",
    )

    storage_account = _schema_nginx_deployment_read.properties.logging.storage_account
    storage_account.account_name = AAZStrType(
        serialized_name="accountName",
    )
    storage_account.container_name = AAZStrType(
        serialized_name="containerName",
    )

    network_profile = _schema_nginx_deployment_read.properties.network_profile
    network_profile.front_end_ip_configuration = AAZObjectType(
        serialized_name="frontEndIPConfiguration",
    )
    network_profile.network_interface_configuration = AAZObjectType(
        serialized_name="networkInterfaceConfiguration",
    )

    front_end_ip_configuration = _schema_nginx_deployment_read.properties.network_profile.front_end_ip_configuration
    front_end_ip_configuration.private_ip_addresses = AAZListType(
        serialized_name="privateIPAddresses",
    )
    front_end_ip_configuration.public_ip_addresses = AAZListType(
        serialized_name="publicIPAddresses",
    )

    private_ip_addresses = _schema_nginx_deployment_read.properties.network_profile.front_end_ip_configuration.private_ip_addresses
    private_ip_addresses.Element = AAZObjectType()

    _element = _schema_nginx_deployment_read.properties.network_profile.front_end_ip_configuration.private_ip_addresses.Element
    _element.private_ip_address = AAZStrType(
        serialized_name="privateIPAddress",
    )
    _element.private_ip_allocation_method = AAZStrType(
        serialized_name="privateIPAllocationMethod",
    )
    _element.subnet_id = AAZStrType(
        serialized_name="subnetId",
    )

    public_ip_addresses = _schema_nginx_deployment_read.properties.network_profile.front_end_ip_configuration.public_ip_addresses
    public_ip_addresses.Element = AAZObjectType()

    _element = _schema_nginx_deployment_read.properties.network_profile.front_end_ip_configuration.public_ip_addresses.Element
    _element.id = AAZStrType()

    network_interface_configuration = _schema_nginx_deployment_read.properties.network_profile.network_interface_configuration
    network_interface_configuration.subnet_id = AAZStrType(
        serialized_name="subnetId",
    )

    sku = _schema_nginx_deployment_read.sku
    sku.name = AAZStrType(
        flags={"required": True},
    )

    system_data = _schema_nginx_deployment_read.system_data
    system_data.created_at = AAZStrType(
        serialized_name="createdAt",
        flags={"read_only": True},
    )
    system_data.created_by = AAZStrType(
        serialized_name="createdBy",
        flags={"read_only": True},
    )
    system_data.created_by_type = AAZStrType(
        serialized_name="createdByType",
        flags={"read_only": True},
    )
    system_data.last_modified_at = AAZStrType(
        serialized_name="lastModifiedAt",
        flags={"read_only": True},
    )
    system_data.last_modified_by = AAZStrType(
        serialized_name="lastModifiedBy",
        flags={"read_only": True},
    )
    system_data.last_modified_by_type = AAZStrType(
        serialized_name="lastModifiedByType",
        flags={"read_only": True},
    )

    tags = _schema_nginx_deployment_read.tags
    tags.Element = AAZStrType()

    _schema.id = _schema_nginx_deployment_read.id
    _schema.identity = _schema_nginx_deployment_read.identity
    _schema.location = _schema_nginx_deployment_read.location
    _schema.name = _schema_nginx_deployment_read.name
    _schema.properties = _schema_nginx_deployment_read.properties
    _schema.sku = _schema_nginx_deployment_read.sku
    _schema.system_data = _schema_nginx_deployment_read.system_data
    _schema.tags = _schema_nginx_deployment_read.tags
    _schema.type = _schema_nginx_deployment_read.type


__all__ = ["Update"]
