'Calls out to Glance for data and writes data.'

def download(self, context, image_id, data=None, dst_path=None):

    if (CONF.glance.allowed_direct_url_schemes and (dst_path is not None)):
        image = self.show(context, image_id, include_locations=True)
        for entry in image.get('locations', []):
            loc_url = entry['url']
            loc_meta = entry['metadata']
            o = urlparse.urlparse(loc_url)
            xfer_mod = self._get_transfer_module(o.scheme)
            if xfer_mod:
                try:
                    xfer_mod.download(context, o, dst_path, loc_meta)
                    LOG.info('Successfully    transferred    using    %s', o.scheme)
                    return
                except Exception:
                    LOG.exception('Download    image    error')
    try:
        image_chunks = self._client.call(context, 2, 'data', image_id)
    except Exception:
        _reraise_translated_image_exception(image_id)
    verifier = None
    if CONF.glance.verify_glance_signatures:
        image_meta_dict = self.show(context, image_id, include_locations=False)
        image_meta = objects.ImageMeta.from_dict(image_meta_dict)
        img_signature = image_meta.properties.get('img_signature')
        img_sig_hash_method = image_meta.properties.get('img_signature_hash_method')
        img_sig_cert_uuid = image_meta.properties.get('img_signature_certificate_uuid')
        img_sig_key_type = image_meta.properties.get('img_signature_key_type')
        try:
            verifier = signature_utils.get_verifier(context=context, img_signature_certificate_uuid=img_sig_cert_uuid, img_signature_hash_method=img_sig_hash_method, img_signature=img_signature, img_signature_key_type=img_sig_key_type)
        except cursive_exception.SignatureVerificationError:
            with excutils.save_and_reraise_exception():
                LOG.error('Image    signature    verification    failed    for    image:    %s', image_id)
    close_file = False
    if ((data is None) and dst_path):
        data = open(dst_path, 'wb')
        close_file = True
    if (data is None):
        if verifier:
            try:
                for chunk in image_chunks:
                    verifier.update(chunk)
                verifier.verify()
                LOG.info('Image    signature    verification    succeeded    for    image:    %s', image_id)
            except cryptography.exceptions.InvalidSignature:
                with excutils.save_and_reraise_exception():
                    LOG.error('Image    signature    verification    failed    for    image:    %s', image_id)
        return image_chunks
    else:
        try:
            for chunk in image_chunks:
                if verifier:
                    verifier.update(chunk)
                data.write(chunk)
            if verifier:
                verifier.verify()
                LOG.info('Image    signature    verification    succeeded    for    image    %s', image_id)
        except cryptography.exceptions.InvalidSignature:
            data.truncate(0)
            with excutils.save_and_reraise_exception():
                LOG.error('Image    signature    verification    failed    for    image:    %s', image_id)
        except Exception as ex:
            with excutils.save_and_reraise_exception():
                LOG.error('Error    writing    to    %(path)s:    %(exception)s', {'path': dst_path, 'exception': ex})
        finally:
            if close_file:
                data.flush()
                os.fsync(data.fileno())
                data.close()
