From d49f0af15f9985f91a5757edaa7cc43f37f4e844 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Fri, 22 Sep 2023 12:54:48 -0700 Subject: [PATCH] Datasource description (#2132) (#2138) * Fix mockito core * Use mockito version from core * Add datasource description --------- (cherry picked from commit 7bad8f9aaaf353eeeb9990fb720367cbfd5849c5) Signed-off-by: Vamsi Manohar Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../datasource/model/DataSourceMetadata.java | 25 +++++++++++++++++-- .../utils/XContentParserUtils.java | 9 ++++++- .../utils/XContentParserUtilsTest.java | 2 +- .../sql/datasource/DataSourceAPIsIT.java | 3 +++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java b/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java index 8e5ff7e1a6..a61f5a7a20 100644 --- a/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java +++ b/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java @@ -11,26 +11,29 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; -import lombok.NoArgsConstructor; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import org.opensearch.sql.datasource.DataSourceService; @Getter @Setter @AllArgsConstructor -@NoArgsConstructor @EqualsAndHashCode @JsonIgnoreProperties(ignoreUnknown = true) public class DataSourceMetadata { @JsonProperty private String name; + @JsonProperty private String description; + @JsonProperty @JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) private DataSourceType connector; @@ -39,6 +42,24 @@ public class DataSourceMetadata { @JsonProperty private Map properties; + public DataSourceMetadata( + String name, + DataSourceType connector, + List allowedRoles, + Map properties) { + this.name = name; + this.connector = connector; + this.description = StringUtils.EMPTY; + this.properties = properties; + this.allowedRoles = allowedRoles; + } + + public DataSourceMetadata() { + this.description = StringUtils.EMPTY; + this.allowedRoles = new ArrayList<>(); + this.properties = new HashMap<>(); + } + /** * Default OpenSearch {@link DataSourceMetadata}. Which is used to register default OpenSearch * {@link DataSource} to {@link DataSourceService}. diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java b/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java index 1ad79addac..53f0054dc2 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import lombok.experimental.UtilityClass; +import org.apache.commons.lang3.StringUtils; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.xcontent.DeprecationHandler; @@ -26,6 +27,7 @@ @UtilityClass public class XContentParserUtils { public static final String NAME_FIELD = "name"; + public static final String DESCRIPTION_FIELD = "description"; public static final String CONNECTOR_FIELD = "connector"; public static final String PROPERTIES_FIELD = "properties"; public static final String ALLOWED_ROLES_FIELD = "allowedRoles"; @@ -39,6 +41,7 @@ public class XContentParserUtils { */ public static DataSourceMetadata toDataSourceMetadata(XContentParser parser) throws IOException { String name = null; + String description = StringUtils.EMPTY; DataSourceType connector = null; List allowedRoles = new ArrayList<>(); Map properties = new HashMap<>(); @@ -50,6 +53,9 @@ public static DataSourceMetadata toDataSourceMetadata(XContentParser parser) thr case NAME_FIELD: name = parser.textOrNull(); break; + case DESCRIPTION_FIELD: + description = parser.textOrNull(); + break; case CONNECTOR_FIELD: connector = DataSourceType.fromString(parser.textOrNull()); break; @@ -74,7 +80,7 @@ public static DataSourceMetadata toDataSourceMetadata(XContentParser parser) thr if (name == null || connector == null) { throw new IllegalArgumentException("name and connector are required fields."); } - return new DataSourceMetadata(name, connector, allowedRoles, properties); + return new DataSourceMetadata(name, description, connector, allowedRoles, properties); } /** @@ -108,6 +114,7 @@ public static XContentBuilder convertToXContent(DataSourceMetadata metadata) thr XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); builder.field(NAME_FIELD, metadata.getName()); + builder.field(DESCRIPTION_FIELD, metadata.getDescription()); builder.field(CONNECTOR_FIELD, metadata.getConnector().name()); builder.field(ALLOWED_ROLES_FIELD, metadata.getAllowedRoles().toArray()); builder.startObject(PROPERTIES_FIELD); diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java index c0c05c0282..eab0b8e168 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java @@ -31,7 +31,7 @@ public void testConvertToXContent() { XContentBuilder contentBuilder = XContentParserUtils.convertToXContent(dataSourceMetadata); String contentString = BytesReference.bytes(contentBuilder).utf8ToString(); Assertions.assertEquals( - "{\"name\":\"testDS\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.uri\":\"https://localhost:9090\"}}", + "{\"name\":\"testDS\",\"description\":\"\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.uri\":\"https://localhost:9090\"}}", contentString); } diff --git a/integ-test/src/test/java/org/opensearch/sql/datasource/DataSourceAPIsIT.java b/integ-test/src/test/java/org/opensearch/sql/datasource/DataSourceAPIsIT.java index c2857a2bc1..6a6b4e7ba3 100644 --- a/integ-test/src/test/java/org/opensearch/sql/datasource/DataSourceAPIsIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/datasource/DataSourceAPIsIT.java @@ -51,6 +51,7 @@ public void createDataSourceAPITest() { DataSourceMetadata createDSM = new DataSourceMetadata( "create_prometheus", + "Prometheus Creation for Integ test", DataSourceType.PROMETHEUS, ImmutableList.of(), ImmutableMap.of( @@ -79,6 +80,7 @@ public void createDataSourceAPITest() { new Gson().fromJson(getResponseString, DataSourceMetadata.class); Assert.assertEquals( "https://localhost:9090", dataSourceMetadata.getProperties().get("prometheus.uri")); + Assert.assertEquals("Prometheus Creation for Integ test", dataSourceMetadata.getDescription()); } @SneakyThrows @@ -122,6 +124,7 @@ public void updateDataSourceAPITest() { new Gson().fromJson(getResponseString, DataSourceMetadata.class); Assert.assertEquals( "https://randomtest.com:9090", dataSourceMetadata.getProperties().get("prometheus.uri")); + Assert.assertEquals("", dataSourceMetadata.getDescription()); } @SneakyThrows