.. versionadded:: 2019.2.0

    Ensure a route exists within a route table.

    :param name:
        Name of the route.

    :param address_prefix:
        The destination CIDR to which the route applies.

    :param next_hop_type:
        The type of Azure hop the packet should be sent to. Possible values are: 'VirtualNetworkGateway', 'VnetLocal',
        'Internet', 'VirtualAppliance', and 'None'.

    :param next_hop_ip_address:
        The IP address packets should be forwarded to. Next hop values are only allowed in routes where the next hop
        type is 'VirtualAppliance'.

    :param route_table:
        The name of the existing route table which will contain the route.

    :param resource_group:
        The resource group assigned to the route table.

    :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 route exists:
            azurearm_network.route_present:
                - name: rt1_route2
                - route_table: rt1
                - resource_group: group1
                - address_prefix: '192.168.0.0/16'
                - next_hop_type: vnetlocal
                - connection_auth: {{ profile }}
                - require:
                  - azurearm_network: Ensure route table exists

def route_present(name, address_prefix, next_hop_type, route_table, resource_group, next_hop_ip_address=None,
                  connection_auth=None, **kwargs):
    '''
    .. versionadded:: 2019.2.0

    Ensure a route exists within a route table.

    :param name:
        Name of the route.

    :param address_prefix:
        The destination CIDR to which the route applies.

    :param next_hop_type:
        The type of Azure hop the packet should be sent to. Possible values are: 'VirtualNetworkGateway', 'VnetLocal',
        'Internet', 'VirtualAppliance', and 'None'.

    :param next_hop_ip_address:
        The IP address packets should be forwarded to. Next hop values are only allowed in routes where the next hop
        type is 'VirtualAppliance'.

    :param route_table:
        The name of the existing route table which will contain the route.

    :param resource_group:
        The resource group assigned to the route table.

    :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 route exists:
            azurearm_network.route_present:
                - name: rt1_route2
                - route_table: rt1
                - resource_group: group1
                - address_prefix: '192.168.0.0/16'
                - next_hop_type: vnetlocal
                - connection_auth: {{ profile }}
                - require:
                  - azurearm_network: Ensure route table exists

    '''
    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

    route = __salt__['azurearm_network.route_get'](
        name,
        route_table,
        resource_group,
        azurearm_log_level='info',
        **connection_auth
    )

    if 'error' not in route:
        if address_prefix != route.get('address_prefix'):
            ret['changes']['address_prefix'] = {
                'old': route.get('address_prefix'),
                'new': address_prefix
            }

        if next_hop_type.lower() != route.get('next_hop_type', '').lower():
            ret['changes']['next_hop_type'] = {
                'old': route.get('next_hop_type'),
                'new': next_hop_type
            }

        if next_hop_type.lower() == 'virtualappliance' and next_hop_ip_address != route.get('next_hop_ip_address'):
            ret['changes']['next_hop_ip_address'] = {
                'old': route.get('next_hop_ip_address'),
                'new': next_hop_ip_address
            }

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

        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = 'Route {0} would be updated.'.format(name)
            return ret

    else:
        ret['changes'] = {
            'old': {},
            'new': {
                'name': name,
                'address_prefix': address_prefix,
                'next_hop_type': next_hop_type,
                'next_hop_ip_address': next_hop_ip_address
            }
        }

    if __opts__['test']:
        ret['comment'] = 'Route {0} would be created.'.format(name)
        ret['result'] = None
        return ret

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

    route = __salt__['azurearm_network.route_create_or_update'](
        name=name,
        route_table=route_table,
        resource_group=resource_group,
        address_prefix=address_prefix,
        next_hop_type=next_hop_type,
        next_hop_ip_address=next_hop_ip_address,
        **route_kwargs
    )

    if 'error' not in route:
        ret['result'] = True
        ret['comment'] = 'Route {0} has been created.'.format(name)
        return ret

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