@@ -76,6 +76,8 @@ import javax.management.MBeanServerInvocationHandler
76
76
import javax.management.ObjectName
77
77
import javax.management.remote.JMXConnectorFactory
78
78
import javax.management.remote.JMXServiceURL
79
+ import kotlin.collections.ArrayList
80
+ import kotlin.collections.HashMap
79
81
80
82
/* *
81
83
* Superclass for tests that interact with an external test cluster using OpenSearch's RestClient
@@ -909,7 +911,7 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
909
911
private fun indexDoc (client : RestClient , index : String , id : String , doc : String , refresh : Boolean = true): Response {
910
912
val requestBody = StringEntity (doc, APPLICATION_JSON )
911
913
val params = if (refresh) mapOf (" refresh" to " true" ) else mapOf ()
912
- val response = client.makeRequest(" PUT " , " $index /_doc/$id " , params, requestBody)
914
+ val response = client.makeRequest(" POST " , " $index /_doc/$id ?op_type=create " , params, requestBody)
913
915
assertTrue(
914
916
" Unable to index doc: '${doc.take(15 )} ...' to index: '$index '" ,
915
917
listOf (RestStatus .OK , RestStatus .CREATED ).contains(response.restStatus())
@@ -945,6 +947,11 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
945
947
return index
946
948
}
947
949
950
+ protected fun createTestIndex (index : String , mapping : String? , alias : String ): String {
951
+ createIndex(index, Settings .EMPTY , mapping?.trimIndent(), alias)
952
+ return index
953
+ }
954
+
948
955
protected fun createTestConfigIndex (index : String = "." + randomAlphaOfLength(10).lowercase(Locale .ROOT )): String {
949
956
try {
950
957
createIndex(
@@ -981,7 +988,7 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
981
988
val indicesMap = mutableMapOf<String , Boolean >()
982
989
val indicesJson = jsonBuilder().startObject().startArray(" actions" )
983
990
indices.keys.map {
984
- val indexName = createTestIndex(index = it.lowercase( Locale . ROOT ) , mapping = " " )
991
+ val indexName = createTestIndex(index = it, mapping = " " )
985
992
val isWriteIndex = indices.getOrDefault(indexName, false )
986
993
indicesMap[indexName] = isWriteIndex
987
994
val indexMap = mapOf (
@@ -998,17 +1005,155 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
998
1005
return mutableMapOf (alias to indicesMap)
999
1006
}
1000
1007
1008
+ protected fun createDataStream (datastream : String , mappings : String? , useComponentTemplate : Boolean ) {
1009
+ val indexPattern = " $datastream *"
1010
+ var componentTemplateMappings = " \" properties\" : {" +
1011
+ " \" netflow.destination_transport_port\" :{ \" type\" : \" long\" }," +
1012
+ " \" netflow.destination_ipv4_address\" :{ \" type\" : \" ip\" }" +
1013
+ " }"
1014
+ if (mappings != null ) {
1015
+ componentTemplateMappings = mappings
1016
+ }
1017
+ if (useComponentTemplate) {
1018
+ // Setup index_template
1019
+ createComponentTemplateWithMappings(
1020
+ " my_ds_component_template-$datastream " ,
1021
+ componentTemplateMappings
1022
+ )
1023
+ }
1024
+ createComposableIndexTemplate(
1025
+ " my_index_template_ds-$datastream " ,
1026
+ listOf (indexPattern),
1027
+ (if (useComponentTemplate) " my_ds_component_template-$datastream " else null ),
1028
+ mappings,
1029
+ true ,
1030
+ 0
1031
+ )
1032
+ createDataStream(datastream)
1033
+ }
1034
+
1035
+ protected fun createDataStream (datastream : String? = randomAlphaOfLength(10).lowercase(Locale .ROOT )) {
1036
+ client().makeRequest(" PUT" , " _data_stream/$datastream " )
1037
+ }
1038
+
1039
+ protected fun deleteDataStream (datastream : String ) {
1040
+ client().makeRequest(" DELETE" , " _data_stream/$datastream " )
1041
+ }
1042
+
1043
+ protected fun createIndexAlias (alias : String , mappings : String? ) {
1044
+ val indexPattern = " $alias *"
1045
+ var componentTemplateMappings = " \" properties\" : {" +
1046
+ " \" netflow.destination_transport_port\" :{ \" type\" : \" long\" }," +
1047
+ " \" netflow.destination_ipv4_address\" :{ \" type\" : \" ip\" }" +
1048
+ " }"
1049
+ if (mappings != null ) {
1050
+ componentTemplateMappings = mappings
1051
+ }
1052
+ createComponentTemplateWithMappings(
1053
+ " my_alias_component_template-$alias " ,
1054
+ componentTemplateMappings
1055
+ )
1056
+ createComposableIndexTemplate(
1057
+ " my_index_template_alias-$alias " ,
1058
+ listOf (indexPattern),
1059
+ " my_alias_component_template-$alias " ,
1060
+ mappings,
1061
+ false ,
1062
+ 0
1063
+ )
1064
+ createTestIndex(
1065
+ " $alias -000001" ,
1066
+ null ,
1067
+ """
1068
+ "$alias ": {
1069
+ "is_write_index": true
1070
+ }
1071
+ """ .trimIndent()
1072
+ )
1073
+ }
1074
+
1075
+ protected fun deleteIndexAlias (alias : String ) {
1076
+ client().makeRequest(" DELETE" , " $alias */_alias/$alias " )
1077
+ }
1078
+
1079
+ protected fun createComponentTemplateWithMappings (componentTemplateName : String , mappings : String? ) {
1080
+ val body = """ {"template" : { "mappings": {$mappings } }}"""
1081
+ client().makeRequest(
1082
+ " PUT" ,
1083
+ " _component_template/$componentTemplateName " ,
1084
+ emptyMap(),
1085
+ StringEntity (body, ContentType .APPLICATION_JSON ),
1086
+ BasicHeader (" Content-Type" , " application/json" )
1087
+ )
1088
+ }
1089
+
1090
+ protected fun createComposableIndexTemplate (
1091
+ templateName : String ,
1092
+ indexPatterns : List <String >,
1093
+ componentTemplateName : String? ,
1094
+ mappings : String? ,
1095
+ isDataStream : Boolean ,
1096
+ priority : Int
1097
+ ) {
1098
+ var body = " {\n "
1099
+ if (isDataStream) {
1100
+ body + = " \" data_stream\" : { },"
1101
+ }
1102
+ body + = " \" index_patterns\" : [" +
1103
+ indexPatterns.stream().collect(
1104
+ Collectors .joining(" ," , " \" " , " \" " )
1105
+ ) + " ],"
1106
+ if (componentTemplateName == null ) {
1107
+ body + = " \" template\" : {\" mappings\" : {$mappings }},"
1108
+ }
1109
+ if (componentTemplateName != null ) {
1110
+ body + = " \" composed_of\" : [\" $componentTemplateName \" ],"
1111
+ }
1112
+ body + = " \" priority\" :$priority }"
1113
+ client().makeRequest(
1114
+ " PUT" ,
1115
+ " _index_template/$templateName " ,
1116
+ emptyMap(),
1117
+ StringEntity (body, APPLICATION_JSON ),
1118
+ BasicHeader (" Content-Type" , " application/json" )
1119
+ )
1120
+ }
1121
+
1122
+ protected fun getDatastreamWriteIndex (datastream : String ): String {
1123
+ val response = client().makeRequest(" GET" , " _data_stream/$datastream " , emptyMap(), null )
1124
+ var respAsMap = responseAsMap(response)
1125
+ if (respAsMap.containsKey(" data_streams" )) {
1126
+ respAsMap = (respAsMap[" data_streams" ] as ArrayList <HashMap <String , * >>)[0 ]
1127
+ val indices = respAsMap[" indices" ] as List <Map <String , Any >>
1128
+ val index = indices.last()
1129
+ return index[" index_name" ] as String
1130
+ } else {
1131
+ respAsMap = respAsMap[datastream] as Map <String , Object >
1132
+ }
1133
+ val indices = respAsMap[" indices" ] as Array <String >
1134
+ return indices.last()
1135
+ }
1136
+
1137
+ protected fun rolloverDatastream (datastream : String ) {
1138
+ client().makeRequest(
1139
+ " POST" ,
1140
+ datastream + " /_rollover" ,
1141
+ emptyMap(),
1142
+ null
1143
+ )
1144
+ }
1145
+
1001
1146
protected fun randomAliasIndices (
1002
1147
alias : String ,
1003
1148
num : Int = randomIntBetween(1, 10),
1004
1149
includeWriteIndex : Boolean = true,
1005
1150
): Map <String , Boolean > {
1006
1151
val indices = mutableMapOf<String , Boolean >()
1007
- val writeIndex = randomIntBetween(0 , num)
1152
+ val writeIndex = randomIntBetween(0 , num - 1 )
1008
1153
for (i: Int in 0 until num) {
1009
- var indexName = randomAlphaOfLength(10 )
1154
+ var indexName = randomAlphaOfLength(10 ).lowercase( Locale . ROOT )
1010
1155
while (indexName.equals(alias) || indices.containsKey(indexName))
1011
- indexName = randomAlphaOfLength(10 )
1156
+ indexName = randomAlphaOfLength(10 ).lowercase( Locale . ROOT )
1012
1157
indices[indexName] = includeWriteIndex && i == writeIndex
1013
1158
}
1014
1159
return indices
0 commit comments