@@ -70,10 +70,13 @@ import java.time.format.DateTimeFormatter
70
70
import java.time.temporal.ChronoUnit
71
71
import java.util.Locale
72
72
import java.util.UUID
73
+ import java.util.stream.Collectors
73
74
import javax.management.MBeanServerInvocationHandler
74
75
import javax.management.ObjectName
75
76
import javax.management.remote.JMXConnectorFactory
76
77
import javax.management.remote.JMXServiceURL
78
+ import kotlin.collections.ArrayList
79
+ import kotlin.collections.HashMap
77
80
78
81
/* *
79
82
* Superclass for tests that interact with an external test cluster using OpenSearch's RestClient
@@ -774,14 +777,19 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
774
777
private fun indexDoc (client : RestClient , index : String , id : String , doc : String , refresh : Boolean = true): Response {
775
778
val requestBody = StringEntity (doc, APPLICATION_JSON )
776
779
val params = if (refresh) mapOf (" refresh" to " true" ) else mapOf ()
777
- val response = client.makeRequest(" PUT " , " $index /_doc/$id " , params, requestBody)
780
+ val response = client.makeRequest(" POST " , " $index /_doc/$id ?op_type=create " , params, requestBody)
778
781
assertTrue(
779
782
" Unable to index doc: '${doc.take(15 )} ...' to index: '$index '" ,
780
783
listOf (RestStatus .OK , RestStatus .CREATED ).contains(response.restStatus())
781
784
)
782
785
return response
783
786
}
784
787
788
+ protected fun createTestIndex (index : String , mapping : String? , alias : String ): String {
789
+ createIndex(index, Settings .EMPTY , mapping?.trimIndent(), alias)
790
+ return index
791
+ }
792
+
785
793
protected fun deleteDoc (index : String , id : String , refresh : Boolean = true): Response {
786
794
val params = if (refresh) mapOf (" refresh" to " true" ) else mapOf ()
787
795
val response = client().makeRequest(" DELETE" , " $index /_doc/$id " , params)
@@ -846,7 +854,7 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
846
854
val indicesMap = mutableMapOf<String , Boolean >()
847
855
val indicesJson = jsonBuilder().startObject().startArray(" actions" )
848
856
indices.keys.map {
849
- val indexName = createTestIndex(index = it.lowercase( Locale . ROOT ) , mapping = " " )
857
+ val indexName = createTestIndex(index = it, mapping = " " )
850
858
val isWriteIndex = indices.getOrDefault(indexName, false )
851
859
indicesMap[indexName] = isWriteIndex
852
860
val indexMap = mapOf (
@@ -863,17 +871,155 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
863
871
return mutableMapOf (alias to indicesMap)
864
872
}
865
873
874
+ protected fun createDataStream (datastream : String , mappings : String? , useComponentTemplate : Boolean ) {
875
+ val indexPattern = " $datastream *"
876
+ var componentTemplateMappings = " \" properties\" : {" +
877
+ " \" netflow.destination_transport_port\" :{ \" type\" : \" long\" }," +
878
+ " \" netflow.destination_ipv4_address\" :{ \" type\" : \" ip\" }" +
879
+ " }"
880
+ if (mappings != null ) {
881
+ componentTemplateMappings = mappings
882
+ }
883
+ if (useComponentTemplate) {
884
+ // Setup index_template
885
+ createComponentTemplateWithMappings(
886
+ " my_ds_component_template-$datastream " ,
887
+ componentTemplateMappings
888
+ )
889
+ }
890
+ createComposableIndexTemplate(
891
+ " my_index_template_ds-$datastream " ,
892
+ listOf (indexPattern),
893
+ (if (useComponentTemplate) " my_ds_component_template-$datastream " else null ),
894
+ mappings,
895
+ true ,
896
+ 0
897
+ )
898
+ createDataStream(datastream)
899
+ }
900
+
901
+ protected fun createDataStream (datastream : String? = randomAlphaOfLength(10).lowercase(Locale .ROOT )) {
902
+ client().makeRequest(" PUT" , " _data_stream/$datastream " )
903
+ }
904
+
905
+ protected fun deleteDataStream (datastream : String ) {
906
+ client().makeRequest(" DELETE" , " _data_stream/$datastream " )
907
+ }
908
+
909
+ protected fun createIndexAlias (alias : String , mappings : String? ) {
910
+ val indexPattern = " $alias *"
911
+ var componentTemplateMappings = " \" properties\" : {" +
912
+ " \" netflow.destination_transport_port\" :{ \" type\" : \" long\" }," +
913
+ " \" netflow.destination_ipv4_address\" :{ \" type\" : \" ip\" }" +
914
+ " }"
915
+ if (mappings != null ) {
916
+ componentTemplateMappings = mappings
917
+ }
918
+ createComponentTemplateWithMappings(
919
+ " my_alias_component_template-$alias " ,
920
+ componentTemplateMappings
921
+ )
922
+ createComposableIndexTemplate(
923
+ " my_index_template_alias-$alias " ,
924
+ listOf (indexPattern),
925
+ " my_alias_component_template-$alias " ,
926
+ mappings,
927
+ false ,
928
+ 0
929
+ )
930
+ createTestIndex(
931
+ " $alias -000001" ,
932
+ null ,
933
+ """
934
+ "$alias ": {
935
+ "is_write_index": true
936
+ }
937
+ """ .trimIndent()
938
+ )
939
+ }
940
+
941
+ protected fun deleteIndexAlias (alias : String ) {
942
+ client().makeRequest(" DELETE" , " $alias */_alias/$alias " )
943
+ }
944
+
945
+ protected fun createComponentTemplateWithMappings (componentTemplateName : String , mappings : String? ) {
946
+ val body = """ {"template" : { "mappings": {$mappings } }}"""
947
+ client().makeRequest(
948
+ " PUT" ,
949
+ " _component_template/$componentTemplateName " ,
950
+ emptyMap(),
951
+ StringEntity (body, ContentType .APPLICATION_JSON ),
952
+ BasicHeader (" Content-Type" , " application/json" )
953
+ )
954
+ }
955
+
956
+ protected fun createComposableIndexTemplate (
957
+ templateName : String ,
958
+ indexPatterns : List <String >,
959
+ componentTemplateName : String? ,
960
+ mappings : String? ,
961
+ isDataStream : Boolean ,
962
+ priority : Int
963
+ ) {
964
+ var body = " {\n "
965
+ if (isDataStream) {
966
+ body + = " \" data_stream\" : { },"
967
+ }
968
+ body + = " \" index_patterns\" : [" +
969
+ indexPatterns.stream().collect(
970
+ Collectors .joining(" ," , " \" " , " \" " )
971
+ ) + " ],"
972
+ if (componentTemplateName == null ) {
973
+ body + = " \" template\" : {\" mappings\" : {$mappings }},"
974
+ }
975
+ if (componentTemplateName != null ) {
976
+ body + = " \" composed_of\" : [\" $componentTemplateName \" ],"
977
+ }
978
+ body + = " \" priority\" :$priority }"
979
+ client().makeRequest(
980
+ " PUT" ,
981
+ " _index_template/$templateName " ,
982
+ emptyMap(),
983
+ StringEntity (body, APPLICATION_JSON ),
984
+ BasicHeader (" Content-Type" , " application/json" )
985
+ )
986
+ }
987
+
988
+ protected fun getDatastreamWriteIndex (datastream : String ): String {
989
+ val response = client().makeRequest(" GET" , " _data_stream/$datastream " , emptyMap(), null )
990
+ var respAsMap = responseAsMap(response)
991
+ if (respAsMap.containsKey(" data_streams" )) {
992
+ respAsMap = (respAsMap[" data_streams" ] as ArrayList <HashMap <String , * >>)[0 ]
993
+ val indices = respAsMap[" indices" ] as List <Map <String , Any >>
994
+ val index = indices.last()
995
+ return index[" index_name" ] as String
996
+ } else {
997
+ respAsMap = respAsMap[datastream] as Map <String , Object >
998
+ }
999
+ val indices = respAsMap[" indices" ] as Array <String >
1000
+ return indices.last()
1001
+ }
1002
+
1003
+ protected fun rolloverDatastream (datastream : String ) {
1004
+ client().makeRequest(
1005
+ " POST" ,
1006
+ datastream + " /_rollover" ,
1007
+ emptyMap(),
1008
+ null
1009
+ )
1010
+ }
1011
+
866
1012
protected fun randomAliasIndices (
867
1013
alias : String ,
868
1014
num : Int = randomIntBetween(1, 10),
869
- includeWriteIndex : Boolean = true
1015
+ includeWriteIndex : Boolean = true,
870
1016
): Map <String , Boolean > {
871
1017
val indices = mutableMapOf<String , Boolean >()
872
- val writeIndex = randomIntBetween(0 , num)
1018
+ val writeIndex = randomIntBetween(0 , num - 1 )
873
1019
for (i: Int in 0 until num) {
874
- var indexName = randomAlphaOfLength(10 )
1020
+ var indexName = randomAlphaOfLength(10 ).lowercase( Locale . ROOT )
875
1021
while (indexName.equals(alias) || indices.containsKey(indexName))
876
- indexName = randomAlphaOfLength(10 )
1022
+ indexName = randomAlphaOfLength(10 ).lowercase( Locale . ROOT )
877
1023
indices[indexName] = includeWriteIndex && i == writeIndex
878
1024
}
879
1025
return indices
0 commit comments