16
16
import java .util .HashMap ;
17
17
import java .util .Map ;
18
18
import java .util .function .Consumer ;
19
+ import java .util .function .Function ;
19
20
20
21
/**
21
22
* Helper methods for access and storage of an enrich policy.
@@ -43,9 +44,11 @@ public static void putPolicy(String name, EnrichPolicy policy, ClusterService cl
43
44
}
44
45
// TODO: add policy validation
45
46
46
- final Map <String , EnrichPolicy > policies = getPolicies (clusterService .state ());
47
- policies .put (name , policy );
48
- updateClusterState (policies , clusterService , handler );
47
+ updateClusterState (clusterService , handler , current -> {
48
+ final Map <String , EnrichPolicy > policies = getPolicies (current );
49
+ policies .put (name , policy );
50
+ return policies ;
51
+ });
49
52
}
50
53
51
54
/**
@@ -62,13 +65,15 @@ public static void deletePolicy(String name, ClusterService clusterService, Cons
62
65
throw new IllegalArgumentException ("name is missing or empty" );
63
66
}
64
67
65
- final Map <String , EnrichPolicy > policies = getPolicies (clusterService .state ());
66
- if (policies .containsKey (name ) == false ) {
67
- throw new ResourceNotFoundException ("policy [{}] not found" , name );
68
- }
68
+ updateClusterState (clusterService , handler , current -> {
69
+ final Map <String , EnrichPolicy > policies = getPolicies (current );
70
+ if (policies .containsKey (name ) == false ) {
71
+ throw new ResourceNotFoundException ("policy [{}] not found" , name );
72
+ }
69
73
70
- policies .remove (name );
71
- updateClusterState (policies , clusterService , handler );
74
+ policies .remove (name );
75
+ return policies ;
76
+ });
72
77
}
73
78
74
79
/**
@@ -103,12 +108,14 @@ public static Map<String, EnrichPolicy> getPolicies(ClusterState state) {
103
108
return policies ;
104
109
}
105
110
106
- private static void updateClusterState (Map <String , EnrichPolicy > policies , ClusterService clusterService ,
107
- Consumer <Exception > handler ) {
108
- clusterService .submitStateUpdateTask ("update-enrich-policy" , new ClusterStateUpdateTask () {
111
+ private static void updateClusterState (ClusterService clusterService ,
112
+ Consumer <Exception > handler ,
113
+ Function <ClusterState , Map <String , EnrichPolicy >> function ) {
114
+ clusterService .submitStateUpdateTask ("update-enrich-metadata" , new ClusterStateUpdateTask () {
109
115
110
116
@ Override
111
117
public ClusterState execute (ClusterState currentState ) throws Exception {
118
+ Map <String , EnrichPolicy > policies = function .apply (currentState );
112
119
MetaData metaData = MetaData .builder (currentState .metaData ())
113
120
.putCustom (EnrichMetadata .TYPE , new EnrichMetadata (policies ))
114
121
.build ();
0 commit comments