<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def snapshot(gandi, name, resource, background):
 """ Create a snapshot on the fly. """

    name = name or randomstring('snp')

    source_info = gandi.disk.info(resource)
    datacenter = source_info['datacenter_id']
    result = gandi.disk.create(name, None, None, None, datacenter, resource,
                               'snapshot', background)

    if background:
        gandi.pretty_echo(result)
    return result