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

feat(server): autodiscovery circuit breaker call #8022

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -208,11 +208,6 @@
<artifactId>jackson-module-jsonSchema</artifactId>
</dependency>

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

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</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 @@ -140,7 +140,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 @@ -155,9 +155,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.annotations.Api;
import io.swagger.annotations.ApiParam;
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this from the GET /connectors/{id}. I'd like the retrieval of dynamic metadata, i.e. invoking of the syndesis-meta service to be tied to an explicit request from the UI for it, not in every case when the connector is fetched.

GET requests should be idempotent and should not have side effects, this will endanger that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, though it would be out the scope of the request for this PR (circuit breaker). I have created further issue to take care of this refactory #8023 separately. Are you okey to proceed like this?

Also, from performance perspective, right now the "enriching" call is conditioned to the presence of a tag that we are configuring on the connectors which use this feature.

}

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("/{id}/actions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +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.ClientBuilder;
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.swagger.annotations.Api;
import io.syndesis.common.model.connection.DynamicConnectionPropertiesMetadata;
import io.syndesis.server.verifier.MetadataConfigurationProperties;

@Api(value = "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 ClientBuilder.newClient();
protected HystrixExecutable<DynamicConnectionPropertiesMetadata> createMetadataConnectionPropertiesCommand(final String connectorId) {
return new MetadataConnectionPropertiesCommand(config, connectorId, Collections.emptyMap());
}

}
Loading