100
100
import static org .elasticsearch .rest .action .admin .indices .RestCloseIndexAction .WAIT_FOR_ACTIVE_SHARDS_DEFAULT_DEPRECATION_MESSAGE ;
101
101
import static org .hamcrest .Matchers .anEmptyMap ;
102
102
import static org .hamcrest .Matchers .anyOf ;
103
- import static org .hamcrest .Matchers .containsString ;
104
103
import static org .hamcrest .Matchers .equalTo ;
105
104
import static org .hamcrest .Matchers .everyItem ;
106
105
import static org .hamcrest .Matchers .in ;
@@ -603,31 +602,31 @@ private void wipeCluster() throws Exception {
603
602
* slows down the test because xpack will just recreate
604
603
* them.
605
604
*/
606
- Request request = new Request ("GET" , "_cat/templates" );
607
- request .addParameter ("h" , "name" );
608
- String templates = EntityUtils .toString (adminClient ().performRequest (request ).getEntity ());
609
- if (false == "" .equals (templates )) {
610
- for (String template : templates .split ("\n " )) {
611
- if (isXPackTemplate (template )) continue ;
612
- if ("" .equals (template )) {
613
- throw new IllegalStateException ("empty template in templates list:\n " + templates );
605
+ try {
606
+ Request getTemplatesRequest = new Request ("GET" , "_index_template" );
607
+ getTemplatesRequest .setOptions (allowTypesRemovalWarnings ());
608
+ Map <String , Object > composableIndexTemplates = XContentHelper .convertToMap (JsonXContent .jsonXContent ,
609
+ EntityUtils .toString (adminClient ().performRequest (getTemplatesRequest ).getEntity ()), false );
610
+ List <String > names = ((List <?>) composableIndexTemplates .get ("index_templates" )).stream ()
611
+ .map (ct -> (String ) ((Map <?, ?>) ct ).get ("name" ))
612
+ .collect (Collectors .toList ());
613
+ for (String name : names ) {
614
+ if (isXPackTemplate (name )) {
615
+ continue ;
614
616
}
615
- logger .info ("Clearing template [{}]" , template );
616
617
try {
617
- adminClient ().performRequest (new Request ("DELETE" , "_template /" + template ));
618
+ adminClient ().performRequest (new Request ("DELETE" , "_index_template /" + name ));
618
619
} catch (ResponseException e ) {
619
- // This is fine, it could be a V2 template
620
- assertThat (e .getMessage (), containsString ("index_template [" + template + "] missing" ));
621
- try {
622
- adminClient ().performRequest (new Request ("DELETE" , "_index_template/" + template ));
623
- } catch (ResponseException e2 ) {
624
- // We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
625
- }
620
+ logger .debug (new ParameterizedMessage ("unable to remove index template {}" , name ), e );
626
621
}
627
622
}
623
+ } catch (Exception e ) {
624
+ logger .info ("ignoring exception removing all composable index templates" , e );
625
+ // We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
628
626
}
629
627
try {
630
628
Request compReq = new Request ("GET" , "_component_template" );
629
+ compReq .setOptions (allowTypesRemovalWarnings ());
631
630
String componentTemplates = EntityUtils .toString (adminClient ().performRequest (compReq ).getEntity ());
632
631
Map <String , Object > cTemplates = XContentHelper .convertToMap (JsonXContent .jsonXContent , componentTemplates , false );
633
632
@ SuppressWarnings ("unchecked" )
@@ -641,13 +640,27 @@ private void wipeCluster() throws Exception {
641
640
}
642
641
adminClient ().performRequest (new Request ("DELETE" , "_component_template/" + componentTemplate ));
643
642
} catch (ResponseException e ) {
644
- logger .debug (new ParameterizedMessage ("unable to remove component template {}" , componentTemplate ), e );
643
+ logger .debug (new ParameterizedMessage ("unable to remove component template {}" , componentTemplate ), e );
645
644
}
646
645
}
647
646
} catch (Exception e ) {
648
647
logger .info ("ignoring exception removing all component templates" , e );
649
648
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
650
649
}
650
+ Request getLegacyTemplatesRequest = new Request ("GET" , "_template" );
651
+ getLegacyTemplatesRequest .setOptions (allowTypesRemovalWarnings ());
652
+ Map <String , Object > legacyTemplates = XContentHelper .convertToMap (JsonXContent .jsonXContent ,
653
+ EntityUtils .toString (adminClient ().performRequest (getLegacyTemplatesRequest ).getEntity ()), false );
654
+ for (String name : legacyTemplates .keySet ()) {
655
+ if (isXPackTemplate (name )) {
656
+ continue ;
657
+ }
658
+ try {
659
+ adminClient ().performRequest (new Request ("DELETE" , "_template/" + name ));
660
+ } catch (ResponseException e ) {
661
+ logger .debug (new ParameterizedMessage ("unable to remove index template {}" , name ), e );
662
+ }
663
+ }
651
664
} else {
652
665
logger .debug ("Clearing all templates" );
653
666
adminClient ().performRequest (new Request ("DELETE" , "_template/*" ));
0 commit comments