# --------------------------------------------------------------------------------------------
# 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(
    "sentinel analytics-setting update",
    is_experimental=True,
)
class Update(AAZCommand):
    """Update the Security ML Analytics Settings.
    """

    _aaz_info = {
        "version": "2022-06-01-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.operationalinsights/workspaces/{}/providers/microsoft.securityinsights/securitymlanalyticssettings/{}", "2022-06-01-preview"],
        ]
    }

    AZ_SUPPORT_GENERIC_UPDATE = True

    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.resource_group = AAZResourceGroupNameArg(
            required=True,
        )
        _args_schema.settings_resource_name = AAZStrArg(
            options=["-n", "--name", "--settings-resource-name"],
            help="Security ML Analytics Settings resource name",
            required=True,
            id_part="child_name_1",
        )
        _args_schema.workspace_name = AAZStrArg(
            options=["-w", "--workspace-name"],
            help="The name of the workspace.",
            required=True,
            is_experimental=True,
            id_part="name",
        )

        # define Arg Group "SecurityMLAnalyticsSetting"

        _args_schema = cls._args_schema
        _args_schema.anomaly = AAZObjectArg(
            options=["--anomaly"],
            arg_group="SecurityMLAnalyticsSetting",
        )
        _args_schema.etag = AAZStrArg(
            options=["--etag"],
            arg_group="SecurityMLAnalyticsSetting",
            help="Etag of the azure resource",
            nullable=True,
        )

        anomaly = cls._args_schema.anomaly
        anomaly.anomaly_settings_version = AAZIntArg(
            options=["anomaly-settings-version"],
            help="The anomaly settings version of the Anomaly security ml analytics settings that dictates whether job version gets updated or not.",
            nullable=True,
        )
        anomaly.anomaly_version = AAZStrArg(
            options=["anomaly-version"],
            help="The anomaly version of the AnomalySecurityMLAnalyticsSettings.",
        )
        anomaly.description = AAZStrArg(
            options=["description"],
            help="The description of the SecurityMLAnalyticsSettings.",
            nullable=True,
        )
        anomaly.display_name = AAZStrArg(
            options=["display-name"],
            help="The display name for settings created by this SecurityMLAnalyticsSettings.",
        )
        anomaly.enabled = AAZBoolArg(
            options=["enabled"],
            help="Determines whether this settings is enabled or disabled.",
        )
        anomaly.frequency = AAZStrArg(
            options=["frequency"],
            help="The frequency that this SecurityMLAnalyticsSettings will be run.",
        )
        anomaly.is_default_settings = AAZBoolArg(
            options=["is-default-settings"],
            help="Determines whether this anomaly security ml analytics settings is a default settings",
        )
        anomaly.required_data_connectors = AAZListArg(
            options=["required-data-connectors"],
            help="The required data sources for this SecurityMLAnalyticsSettings",
            nullable=True,
        )
        anomaly.settings_definition_id = AAZStrArg(
            options=["settings-definition-id"],
            help="The anomaly settings definition Id",
            nullable=True,
        )
        anomaly.settings_status = AAZStrArg(
            options=["settings-status"],
            help="The anomaly SecurityMLAnalyticsSettings status",
            enum={"Flighting": "Flighting", "Production": "Production"},
        )
        anomaly.tactics = AAZListArg(
            options=["tactics"],
            help="The tactics of the SecurityMLAnalyticsSettings",
            nullable=True,
        )
        anomaly.techniques = AAZListArg(
            options=["techniques"],
            help="The techniques of the SecurityMLAnalyticsSettings",
            nullable=True,
        )

        required_data_connectors = cls._args_schema.anomaly.required_data_connectors
        required_data_connectors.Element = AAZObjectArg()

        _element = cls._args_schema.anomaly.required_data_connectors.Element
        _element.connector_id = AAZStrArg(
            options=["connector-id"],
            help="The connector id that provides the following data types",
            nullable=True,
        )
        _element.data_types = AAZListArg(
            options=["data-types"],
            help="The data types used by the security ml analytics settings",
            nullable=True,
        )

        data_types = cls._args_schema.anomaly.required_data_connectors.Element.data_types
        data_types.Element = AAZStrArg()

        tactics = cls._args_schema.anomaly.tactics
        tactics.Element = AAZStrArg(
            enum={"Collection": "Collection", "CommandAndControl": "CommandAndControl", "CredentialAccess": "CredentialAccess", "DefenseEvasion": "DefenseEvasion", "Discovery": "Discovery", "Execution": "Execution", "Exfiltration": "Exfiltration", "Impact": "Impact", "ImpairProcessControl": "ImpairProcessControl", "InhibitResponseFunction": "InhibitResponseFunction", "InitialAccess": "InitialAccess", "LateralMovement": "LateralMovement", "Persistence": "Persistence", "PreAttack": "PreAttack", "PrivilegeEscalation": "PrivilegeEscalation", "Reconnaissance": "Reconnaissance", "ResourceDevelopment": "ResourceDevelopment"},
        )

        techniques = cls._args_schema.anomaly.techniques
        techniques.Element = AAZStrArg()
        return cls._args_schema

    def _execute_operations(self):
        self.SecurityMLAnalyticsSettingsGet(ctx=self.ctx)()
        self.InstanceUpdateByJson(ctx=self.ctx)()
        self.InstanceUpdateByGeneric(ctx=self.ctx)()
        self.SecurityMLAnalyticsSettingsCreateOrUpdate(ctx=self.ctx)()

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

    class SecurityMLAnalyticsSettingsGet(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/Microsoft.OperationalInsights/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/securityMLAnalyticsSettings/{settingsResourceName}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "settingsResourceName", self.ctx.args.settings_resource_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "workspaceName", self.ctx.args.workspace_name,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2022-06-01-preview",
                    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_security_ml_analytics_setting_read(cls._schema_on_200)

            return cls._schema_on_200

    class SecurityMLAnalyticsSettingsCreateOrUpdate(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.OperationalInsights/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/securityMLAnalyticsSettings/{settingsResourceName}",
                **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(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "settingsResourceName", self.ctx.args.settings_resource_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "workspaceName", self.ctx.args.workspace_name,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2022-06-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,
                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_security_ml_analytics_setting_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("etag", AAZStrType, ".etag")
            _builder.set_const("kind", "Anomaly", AAZStrType, ".anomaly", typ_kwargs={"flags": {"required": True}})
            _builder.discriminate_by("kind", "Anomaly")

            disc_anomaly = _builder.get("{kind:Anomaly}")
            if disc_anomaly is not None:
                disc_anomaly.set_prop("properties", AAZObjectType, typ_kwargs={"flags": {"client_flatten": True}})

            properties = _builder.get("{kind:Anomaly}.properties")
            if properties is not None:
                properties.set_prop("anomalySettingsVersion", AAZIntType, ".anomaly.anomaly_settings_version")
                properties.set_prop("anomalyVersion", AAZStrType, ".anomaly.anomaly_version", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("description", AAZStrType, ".anomaly.description")
                properties.set_prop("displayName", AAZStrType, ".anomaly.display_name", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("enabled", AAZBoolType, ".anomaly.enabled", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("frequency", AAZStrType, ".anomaly.frequency", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("isDefaultSettings", AAZBoolType, ".anomaly.is_default_settings", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("requiredDataConnectors", AAZListType, ".anomaly.required_data_connectors")
                properties.set_prop("settingsDefinitionId", AAZStrType, ".anomaly.settings_definition_id")
                properties.set_prop("settingsStatus", AAZStrType, ".anomaly.settings_status", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("tactics", AAZListType, ".anomaly.tactics")
                properties.set_prop("techniques", AAZListType, ".anomaly.techniques")

            required_data_connectors = _builder.get("{kind:Anomaly}.properties.requiredDataConnectors")
            if required_data_connectors is not None:
                required_data_connectors.set_elements(AAZObjectType)

            _elements = _builder.get("{kind:Anomaly}.properties.requiredDataConnectors[]")
            if _elements is not None:
                _elements.set_prop("connectorId", AAZStrType, ".connector_id")
                _elements.set_prop("dataTypes", AAZListType, ".data_types")

            data_types = _builder.get("{kind:Anomaly}.properties.requiredDataConnectors[].dataTypes")
            if data_types is not None:
                data_types.set_elements(AAZStrType, ".")

            tactics = _builder.get("{kind:Anomaly}.properties.tactics")
            if tactics is not None:
                tactics.set_elements(AAZStrType, ".")

            techniques = _builder.get("{kind:Anomaly}.properties.techniques")
            if techniques is not None:
                techniques.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_security_ml_analytics_setting_read = None


def _build_schema_security_ml_analytics_setting_read(_schema):
    global _schema_security_ml_analytics_setting_read
    if _schema_security_ml_analytics_setting_read is not None:
        _schema.etag = _schema_security_ml_analytics_setting_read.etag
        _schema.id = _schema_security_ml_analytics_setting_read.id
        _schema.kind = _schema_security_ml_analytics_setting_read.kind
        _schema.name = _schema_security_ml_analytics_setting_read.name
        _schema.system_data = _schema_security_ml_analytics_setting_read.system_data
        _schema.type = _schema_security_ml_analytics_setting_read.type
        _schema.discriminate_by(
            "kind",
            "Anomaly",
            _schema_security_ml_analytics_setting_read.discriminate_by(
                "kind",
                "Anomaly",
            )
        )
        return

    _schema_security_ml_analytics_setting_read = AAZObjectType()

    security_ml_analytics_setting_read = _schema_security_ml_analytics_setting_read
    security_ml_analytics_setting_read.etag = AAZStrType()
    security_ml_analytics_setting_read.id = AAZStrType(
        flags={"read_only": True},
    )
    security_ml_analytics_setting_read.kind = AAZStrType(
        flags={"required": True},
    )
    security_ml_analytics_setting_read.name = AAZStrType(
        flags={"read_only": True},
    )
    security_ml_analytics_setting_read.system_data = AAZObjectType(
        serialized_name="systemData",
        flags={"read_only": True},
    )
    security_ml_analytics_setting_read.type = AAZStrType(
        flags={"read_only": True},
    )

    system_data = _schema_security_ml_analytics_setting_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},
    )

    disc_anomaly = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly")
    disc_anomaly.properties = AAZObjectType(
        flags={"client_flatten": True},
    )

    properties = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly").properties
    properties.anomaly_settings_version = AAZIntType(
        serialized_name="anomalySettingsVersion",
    )
    properties.anomaly_version = AAZStrType(
        serialized_name="anomalyVersion",
        flags={"required": True},
    )
    properties.description = AAZStrType()
    properties.display_name = AAZStrType(
        serialized_name="displayName",
        flags={"required": True},
    )
    properties.enabled = AAZBoolType(
        flags={"required": True},
    )
    properties.frequency = AAZStrType(
        flags={"required": True},
    )
    properties.is_default_settings = AAZBoolType(
        serialized_name="isDefaultSettings",
        flags={"required": True},
    )
    properties.last_modified_utc = AAZStrType(
        serialized_name="lastModifiedUtc",
        flags={"read_only": True},
    )
    properties.required_data_connectors = AAZListType(
        serialized_name="requiredDataConnectors",
    )
    properties.settings_definition_id = AAZStrType(
        serialized_name="settingsDefinitionId",
    )
    properties.settings_status = AAZStrType(
        serialized_name="settingsStatus",
        flags={"required": True},
    )
    properties.tactics = AAZListType()
    properties.techniques = AAZListType()

    required_data_connectors = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly").properties.required_data_connectors
    required_data_connectors.Element = AAZObjectType()

    _element = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly").properties.required_data_connectors.Element
    _element.connector_id = AAZStrType(
        serialized_name="connectorId",
    )
    _element.data_types = AAZListType(
        serialized_name="dataTypes",
    )

    data_types = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly").properties.required_data_connectors.Element.data_types
    data_types.Element = AAZStrType()

    tactics = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly").properties.tactics
    tactics.Element = AAZStrType()

    techniques = _schema_security_ml_analytics_setting_read.discriminate_by("kind", "Anomaly").properties.techniques
    techniques.Element = AAZStrType()

    _schema.etag = _schema_security_ml_analytics_setting_read.etag
    _schema.id = _schema_security_ml_analytics_setting_read.id
    _schema.kind = _schema_security_ml_analytics_setting_read.kind
    _schema.name = _schema_security_ml_analytics_setting_read.name
    _schema.system_data = _schema_security_ml_analytics_setting_read.system_data
    _schema.type = _schema_security_ml_analytics_setting_read.type
    _schema.discriminate_by(
            "kind",
            "Anomaly",
            _schema_security_ml_analytics_setting_read.discriminate_by(
                "kind",
                "Anomaly",
            )
        )


__all__ = ["Update"]
