Search in sources :

Example 1 with CacheMetadataLayerObjectTemplate

use of com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate in project jib by google.

the class CacheMetadataTranslator method fromTemplate.

/**
 * Translates {@link CacheMetadataTemplate} to {@link CacheMetadata}.
 */
static CacheMetadata fromTemplate(CacheMetadataTemplate template, Path cacheDirectory) throws CacheMetadataCorruptedException {
    try {
        CacheMetadata cacheMetadata = new CacheMetadata();
        // Converts each layer object in the template to a cache metadata layer.
        for (CacheMetadataLayerObjectTemplate layerObjectTemplate : template.getLayers()) {
            if (layerObjectTemplate.getDigest() == null || layerObjectTemplate.getDiffId() == null) {
                throw new IllegalStateException("Cannot translate cache metadata layer without a digest or diffId");
            }
            Path layerContentFile = CacheFiles.getLayerFile(cacheDirectory, layerObjectTemplate.getDigest());
            // Gets the properties for a layer. Properties only exist for application layers.
            CacheMetadataLayerPropertiesObjectTemplate propertiesObjectTemplate = layerObjectTemplate.getProperties();
            // Constructs the cache metadata layer from a cached layer and layer metadata.
            LayerMetadata layerMetadata = null;
            if (propertiesObjectTemplate != null) {
                layerMetadata = new LayerMetadata(propertiesObjectTemplate.getSourceFiles(), propertiesObjectTemplate.getLastModifiedTime());
            }
            CachedLayer cachedLayer = new CachedLayer(layerContentFile, new BlobDescriptor(layerObjectTemplate.getSize(), layerObjectTemplate.getDigest()), layerObjectTemplate.getDiffId());
            CachedLayerWithMetadata cachedLayerWithMetadata = new CachedLayerWithMetadata(cachedLayer, layerMetadata);
            cacheMetadata.addLayer(cachedLayerWithMetadata);
        }
        return cacheMetadata;
    } catch (LayerPropertyNotFoundException ex) {
        throw new CacheMetadataCorruptedException(ex);
    }
}
Also used : Path(java.nio.file.Path) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) LayerPropertyNotFoundException(com.google.cloud.tools.jib.image.LayerPropertyNotFoundException) CacheMetadataLayerObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate) CacheMetadataLayerPropertiesObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)

Example 2 with CacheMetadataLayerObjectTemplate

use of com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate in project jib by google.

the class CacheMetadataTranslator method toTemplate.

/**
 * Translates {@link CacheMetadata} to {@link CacheMetadataTemplate}.
 */
static CacheMetadataTemplate toTemplate(CacheMetadata cacheMetadata) {
    CacheMetadataTemplate template = new CacheMetadataTemplate();
    for (CachedLayerWithMetadata cachedLayerWithMetadata : cacheMetadata.getLayers()) {
        CacheMetadataLayerObjectTemplate layerObjectTemplate = new CacheMetadataLayerObjectTemplate().setSize(cachedLayerWithMetadata.getBlobDescriptor().getSize()).setDigest(cachedLayerWithMetadata.getBlobDescriptor().getDigest()).setDiffId(cachedLayerWithMetadata.getDiffId());
        if (cachedLayerWithMetadata.getMetadata() != null) {
            layerObjectTemplate.setProperties(new CacheMetadataLayerPropertiesObjectTemplate().setSourceFiles(cachedLayerWithMetadata.getMetadata().getSourceFiles()).setLastModifiedTime(cachedLayerWithMetadata.getMetadata().getLastModifiedTime()));
        }
        template.addLayer(layerObjectTemplate);
    }
    return template;
}
Also used : CacheMetadataTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate) CacheMetadataLayerObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate) CacheMetadataLayerPropertiesObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)

Aggregations

CacheMetadataLayerObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate)2 CacheMetadataLayerPropertiesObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)2 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)1 CacheMetadataTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate)1 LayerPropertyNotFoundException (com.google.cloud.tools.jib.image.LayerPropertyNotFoundException)1 Path (java.nio.file.Path)1