Search in sources :

Example 11 with CustomServicesPrimitiveRestRep

use of com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep in project coprhd-controller by CoprHD.

the class CustomServicesPrimitiveMapper method map.

public static CustomServicesPrimitiveRestRep map(CustomServicesPrimitiveType from) {
    final CustomServicesPrimitiveRestRep to = new CustomServicesPrimitiveRestRep();
    mapDataObjectFields(from.asModelObject(), to);
    mapPrimitiveFields(from, to);
    try {
        to.setResource(makeResourceLink(from));
    } catch (final URISyntaxException e) {
        throw InternalServerErrorException.internalServerErrors.genericApisvcError("Making prmitive resource link failed", e);
    }
    return to;
}
Also used : CustomServicesPrimitiveRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep) URISyntaxException(java.net.URISyntaxException)

Example 12 with CustomServicesPrimitiveRestRep

use of com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep in project coprhd-controller by CoprHD.

the class WorkflowBuilder method createLocalAnsiblePrimitive.

private static void createLocalAnsiblePrimitive(@Valid final LocalAnsiblePrimitiveForm localAnsible) {
    try {
        final CustomServicesPrimitiveResourceRestRep primitiveResourceRestRep;
        if (localAnsible.isExisting()) {
            primitiveResourceRestRep = getCatalogClient().customServicesPrimitives().getPrimitiveResource(new URI(localAnsible.getExistingResource()));
        } else if (null != localAnsible.getAnsiblePackage()) {
            // upload ansible package
            primitiveResourceRestRep = getCatalogClient().customServicesPrimitives().createPrimitiveResource("ANSIBLE", localAnsible.getAnsiblePackage(), localAnsible.getAnsiblePackageName());
        } else {
            throw new Exception("Error while uploading/retrieving primitive resource");
        }
        if (null != primitiveResourceRestRep) {
            try {
                // Upload ansible inventory files
                uploadInventoryFiles(primitiveResourceRestRep.getId(), localAnsible.getInventoryFiles());
            } catch (final Exception e1) {
                if (primitiveResourceRestRep != null) {
                    // Resource was created but primitive creation failed
                    // Resource was created but inventory creation failed. Remove inventory that was created (if any)
                    updateInventoryFiles(primitiveResourceRestRep.getId(), "");
                    getCatalogClient().customServicesPrimitives().deletePrimitiveResource(primitiveResourceRestRep.getId());
                }
                throw e1;
            }
            // Create Primitive
            final CustomServicesPrimitiveCreateParam primitiveCreateParam = new CustomServicesPrimitiveCreateParam();
            primitiveCreateParam.setType(StepType.LOCAL_ANSIBLE.toString());
            primitiveCreateParam.setName(localAnsible.getName());
            primitiveCreateParam.setDescription(localAnsible.getDescription());
            primitiveCreateParam.setFriendlyName(localAnsible.getName());
            primitiveCreateParam.setResource(primitiveResourceRestRep.getId());
            primitiveCreateParam.setAttributes(new HashMap<String, String>());
            primitiveCreateParam.getAttributes().put(CustomServicesConstants.ANSIBLE_PLAYBOOK, localAnsible.getAnsiblePlaybook());
            final ImmutableMap.Builder<String, InputCreateList> builder = ImmutableMap.<String, InputCreateList>builder();
            // Add Input Groups
            addInputs(localAnsible.getInputs(), builder, CustomServicesConstants.INPUT_PARAMS);
            primitiveCreateParam.setInput(builder.build());
            if (StringUtils.isNotEmpty(localAnsible.getOutputs())) {
                primitiveCreateParam.setOutput(getListFromInputOutputString(localAnsible.getOutputs()));
            }
            final CustomServicesPrimitiveRestRep primitiveRestRep;
            try {
                primitiveRestRep = getCatalogClient().customServicesPrimitives().createPrimitive(primitiveCreateParam);
            } catch (final Exception e1) {
                if (primitiveResourceRestRep != null) {
                    // Resource was created but primitive creation failed
                    // Resource was created but primitive creation failed. Remove inventory that was created (if any)
                    updateInventoryFiles(primitiveResourceRestRep.getId(), "");
                    getCatalogClient().customServicesPrimitives().deletePrimitiveResource(primitiveResourceRestRep.getId());
                }
                throw e1;
            }
            if (primitiveRestRep != null) {
                // add this to wf directory
                addResourceToWFDirectory(primitiveRestRep.getId(), localAnsible.getWfDirID());
            } else {
                flash.error("Error while creating primitive");
            }
            flash.success(MessagesUtils.get("wfBuilder.operation.save.success"));
        } else {
            flash.error("Error while uploading primitive resource");
        }
    } catch (final Exception e) {
        Logger.error(e.getMessage());
        flash.error(e.getMessage());
    }
}
Also used : CustomServicesPrimitiveResourceRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep) CustomServicesPrimitiveCreateParam(com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam) CustomServicesPrimitiveRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep) InputCreateList(com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam.InputCreateList) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 13 with CustomServicesPrimitiveRestRep

use of com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep in project coprhd-controller by CoprHD.

the class WorkflowBuilder method createShellScriptPrimitive.

private static void createShellScriptPrimitive(final ShellScriptPrimitiveForm shellPrimitive) {
    try {
        final String filename = FilenameUtils.getBaseName(shellPrimitive.getScript().getName());
        final CustomServicesPrimitiveResourceRestRep primitiveResourceRestRep = getCatalogClient().customServicesPrimitives().createPrimitiveResource("SCRIPT", shellPrimitive.getScript(), filename);
        if (null != primitiveResourceRestRep) {
            final CustomServicesPrimitiveCreateParam primitiveCreateParam = new CustomServicesPrimitiveCreateParam();
            primitiveCreateParam.setType(StepType.SHELL_SCRIPT.toString());
            primitiveCreateParam.setName(shellPrimitive.getName());
            primitiveCreateParam.setFriendlyName(shellPrimitive.getName());
            primitiveCreateParam.setDescription(shellPrimitive.getDescription());
            primitiveCreateParam.setResource(primitiveResourceRestRep.getId());
            if (StringUtils.isNotEmpty(shellPrimitive.getInputs())) {
                final List<String> list = getListFromInputOutputString(shellPrimitive.getInputs());
                final InputCreateList input = new InputCreateList();
                input.setInput(list);
                final ImmutableMap.Builder<String, InputCreateList> builder = ImmutableMap.<String, InputCreateList>builder().put(CustomServicesConstants.INPUT_PARAMS, input);
                primitiveCreateParam.setInput(builder.build());
            }
            if (StringUtils.isNotEmpty(shellPrimitive.getOutputs())) {
                primitiveCreateParam.setOutput(getListFromInputOutputString(shellPrimitive.getOutputs()));
            }
            final CustomServicesPrimitiveRestRep primitiveRestRep;
            try {
                primitiveRestRep = getCatalogClient().customServicesPrimitives().createPrimitive(primitiveCreateParam);
            } catch (final Exception e1) {
                if (primitiveResourceRestRep != null) {
                    // Resource was created but primitive creation failed
                    getCatalogClient().customServicesPrimitives().deletePrimitiveResource(primitiveResourceRestRep.getId());
                }
                throw e1;
            }
            if (primitiveRestRep != null) {
                // add this to wf directory
                addResourceToWFDirectory(primitiveRestRep.getId(), shellPrimitive.getWfDirID());
            } else {
                flash.error("Error while creating primitive");
            }
            flash.success(MessagesUtils.get("wfBuilder.operation.save.success"));
        } else {
            flash.error("Error while uploading primitive resource");
        }
    } catch (final Exception e) {
        Logger.error(e.getMessage());
        flash.error(e.getMessage());
    }
}
Also used : CustomServicesPrimitiveResourceRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep) CustomServicesPrimitiveCreateParam(com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam) CustomServicesPrimitiveRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep) InputCreateList(com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam.InputCreateList) ImmutableMap(com.google.common.collect.ImmutableMap) URISyntaxException(java.net.URISyntaxException)

Example 14 with CustomServicesPrimitiveRestRep

use of com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep in project coprhd-controller by CoprHD.

the class WorkflowBuilder method createRestAPIPrimitive.

private static void createRestAPIPrimitive(final RestAPIPrimitiveForm restAPIPrimitive) {
    try {
        final CustomServicesPrimitiveCreateParam primitiveCreateParam = new CustomServicesPrimitiveCreateParam();
        primitiveCreateParam.setType(StepType.REST.toString());
        primitiveCreateParam.setName(restAPIPrimitive.getName());
        primitiveCreateParam.setDescription(restAPIPrimitive.getDescription());
        primitiveCreateParam.setFriendlyName(restAPIPrimitive.getName());
        primitiveCreateParam.setAttributes(new HashMap<String, String>());
        primitiveCreateParam.getAttributes().put(CustomServicesConstants.PATH.toString(), restAPIPrimitive.getRequestURL());
        if (restAPIPrimitive.getRawBody() == null) {
            primitiveCreateParam.getAttributes().put(CustomServicesConstants.BODY.toString(), "");
        } else {
            primitiveCreateParam.getAttributes().put(CustomServicesConstants.BODY.toString(), restAPIPrimitive.getRawBody());
        }
        // Only supported protocol is "https". Once other protocols are supported this value should come from UI form
        primitiveCreateParam.getAttributes().put(CustomServicesConstants.PROTOCOL.toString(), "https");
        primitiveCreateParam.getAttributes().put(CustomServicesConstants.METHOD.toString(), restAPIPrimitive.getMethod());
        primitiveCreateParam.getAttributes().put(CustomServicesConstants.AUTH_TYPE.toString(), restAPIPrimitive.getAuthType());
        final ImmutableMap.Builder<String, InputCreateList> builder = ImmutableMap.<String, InputCreateList>builder();
        // Add Input Groups
        addInputs(restAPIPrimitive.getHeaders(), builder, CustomServicesConstants.HEADERS);
        addInputs(restAPIPrimitive.getQueryParams(), builder, CustomServicesConstants.QUERY_PARAMS);
        primitiveCreateParam.setInput(builder.build());
        // Add Outputs
        if (StringUtils.isNotEmpty(restAPIPrimitive.getOutputs())) {
            primitiveCreateParam.setOutput(getListFromInputOutputString(restAPIPrimitive.getOutputs()));
        }
        final CustomServicesPrimitiveRestRep primitiveRestRep = getCatalogClient().customServicesPrimitives().createPrimitive(primitiveCreateParam);
        if (primitiveRestRep != null) {
            // add this to wf directory
            addResourceToWFDirectory(primitiveRestRep.getId(), restAPIPrimitive.getWfDirID());
            flash.success(MessagesUtils.get("wfBuilder.operation.save.success"));
        } else {
            flash.error("Error while creating primitive");
        }
    } catch (final Exception e) {
        Logger.error(e.getMessage());
        flash.error(e.getMessage());
    }
}
Also used : CustomServicesPrimitiveCreateParam(com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam) CustomServicesPrimitiveRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep) InputCreateList(com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam.InputCreateList) ImmutableMap(com.google.common.collect.ImmutableMap) URISyntaxException(java.net.URISyntaxException)

Example 15 with CustomServicesPrimitiveRestRep

use of com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep in project coprhd-controller by CoprHD.

the class WorkflowBuilder method addInventoryFileInputs.

// For each ansible step in workflow this method will look for host_file input and set the options to <hostid, hostname>
private static void addInventoryFileInputs(final CustomServicesWorkflowDocument.Step step) {
    if (StringUtils.isNotEmpty(step.getType()) && StepType.LOCAL_ANSIBLE.toString().equals(step.getType())) {
        CustomServicesWorkflowDocument.InputGroup inputGroup = step.getInputGroups().get(CustomServicesConstants.ANSIBLE_OPTIONS);
        for (final CustomServicesWorkflowDocument.Input input : inputGroup.getInputGroup()) {
            if (StringUtils.equals(CustomServicesConstants.ANSIBLE_HOST_FILE, input.getName())) {
                // Setting type and options for host_file
                input.setType(CustomServicesConstants.InputType.FROM_USER_MULTI.toString());
                final CustomServicesPrimitiveRestRep primitiveRestRep = getCatalogClient().customServicesPrimitives().getPrimitive(step.getOperation());
                final CustomServicesPrimitiveResourceList customServicesPrimitiveResourceList = getCatalogClient().customServicesPrimitives().getPrimitiveResourcesByType(CustomServicesConstants.ANSIBLE_INVENTORY_TYPE, primitiveRestRep.getResource().getId());
                if (customServicesPrimitiveResourceList != null && CollectionUtils.isNotEmpty(customServicesPrimitiveResourceList.getResources())) {
                    final Map<String, String> options = new HashMap<String, String>();
                    for (NamedRelatedResourceRep n : customServicesPrimitiveResourceList.getResources()) {
                        options.put(n.getId().toString(), n.getName());
                    }
                    Logger.debug("Adding inventory file options {}", options);
                    input.setOptions(options);
                } else {
                    // remove inventory files that have been set previously but deleted.
                    Logger.debug("Removing all the inventory files");
                    input.setOptions(new HashMap<String, String>());
                }
                break;
            }
        }
    }
}
Also used : CustomServicesPrimitiveRestRep(com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep) CustomServicesPrimitiveResourceList(com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceList) HashMap(java.util.HashMap) CustomServicesWorkflowDocument(com.emc.storageos.model.customservices.CustomServicesWorkflowDocument) NamedRelatedResourceRep(com.emc.storageos.model.NamedRelatedResourceRep)

Aggregations

CustomServicesPrimitiveRestRep (com.emc.storageos.model.customservices.CustomServicesPrimitiveRestRep)15 URISyntaxException (java.net.URISyntaxException)9 URI (java.net.URI)8 CustomServicesPrimitiveResourceRestRep (com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep)5 CustomServicesPrimitiveUpdateParam (com.emc.storageos.model.customservices.CustomServicesPrimitiveUpdateParam)5 CustomServicesPrimitiveCreateParam (com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam)4 InputCreateList (com.emc.storageos.model.customservices.CustomServicesPrimitiveCreateParam.InputCreateList)4 InputUpdateParam (com.emc.storageos.model.customservices.InputUpdateParam)4 OutputUpdateParam (com.emc.storageos.model.customservices.OutputUpdateParam)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 ResourcePackage (com.emc.sa.workflow.CustomServicesWorkflowPackage.ResourcePackage)2 NamedURI (com.emc.storageos.db.client.model.NamedURI)2 CustomServicesWorkflowList (com.emc.storageos.model.customservices.CustomServicesWorkflowList)2 CustomServicesWorkflowRestRep (com.emc.storageos.model.customservices.CustomServicesWorkflowRestRep)2 ModelObject (com.emc.storageos.db.client.model.ModelObject)1 CustomServicesWorkflow (com.emc.storageos.db.client.model.uimodels.CustomServicesWorkflow)1 NamedRelatedResourceRep (com.emc.storageos.model.NamedRelatedResourceRep)1 CustomServicesPrimitiveList (com.emc.storageos.model.customservices.CustomServicesPrimitiveList)1 CustomServicesPrimitiveResourceList (com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceList)1 CustomServicesWorkflowDocument (com.emc.storageos.model.customservices.CustomServicesWorkflowDocument)1