Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: support template configuration #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion aws-kendra-datasource/aws-kendra-datasource.json
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,18 @@
"OrganizationId"
]
},
"TemplateConfiguration": {
"type": "object",
"properties": {
"Template": {
"type": "object"
}
},
"additionalProperties": false,
"required": [
"Template"
]
},
"DataSourceConfiguration": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1416,6 +1428,9 @@
},
"WorkDocsConfiguration": {
"$ref": "#/definitions/WorkDocsConfiguration"
},
"TemplateConfiguration": {
"$ref": "#/definitions/TemplateConfiguration"
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -1469,6 +1484,11 @@
"required": [
"WorkDocsConfiguration"
]
},
{
"required": [
"TemplateConfiguration"
]
}
]
},
Expand All @@ -1492,7 +1512,8 @@
"CONFLUENCE",
"GOOGLEDRIVE",
"WEBCRAWLER",
"WORKDOCS"
"WORKDOCS",
"TEMPLATE"
]
},
"Description": {
Expand Down
7 changes: 6 additions & 1 deletion aws-kendra-datasource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>software.amazon.cloudformation</groupId>
<artifactId>aws-cloudformation-rpdk-java-plugin</artifactId>
<version>2.0.6</version>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
Expand Down Expand Up @@ -66,6 +66,11 @@
<version>2.26.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import software.amazon.kendra.datasource.convert.SalesforceConverter;
import software.amazon.kendra.datasource.convert.ServiceNowConverter;
import software.amazon.kendra.datasource.convert.SharePointConverter;
import software.amazon.kendra.datasource.convert.TemplateConverter;
import software.amazon.kendra.datasource.convert.WebCrawlerConverter;
import software.amazon.kendra.datasource.convert.WorkDocsConverter;
import software.amazon.kendra.datasource.convert.cde.CustomDocumentEnrichmentConfigurationConverter;
Expand Down Expand Up @@ -233,6 +234,7 @@ static software.amazon.awssdk.services.kendra.model.DataSourceConfiguration toSd
modelDataSourceConfiguration.googleDriveConfiguration(GoogleDriveConverter.toSdkDataSourceConfiguration(dataSourceConfiguration.getGoogleDriveConfiguration()));
modelDataSourceConfiguration.webCrawlerConfiguration(WebCrawlerConverter.toSdkDataSourceConfiguration(dataSourceConfiguration.getWebCrawlerConfiguration()));
modelDataSourceConfiguration.workDocsConfiguration(WorkDocsConverter.toSdkDataSourceConfiguration(dataSourceConfiguration.getWorkDocsConfiguration()));
modelDataSourceConfiguration.templateConfiguration(TemplateConverter.toSdkDataSourceConfiguration(dataSourceConfiguration.getTemplateConfiguration()));
return modelDataSourceConfiguration.build();
}

Expand All @@ -258,7 +260,9 @@ static DataSourceConfiguration toModelDataSourceConfiguration(
return WebCrawlerConverter.toModelDataSourceConfiguration(dataSourceConfiguration.webCrawlerConfiguration());
} else if (DataSourceType.WORKDOCS.toString().equals(dataSourceType)) {
return WorkDocsConverter.toModelDataSourceConfiguration(dataSourceConfiguration.workDocsConfiguration());
} else {
} else if(DataSourceType.TEMPLATE.toString().equals(dataSourceType)){
return TemplateConverter.toModelDataSourceConfiguration(dataSourceConfiguration.templateConfiguration());
}else {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package software.amazon.kendra.datasource.convert;

import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;

import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.core.document.internal.BooleanDocument;
import software.amazon.awssdk.core.document.internal.ListDocument;
import software.amazon.awssdk.core.document.internal.MapDocument;
import software.amazon.awssdk.core.document.internal.NullDocument;
import software.amazon.awssdk.core.document.internal.NumberDocument;
import software.amazon.awssdk.core.document.internal.StringDocument;
import software.amazon.kendra.datasource.DataSourceConfiguration;
import software.amazon.kendra.datasource.TemplateConfiguration;
import software.amazon.kendra.datasource.utils.DocumentTypeAdapter;

public class TemplateConverter {
private static final Gson builder = new GsonBuilder()
.registerTypeAdapter(Document.class, new DocumentTypeAdapter())
.registerTypeAdapter(BooleanDocument.class, new DocumentTypeAdapter())
.registerTypeAdapter(ListDocument.class, new DocumentTypeAdapter())
.registerTypeAdapter(MapDocument.class, new DocumentTypeAdapter())
.registerTypeAdapter(NullDocument.class, new DocumentTypeAdapter())
.registerTypeAdapter(NumberDocument.class, new DocumentTypeAdapter())
.registerTypeAdapter(StringDocument.class, new DocumentTypeAdapter())
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.setPrettyPrinting()
.serializeNulls()
.create();

public static software.amazon.awssdk.services.kendra.model.TemplateConfiguration toSdkDataSourceConfiguration(TemplateConfiguration model) {
if (model == null) {
return null;
}
return software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder()
.template(builder.fromJson(builder.toJson(model.getTemplate()), Document.class))
.build();
}

public static DataSourceConfiguration toModelDataSourceConfiguration(
software.amazon.awssdk.services.kendra.model.TemplateConfiguration templateConfiguration) {
return DataSourceConfiguration.builder()
.templateConfiguration(toModel(templateConfiguration))
.build();
}

@SuppressWarnings("unchecked")
private static TemplateConfiguration toModel(software.amazon.awssdk.services.kendra.model.TemplateConfiguration sdk) {
if (sdk == null) {
return null;
}
return TemplateConfiguration.builder()
.template(builder.fromJson(builder.toJson(sdk.template().asMap()), Map.class))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package software.amazon.kendra.datasource.utils;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.protocols.json.internal.unmarshall.document.DocumentUnmarshaller;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;

public class DocumentTypeAdapter implements JsonDeserializer<Document>, JsonSerializer<Document> {
private final DocumentUnmarshaller documentUnmarshaller = new DocumentUnmarshaller();

@Override
public Document deserialize(
final JsonElement jsonElement, final Type type, final JsonDeserializationContext context)
throws JsonParseException {
final JsonNodeParser jsonNodeParser = JsonNodeParser.create();
final JsonNode jsonNode = jsonNodeParser.parse(jsonElement.toString());
return jsonNode.visit(documentUnmarshaller);
}

@Override
public JsonElement serialize(
final Document document, final Type type, final JsonSerializationContext context) {
if(document.isNumber()) {
if (document.asNumber().stringValue().contains(".")) {
return context.serialize(document.asNumber().doubleValue());
}
return context.serialize(document.asNumber().intValue());
}
return context.serialize(document.unwrap());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package software.amazon.kendra.datasource;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.document.Document;
Expand All @@ -20,6 +21,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import software.amazon.awssdk.services.kendra.model.DataSourceType;
import software.amazon.kendra.datasource.utils.DocumentTypeAdapter;

public class TranslatorTest {
private static final String DATASOURCE_CONFIGURATION =
Expand Down Expand Up @@ -546,5 +548,44 @@ void testTranslateS3_WHEN_Configuration_NULL() {
assertThat(Translator.translateToCreateRequest(resourceModel)).isEqualTo(expected);
}

@Test
void testTranslateToSdkTemplate() throws IOException {
DataSourceConfiguration dataSourceConfiguration = DataSourceConfiguration
.builder()
.templateConfiguration(TemplateConfiguration.builder().template(readFileFromLocal()).build())
.build();

assertThat(Translator.toSdkDataSourceConfiguration(dataSourceConfiguration))
.isEqualTo(software.amazon.awssdk.services.kendra.model.DataSourceConfiguration.builder()
.templateConfiguration(software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder().
template(getTemplate()).build())
.build());
}

@Test
void testTranslateToCreateRequest_WithTemplateConfig() throws IOException {
String indexId = "indexId";
ResourceModel resourceModel = ResourceModel
.builder()
.indexId(indexId)
.type("TEMPLATE")
.dataSourceConfiguration(DataSourceConfiguration.builder()
.templateConfiguration(TemplateConfiguration.builder().template(readFileFromLocal()).build())
.build())
.build();
CreateDataSourceRequest createDataSourceRequest = Translator.translateToCreateRequest(resourceModel);
assertThat(createDataSourceRequest.indexId()).isEqualTo(indexId);
assertThat(createDataSourceRequest.configuration().templateConfiguration()).isNotNull();
}

private Document getTemplate() throws IOException {
Gson builder = new GsonBuilder()
.registerTypeAdapter(Document.class, new DocumentTypeAdapter())
.create();
return builder.fromJson(this.readFileFromLocal(), Document.class);
}

private String readFileFromLocal() throws IOException {
return FileUtils.readFileToString(new File(TranslatorTest.DATASOURCE_CONFIGURATION), Charset.defaultCharset());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package software.amazon.kendra.datasource.convert;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.document.Document;
import software.amazon.kendra.datasource.DataSourceConfiguration;
import software.amazon.kendra.datasource.TemplateConfiguration;
import software.amazon.kendra.datasource.utils.DocumentTypeAdapter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;


public class TemplateConverterTest {
private static final String DATASOURCE_CONFIGURATION =
"testdata/datasourceconfiguration/valid_datasource_configuration.json";

@Test
public void testSdkDataSourceConfiguration_Null() {
assertThat(TemplateConverter.toSdkDataSourceConfiguration(null))
.isEqualTo(null);
}

@Test
public void testModelDataSourceConfiguration_Null() {
assertThat(TemplateConverter.toModelDataSourceConfiguration(null)
.getTemplateConfiguration()).isEqualTo(null);
}

@Test
public void testSDKDataSourceConfigurationWithValidConfig() throws IOException {
DataSourceConfiguration dataSourceConfiguration = DataSourceConfiguration.builder()
.templateConfiguration(TemplateConfiguration.builder()
.template(readFileFromLocal(DATASOURCE_CONFIGURATION))
.build())
.build();

software.amazon.awssdk.services.kendra.model.TemplateConfiguration expectedDataSourceConfiguration =
software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder()
.template(getTemplate(DATASOURCE_CONFIGURATION)).build();

assertThat(TemplateConverter.toSdkDataSourceConfiguration(dataSourceConfiguration.getTemplateConfiguration()))
.isEqualTo(expectedDataSourceConfiguration);

}

@Test
public void testModelDataSourceConfigurationWithValidConfig() throws IOException {
software.amazon.awssdk.services.kendra.model.TemplateConfiguration sdkDataSourceConfiguration =
software.amazon.awssdk.services.kendra.model.TemplateConfiguration.builder()
.template(getTemplate(DATASOURCE_CONFIGURATION)).build();

DataSourceConfiguration expectedDataSourceConfiguration = DataSourceConfiguration.builder()
.templateConfiguration(TemplateConfiguration.builder()
.template(readFileFromLocal(DATASOURCE_CONFIGURATION))
.build())
.build();

Gson gson = new Gson();
DataSourceConfiguration result = TemplateConverter.toModelDataSourceConfiguration(sdkDataSourceConfiguration);
JsonObject resultTemplateConfig = gson.fromJson(result.getTemplateConfiguration().getTemplate(), JsonObject.class);

JsonObject expectedDataSourceConfigurationTemplateConfig = gson.fromJson(
expectedDataSourceConfiguration.getTemplateConfiguration().getTemplate(), JsonObject.class
);

assertThat(expectedDataSourceConfigurationTemplateConfig)
.isEqualTo(resultTemplateConfig);
}

private Document getTemplate(String filePath) throws IOException {
Gson builder = new GsonBuilder()
.registerTypeAdapter(Document.class, new DocumentTypeAdapter())
.create();
return builder.fromJson(this.readFileFromLocal(filePath), Document.class);
}

private String readFileFromLocal(String filePath) throws IOException {
return FileUtils.readFileToString(new File(filePath), Charset.defaultCharset());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package software.amazon.kendra.datasource.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.document.Document;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DocumentTypeAdapterTest {

private static final String TYPE = "type";

private static final String SERIALIZED_DOCUMENT = "{\"type\":\"test\"}";
private static final Document DOCUMENT = Document.mapBuilder().putString(TYPE, "test").build();
private Gson gson;

@BeforeEach
void setup() {
this.gson = new GsonBuilder()
.registerTypeAdapter(Document.class, new DocumentTypeAdapter())
.create();
}

@Test
public void documentSerialization_valid_argument_success() {
assertEquals(SERIALIZED_DOCUMENT, gson.toJson(DOCUMENT, Document.class));
}

@Test
public void documentDeserialization_valid_argument_success() {
assertEquals(DOCUMENT, gson.fromJson(SERIALIZED_DOCUMENT, Document.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,9 @@
"isCrawlCommunityTopic": "true",
"isCrawlCommunityPost": "true",
"isCrawlCommunityPostComment": "true"
}
},
"booleanValue": true,
"numberValue": 1.23,
"integerValue": 123,
"nullValue": null
}
Loading