Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
feat(server): autodiscovery circuit breaker call (#8022)
Browse files Browse the repository at this point in the history
* Added circuit breaker instead of plain REST call to meta connection properties
* Refactored common-model to add dynamic metadata modeling
* Refactored MetadataCommand to be used as abstract base for MetadataCommandAction and MetadataConnectionPropertiesCommand
* Refactored unit test to use DynamicConnectionPropertiesMetadata instead of raw Response
* Removed useless pom dependencies

Closes ENTESB-12498
  • Loading branch information
squakez committed May 18, 2020
1 parent 6f3db5f commit db820ca
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import io.syndesis.common.model.Ordered;
import io.syndesis.common.model.WithTags;
import io.syndesis.common.model.connection.DynamicActionMetadata.ActionPropertySuggestion;
import io.syndesis.common.model.connection.WithDynamicProperties.ActionPropertySuggestion;

import org.immutables.value.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,15 @@

@Value.Immutable
@JsonDeserialize(builder = DynamicActionMetadata.Builder.class)
public interface DynamicActionMetadata {
public interface DynamicActionMetadata extends WithDynamicProperties {

DynamicActionMetadata NOTHING = new DynamicActionMetadata.Builder().build();

@Value.Immutable
@JsonDeserialize(builder = ActionPropertySuggestion.Builder.class)
interface ActionPropertySuggestion {

final class Builder extends ImmutableActionPropertySuggestion.Builder {
public static ActionPropertySuggestion of(final String value, final String displayValue) {
return new ActionPropertySuggestion.Builder().value(value).displayValue(displayValue).build();
}
}

String displayValue();

String value();
}

final class Builder extends ImmutableDynamicActionMetadata.Builder {
// make ImmutableDynamicActionMetadata.Builder accessible
}

@Override
Map<String, List<ActionPropertySuggestion>> properties();

DataShape inputShape();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.syndesis.common.model.connection;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(builder = DynamicConnectionPropertiesMetadata.Builder.class)
public interface DynamicConnectionPropertiesMetadata extends WithDynamicProperties {

DynamicConnectionPropertiesMetadata NOTHING = new DynamicConnectionPropertiesMetadata.Builder().build();

final class Builder extends ImmutableDynamicConnectionPropertiesMetadata.Builder {
// make ImmutableDynamicConnectionPropertiesMetadata.Builder accessible
}

@Override
Map<String, List<ActionPropertySuggestion>> properties();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.syndesis.common.model.connection;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.immutables.value.Value;

public interface WithDynamicProperties {

@Value.Immutable
@JsonDeserialize(builder = ActionPropertySuggestion.Builder.class)
interface ActionPropertySuggestion {

final class Builder extends ImmutableActionPropertySuggestion.Builder {
public static ActionPropertySuggestion of(final String value, final String displayValue) {
return new ActionPropertySuggestion.Builder().value(value).displayValue(displayValue).build();
}
}

String displayValue();

String value();
}

Map<String, List<ActionPropertySuggestion>> properties();
}
10 changes: 0 additions & 10 deletions app/server/endpoint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@
<artifactId>jackson-module-jsonSchema</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
Expand Down Expand Up @@ -278,11 +273,6 @@
<artifactId>hystrix-core</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Response enrichWithMetadata(
// lastly put all connection properties
parameters.putAll(encryptionComponent.decrypt(connection.getConfiguredProperties()));

final HystrixExecutable<DynamicActionMetadata> meta = createMetadataCommand(action, parameters);
final HystrixExecutable<DynamicActionMetadata> meta = createMetadataCommandAction(action, parameters);
final DynamicActionMetadata dynamicActionMetadata = meta.execute();

final ConnectorDescriptor enrichedDescriptor = applyMetadataTo(generatedDescriptor, dynamicActionMetadata);
Expand All @@ -157,9 +157,9 @@ public Response enrichWithMetadata(
return Response.status(status).entity(metaResult).build();
}

protected HystrixExecutable<DynamicActionMetadata> createMetadataCommand(final ConnectorAction action,
final Map<String, String> parameters) {
return new MetadataCommand(config, connectorId, action, parameters);
protected HystrixExecutable<DynamicActionMetadata> createMetadataCommandAction(final ConnectorAction action,
final Map<String, String> parameters) {
return new MetadataCommandAction(config, connectorId, action, parameters);
}

private static DataShape adaptDataShape(final Optional<DataShape> maybeDataShape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.UriInfo;
import java.io.BufferedInputStream;
import java.io.IOException;
Expand All @@ -35,14 +33,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.syndesis.common.model.Kind;
Expand All @@ -52,6 +48,8 @@
import io.syndesis.common.model.connection.ConfigurationProperty;
import io.syndesis.common.model.connection.Connection;
import io.syndesis.common.model.connection.Connector;
import io.syndesis.common.model.connection.DynamicConnectionPropertiesMetadata;
import io.syndesis.common.model.connection.WithDynamicProperties;
import io.syndesis.common.model.filter.FilterOptions;
import io.syndesis.common.model.filter.Op;
import io.syndesis.common.model.icon.Icon;
Expand Down Expand Up @@ -129,7 +127,7 @@ public Connector get(final String id) {

// Retrieve dynamic properties, if connector is dynamic
if (connector.getTags().contains("dynamic")) {
connector = enrichWithDynamicProperties(connector);
connector = enrichConnectorWithDynamicProperties(connector);
}

final Optional<String> connectorGroupId = connector.getConnectorGroupId();
Expand All @@ -149,48 +147,42 @@ public Connector get(final String id) {
* @param connector
* @return an enriched {@link Connector}
*/
Connector enrichWithDynamicProperties(Connector connector) {
Connector enrichConnectorWithDynamicProperties(Connector connector) {
final String connectorId = connector.getId().get();
final ConnectorPropertiesHandler propertiesHandler = properties(connectorId);

final JsonNode jsonResponse;
try (Response response = propertiesHandler.enrichWithDynamicProperties(connectorId, null)) {
if (response.getStatusInfo().getFamily() != Family.SUCCESSFUL) {
return connector;
}

jsonResponse = response.readEntity(JsonNode.class);
final Map<String, ConfigurationProperty> dynamicProperties = enrichConfigurationPropertiesWithDynamicProperties(
connector.getProperties(),
propertiesHandler.dynamicConnectionProperties(connectorId)
);
if (!dynamicProperties.isEmpty()) {
return connector.builder().properties(dynamicProperties).build();
}
return connector;
}

final Iterator<Map.Entry<String, JsonNode>> iterator = jsonResponse.path("properties").fields();
final Map<String, ConfigurationProperty> dynamicProperties = new HashMap<>();

while (iterator.hasNext()) {
Map.Entry<String, JsonNode> next = iterator.next();
String propertyName = next.getKey();
JsonNode property = next.getValue();
private static Map<String, ConfigurationProperty> enrichConfigurationPropertiesWithDynamicProperties(Map<String, ConfigurationProperty> inputProperties,
DynamicConnectionPropertiesMetadata dynamicConnectionProperties) {
Map<String, ConfigurationProperty> dynamicProperties = new HashMap<>();
for(Map.Entry<String, List<WithDynamicProperties.ActionPropertySuggestion>> entry: dynamicConnectionProperties.properties().entrySet()){
String propertyName = entry.getKey();
List<WithDynamicProperties.ActionPropertySuggestion> list = entry.getValue();
List<ConfigurationProperty.PropertyValue> values = new ArrayList<>();
if (property.isArray()) {
for (final JsonNode value : property) {
ConfigurationProperty.PropertyValue val = new ConfigurationProperty.PropertyValue.Builder()
.label(value.get("displayValue").asText())
.value(value.get("value").asText())
.build();
values.add(val);
}

for (WithDynamicProperties.ActionPropertySuggestion suggestion : list) {
ConfigurationProperty.PropertyValue val = ConfigurationProperty.PropertyValue.Builder.from(suggestion);
values.add(val);
}

if (!values.isEmpty()) {
ConfigurationProperty configurationProperty = new ConfigurationProperty.Builder()
.createFrom(connector.getProperties().get(propertyName))
.createFrom(inputProperties.get(propertyName))
.addEnum(values.toArray(new ConfigurationProperty.PropertyValue[0]))
.build();
dynamicProperties.put(propertyName, configurationProperty);
}
}
if (!dynamicProperties.isEmpty()) {
return connector.builder().properties(dynamicProperties).build();
}
return connector;

return dynamicProperties;
}

@Path("/{connectorId}/actions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,34 @@
*/
package io.syndesis.server.endpoint.v1.handler.connection;

import java.util.Map;

import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collections;

import com.netflix.hystrix.HystrixExecutable;
import io.syndesis.common.model.connection.DynamicConnectionPropertiesMetadata;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.syndesis.server.verifier.MetadataConfigurationProperties;

import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;

@Tag(name = "properties")
public class ConnectorPropertiesHandler {
private final String metaPropertiesUrl;
private final MetadataConfigurationProperties config;

public ConnectorPropertiesHandler(final MetadataConfigurationProperties config) {
metaPropertiesUrl = String.format("http://%s/api/v1/connectors/%%s/properties/meta", config.getService());
this.config = config;
}

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response enrichWithDynamicProperties(@PathParam("id") final String connectorId, final Map<String, Object> props) {
// TODO replace properly with circuit breaker
String metadataUrl = String.format(metaPropertiesUrl, connectorId);
Client client = createClient();
WebTarget target = client.target(metadataUrl);

return target.request().post(Entity.entity(props, MediaType.APPLICATION_JSON_TYPE));
public DynamicConnectionPropertiesMetadata dynamicConnectionProperties(@PathParam("id") final String connectorId) {
final HystrixExecutable<DynamicConnectionPropertiesMetadata> meta = createMetadataConnectionPropertiesCommand(connectorId);
return meta.execute();
}

Client createClient() {
return new ResteasyClientBuilder().build();
protected HystrixExecutable<DynamicConnectionPropertiesMetadata> createMetadataConnectionPropertiesCommand(final String connectorId) {
return new MetadataConnectionPropertiesCommand(config, connectorId, Collections.emptyMap());
}

}
Loading

0 comments on commit db820ca

Please sign in to comment.