{%- set ns = 'com.canonical.certification::' -%}
{%- set state = manager.default_device_context.state -%}
{%- set resource_map = state.resource_map -%}
{%- set job_state_map = state.job_state_map -%}
{
{%- if ns ~ 'package' in state.resource_map %}
    "packages": [
    {%- set package_resource_list = state.resource_map[ns ~ 'package'] %}
    {%- for package_resource in package_resource_list if package_resource.name %}
        {
            "name": "{{ package_resource.name }}",
            "version": "{{ package_resource.version }}"
        }{%- if not loop.last -%},{%- endif %}
    {%- endfor %}
    ],
{%- endif %}
{%- if ns ~ 'snap' in state.resource_map %}
    "snap-packages": [
    {%- set snap_package_resource_list = state.resource_map[ns ~ 'snap'] %}
    {%- for snap_package_resource in snap_package_resource_list if snap_package_resource.name %}
        {
            "name": "{{ snap_package_resource.name }}",
            {%- for key in snap_package_resource|reject('is_name')|sort %}
            "{{ key }}": "{{ snap_package_resource[key] }}"{%- if not loop.last -%},{%- endif %}
            {%- endfor %}
        }{%- if not loop.last -%},{%- endif %}
    {%- endfor %}
    ],
{%- endif %}
{%- if ns ~ 'lsb' in state.resource_map and state.resource_map[ns ~ 'lsb'][0] %}
{%- set lsb_resource = state.resource_map[ns ~ 'lsb'][0] %}
    "distribution": {
        {%- for key in lsb_resource|sort %}
        "{{ key }}": "{{ lsb_resource[key] }}"{%- if not loop.last -%},{%- endif %}
        {%- endfor %}
    },
{%- endif %}
    "results": [
    {%- for job_id, job_state in job_state_map|dictsort if job_state.result.outcome != None and job_state.job.plugin not in ("resource", "attachment") %}
        {
            "id": "{{ job_id|strip_ns }}",
            "name": "{{ job_state.job.tr_summary() }}",
            "certification_status": "{{ job_state.effective_certification_status }}",
            "category": "{{ job_state.effective_category_id }}",
            "status": "{{ job_state.result.outcome_meta().hexr_mapping }}",
            "comments": {{ job_state.result.comments | jsonify | safe }},
            "io_log": {{ job_state.result.io_log_as_flat_text | jsonify | safe }},
            "type": "test",
            "project": "certification"
        }{%- if not loop.last -%},{%- endif %}
    {%- endfor %}
    ]
{%- if ns ~ 'dkms_info_json' in state.job_state_map and state.job_state_map[ns ~ 'dkms_info_json'].result.outcome %},
{%- set dkms_info_json = state.job_state_map[ns ~ 'dkms_info_json'].result.io_log_as_text_attachment %}
    "dkms_info": {{ dkms_info_json | indent(4, false) | safe }}
{%- endif %}
{%- if ns ~ 'udev_json' in state.job_state_map and state.job_state_map[ns ~ 'udev_json'].result.outcome %},
{%- set udev_json = state.job_state_map[ns ~ 'udev_json'].result.io_log_as_text_attachment %}
    "devices": {{ udev_json | indent(4, false) | safe }}
{%- endif %}
{%- if ns ~ 'raw_devices_dmi_json' in state.job_state_map and state.job_state_map[ns ~ 'raw_devices_dmi_json'].result.outcome %},
{%- set raw_devices_dmi_json = state.job_state_map[ns ~ 'raw_devices_dmi_json'].result.io_log_as_text_attachment %}
    "raw-devices-dmi": {{ raw_devices_dmi_json | indent(4, false) | safe }}
{%- endif %}
{%- if ns ~ 'modprobe_json' in state.job_state_map and state.job_state_map[ns ~ 'modprobe_json'].result.outcome %},
{%- set modprobe_json = state.job_state_map[ns ~ 'modprobe_json'].result.io_log_as_text_attachment %}
    "modprobe-info": {{ modprobe_json | indent(4, false) | safe }}
{%- endif %}
{%- if ns ~ 'lspci_standard_config_json' in state.job_state_map and state.job_state_map[ns ~ 'lspci_standard_config_json'].result.outcome %},
{%- set lspci_standard_config_json = state.job_state_map[ns ~ 'lspci_standard_config_json'].result.io_log_as_text_attachment %}
    "pci_subsystem_id": {{ lspci_standard_config_json | indent(4, false) | safe }}
{%- endif %}
{%- if ns ~ 'uname' in state.resource_map and state.resource_map[ns ~ 'uname'][0] %},
{%- set uname_resource = state.resource_map[ns ~ 'uname'][0] %}
    "kernel": "{{ uname_resource.release }}"
{%- endif %}
{%- if ns ~ 'dpkg' in state.resource_map and state.resource_map[ns ~ 'dpkg'][0] %},
{%- set dpkg_resource = state.resource_map[ns ~ 'dpkg'][0] %}
    "architecture": "{{ dpkg_resource.architecture }}"
{%- endif %}
{%- if ns ~ 'meminfo' in state.resource_map and state.resource_map[ns ~ 'meminfo'][0] %},
{%- set meminfo = state.resource_map[ns ~ 'meminfo'][0] %}
    "memory": {
        "swap": {{ meminfo.swap }},
        "total": {{ meminfo.total }}
    }
{%- endif %}
{%- if ns ~ 'cpuinfo' in state.resource_map and state.resource_map[ns ~ 'cpuinfo'][0] %},
{%- set processor_resource = state.resource_map[ns ~ 'cpuinfo'][0] %}
    "processor": {
    {%- for key in processor_resource|sort %}
        "{{ key }}": "{{ processor_resource[key] }}"{%- if not loop.last -%},{%- endif %}
    {%- endfor %}
    }
{%- endif %}
{%- if ns ~ 'kernel_cmdline_attachment' in state.job_state_map and state.job_state_map[ns ~ 'kernel_cmdline_attachment'].result.outcome %},
{%- set kernel_cmdline = state.job_state_map[ns ~ 'kernel_cmdline_attachment'].result.io_log_as_text_attachment %}
    "kernel-cmdline": {{ kernel_cmdline.strip() | jsonify | safe }}
{%- endif %}
{%- if ns ~ 'dell_bto_xml_attachment_json' in state.job_state_map and state.job_state_map[ns ~ 'dell_bto_xml_attachment_json'].result.outcome == 'pass' %},
{%- set bto = state.job_state_map[ns ~ 'dell_bto_xml_attachment_json'].result.io_log_as_text_attachment %}
    "bto-info": {{ bto | indent(8, false) | safe }}
{%- endif %}
{%- if ns ~ 'recovery_info_attachment_json' in state.job_state_map and state.job_state_map[ns ~ 'recovery_info_attachment_json'].result.outcome == 'pass' %},
{%- set recovery = state.job_state_map[ns ~ 'recovery_info_attachment_json'].result.io_log_as_text_attachment %}
    "image-version": {{ recovery | indent(8, false) | safe }}
{%- endif %}
{%- if ns ~ 'info/buildstamp' in state.job_state_map and state.job_state_map[ns ~ 'info/buildstamp'].result.outcome %},
{%- set buildstamp = state.job_state_map[ns ~ 'info/buildstamp'].result.io_log_as_text_attachment.rstrip().splitlines() %}
{%- if buildstamp|length == 2 %}
    "buildstamp": {{ buildstamp[1] | jsonify | safe }}
{%- endif %}
{%- endif %}
}
