public Optional checkAndCreatePolicy()

in data-prepper-plugins/opensearch/src/main/java/com/amazon/dataprepper/plugins/sink/opensearch/index/IsmPolicyManagement.java [76:121]


    public Optional<String> checkAndCreatePolicy() throws IOException {
        final String policyManagementEndpoint = POLICY_MANAGEMENT_ENDPOINT + policyName;

        String policyJsonString = retrievePolicyJsonString(policyFile);
        Request request = createPolicyRequestFromFile(policyManagementEndpoint, policyJsonString);

        try {
            restHighLevelClient.getLowLevelClient().performRequest(request);
        } catch (ResponseException e1) {
            final String msg = e1.getMessage();
            if (msg.contains("Invalid field: [ism_template]")) {

                if(StringUtils.isEmpty(policyFileWithoutIsmTemplate)) {
                    policyJsonString = dropIsmTemplateFromPolicy(policyJsonString);
                } else {
                    policyJsonString = retrievePolicyJsonString(policyFileWithoutIsmTemplate);
                }

                request = createPolicyRequestFromFile(policyManagementEndpoint, policyJsonString);
                try {
                    restHighLevelClient.getLowLevelClient().performRequest(request);
                } catch (ResponseException e2) {
                    if (e2.getMessage().contains("version_conflict_engine_exception")
                            || e2.getMessage().contains("resource_already_exists_exception")) {
                        // Do nothing - likely caused by
                        // (1) a race condition where the resource was created by another host before this host's
                        // restClient made its request;
                        // (2) policy already exists in the cluster
                    } else {
                        throw e2;
                    }
                }
                return Optional.of(policyName);
            } else if (e1.getMessage().contains("version_conflict_engine_exception")
                    || e1.getMessage().contains("resource_already_exists_exception")) {
                // Do nothing - likely caused by
                // (1) a race condition where the resource was created by another host before this host's
                // restClient made its request;
                // (2) policy already exists in the cluster
            } else {
                throw e1;
            }
        }

        return Optional.empty();
    }