mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-13 08:25:26 +00:00
Ensure enrich policy is immutable (#43604)
This commit ensures the policy cannot be overwritten. An error is thrown if the policy exists. All tests have been updated accordingly.
This commit is contained in:
parent
d2c3f4bae9
commit
b4b2ad3593
@ -8,6 +8,7 @@ package org.elasticsearch.test.enrich;
|
|||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.elasticsearch.client.Request;
|
import org.elasticsearch.client.Request;
|
||||||
import org.elasticsearch.client.Response;
|
import org.elasticsearch.client.Response;
|
||||||
|
import org.elasticsearch.client.ResponseException;
|
||||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||||
import org.elasticsearch.test.rest.ESRestTestCase;
|
import org.elasticsearch.test.rest.ESRestTestCase;
|
||||||
@ -76,6 +77,16 @@ public abstract class CommonEnrichRestTestCase extends ESRestTestCase {
|
|||||||
assertThat(_source.get("tld_rank"), equalTo(7));
|
assertThat(_source.get("tld_rank"), equalTo(7));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testImmutablePolicy() throws IOException {
|
||||||
|
Request putPolicyRequest = new Request("PUT", "/_enrich/policy/my_policy");
|
||||||
|
putPolicyRequest.setJsonEntity("{\"type\": \"exact_match\",\"indices\": [\"my-source-index\"], \"enrich_key\": \"host\", " +
|
||||||
|
"\"enrich_values\": [\"globalRank\", \"tldRank\", \"tld\"]}");
|
||||||
|
assertOK(client().performRequest(putPolicyRequest));
|
||||||
|
|
||||||
|
ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(putPolicyRequest));
|
||||||
|
assertTrue(exc.getMessage().contains("policy [my_policy] already exists"));
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String, Object> toMap(Response response) throws IOException {
|
private static Map<String, Object> toMap(Response response) throws IOException {
|
||||||
return toMap(EntityUtils.toString(response.getEntity()));
|
return toMap(EntityUtils.toString(response.getEntity()));
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.enrich;
|
package org.elasticsearch.xpack.enrich;
|
||||||
|
|
||||||
|
import org.elasticsearch.ResourceAlreadyExistsException;
|
||||||
import org.elasticsearch.ResourceNotFoundException;
|
import org.elasticsearch.ResourceNotFoundException;
|
||||||
import org.elasticsearch.cluster.ClusterState;
|
import org.elasticsearch.cluster.ClusterState;
|
||||||
import org.elasticsearch.cluster.ClusterStateUpdateTask;
|
import org.elasticsearch.cluster.ClusterStateUpdateTask;
|
||||||
@ -46,6 +47,9 @@ public final class EnrichStore {
|
|||||||
|
|
||||||
updateClusterState(clusterService, handler, current -> {
|
updateClusterState(clusterService, handler, current -> {
|
||||||
final Map<String, EnrichPolicy> policies = getPolicies(current);
|
final Map<String, EnrichPolicy> policies = getPolicies(current);
|
||||||
|
if (policies.get(name) != null) {
|
||||||
|
throw new ResourceAlreadyExistsException("policy [{}] already exists", name);
|
||||||
|
}
|
||||||
policies.put(name, policy);
|
policies.put(name, policy);
|
||||||
return policies;
|
return policies;
|
||||||
});
|
});
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.enrich;
|
package org.elasticsearch.xpack.enrich;
|
||||||
|
|
||||||
|
import org.elasticsearch.ResourceAlreadyExistsException;
|
||||||
import org.elasticsearch.action.ingest.PutPipelineRequest;
|
import org.elasticsearch.action.ingest.PutPipelineRequest;
|
||||||
import org.elasticsearch.common.bytes.BytesArray;
|
import org.elasticsearch.common.bytes.BytesArray;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
@ -21,7 +22,6 @@ import java.util.Collections;
|
|||||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
import static org.hamcrest.Matchers.sameInstance;
|
|
||||||
|
|
||||||
public class EnrichPolicyUpdateTests extends ESSingleNodeTestCase {
|
public class EnrichPolicyUpdateTests extends ESSingleNodeTestCase {
|
||||||
|
|
||||||
@ -49,11 +49,8 @@ public class EnrichPolicyUpdateTests extends ESSingleNodeTestCase {
|
|||||||
|
|
||||||
EnrichPolicy instance2 = new EnrichPolicy(EnrichPolicy.EXACT_MATCH_TYPE, null, Collections.singletonList("index"),
|
EnrichPolicy instance2 = new EnrichPolicy(EnrichPolicy.EXACT_MATCH_TYPE, null, Collections.singletonList("index"),
|
||||||
"key2", Collections.singletonList("field2"));
|
"key2", Collections.singletonList("field2"));
|
||||||
putPolicyRequest = new PutEnrichPolicyAction.Request("my_policy", instance2);
|
ResourceAlreadyExistsException exc = expectThrows(ResourceAlreadyExistsException.class, () ->
|
||||||
assertAcked(client().execute(PutEnrichPolicyAction.INSTANCE, putPolicyRequest).actionGet());
|
client().execute(PutEnrichPolicyAction.INSTANCE, new PutEnrichPolicyAction.Request("my_policy", instance2)).actionGet());
|
||||||
assertThat(enrichProcessorFactory.policies.get("my_policy"), equalTo(instance2));
|
assertTrue(exc.getMessage().contains("policy [my_policy] already exists"));
|
||||||
|
|
||||||
Pipeline pipelineInstance2 = ingestService.getPipeline("1");
|
|
||||||
assertThat(pipelineInstance2, sameInstance(pipelineInstance1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,22 @@ public class EnrichStoreTests extends ESSingleNodeTestCase {
|
|||||||
assertThat(result, nullValue());
|
assertThat(result, nullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testImmutability() throws Exception {
|
||||||
|
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
|
||||||
|
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
|
||||||
|
String name = "my-policy";
|
||||||
|
|
||||||
|
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
|
||||||
|
assertThat(error.get(), nullValue());
|
||||||
|
|
||||||
|
error = saveEnrichPolicy(name, policy, clusterService);
|
||||||
|
assertTrue(error.get().getMessage().contains("policy [my-policy] already exists"));;
|
||||||
|
|
||||||
|
deleteEnrichPolicy(name, clusterService);
|
||||||
|
EnrichPolicy result = EnrichStore.getPolicy(name, clusterService.state());
|
||||||
|
assertThat(result, nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
public void testPutValidation() throws Exception {
|
public void testPutValidation() throws Exception {
|
||||||
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
|
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
|
||||||
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
|
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user