Skip to content

Commit

Permalink
Datasource description (#2132) (#2138)
Browse files Browse the repository at this point in the history
* Fix mockito core



* Use mockito version from core



* Add datasource description



---------


(cherry picked from commit 7bad8f9)

Signed-off-by: Vamsi Manohar <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 59dacdc commit d49f0af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +42,24 @@ public class DataSourceMetadata {

@JsonProperty private Map<String, String> properties;

public DataSourceMetadata(
String name,
DataSourceType connector,
List<String> allowedRoles,
Map<String, String> 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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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<String> allowedRoles = new ArrayList<>();
Map<String, String> properties = new HashMap<>();
Expand All @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void createDataSourceAPITest() {
DataSourceMetadata createDSM =
new DataSourceMetadata(
"create_prometheus",
"Prometheus Creation for Integ test",
DataSourceType.PROMETHEUS,
ImmutableList.of(),
ImmutableMap.of(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d49f0af

Please sign in to comment.