package com.dtolabs.rundeck.core.storage;

import com.dtolabs.rundeck.plugins.storage.StorageConverterPlugin;
import org.rundeck.storage.api.HasInputStream;
import org.rundeck.storage.api.Path;
import org.rundeck.storage.api.StorageException;

import java.util.HashMap;

/**
 * Adapter for a {@link com.dtolabs.rundeck.plugins.storage.StorageConverterPlugin} to use as a {@link
 * StorageConverter}
 */
public class StorageConverterPluginAdapter implements StorageConverter {
    StorageConverterPlugin plugin;
    String providerName;

    public StorageConverterPluginAdapter(String providerName,StorageConverterPlugin plugin) {
        this.providerName=providerName;
        this.plugin = plugin;
    }

    static enum Operation {READ, UPDATE, CREATE}

    /**
     * perform appropriate plugin filter method based on the operation enacted
     *
     * @param path         path
     * @param resourceMeta resource
     * @param op           operation
     *
     * @return new resource
     */
    private ResourceMeta filter(Path path, ResourceMeta resourceMeta, Operation op) {
        ResourceMetaBuilder resourceMetaBuilder = StorageUtil.create(new HashMap<String,
                String>(resourceMeta.getMeta()));
        final HasInputStream result;
        switch (op) {
            case READ:
            case UPDATE:
            case CREATE:
                try {
                    if (op == Operation.CREATE) {
                        result = plugin.createResource(path, resourceMetaBuilder, resourceMeta);
                    } else if (op == Operation.READ) {
                        result = plugin.readResource(path, resourceMetaBuilder, resourceMeta);
                    } else {
                        result = plugin.updateResource(path, resourceMetaBuilder, resourceMeta);
                    }
                } catch (Throwable e) {
                    throw new StorageException("Converter Plugin " + providerName + " threw exception during " + op +
                            ": " + e.getMessage(), e, StorageException.Event.valueOf(op.toString()), path);
                }
                break;
            default:
                throw new IllegalStateException();
        }
        //construct the new data
        return StorageUtil.withStream(null == result ? resourceMeta : result, resourceMetaBuilder.getResourceMeta());
    }

    @Override
    public ResourceMeta convertReadData(Path path, ResourceMeta resourceMeta) {
        return filter(path, resourceMeta, Operation.READ);
    }

    @Override
    public ResourceMeta convertCreateData(Path path, ResourceMeta resourceMeta) {
        return filter(path, resourceMeta, Operation.CREATE);
    }

    @Override
    public ResourceMeta convertUpdateData(Path path, ResourceMeta resourceMeta) {
        return filter(path, resourceMeta, Operation.UPDATE);
    }
}

@@ -103,16 +103,48 @@
 Plugin;%0A
+import org.apache.log4j.Logger;%0A
 import o
@@ -501,16 +501,104 @@
 erter %7B%0A
+    static final Logger logger = Logger.getLogger(StorageConverterPluginAdapter.class);%0A
     Stor
@@ -2318,32 +2318,230 @@
 on();%0A        %7D%0A
+        logger.debug(%0A                %22Plugin(%22 + providerName + %22):%22 + op + %22:%22 + path +%0A                %22;%22 + (null == result ? %22_%22 : %22+%22) + %22:%22 + resourceMetaBuilder.getResourceMeta()%0A        );%0A
         //constr

Debug logging for storage converter plugins