@@ -84,7 +84,7 @@ public void testAddAndRemove() {
84
84
// Remove the alias from it while adding another one
85
85
before = after ;
86
86
after = service .applyAliasActions (before , Arrays .asList (
87
- new AliasAction .Remove (index , "test" ),
87
+ new AliasAction .Remove (index , "test" , null ),
88
88
new AliasAction .Add (index , "test_2" , null , null , null , null , null )));
89
89
assertNull (after .metadata ().getIndicesLookup ().get ("test" ));
90
90
alias = after .metadata ().getIndicesLookup ().get ("test_2" );
@@ -95,12 +95,52 @@ public void testAddAndRemove() {
95
95
96
96
// Now just remove on its own
97
97
before = after ;
98
- after = service .applyAliasActions (before , singletonList (new AliasAction .Remove (index , "test_2" )));
98
+ after = service .applyAliasActions (before , singletonList (new AliasAction .Remove (index , "test_2" , randomBoolean () )));
99
99
assertNull (after .metadata ().getIndicesLookup ().get ("test" ));
100
100
assertNull (after .metadata ().getIndicesLookup ().get ("test_2" ));
101
101
assertAliasesVersionIncreased (index , before , after );
102
102
}
103
103
104
+ public void testMustExist () {
105
+ // Create a state with a single index
106
+ String index = randomAlphaOfLength (5 );
107
+ ClusterState before = createIndex (ClusterState .builder (ClusterName .DEFAULT ).build (), index );
108
+
109
+ // Add an alias to it
110
+ ClusterState after = service .applyAliasActions (before , singletonList (new AliasAction .Add (index , "test" , null , null , null , null ,
111
+ null )));
112
+ IndexAbstraction alias = after .metadata ().getIndicesLookup ().get ("test" );
113
+ assertNotNull (alias );
114
+ assertThat (alias .getType (), equalTo (IndexAbstraction .Type .ALIAS ));
115
+ assertThat (alias .getIndices (), contains (after .metadata ().index (index )));
116
+ assertAliasesVersionIncreased (index , before , after );
117
+
118
+ // Remove the alias from it with mustExist == true while adding another one
119
+ before = after ;
120
+ after = service .applyAliasActions (before , Arrays .asList (
121
+ new AliasAction .Remove (index , "test" , true ),
122
+ new AliasAction .Add (index , "test_2" , null , null , null , null , null )));
123
+ assertNull (after .metadata ().getIndicesLookup ().get ("test" ));
124
+ alias = after .metadata ().getIndicesLookup ().get ("test_2" );
125
+ assertNotNull (alias );
126
+ assertThat (alias .getType (), equalTo (IndexAbstraction .Type .ALIAS ));
127
+ assertThat (alias .getIndices (), contains (after .metadata ().index (index )));
128
+ assertAliasesVersionIncreased (index , before , after );
129
+
130
+ // Now just remove on its own
131
+ before = after ;
132
+ after = service .applyAliasActions (before , singletonList (new AliasAction .Remove (index , "test_2" , randomBoolean ())));
133
+ assertNull (after .metadata ().getIndicesLookup ().get ("test" ));
134
+ assertNull (after .metadata ().getIndicesLookup ().get ("test_2" ));
135
+ assertAliasesVersionIncreased (index , before , after );
136
+
137
+ // Show that removing non-existing alias with mustExist == true fails
138
+ final ClusterState finalCS = after ;
139
+ final IllegalArgumentException iae = expectThrows (IllegalArgumentException .class ,
140
+ () -> service .applyAliasActions (finalCS , singletonList (new AliasAction .Remove (index , "test_2" , true ))));
141
+ assertThat (iae .getMessage (), containsString ("required alias [test_2] does not exist" ));
142
+ }
143
+
104
144
public void testMultipleIndices () {
105
145
final var length = randomIntBetween (2 , 8 );
106
146
final var indices = new HashSet <String >(length );
@@ -183,7 +223,7 @@ public void testAliasesVersionUnchangedWhenActionsAreIdempotent() {
183
223
// now perform a remove and add for each alias which is idempotent, the resulting aliases are unchanged
184
224
final var removeAndAddActions = new ArrayList <AliasAction >(2 * length );
185
225
for (final var aliasName : aliasNames ) {
186
- removeAndAddActions .add (new AliasAction .Remove (index , aliasName ));
226
+ removeAndAddActions .add (new AliasAction .Remove (index , aliasName , null ));
187
227
removeAndAddActions .add (new AliasAction .Add (index , aliasName , null , null , null , null , null ));
188
228
}
189
229
final ClusterState afterRemoveAndAddAlias = service .applyAliasActions (afterAddingAlias , removeAndAddActions );
0 commit comments