use of com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep in project coprhd-controller by CoprHD.
the class WorkflowHelper method importWorkflow.
private static CustomServicesWorkflow importWorkflow(final CustomServicesWorkflowPackage workflowPackage, final WFDirectory wfDirectory, final ModelClient client, final CustomServicesPrimitiveDAOs daos, final CustomServicesResourceDAOs resourceDAOs) throws JsonGenerationException, JsonMappingException, IOException {
for (final Entry<URI, ResourcePackage> resource : workflowPackage.resources().entrySet()) {
final CustomServicesResourceDAO<?> dao = resourceDAOs.getByModel(URIUtil.getTypeName(resource.getKey()));
final CustomServicesPrimitiveResourceRestRep metadata = resource.getValue().metadata();
if (null == dao) {
throw new RuntimeException("Type not found for ID " + metadata.getId());
}
if (!dao.importResource(metadata, resource.getValue().bytes())) {
log.info("Resource " + resource.getKey() + " previously imported");
}
}
for (final Entry<URI, CustomServicesPrimitiveRestRep> operation : workflowPackage.operations().entrySet()) {
final CustomServicesPrimitiveDAO<?> dao = daos.getByModel(URIUtil.getTypeName(operation.getKey()));
if (null == dao) {
throw new RuntimeException("Type not found for ID " + operation.getKey());
}
if (dao.importPrimitive(operation.getValue())) {
wfDirectory.addWorkflows(Collections.singleton(operation.getKey()));
} else {
log.info("Primitive " + operation.getKey() + " previously imported");
}
}
for (final Entry<URI, CustomServicesWorkflowRestRep> workflow : workflowPackage.workflows().entrySet()) {
final CustomServicesWorkflow model = client.customServicesWorkflows().findById(workflow.getKey());
if (null == model || model.getInactive()) {
importWorkflow(workflow.getValue(), client, wfDirectory);
} else {
log.info("Workflow " + workflow.getKey() + " previously imported");
}
}
return client.customServicesWorkflows().findById(workflowPackage.metadata().getId());
}
use of com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep in project coprhd-controller by CoprHD.
the class WorkflowBuilder method uploadInventoryFiles.
private static boolean uploadInventoryFiles(final URI packageId, final File[] inventoryFiles) throws java.io.IOException {
boolean uploadDone = false;
if (null != packageId && null != inventoryFiles && inventoryFiles.length > 0) {
for (File inventoryFile : inventoryFiles) {
CustomServicesPrimitiveResourceRestRep inventoryFilesResourceRestRep = getCatalogClient().customServicesPrimitives().createPrimitiveResource(CustomServicesConstants.ANSIBLE_INVENTORY_TYPE, inventoryFile, inventoryFile.getName(), packageId);
if (null == inventoryFilesResourceRestRep) {
Logger.error("Error while uploading primitive resource - inventory file %s for ansible package %s", inventoryFile.getName(), packageId);
flash.error("Error while uploading primitive resource - inventory file %s for ansible package %s", inventoryFile.getName(), packageId);
}
uploadDone = true;
}
}
return uploadDone;
}
use of com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep in project coprhd-controller by CoprHD.
the class WorkflowBuilder method editShellScriptPrimitive.
private static void editShellScriptPrimitive(final ShellScriptPrimitiveForm shellPrimitive) {
try {
final URI shellPrimitiveID = new URI(shellPrimitive.getId());
// Check primitive is already used in workflow/s
final CustomServicesPrimitiveRestRep primitiveRestRep = getCatalogClient().customServicesPrimitives().getPrimitive(shellPrimitiveID);
if (null != primitiveRestRep) {
final CustomServicesWorkflowList customServicesWorkflowList = getCatalogClient().customServicesPrimitives().getWorkflows(shellPrimitive.getId());
if (customServicesWorkflowList != null && customServicesWorkflowList.getWorkflows() != null) {
if (!customServicesWorkflowList.getWorkflows().isEmpty()) {
flash.error("Operation %s is being used in Workflow", primitiveRestRep.getName());
return;
}
}
// Update name, description
final CustomServicesPrimitiveUpdateParam primitiveUpdateParam = new CustomServicesPrimitiveUpdateParam();
primitiveUpdateParam.setName(shellPrimitive.getName());
primitiveUpdateParam.setFriendlyName(shellPrimitive.getName());
primitiveUpdateParam.setDescription(shellPrimitive.getDescription());
// Get and update differences between existing and new inputs
final List<String> newInputs = getListFromInputOutputString(shellPrimitive.getInputs());
final List<String> existingInputs = convertInputParamsGroupsToList(primitiveRestRep.getInputGroups());
final InputUpdateParam inputUpdateParam = new InputUpdateParam();
inputUpdateParam.setRemove(getInputParamsDiff(existingInputs, newInputs));
inputUpdateParam.setAdd(getInputParamsDiff(newInputs, existingInputs));
primitiveUpdateParam.setInput(inputUpdateParam);
// Get and update differences between existing and new outputs
final List<String> newOutputs = getListFromInputOutputString(shellPrimitive.getOutputs());
final List<String> existingOutputs = convertOutputGroupsToList(primitiveRestRep.getOutput());
OutputUpdateParam outputUpdateParam = new OutputUpdateParam();
outputUpdateParam.setRemove((List<String>) CollectionUtils.subtract(existingOutputs, newOutputs));
outputUpdateParam.setAdd((List<String>) CollectionUtils.subtract(newOutputs, existingOutputs));
primitiveUpdateParam.setOutput(outputUpdateParam);
final CustomServicesPrimitiveResourceRestRep primitiveResourceRestRep;
if (shellPrimitive.isNewScript()) {
// create new resource
String filename = FilenameUtils.getBaseName(shellPrimitive.getScript().getName());
primitiveResourceRestRep = getCatalogClient().customServicesPrimitives().createPrimitiveResource("SCRIPT", shellPrimitive.getScript(), filename);
if (null != primitiveResourceRestRep) {
// Update resource link
primitiveUpdateParam.setResource(primitiveResourceRestRep.getId());
try {
getCatalogClient().customServicesPrimitives().updatePrimitive(shellPrimitiveID, primitiveUpdateParam);
} catch (final Exception e1) {
if (primitiveResourceRestRep != null) {
// Resource was created but primitive creation failed
getCatalogClient().customServicesPrimitives().deletePrimitiveResource(primitiveResourceRestRep.getId());
}
throw e1;
}
}
} else {
getCatalogClient().customServicesPrimitives().updatePrimitive(shellPrimitiveID, primitiveUpdateParam);
}
flash.success(MessagesUtils.get("wfBuilder.operation.edit.success"));
}
} catch (final Exception e) {
Logger.error(e.getMessage());
flash.error(e.getMessage());
}
}
use of com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep in project coprhd-controller by CoprHD.
the class WorkflowBuilder method editLocalAnsiblePrimitive.
private static void editLocalAnsiblePrimitive(final LocalAnsiblePrimitiveForm localAnsible) {
try {
final URI localAnsiblePrimitiveID = new URI(localAnsible.getId());
final CustomServicesPrimitiveRestRep primitiveRestRep = getCatalogClient().customServicesPrimitives().getPrimitive(localAnsiblePrimitiveID);
if (null != primitiveRestRep) {
// Update name, description
final CustomServicesPrimitiveUpdateParam primitiveUpdateParam = new CustomServicesPrimitiveUpdateParam();
primitiveUpdateParam.setName(localAnsible.getName());
primitiveUpdateParam.setFriendlyName(localAnsible.getName());
primitiveUpdateParam.setDescription(localAnsible.getDescription());
// Get and update differences between existing and new inputs
final List<String> newInputs = getListFromInputOutputString(localAnsible.getInputs());
final List<String> existingInputs = convertInputParamsGroupsToList(primitiveRestRep.getInputGroups());
final InputUpdateParam inputUpdateParam = new InputUpdateParam();
inputUpdateParam.setRemove(getInputParamsDiff(existingInputs, newInputs));
inputUpdateParam.setAdd(getInputParamsDiff(newInputs, existingInputs));
primitiveUpdateParam.setInput(inputUpdateParam);
// Get and update differences between existing and new outputs
final List<String> newOutputs = getListFromInputOutputString(localAnsible.getOutputs());
final List<String> existingOutputs = convertOutputGroupsToList(primitiveRestRep.getOutput());
final OutputUpdateParam outputUpdateParam = new OutputUpdateParam();
outputUpdateParam.setRemove((List<String>) CollectionUtils.subtract(existingOutputs, newOutputs));
outputUpdateParam.setAdd((List<String>) CollectionUtils.subtract(newOutputs, existingOutputs));
primitiveUpdateParam.setOutput(outputUpdateParam);
// Set playbook
primitiveUpdateParam.setAttributes(new HashMap<String, String>());
primitiveUpdateParam.getAttributes().put(CustomServicesConstants.ANSIBLE_PLAYBOOK, localAnsible.getAnsiblePlaybook());
URI packageId = null;
boolean updateDone = false;
if (!localAnsible.isExisting()) {
// NEW RESOURCE
// Before creating resource check if this primitive is not used
final CustomServicesWorkflowList customServicesWorkflowList = getCatalogClient().customServicesPrimitives().getWorkflows(localAnsible.getId());
if (customServicesWorkflowList != null && customServicesWorkflowList.getWorkflows() != null) {
if (!customServicesWorkflowList.getWorkflows().isEmpty()) {
flash.error("Primitive %s is being used in Workflow", primitiveRestRep.getName());
return;
}
}
// create new resource
final CustomServicesPrimitiveResourceRestRep primitiveResourceRestRep = getCatalogClient().customServicesPrimitives().createPrimitiveResource("ANSIBLE", localAnsible.getAnsiblePackage(), localAnsible.getAnsiblePackageName());
if (null != primitiveResourceRestRep) {
// Update resource link
packageId = primitiveResourceRestRep.getId();
primitiveUpdateParam.setResource(packageId);
}
} else {
// EXISTING RESOURCE
packageId = new URI(localAnsible.getExistingResource());
primitiveUpdateParam.setResource(packageId);
// Changes to existing inventory files
updateDone = updateInventoryFiles(packageId, localAnsible.getUpdatedInventoryFiles());
}
// Upload new inventory files
final boolean uploadDone = uploadInventoryFiles(packageId, localAnsible.getInventoryFiles());
// Update workflows with new inventory files
boolean updatedWorkflows = false;
if (uploadDone || updateDone) {
updatedWorkflows = updateWorkflowInventoryFiles(localAnsible.getId());
}
// If this primitive is part of any workflow, ignore update
if (!updatedWorkflows) {
getCatalogClient().customServicesPrimitives().updatePrimitive(localAnsiblePrimitiveID, primitiveUpdateParam);
} else {
Logger.info("Ignoring local ansible primitive {} update as it is part of workflow", localAnsible.getName());
}
flash.success(MessagesUtils.get("wfBuilder.operation.edit.success"));
}
} catch (final Exception e) {
Logger.error(e.getMessage());
flash.error(e.getMessage());
}
}
use of com.emc.storageos.model.customservices.CustomServicesPrimitiveResourceRestRep 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());
}
}
Aggregations