Search in sources :

Example 1 with DynamicActionMetadata

use of io.syndesis.common.model.connection.DynamicActionMetadata in project syndesis by syndesisio.

the class ConnectionActionHandlerTest method shouldSetInoutOutputShapesToAnyIfMetadataCallFails.

@Test
public void shouldSetInoutOutputShapesToAnyIfMetadataCallFails() {
    @SuppressWarnings({ "unchecked", "rawtypes" }) final Class<Entity<Map<String, Object>>> entityType = (Class) Entity.class;
    ArgumentCaptor.forClass(entityType);
    // simulates fallback return
    final DynamicActionMetadata fallback = new DynamicActionMetadata.Builder().build();
    when(metadataCommand.execute()).thenReturn(fallback);
    when(((HystrixInvokableInfo<?>) metadataCommand).isSuccessfulExecution()).thenReturn(false);
    final Response response = handler.enrichWithMetadata(SALESFORCE_CREATE_OR_UPDATE, Collections.emptyMap());
    @SuppressWarnings("unchecked") final Meta<ConnectorDescriptor> meta = (Meta<ConnectorDescriptor>) response.getEntity();
    final ConnectorDescriptor descriptor = meta.getValue();
    assertThat(descriptor.getInputDataShape()).contains(ConnectionActionHandler.ANY_SHAPE);
    assertThat(descriptor.getOutputDataShape()).contains(salesforceOutputShape);
}
Also used : Entity(javax.ws.rs.client.Entity) Meta(io.syndesis.server.endpoint.v1.dto.Meta) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) Response(javax.ws.rs.core.Response) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Test(org.junit.Test)

Example 2 with DynamicActionMetadata

use of io.syndesis.common.model.connection.DynamicActionMetadata in project syndesis-qe by syndesisio.

the class StepDescriptorEndpoint method getStepDescriptor.

public StepDescriptor getStepDescriptor(String stepKind, DataShape in, DataShape out) {
    super.setEndpointName("/steps/" + stepKind + "/descriptor");
    log.debug("POST, destination : {}", getEndpointUrl());
    final Invocation.Builder invocation = createInvocation(null);
    DynamicActionMetadata metadata = new DynamicActionMetadata.Builder().inputShape(in).outputShape(out).build();
    JsonNode response;
    try {
        response = invocation.post(Entity.entity(metadata, MediaType.APPLICATION_JSON), JsonNode.class);
    } catch (BadRequestException ex) {
        log.error("Bad request, trying again in 15 seconds");
        try {
            Thread.sleep(15000L);
        } catch (InterruptedException ignore) {
        // ignore
        }
        response = invocation.post(Entity.entity(metadata, MediaType.APPLICATION_JSON), JsonNode.class);
    }
    return transformJsonNode(response, StepDescriptor.class);
}
Also used : Invocation(javax.ws.rs.client.Invocation) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) BadRequestException(javax.ws.rs.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 3 with DynamicActionMetadata

use of io.syndesis.common.model.connection.DynamicActionMetadata in project syndesis by syndesisio.

the class ConnectionActionHandler method applyMetadataTo.

private static ConnectorDescriptor applyMetadataTo(final ConnectorDescriptor descriptor, final DynamicActionMetadata dynamicMetadata) {
    final Map<String, List<DynamicActionMetadata.ActionPropertySuggestion>> actionPropertySuggestions = dynamicMetadata.properties();
    final ConnectorDescriptor.Builder enriched = new ConnectorDescriptor.Builder().createFrom(descriptor);
    actionPropertySuggestions.forEach((k, vals) -> enriched.replaceConfigurationProperty(k, b -> b.addAllEnum(vals.stream().map(s -> ConfigurationProperty.PropertyValue.Builder.from(s))::iterator)));
    // Setting the defaultValue as suggested by the metadata
    for (final Entry<String, List<DynamicActionMetadata.ActionPropertySuggestion>> suggestions : actionPropertySuggestions.entrySet()) {
        if (suggestions.getValue().size() == 1) {
            for (final DynamicActionMetadata.ActionPropertySuggestion suggestion : suggestions.getValue()) {
                enriched.replaceConfigurationProperty(suggestion.displayValue(), v -> v.defaultValue(suggestion.value()));
            }
        }
    }
    final DataShape input = dynamicMetadata.inputShape();
    if (shouldEnrichDataShape(descriptor.getInputDataShape(), input)) {
        enriched.inputDataShape(input);
    }
    final DataShape output = dynamicMetadata.outputShape();
    if (shouldEnrichDataShape(descriptor.getOutputDataShape(), output)) {
        enriched.outputDataShape(output);
    }
    return adaptDataShapes(enriched);
}
Also used : ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) HashMap(java.util.HashMap) ConfigurationProperty(io.syndesis.common.model.connection.ConfigurationProperty) ApiResponses(io.swagger.annotations.ApiResponses) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) EncryptionComponent(io.syndesis.server.dao.manager.EncryptionComponent) Map(java.util.Map) EntityNotFoundException(javax.persistence.EntityNotFoundException) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) POST(javax.ws.rs.POST) DataShape(io.syndesis.common.model.DataShape) MetadataConfigurationProperties(io.syndesis.server.verifier.MetadataConfigurationProperties) Connector(io.syndesis.common.model.connection.Connector) Collectors(java.util.stream.Collectors) DataShapeKinds(io.syndesis.common.model.DataShapeKinds) Meta(io.syndesis.server.endpoint.v1.dto.Meta) List(java.util.List) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) HystrixExecutable(com.netflix.hystrix.HystrixExecutable) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) Entry(java.util.Map.Entry) Optional(java.util.Optional) ConnectionBase(io.syndesis.common.model.connection.ConnectionBase) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) List(java.util.List) DataShape(io.syndesis.common.model.DataShape)

Example 4 with DynamicActionMetadata

use of io.syndesis.common.model.connection.DynamicActionMetadata in project syndesis by syndesisio.

the class ConnectionActionHandler method enrichWithMetadata.

@POST
@Path(value = "/{id}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Retrieves enriched action definition, that is an action definition that has input/output data shapes and property enums defined with respect to the given action properties")
@ApiResponses(@ApiResponse(code = 200, reference = "#/definitions/ConnectorDescriptor", message = "A map of zero or more action property suggestions keyed by the property name"))
public Response enrichWithMetadata(@PathParam("id") @ApiParam(required = true, example = "io.syndesis:salesforce-create-or-update:latest") final String id, final Map<String, String> properties) {
    final ConnectorAction action = // 
    actions.stream().filter(// 
    a -> a.idEquals(id)).findAny().orElseThrow(() -> new EntityNotFoundException("Action with id: " + id));
    final ConnectorDescriptor defaultDescriptor = action.getDescriptor();
    if (!action.getTags().contains("dynamic")) {
        return Response.ok().entity(Meta.verbatim(defaultDescriptor)).build();
    }
    final Map<String, String> parameters = encryptionComponent.decrypt(new HashMap<>(Optional.ofNullable(properties).orElseGet(HashMap::new)));
    // put all action parameters with `null` values
    defaultDescriptor.getPropertyDefinitionSteps().forEach(step -> step.getProperties().forEach((k, v) -> parameters.putIfAbsent(k, null)));
    // add the pattern as a property
    if (action.getPattern() != null) {
        parameters.put(action.getPattern().getDeclaringClass().getSimpleName(), action.getPattern().name());
    }
    // lastly put all connection properties
    parameters.putAll(encryptionComponent.decrypt(connection.getConfiguredProperties()));
    final HystrixExecutable<DynamicActionMetadata> meta = createMetadataCommand(action, parameters);
    final DynamicActionMetadata dynamicActionMetadata = meta.execute();
    final ConnectorDescriptor enrichedDescriptor = applyMetadataTo(defaultDescriptor, dynamicActionMetadata);
    @SuppressWarnings("unchecked") final HystrixInvokableInfo<ConnectorDescriptor> metaInfo = (HystrixInvokableInfo<ConnectorDescriptor>) meta;
    final Meta<ConnectorDescriptor> metaResult = Meta.from(enrichedDescriptor, metaInfo);
    final Status status = metaResult.getData().getType().map(t -> t.status).orElse(Status.OK);
    return Response.status(status).entity(metaResult).build();
}
Also used : ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) HashMap(java.util.HashMap) ConfigurationProperty(io.syndesis.common.model.connection.ConfigurationProperty) ApiResponses(io.swagger.annotations.ApiResponses) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) EncryptionComponent(io.syndesis.server.dao.manager.EncryptionComponent) Map(java.util.Map) EntityNotFoundException(javax.persistence.EntityNotFoundException) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) POST(javax.ws.rs.POST) DataShape(io.syndesis.common.model.DataShape) MetadataConfigurationProperties(io.syndesis.server.verifier.MetadataConfigurationProperties) Connector(io.syndesis.common.model.connection.Connector) Collectors(java.util.stream.Collectors) DataShapeKinds(io.syndesis.common.model.DataShapeKinds) Meta(io.syndesis.server.endpoint.v1.dto.Meta) List(java.util.List) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) HystrixExecutable(com.netflix.hystrix.HystrixExecutable) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) Entry(java.util.Map.Entry) Optional(java.util.Optional) ConnectionBase(io.syndesis.common.model.connection.ConnectionBase) Status(javax.ws.rs.core.Response.Status) HashMap(java.util.HashMap) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) EntityNotFoundException(javax.persistence.EntityNotFoundException) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with DynamicActionMetadata

use of io.syndesis.common.model.connection.DynamicActionMetadata in project syndesis by syndesisio.

the class ConnectionActionHandlerTest method shouldProvideActionDefinition.

@Test
public void shouldProvideActionDefinition() {
    @SuppressWarnings({ "unchecked", "rawtypes" }) final Class<Entity<Map<String, Object>>> entityType = (Class) Entity.class;
    ArgumentCaptor.forClass(entityType);
    final DynamicActionMetadata suggestions = new DynamicActionMetadata.Builder().putProperty("sObjectName", Arrays.asList(DynamicActionMetadata.ActionPropertySuggestion.Builder.of("Account", "Account"), DynamicActionMetadata.ActionPropertySuggestion.Builder.of("Contact", "Contact"))).build();
    when(metadataCommand.execute()).thenReturn(suggestions);
    when(((HystrixInvokableInfo<?>) metadataCommand).isSuccessfulExecution()).thenReturn(true);
    final Response response = handler.enrichWithMetadata(SALESFORCE_CREATE_OR_UPDATE, Collections.emptyMap());
    assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
    @SuppressWarnings("unchecked") final Meta<ConnectorDescriptor> meta = (Meta<ConnectorDescriptor>) response.getEntity();
    final ConnectorDescriptor enrichedDefinitioin = new ConnectorDescriptor.Builder().createFrom(createOrUpdateSalesforceObjectDescriptor).replaceConfigurationProperty("sObjectName", c -> c.addEnum(ConfigurationProperty.PropertyValue.Builder.of("Account", "Account"), ConfigurationProperty.PropertyValue.Builder.of("Contact", "Contact"))).inputDataShape(// 
    ConnectionActionHandler.ANY_SHAPE).build();
    assertThat(meta.getValue()).isEqualTo(enrichedDefinitioin);
}
Also used : Entity(javax.ws.rs.client.Entity) Meta(io.syndesis.server.endpoint.v1.dto.Meta) DynamicActionMetadata(io.syndesis.common.model.connection.DynamicActionMetadata) Response(javax.ws.rs.core.Response) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Test(org.junit.Test)

Aggregations

DynamicActionMetadata (io.syndesis.common.model.connection.DynamicActionMetadata)7 HystrixInvokableInfo (com.netflix.hystrix.HystrixInvokableInfo)6 ConnectorDescriptor (io.syndesis.common.model.action.ConnectorDescriptor)6 Meta (io.syndesis.server.endpoint.v1.dto.Meta)6 Response (javax.ws.rs.core.Response)6 Entity (javax.ws.rs.client.Entity)4 Test (org.junit.Test)4 HystrixExecutable (com.netflix.hystrix.HystrixExecutable)3 DataShape (io.syndesis.common.model.DataShape)3 DataShapeKinds (io.syndesis.common.model.DataShapeKinds)3 ConnectorAction (io.syndesis.common.model.action.ConnectorAction)3 ConfigurationProperty (io.syndesis.common.model.connection.ConfigurationProperty)3 Connector (io.syndesis.common.model.connection.Connector)3 EncryptionComponent (io.syndesis.server.dao.manager.EncryptionComponent)3 MetadataConfigurationProperties (io.syndesis.server.verifier.MetadataConfigurationProperties)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Status (javax.ws.rs.core.Response.Status)3 Api (io.swagger.annotations.Api)2 ApiOperation (io.swagger.annotations.ApiOperation)2