# --------------------------------------------------------------------------------------------
# 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(
    "network perimeter link create",
)
class Create(AAZCommand):
    """Create NSP link resource.

    :example: Create NSP Link
        az network perimeter link create --name link1 --perimeter-name nsp1 --resource-group rg1 --auto-remote-nsp-id <NspId> --local-inbound-profile "[\'*\']" --remote-inbound-profile "[\'*\']" '
    """

    _aaz_info = {
        "version": "2021-02-01-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.network/networksecurityperimeters/{}/links/{}", "2021-02-01-preview"],
        ]
    }

    def _handler(self, command_args):
        super()._handler(command_args)
        self._execute_operations()
        return 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.link_name = AAZStrArg(
            options=["-n", "--name", "--link-name"],
            help="The name of the NSP link.",
            required=True,
        )
        _args_schema.perimeter_name = AAZStrArg(
            options=["--perimeter-name"],
            help="The name of the network security perimeter.",
            required=True,
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.auto_remote_nsp_id = AAZStrArg(
            options=["--auto-remote-nsp-id"],
            arg_group="Properties",
            help="Perimeter ARM Id for the remote NSP with which the link gets created in Auto-approval mode. It should be used when the NSP admin have Microsoft.Network/networkSecurityPerimeters/linkPerimeter/action permission on the remote NSP resource.",
        )
        _args_schema.description = AAZStrArg(
            options=["--description"],
            arg_group="Properties",
            help="A message passed to the owner of the remote NSP link resource with this connection request. In case of Auto-approved flow, it is default to 'Auto Approved'. Restricted to 140 chars.",
        )
        _args_schema.local_inbound_profile = AAZListArg(
            options=["--local-inbound-profile"],
            arg_group="Properties",
            help="List of local Inbound profile names to which Inbound is allowed. Use ['*'] to allow inbound to all profiles. It's default value is ['*'].",
            fmt=AAZListArgFormat(
                unique=True,
            ),
        )
        _args_schema.remote_inbound_profile = AAZListArg(
            options=["--remote-inbound-profile"],
            arg_group="Properties",
            help="List of remote Inbound profile names to which Inbound is allowed. Use ['*'] to allow inbound to all profiles. This property can only be updated in auto-approval mode. It's default value is ['*'].",
            fmt=AAZListArgFormat(
                unique=True,
            ),
        )

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

        remote_inbound_profile = cls._args_schema.remote_inbound_profile
        remote_inbound_profile.Element = AAZStrArg()
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        self.NspLinksCreateOrUpdate(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 NspLinksCreateOrUpdate(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, 201]:
                return self.on_200_201(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityPerimeters/{networkSecurityPerimeterName}/links/{linkName}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "linkName", self.ctx.args.link_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "networkSecurityPerimeterName", self.ctx.args.perimeter_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", "2021-02-01-preview",
                    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": {"required": True, "client_flatten": True}}
            )
            _builder.set_prop("properties", AAZObjectType)

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("autoApprovedRemotePerimeterResourceId", AAZStrType, ".auto_remote_nsp_id")
                properties.set_prop("description", AAZStrType, ".description")
                properties.set_prop("localInboundProfiles", AAZListType, ".local_inbound_profile")
                properties.set_prop("remoteInboundProfiles", AAZListType, ".remote_inbound_profile")

            local_inbound_profiles = _builder.get(".properties.localInboundProfiles")
            if local_inbound_profiles is not None:
                local_inbound_profiles.set_elements(AAZStrType, ".")

            remote_inbound_profiles = _builder.get(".properties.remoteInboundProfiles")
            if remote_inbound_profiles is not None:
                remote_inbound_profiles.set_elements(AAZStrType, ".")

            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()

            _schema_on_200_201 = cls._schema_on_200_201
            _schema_on_200_201.etag = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.properties = AAZObjectType()
            _schema_on_200_201.type = AAZStrType(
                flags={"read_only": True},
            )

            properties = cls._schema_on_200_201.properties
            properties.auto_approved_remote_perimeter_resource_id = AAZStrType(
                serialized_name="autoApprovedRemotePerimeterResourceId",
            )
            properties.description = AAZStrType()
            properties.local_inbound_profiles = AAZListType(
                serialized_name="localInboundProfiles",
            )
            properties.local_outbound_profiles = AAZListType(
                serialized_name="localOutboundProfiles",
                flags={"read_only": True},
            )
            properties.provisioning_state = AAZStrType(
                serialized_name="provisioningState",
                flags={"read_only": True},
            )
            properties.remote_inbound_profiles = AAZListType(
                serialized_name="remoteInboundProfiles",
            )
            properties.remote_outbound_profiles = AAZListType(
                serialized_name="remoteOutboundProfiles",
                flags={"read_only": True},
            )
            properties.remote_perimeter_guid = AAZStrType(
                serialized_name="remotePerimeterGuid",
                flags={"read_only": True},
            )
            properties.remote_perimeter_location = AAZStrType(
                serialized_name="remotePerimeterLocation",
                flags={"read_only": True},
            )
            properties.status = AAZStrType(
                flags={"read_only": True},
            )

            local_inbound_profiles = cls._schema_on_200_201.properties.local_inbound_profiles
            local_inbound_profiles.Element = AAZStrType()

            local_outbound_profiles = cls._schema_on_200_201.properties.local_outbound_profiles
            local_outbound_profiles.Element = AAZStrType()

            remote_inbound_profiles = cls._schema_on_200_201.properties.remote_inbound_profiles
            remote_inbound_profiles.Element = AAZStrType()

            remote_outbound_profiles = cls._schema_on_200_201.properties.remote_outbound_profiles
            remote_outbound_profiles.Element = AAZStrType()

            return cls._schema_on_200_201


class _CreateHelper:
    """Helper class for Create"""


__all__ = ["Create"]
