.. versionadded:: 2019.2.0

    Ensure a resource group exists.

    :param name:
        Name of the resource group.

    :param location:
        The Azure location in which to create the resource group. This value cannot be updated once
        the resource group is created.

    :param managed_by:
        The ID of the resource that manages this resource group. This value cannot be updated once
        the resource group is created.

    :param tags:
        A dictionary of strings can be passed as tag metadata to the resource group object.

    :param connection_auth:
        A dict with subscription and authentication parameters to be used in connecting to the
        Azure Resource Manager API.

    Example usage:

    .. code-block:: yaml

        Ensure resource group exists:
            azurearm_resource.resource_group_present:
                - name: group1
                - location: eastus
                - tags:
                    contact_name: Elmer Fudd Gantry
                - connection_auth: {{ profile }}

def resource_group_present(name, location, managed_by=None, tags=None, connection_auth=None, **kwargs):
    '''
    .. versionadded:: 2019.2.0

    Ensure a resource group exists.

    :param name:
        Name of the resource group.

    :param location:
        The Azure location in which to create the resource group. This value cannot be updated once
        the resource group is created.

    :param managed_by:
        The ID of the resource that manages this resource group. This value cannot be updated once
        the resource group is created.

    :param tags:
        A dictionary of strings can be passed as tag metadata to the resource group object.

    :param connection_auth:
        A dict with subscription and authentication parameters to be used in connecting to the
        Azure Resource Manager API.

    Example usage:

    .. code-block:: yaml

        Ensure resource group exists:
            azurearm_resource.resource_group_present:
                - name: group1
                - location: eastus
                - tags:
                    contact_name: Elmer Fudd Gantry
                - connection_auth: {{ profile }}

    '''
    ret = {
        'name': name,
        'result': False,
        'comment': '',
        'changes': {}
    }

    if not isinstance(connection_auth, dict):
        ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'
        return ret

    group = {}

    present = __salt__['azurearm_resource.resource_group_check_existence'](name, **connection_auth)

    if present:
        group = __salt__['azurearm_resource.resource_group_get'](name, **connection_auth)
        ret['changes'] = __utils__['dictdiffer.deep_diff'](group.get('tags', {}), tags or {})

        if not ret['changes']:
            ret['result'] = True
            ret['comment'] = 'Resource group {0} is already present.'.format(name)
            return ret

        if __opts__['test']:
            ret['comment'] = 'Resource group {0} tags would be updated.'.format(name)
            ret['result'] = None
            ret['changes'] = {
                'old': group.get('tags', {}),
                'new': tags
            }
            return ret

    elif __opts__['test']:
        ret['comment'] = 'Resource group {0} would be created.'.format(name)
        ret['result'] = None
        ret['changes'] = {
            'old': {},
            'new': {
                'name': name,
                'location': location,
                'managed_by': managed_by,
                'tags': tags,
            }
        }
        return ret

    group_kwargs = kwargs.copy()
    group_kwargs.update(connection_auth)

    group = __salt__['azurearm_resource.resource_group_create_or_update'](
        name,
        location,
        managed_by=managed_by,
        tags=tags,
        **group_kwargs
    )
    present = __salt__['azurearm_resource.resource_group_check_existence'](name, **connection_auth)

    if present:
        ret['result'] = True
        ret['comment'] = 'Resource group {0} has been created.'.format(name)
        ret['changes'] = {
            'old': {},
            'new': group
        }
        return ret

    ret['comment'] = 'Failed to create resource group {0}! ({1})'.format(name, group.get('error'))
    return ret