Device is monitored with Server Density.

    name
        Device name in Server Density.

    salt_name
        If ``True`` (default), takes the name from the ``id`` grain. If
        ``False``, the provided name is used.

    group
        Group name under with device will appear in Server Density dashboard.
        Default - `None`.

    agent_version
        The agent version you want to use. Valid values are 1 or 2.
        Default - 1.

    salt_params
        If ``True`` (default), needed config parameters will be sourced from
        grains and from :mod:`status.all_status
        <salt.modules.status.all_status>`.

    params
        Add parameters that you want to appear in the Server Density dashboard.
        Will overwrite the `salt_params` parameters. For more info, see the
        `API docs`__.

    .. __: https://apidocs.serverdensity.com/Inventory/Devices/Creating

    Usage example:

    .. code-block:: yaml

        'server_name':
          serverdensity_device.monitored

    .. code-block:: yaml

        'server_name':
          serverdensity_device.monitored:
            - group: web-servers

    .. code-block:: yaml

        'my_special_server':
          serverdensity_device.monitored:
            - salt_name: False
            - group: web-servers
            - cpuCores: 2
            - os: '{"code": "linux", "name": "Linux"}'

def monitored(name, group=None, salt_name=True, salt_params=True, agent_version=1, **params):
    '''
    Device is monitored with Server Density.

    name
        Device name in Server Density.

    salt_name
        If ``True`` (default), takes the name from the ``id`` grain. If
        ``False``, the provided name is used.

    group
        Group name under with device will appear in Server Density dashboard.
        Default - `None`.

    agent_version
        The agent version you want to use. Valid values are 1 or 2.
        Default - 1.

    salt_params
        If ``True`` (default), needed config parameters will be sourced from
        grains and from :mod:`status.all_status
        <salt.modules.status.all_status>`.

    params
        Add parameters that you want to appear in the Server Density dashboard.
        Will overwrite the `salt_params` parameters. For more info, see the
        `API docs`__.

    .. __: https://apidocs.serverdensity.com/Inventory/Devices/Creating

    Usage example:

    .. code-block:: yaml

        'server_name':
          serverdensity_device.monitored

    .. code-block:: yaml

        'server_name':
          serverdensity_device.monitored:
            - group: web-servers

    .. code-block:: yaml

        'my_special_server':
          serverdensity_device.monitored:
            - salt_name: False
            - group: web-servers
            - cpuCores: 2
            - os: '{"code": "linux", "name": "Linux"}'
    '''
    ret = {'name': name, 'changes': {}, 'result': None, 'comment': ''}
    params_from_salt = _get_salt_params()

    if salt_name:
        name = params_from_salt.pop('name')
        ret['name'] = name
    else:
        params_from_salt.pop('name')

    if group:
        params['group'] = group

    if agent_version != 2:
        # Anything different from 2 will fallback into the v1.
        agent_version = 1

    # override salt_params with given params
    if salt_params:
        for key, value in six.iteritems(params):
            params_from_salt[key] = value
        params_to_use = params_from_salt
    else:
        params_to_use = params

    device_in_sd = True if __salt__['serverdensity_device.ls'](name=name) else False
    sd_agent_installed = True if 'sd-agent' in __salt__['pkg.list_pkgs']() else False

    if device_in_sd and sd_agent_installed:
        ret['result'] = True
        ret['comment'] = 'Such server name already exists in this Server Density account. And sd-agent is installed'
        ret['changes'] = {}
        return ret

    if __opts__['test']:
        if not device_in_sd or not sd_agent_installed:
            ret['result'] = None
            ret['comment'] = 'Server Density agent is set to be installed and/or device created in the Server Density DB'
            return ret
        else:
            ret['result'] = None
            ret['comment'] = 'Server Density agent is already installed, or device already exists'
            return ret

    elif device_in_sd:
        device = __salt__['serverdensity_device.ls'](name=name)[0]
        agent_key = device['agentKey']
        ret['comment'] = 'Device was already in Server Density db.'

    if not device_in_sd:
        device = __salt__['serverdensity_device.create'](name, **params_from_salt)
        agent_key = device['agentKey']
        ret['comment'] = 'Device created in Server Density db.'
        ret['changes'] = {'device_created': device}

    else:
        ret['result'] = False
        ret['comment'] = 'Failed to create device in Server Density DB and this device does not exist in db either.'
        ret['changes'] = {}

    installed_agent = __salt__['serverdensity_device.install_agent'](agent_key, agent_version)

    ret['result'] = True
    ret['comment'] = 'Successfully installed agent and created device in Server Density db.'
    ret['changes'] = {'created_device': device, 'installed_agent': installed_agent}
    return ret