Skip to content

Commit

Permalink
Generate basic index template operations (opensearch-project#1293)
Browse files Browse the repository at this point in the history
* Generate basic index template operations

Generate indices.get_index_template

Signed-off-by: Thomas Farr <[email protected]>

Generate indices.put_index_template

Signed-off-by: Thomas Farr <[email protected]>

Generate indices.exists_index_template

Signed-off-by: Thomas Farr <[email protected]>

Generate indices.delete_index_template

Signed-off-by: Thomas Farr <[email protected]>

Generate indices.simulate_index_template

Signed-off-by: Thomas Farr <[email protected]>

* Allow deserializing path params from JSON

Signed-off-by: Thomas Farr <[email protected]>

* Fix compile

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit c7103d8)
  • Loading branch information
Xtansia committed Nov 20, 2024
1 parent 12aed71 commit df2b1bd
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.hc.core5.http.impl.EnglishReasonPhraseCatalog;

public class FunnellingHttpsProxy implements Closeable {
private static final int SO_TIMEOUT = 5000;
private static final int SO_TIMEOUT = 10000;

@Nonnull
private final ServerSocket serverSocket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.opensearch.client.opensearch.indices.CreateDataStreamResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.CreateIndexResponse;
import org.opensearch.client.opensearch.indices.DataStream;
import org.opensearch.client.opensearch.indices.DataStreamsStatsResponse;
import org.opensearch.client.opensearch.indices.DeleteDataStreamResponse;
import org.opensearch.client.opensearch.indices.GetAliasRequest;
Expand Down Expand Up @@ -127,7 +126,7 @@ public void testDataStream() throws IOException {
PutIndexTemplateResponse putIndexTemplateResponse = javaClient().indices()
.putIndexTemplate(
b -> b.name(dataStreamIndexTemplateName)
.dataStream(new DataStream.Builder().timestampField(bd -> bd.name(timestampFieldName)).build())
.dataStream(ds -> ds.timestampField(bd -> bd.name(timestampFieldName)))
.indexPatterns(namePattern)
);
assertTrue(putIndexTemplateResponse.acknowledged());
Expand Down
22 changes: 17 additions & 5 deletions java-codegen/opensearch-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10210,8 +10210,8 @@ paths:
requestBody:
$ref: '#/components/requestBodies/create'
responses:
'200':
$ref: '#/components/responses/create___200'
'201':
$ref: '#/components/responses/create___201'
put:
operationId: create.1
x-operation-group: create
Expand Down Expand Up @@ -10240,8 +10240,8 @@ paths:
requestBody:
$ref: '#/components/requestBodies/create'
responses:
'200':
$ref: '#/components/responses/create___200'
'201':
$ref: '#/components/responses/create___201'
/{index}/_delete_by_query:
post:
operationId: delete_by_query.0
Expand Down Expand Up @@ -10415,6 +10415,8 @@ paths:
responses:
'200':
$ref: '#/components/responses/exists___200'
'404':
$ref: '#/components/responses/exists___404'
post:
operationId: index.1
x-operation-group: index
Expand Down Expand Up @@ -28037,7 +28039,7 @@ components:
type: integer
format: int64
description: ''
create___200:
create___201:
content:
application/json:
schema:
Expand Down Expand Up @@ -28137,6 +28139,10 @@ components:
content:
application/json: {}
description: ''
exists___404:
content:
application/json: {}
description: ''
explain___200:
content:
application/json:
Expand Down Expand Up @@ -39275,6 +39281,12 @@ components:
$ref: '#/components/schemas/_common.analysis___IcuCollationStrength'
variable_top:
type: string
country:
type: string
language:
type: string
variant:
type: string
required:
- type
_common.mapping___IndexField:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ public void setShortcutProperty(String shortcutProperty) {
this.shortcutProperty = shortcutProperty;
}

public Collection<Field> getFieldsToSerialize() {
return getBodyFields();
}

public boolean hasFieldsToSerialize() {
return !bodyFields.isEmpty() || additionalPropertiesField != null;
}

public Collection<Field> getFieldsToDeserialize() {
return getBodyFields();
}

public Collection<ReferencingDiscriminatedUnion> getReferencingDiscriminatedUnions() {
return getIncomingReference(ReferenceKind.UnionVariant).stream()
.map(s -> (TaggedUnionShape) s)
Expand Down Expand Up @@ -119,7 +127,11 @@ public Collection<Type> getAnnotations() {
}

public boolean shouldImplementPlainDeserializable() {
return Set.of("SourceField", "TypeMapping").contains(getClassName());
return Set.of("IndexTemplateMapping", "SourceField", "TypeMapping").contains(getClassName());
}

public boolean canBeSingleton() {
return bodyFields.isEmpty() && additionalPropertiesField == null && !extendsOtherShape();
}

public static class ReferencingDiscriminatedUnion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@ public Type getResponseType() {
: Types.Client.Transport.Endpoints.BooleanResponse;
}

@Override
public boolean canBeSingleton() {
return !hasRequestBody() && !hasQueryParams() && hasSinglePath() && !getFirstPath().hasParams();
}

@Override
public Collection<Field> getFieldsToDeserialize() {
var fields = new TreeMap<>(bodyFields);
fields.putAll(pathParams);
return fields.values();
}

public boolean hasRequestBody() {
return !getBodyFields().isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{{>Partials/ClassDeclaration}} {
{{#canBeSingleton}}
public {{className}}() {}

/**
* Singleton instance for {@link {{className}}}.
*/
public static final {{className}} _INSTANCE = new {{className}}();

public static final {{TYPES.Client.Json.JsonpDeserializer}}<{{className}}> _DESERIALIZER = {{TYPES.Client.Json.JsonpDeserializer}}.fixedValue({{className}}._INSTANCE);
{{/canBeSingleton}}
{{^canBeSingleton}}
{{>ObjectShape/Fields}}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -28,6 +39,7 @@
// ---------------------------------------------------------------------------------------------

{{>ObjectShape/Deserialize}}
{{/canBeSingleton}}

{{>ObjectShape/HashCode}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
{{#extendsOtherShape}}
setup{{extendsType.name}}Deserializer(op{{#extendsType.typeParams}}, {{>Type/deserializer}}{{/extendsType.typeParams}});
{{/extendsOtherShape}}
{{#bodyFields}}
{{#fieldsToDeserialize}}
op.add({{#extendedByOtherShape}}Abstract{{/extendedByOtherShape}}Builder::{{name}}, {{#type}}{{>Type/deserializer}}{{/type}}, {{#quoted}}{{wireName}}{{/quoted}}{{#aliases}}, {{#quoted}}{{.}}{{/quoted}}{{/aliases}});
{{/bodyFields}}
{{/fieldsToDeserialize}}
{{#additionalPropertiesField}}
op.setUnknownFieldHandler((builder, name, parser, mapper) -> {
if (builder.{{name}} == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{{#additionalPropertiesField}}
{{#type.directSerializer}}this.{{name}}{{/type.directSerializer}}
{{/additionalPropertiesField}}
{{#bodyFields}}
{{#fieldsToSerialize}}
{{^required}}
if ({{#type.isDefined}}this.{{name}}{{/type.isDefined}}) {
{{/required}}
Expand All @@ -32,5 +32,5 @@
{{^-last}}

{{/-last}}
{{/bodyFields}}
{{/fieldsToSerialize}}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{>Partials/ClassDeclaration}} {
{{#canBeSingleton}}
{{#canBeSingleton}}
public {{className}}() {}

/**
* Singleton instance for {@link {{className}}}.
*/
public static final {{className}} _INSTANCE = new {{className}}();
{{/canBeSingleton}}
{{^canBeSingleton}}
{{/canBeSingleton}}
{{^canBeSingleton}}
{{>ObjectShape/Fields}}

// ---------------------------------------------------------------------------------------------
Expand All @@ -16,19 +16,21 @@

{{>ObjectShape/Getters}}

{{#hasRequestBody}}
{{#hasRequestBody}}
{{>ObjectShape/Serialize}}
{{/hasRequestBody}}
{{/hasRequestBody}}

// ---------------------------------------------------------------------------------------------

{{>ObjectShape/Builder}}
{{#hasRequestBody}}
{{#hasRequestBody}}

// ---------------------------------------------------------------------------------------------

{{>ObjectShape/Deserialize}}{{/hasRequestBody}}
{{/canBeSingleton}}
{{>ObjectShape/Deserialize}}

{{/hasRequestBody}}
{{/canBeSingleton}}

// ---------------------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.opensearch.indices.CreateDataStreamRequest;
import org.opensearch.client.opensearch.indices.DataStream;
import org.opensearch.client.opensearch.indices.DataStreamsStatsRequest;
import org.opensearch.client.opensearch.indices.DataStreamsStatsResponse;
import org.opensearch.client.opensearch.indices.DeleteDataStreamRequest;
Expand Down Expand Up @@ -40,7 +39,7 @@ public static void main(String[] args) {
// Create an index template which configures data stream
PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder().name(dataStreamIndexTemplateName)
.indexPatterns(namePattern)
.dataStream(new DataStream.Builder().timestampField(t -> t.name(timestampFieldName)).build())
.dataStream(ds -> ds.timestampField(t -> t.name(timestampFieldName)))
.build();
client.indices().putIndexTemplate(putIndexTemplateRequest);

Expand Down

0 comments on commit df2b1bd

Please sign in to comment.