Search in sources :

Example 1 with ComponentRecipe

use of com.amazon.aws.iot.greengrass.component.common.ComponentRecipe in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentService method copyRecipeFileToComponentStore.

/**
 * Copy the given recipe file to local component store.
 *
 * @param componentStore ComponentStore instance
 * @param recipePath path to the recipe file
 * @param logger Logger instance
 * @return ComponentRecipe file content
 * @throws IOException on I/O error
 */
@SuppressWarnings("PMD.ExceptionAsFlowControl")
public static ComponentRecipe copyRecipeFileToComponentStore(ComponentStore componentStore, Path recipePath, Logger logger) throws IOException {
    String ext = Utils.extension(recipePath.toString());
    ComponentRecipe recipe = null;
    // the file when writing it into the local recipe store.
    try {
        if (recipePath.toFile().length() > 0) {
            switch(ext.toLowerCase()) {
                case "yaml":
                case "yml":
                    recipe = getRecipeSerializer().readValue(recipePath.toFile(), ComponentRecipe.class);
                    break;
                case "json":
                    recipe = getRecipeSerializerJson().readValue(recipePath.toFile(), ComponentRecipe.class);
                    break;
                default:
                    break;
            }
        }
    } catch (IOException e) {
        // This is to fail fast while providing actionable feedback.
        throw new IOException(String.format("Unable to parse %s as a recipe due to: %s", recipePath.toString(), e.getMessage()), e);
    }
    if (recipe == null) {
        logger.atError().log("Skipping file {} because it was not recognized as a recipe", recipePath);
        return null;
    }
    // Write the recipe as YAML with the proper filename into the store
    ComponentIdentifier componentIdentifier = new ComponentIdentifier(recipe.getComponentName(), recipe.getComponentVersion());
    try {
        componentStore.savePackageRecipe(componentIdentifier, getRecipeSerializer().writeValueAsString(recipe));
    } catch (PackageLoadingException e) {
        // This is to fail fast while providing actionable feedback.
        throw new IOException(String.format("Unable to copy recipe for '%s' to component store due to: %s", componentIdentifier.toString(), e.getMessage()), e);
    }
    return recipe;
}
Also used : ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) ComponentRecipe(com.amazon.aws.iot.greengrass.component.common.ComponentRecipe) IOException(java.io.IOException) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)

Aggregations

ComponentRecipe (com.amazon.aws.iot.greengrass.component.common.ComponentRecipe)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)1 IOException (java.io.IOException)1