use of org.apache.nifi.web.api.dto.TemplateDTO in project kylo by Teradata.
the class LegacyNifiRestClient method getPropertiesForTemplateByName.
/**
* Expose all Properties for a given Template as parameters for external use If template is not found it will return an Empty ArrayList()
*/
public List<NifiProperty> getPropertiesForTemplateByName(String templateName) {
TemplateDTO dto = getTemplateByName(templateName);
if (dto != null) {
// populate the snippet
dto = getTemplateById(dto.getId());
}
ProcessGroupDTO rootProcessGroup = getProcessGroup("root", false, false);
return NifiPropertyUtil.getPropertiesForTemplate(rootProcessGroup, dto, propertyDescriptorTransform);
}
use of org.apache.nifi.web.api.dto.TemplateDTO in project kylo by Teradata.
the class AbstractNiFiTemplatesRestClientTest method findByInputPortName.
/**
* Verifies finding templates by an input port name
*/
@Test
public void findByInputPortName() {
// Mock templates with only basic info
final TemplateDTO basicTemplate1 = new TemplateDTO();
basicTemplate1.setId("cf54ca27-ccb0-49a1-94f2-2834c4379b70");
final TemplateDTO basicTemplate2 = new TemplateDTO();
basicTemplate2.setId("43ce4a07-10ac-40c0-b195-598bc753988b");
final TemplateDTO basicTemplate3 = new TemplateDTO();
basicTemplate3.setId("7f57c685-f9dd-497e-8e97-7d1e6d43b59c");
// Mock templates with port info
final PortDTO port1 = new PortDTO();
port1.setId("ee77fc99-2f19-43d3-ae26-091f9caa1401");
port1.setName("to-other-template");
final PortDTO port2 = new PortDTO();
port2.setId("1b2a3c92-1964-4f87-9835-b8a19f024249");
port2.setName("to-standard-ingest");
final FlowSnippetDTO flow1 = new FlowSnippetDTO();
flow1.setInputPorts(ImmutableSet.of(port1));
final FlowSnippetDTO flow2 = new FlowSnippetDTO();
flow2.setInputPorts(ImmutableSet.of(port2));
final TemplateDTO fullTemplate1 = new TemplateDTO();
fullTemplate1.setId("cf54ca27-ccb0-49a1-94f2-2834c4379b70");
fullTemplate1.setSnippet(flow1);
final TemplateDTO fullTemplate2 = new TemplateDTO();
fullTemplate2.setId("43ce4a07-10ac-40c0-b195-598bc753988b");
fullTemplate2.setSnippet(flow2);
// Mock the NiFi Templates REST client
final NiFiTemplatesRestClient client = Mockito.mock(AbstractNiFiTemplatesRestClient.class, Mockito.CALLS_REAL_METHODS);
Mockito.when(client.findAll()).thenReturn(ImmutableSet.of(basicTemplate1, basicTemplate2, basicTemplate3));
Mockito.when(client.findById("cf54ca27-ccb0-49a1-94f2-2834c4379b70")).thenReturn(Optional.of(fullTemplate1));
Mockito.when(client.findById("43ce4a07-10ac-40c0-b195-598bc753988b")).thenReturn(Optional.of(fullTemplate2));
Mockito.when(client.findById("7f57c685-f9dd-497e-8e97-7d1e6d43b59c")).thenReturn(Optional.empty());
// Test finding matching templates
final Set<TemplateDTO> matches = client.findByInputPortName("to-standard-ingest");
Assert.assertEquals(fullTemplate2, matches.stream().findFirst().get());
Assert.assertEquals(0, client.findByInputPortName("invalid").size());
}
use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.
the class StandardNiFiServiceFacade method exportTemplate.
@Override
public TemplateDTO exportTemplate(final String id) {
final Template template = templateDAO.getTemplate(id);
final TemplateDTO templateDetails = template.getDetails();
final TemplateDTO templateDTO = dtoFactory.createTemplateDTO(template);
templateDTO.setSnippet(dtoFactory.copySnippetContents(templateDetails.getSnippet()));
return templateDTO;
}
use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.
the class TemplateResource method exportTemplate.
/**
* Retrieves the specified template.
*
* @param id The id of the template to retrieve
* @return A templateEntity.
*/
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_XML)
@Path("{id}/download")
@ApiOperation(value = "Exports a template", response = String.class, authorizations = { @Authorization(value = "Read - /templates/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response exportTemplate(@ApiParam(value = "The template id.", required = true) @PathParam("id") final String id) {
if (isReplicateRequest()) {
return replicate(HttpMethod.GET);
}
// authorize access
serviceFacade.authorizeAccess(lookup -> {
final Authorizable template = lookup.getTemplate(id);
template.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
});
// get the template
final TemplateDTO template = serviceFacade.exportTemplate(id);
// prune the template id
template.setId(null);
// determine the name of the attachement - possible issues with spaces in file names
String attachmentName = template.getName();
if (StringUtils.isBlank(attachmentName)) {
attachmentName = "template";
} else {
attachmentName = attachmentName.replaceAll("\\s", "_");
}
// generate the response
/*
* Here instead of relying on default JAXB marshalling we are simply
* serializing template to String (formatted, indented etc) and sending
* it as part of the response.
*/
String serializedTemplate = new String(TemplateSerializer.serialize(template), StandardCharsets.UTF_8);
return generateOkResponse(serializedTemplate).header("Content-Disposition", String.format("attachment; filename=\"%s.xml\"", attachmentName)).build();
}
use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.
the class ProcessGroupResource method instantiateTemplate.
/**
* Instantiates the specified template within this ProcessGroup. The template instance that is instantiated cannot be referenced at a later time, therefore there is no
* corresponding URI. Instead the request URI is returned.
* <p>
* Alternatively, we could have performed a PUT request. However, PUT requests are supposed to be idempotent and this endpoint is certainly not.
*
* @param httpServletRequest request
* @param groupId The group id
* @param requestInstantiateTemplateRequestEntity The instantiate template request
* @return A flowEntity.
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/template-instance")
@ApiOperation(value = "Instantiates a template", response = FlowEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{uuid}"), @Authorization(value = "Read - /templates/{uuid}"), @Authorization(value = "Write - if the template contains any restricted components - /restricted-components") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response instantiateTemplate(@Context HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") String groupId, @ApiParam(value = "The instantiate template request.", required = true) InstantiateTemplateRequestEntity requestInstantiateTemplateRequestEntity) {
// ensure the position has been specified
if (requestInstantiateTemplateRequestEntity == null || requestInstantiateTemplateRequestEntity.getOriginX() == null || requestInstantiateTemplateRequestEntity.getOriginY() == null) {
throw new IllegalArgumentException("The origin position (x, y) must be specified.");
}
// ensure the template id was provided
if (requestInstantiateTemplateRequestEntity.getTemplateId() == null) {
throw new IllegalArgumentException("The template id must be specified.");
}
// ensure the template encoding version is valid
if (requestInstantiateTemplateRequestEntity.getEncodingVersion() != null) {
try {
FlowEncodingVersion.parse(requestInstantiateTemplateRequestEntity.getEncodingVersion());
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException("The template encoding version is not valid. The expected format is <number>.<number>");
}
}
// populate the encoding version if necessary
if (requestInstantiateTemplateRequestEntity.getEncodingVersion() == null) {
// if the encoding version is not specified, use the latest encoding version as these options were
// not available pre 1.x, will be overridden if populating from the underlying template below
requestInstantiateTemplateRequestEntity.setEncodingVersion(TemplateDTO.MAX_ENCODING_VERSION);
}
// populate the component bundles if necessary
if (requestInstantiateTemplateRequestEntity.getSnippet() == null) {
// get the desired template in order to determine the supported bundles
final TemplateDTO requestedTemplate = serviceFacade.exportTemplate(requestInstantiateTemplateRequestEntity.getTemplateId());
final FlowSnippetDTO requestTemplateContents = requestedTemplate.getSnippet();
// determine the compatible bundles to use for each component in this template, this ensures the nodes in the cluster
// instantiate the components from the same bundles
discoverCompatibleBundles(requestTemplateContents);
// update the requested template as necessary - use the encoding version from the underlying template
requestInstantiateTemplateRequestEntity.setEncodingVersion(requestedTemplate.getEncodingVersion());
requestInstantiateTemplateRequestEntity.setSnippet(requestTemplateContents);
}
if (isReplicateRequest()) {
return replicate(HttpMethod.POST, requestInstantiateTemplateRequestEntity);
}
return withWriteLock(serviceFacade, requestInstantiateTemplateRequestEntity, lookup -> {
final NiFiUser user = NiFiUserUtils.getNiFiUser();
// ensure write on the group
final Authorizable processGroup = lookup.getProcessGroup(groupId).getAuthorizable();
processGroup.authorize(authorizer, RequestAction.WRITE, user);
final Authorizable template = lookup.getTemplate(requestInstantiateTemplateRequestEntity.getTemplateId());
template.authorize(authorizer, RequestAction.READ, user);
// ensure read on the template
final TemplateContentsAuthorizable templateContents = lookup.getTemplateContents(requestInstantiateTemplateRequestEntity.getSnippet());
final Consumer<ComponentAuthorizable> authorizeRestricted = authorizable -> {
if (authorizable.isRestricted()) {
authorizeRestrictions(authorizer, authorizable);
}
};
// ensure restricted access if necessary
templateContents.getEncapsulatedProcessors().forEach(authorizeRestricted);
templateContents.getEncapsulatedControllerServices().forEach(authorizeRestricted);
}, () -> serviceFacade.verifyComponentTypes(requestInstantiateTemplateRequestEntity.getSnippet()), instantiateTemplateRequestEntity -> {
// create the template and generate the json
final FlowEntity entity = serviceFacade.createTemplateInstance(groupId, instantiateTemplateRequestEntity.getOriginX(), instantiateTemplateRequestEntity.getOriginY(), instantiateTemplateRequestEntity.getEncodingVersion(), instantiateTemplateRequestEntity.getSnippet(), getIdGenerationSeed().orElse(null));
final FlowDTO flowSnippet = entity.getFlow();
// prune response as necessary
for (ProcessGroupEntity childGroupEntity : flowSnippet.getProcessGroups()) {
childGroupEntity.getComponent().setContents(null);
}
// create the response entity
populateRemainingSnippetContent(flowSnippet);
// generate the response
return generateCreatedResponse(getAbsolutePath(), entity).build();
});
}
Aggregations