# --------------------------------------------------------------------------------------------
# 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(
    "dnc orchestrator-instance-service create",
    is_preview=True,
)
class Create(AAZCommand):
    """Create a orchestrator instance.

    :example: Create an orchestrator instance
        az dnc orchestrator-instance-service create --type "SystemAssigned" --location "West US" --kind "Kubernetes" --api-server-endpoint "https://testk8s.cloudapp.net" --cluster-root-ca "ddsadsad344mfdsfdl" --id "/subscriptions/613192d7-503f-477a-9cfe-4efc3ee2bd60/resourceGroups/TestRG/providers/Microsoft.DelegatedNetwork/controller/testcontroller" --orchestrator-app-id "546192d7-503f-477a-9cfe-4efc3ee2b6e1" --orchestrator-tenant-id "da6192d7-503f-477a-9cfe-4efc3ee2b6c3" --priv-link-resource-id "/subscriptions/613192d7-503f-477a-9cfe-4efc3ee2bd60/resourceGroups/TestRG/providers/Microsoft.Network/privateLinkServices/plresource1" --resource-group "TestRG" --resource-name "testk8s1"
    """

    _aaz_info = {
        "version": "2023-05-18-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.delegatednetwork/orchestrators/{}", "2023-05-18-preview"],
        ]
    }

    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.resource_group = AAZResourceGroupNameArg(
            help="Name of resource group. You can configure the default group using `az configure --defaults group=<name>`.",
            required=True,
        )
        _args_schema.resource_name = AAZStrArg(
            options=["-n", "--name", "--resource-name"],
            help="The name of the resource. It must be a minimum of 3 characters, and a maximum of 63.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^[a-z][a-z0-9]*$",
                max_length=63,
                min_length=3,
            ),
        )

        # define Arg Group "ControllerDetails"

        _args_schema = cls._args_schema
        _args_schema.id = AAZStrArg(
            options=["--id"],
            arg_group="ControllerDetails",
            help="controller arm resource id",
        )

        # define Arg Group "Identity"

        _args_schema = cls._args_schema
        _args_schema.type = AAZStrArg(
            options=["--type"],
            arg_group="Identity",
            help="The type of identity used for orchestrator cluster. Type 'SystemAssigned' will use an implicitly created identity orchestrator clusters",
            enum={"None": "None", "SystemAssigned": "SystemAssigned"},
        )

        # define Arg Group "Parameters"

        _args_schema = cls._args_schema
        _args_schema.kind = AAZStrArg(
            options=["--kind"],
            arg_group="Parameters",
            help="The kind of workbook. Choices are user and shared.",
            required=True,
            default="Kubernetes",
            enum={"Kubernetes": "Kubernetes"},
        )
        _args_schema.location = AAZResourceLocationArg(
            arg_group="Parameters",
            help="Location of the resource.",
            fmt=AAZResourceLocationArgFormat(
                resource_group_arg="resource_group",
            ),
        )
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="Parameters",
            help="The resource tags.",
        )

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

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.api_server_endpoint = AAZStrArg(
            options=["--api-server-endpoint"],
            arg_group="Properties",
            help="K8s APIServer url. Either one of apiServerEndpoint or privateLinkResourceId can be specified",
        )
        _args_schema.cluster_root_ca = AAZStrArg(
            options=["--cluster-root-ca"],
            arg_group="Properties",
            help="RootCA certificate of kubernetes cluster base64 encoded",
        )
        _args_schema.orchestrator_app_id = AAZStrArg(
            options=["--orchestrator-app-id"],
            arg_group="Properties",
            help="AAD ID used with apiserver",
        )
        _args_schema.orchestrator_tenant_id = AAZStrArg(
            options=["--orchestrator-tenant-id"],
            arg_group="Properties",
            help="TenantID of server App ID",
        )
        _args_schema.priv_link_resource_id = AAZStrArg(
            options=["--priv-link-resource-id"],
            arg_group="Properties",
            help="private link arm resource id. Either one of apiServerEndpoint or privateLinkResourceId can be specified",
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.OrchestratorInstanceServiceCreate(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 OrchestratorInstanceServiceCreate(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/Microsoft.DelegatedNetwork/orchestrators/{resourceName}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "resourceName", self.ctx.args.resource_name,
                    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-05-18-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("identity", AAZObjectType)
            _builder.set_prop("kind", AAZStrType, ".kind", typ_kwargs={"flags": {"required": True}})
            _builder.set_prop("location", AAZStrType, ".location")
            _builder.set_prop("properties", AAZObjectType)
            _builder.set_prop("tags", AAZDictType, ".tags")

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

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("apiServerEndpoint", AAZStrType, ".api_server_endpoint")
                properties.set_prop("clusterRootCA", AAZStrType, ".cluster_root_ca")
                properties.set_prop("controllerDetails", AAZObjectType, ".", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("orchestratorAppId", AAZStrType, ".orchestrator_app_id")
                properties.set_prop("orchestratorTenantId", AAZStrType, ".orchestrator_tenant_id")
                properties.set_prop("privateLinkResourceId", AAZStrType, ".priv_link_resource_id")

            controller_details = _builder.get(".properties.controllerDetails")
            if controller_details is not None:
                controller_details.set_prop("id", AAZStrType, ".id")

            tags = _builder.get(".tags")
            if tags is not None:
                tags.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.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.identity = AAZObjectType()
            _schema_on_200_201.kind = AAZStrType(
                flags={"required": True},
            )
            _schema_on_200_201.location = AAZStrType()
            _schema_on_200_201.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.properties = AAZObjectType()
            _schema_on_200_201.tags = AAZDictType()
            _schema_on_200_201.type = AAZStrType(
                flags={"read_only": True},
            )

            identity = cls._schema_on_200_201.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()

            properties = cls._schema_on_200_201.properties
            properties.api_server_endpoint = AAZStrType(
                serialized_name="apiServerEndpoint",
            )
            properties.cluster_root_ca = AAZStrType(
                serialized_name="clusterRootCA",
            )
            properties.controller_details = AAZObjectType(
                serialized_name="controllerDetails",
                flags={"required": True},
            )
            properties.orchestrator_app_id = AAZStrType(
                serialized_name="orchestratorAppId",
            )
            properties.orchestrator_tenant_id = AAZStrType(
                serialized_name="orchestratorTenantId",
            )
            properties.private_link_resource_id = AAZStrType(
                serialized_name="privateLinkResourceId",
            )
            properties.provisioning_state = AAZStrType(
                serialized_name="provisioningState",
                flags={"read_only": True},
            )
            properties.resource_guid = AAZStrType(
                serialized_name="resourceGuid",
                flags={"read_only": True},
            )

            controller_details = cls._schema_on_200_201.properties.controller_details
            controller_details.id = AAZStrType()

            tags = cls._schema_on_200_201.tags
            tags.Element = AAZStrType()

            return cls._schema_on_200_201


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


__all__ = ["Create"]
