# Buggy code
def set_nsg_flow_logging(
    cmd,
    client,
    watcher_rg,
    watcher_name,
    nsg,
    storage_account=None,
    resource_group_name=None,
    enabled=None,
    retention=0,
    log_format=None,
    log_version=None,
    traffic_analytics_workspace=None,
    traffic_analytics_interval=None,
    traffic_analytics_enabled=None,
):
    from azure.cli.core.commands import LongRunningOperation

    config = LongRunningOperation(cmd.cli_ctx)(
        client.get_flow_log_status(watcher_rg, watcher_name, nsg)
    )

    try:
        if not config.flow_analytics_configuration.network_watcher_flow_analytics_configuration.workspace_id:
            config.flow_analytics_configuration = None
    except AttributeError:
        pass

    with cmd.update_context(config) as c:
        c.set_param("enabled", enabled if enabled is not None else config.enabled)
        c.set_param("storage_id", storage_account or config.storage_id)
    if retention is not None:
        config.retention_policy = {"days": retention, "enabled": int(retention) > 0}
    if cmd.supported_api_version(min_api="2018-10-01") and (log_format or log_version):
        config.format = {"type": log_format, "version": log_version}
    if cmd.supported_api_version(min_api="2018-10-01") and any(
        [traffic_analytics_workspace is not None, traffic_analytics_enabled is not None]
    ):
        workspace = None
        if traffic_analytics_workspace:
            from azure.cli.core.commands.arm import get_arm_resource_by_id

            workspace = get_arm_resource_by_id(cmd.cli_ctx, traffic_analytics_workspace)

        if not config.flow_analytics_configuration:
            # must create whole object
            if not workspace:
                raise CLIError(
                    "usage error (analytics not already configured): --workspace NAME_OR_ID "
                    "[--enabled {true|false}]"
                )
            # if not all([workspace, traffic_analytics_interval]):
            # raise CLIError('usage error (analytics not already configured): --workspace NAME_OR_ID '
            #               '--interval INT [--enabled {true|false}]')
            if traffic_analytics_enabled is None:
                traffic_analytics_enabled = True
            config.flow_analytics_configuration = {
                "network_watcher_flow_analytics_configuration": {
                    "enabled": traffic_analytics_enabled,
                    "workspace_id": workspace.properties["customerId"],
                    "workspace_region": workspace.location,
                    "workspace_resource_id": traffic_analytics_workspace,
                    # 'traffic_analytics_interval': traffic_analytics_interval,
                }
            }
        else:
            # pylint: disable=line-too-long
            with cmd.update_context(
                config.flow_analytics_configuration.network_watcher_flow_analytics_configuration
            ) as c:
                # update object
                c.set_param("enabled", traffic_analytics_enabled)
                # c.set_param('traffic_analytics_interval', traffic_analytics_interval)
                if traffic_analytics_workspace == "":
                    config.flow_analytics_configuration = None
                elif workspace:
                    c.set_param("workspace_id", workspace.properties["customerId"])
                    c.set_param("workspace_region", workspace.location)
                    c.set_param("workspace_resource_id", traffic_analytics_workspace)
    return client.set_flow_log_configuration(watcher_rg, watcher_name, config)


# Traceback (msrest.exceptions.ValidationError)
Parameter 'TrafficAnalyticsProperties.network_watcher_flow_analytics_configuration' can not be None.
Traceback (most recent call last):
python3.6/site-packages/knack/cli.py, ln 206, in invoke
cmd_result = self.invocation.execute(args)
cli/core/commands/__init__.py, ln 523, in execute
raise ex
cli/core/commands/__init__.py, ln 581, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
...
python3.6/site-packages/msrest/serialization.py, ln 662, in validate
raise ValidationError("required", name, True)
msrest.exceptions.ValidationError: Parameter 'TrafficAnalyticsProperties.network_watcher_flow_analytics_configuration' can not be None.

# Fixed code
def set_nsg_flow_logging(
    cmd,
    client,
    watcher_rg,
    watcher_name,
    nsg,
    storage_account=None,
    resource_group_name=None,
    enabled=None,
    retention=0,
    log_format=None,
    log_version=None,
    traffic_analytics_workspace=None,
    traffic_analytics_interval=None,
    traffic_analytics_enabled=None,
):
    from azure.cli.core.commands import LongRunningOperation

    config = LongRunningOperation(cmd.cli_ctx)(
        client.get_flow_log_status(watcher_rg, watcher_name, nsg)
    )

    try:
        if not config.flow_analytics_configuration.network_watcher_flow_analytics_configuration.workspace_id:
            config.flow_analytics_configuration = None
    except AttributeError:
        config.flow_analytics_configuration = None

    with cmd.update_context(config) as c:
        c.set_param("enabled", enabled if enabled is not None else config.enabled)
        c.set_param("storage_id", storage_account or config.storage_id)
    if retention is not None:
        config.retention_policy = {"days": retention, "enabled": int(retention) > 0}
    if cmd.supported_api_version(min_api="2018-10-01") and (log_format or log_version):
        config.format = {"type": log_format, "version": log_version}

    if cmd.supported_api_version(min_api="2018-10-01") and any(
        [traffic_analytics_workspace is not None, traffic_analytics_enabled is not None]
    ):
        workspace = None

        if traffic_analytics_workspace:
            from azure.cli.core.commands.arm import get_arm_resource_by_id

            workspace = get_arm_resource_by_id(cmd.cli_ctx, traffic_analytics_workspace)

        if not config.flow_analytics_configuration:
            # must create whole object
            if not workspace:
                raise CLIError(
                    "usage error (analytics not already configured): --workspace NAME_OR_ID "
                    "[--enabled {true|false}]"
                )
            if traffic_analytics_enabled is None:
                traffic_analytics_enabled = True
            config.flow_analytics_configuration = {
                "network_watcher_flow_analytics_configuration": {
                    "enabled": traffic_analytics_enabled,
                    "workspace_id": workspace.properties["customerId"],
                    "workspace_region": workspace.location,
                    "workspace_resource_id": traffic_analytics_workspace,
                }
            }
        else:
            # pylint: disable=line-too-long
            with cmd.update_context(
                config.flow_analytics_configuration.network_watcher_flow_analytics_configuration
            ) as c:
                # update object
                c.set_param("enabled", traffic_analytics_enabled)
                if traffic_analytics_workspace == "":
                    config.flow_analytics_configuration = None
                elif workspace:
                    c.set_param("workspace_id", workspace.properties["customerId"])
                    c.set_param("workspace_region", workspace.location)
                    c.set_param("workspace_resource_id", traffic_analytics_workspace)

    return client.set_flow_log_configuration(watcher_rg, watcher_name, config)