Search in sources :

Example 11 with KMeansParams

use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.

the class SecureMLRestIT method testReadOnlyUser_CanGetTask_CanNotDeleteTask.

public void testReadOnlyUser_CanGetTask_CanNotDeleteTask() throws IOException {
    KMeansParams kMeansParams = KMeansParams.builder().build();
    // train model with full access client
    train(mlFullAccessClient, FunctionName.KMEANS, irisIndex, kMeansParams, searchSourceBuilder, trainResult -> {
        assertFalse(trainResult.containsKey("model_id"));
        String taskId = (String) trainResult.get("task_id");
        assertNotNull(taskId);
        String status = (String) trainResult.get("status");
        assertEquals(MLTaskState.CREATED.name(), status);
        try {
            // get task with readonly client
            getTask(mlReadOnlyClient, taskId, task -> {
                String algorithm = (String) task.get("function_name");
                assertEquals(FunctionName.KMEANS.name(), algorithm);
            });
        } catch (IOException e) {
            assertNull(e);
        }
        try {
            // Failed to delete task with read only client
            deleteTask(mlReadOnlyClient, taskId, null);
            throw new RuntimeException("Delete task for readonly user does not fail");
        } catch (Exception e) {
            assertEquals(ResponseException.class, e.getClass());
            assertTrue(Throwables.getStackTraceAsString(e).contains("no permissions for [cluster:admin/opensearch/ml/tasks/delete]"));
        }
    }, true);
}
Also used : KMeansParams(org.opensearch.ml.common.parameter.KMeansParams) ResponseException(org.opensearch.client.ResponseException) IOException(java.io.IOException) IOException(java.io.IOException) ResponseException(org.opensearch.client.ResponseException) ExpectedException(org.junit.rules.ExpectedException)

Example 12 with KMeansParams

use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.

the class SecureMLRestIT method testReadOnlyUser_CanSearchModels.

public void testReadOnlyUser_CanSearchModels() throws IOException {
    KMeansParams kMeansParams = KMeansParams.builder().build();
    // train model with full access client
    train(mlFullAccessClient, FunctionName.KMEANS, irisIndex, kMeansParams, searchSourceBuilder, trainResult -> {
        String modelId = (String) trainResult.get("model_id");
        assertNotNull(modelId);
        String status = (String) trainResult.get("status");
        assertEquals(MLTaskState.COMPLETED.name(), status);
        try {
            // search model with readonly client
            searchModelsWithAlgoName(mlReadOnlyClient, FunctionName.KMEANS.name(), models -> {
                ArrayList<Object> hits = (ArrayList) ((Map<String, Object>) models.get("hits")).get("hits");
                assertTrue(hits.size() > 0);
            });
        } catch (IOException e) {
            assertNull(e);
        }
    }, false);
}
Also used : KMeansParams(org.opensearch.ml.common.parameter.KMeansParams) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 13 with KMeansParams

use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.

the class SecureMLRestIT method testTrainWithReadOnlyMLAccess.

public void testTrainWithReadOnlyMLAccess() throws IOException {
    exceptionRule.expect(ResponseException.class);
    exceptionRule.expectMessage("no permissions for [cluster:admin/opensearch/ml/train]");
    KMeansParams kMeansParams = KMeansParams.builder().build();
    train(mlReadOnlyClient, FunctionName.KMEANS, irisIndex, kMeansParams, searchSourceBuilder, null, false);
}
Also used : KMeansParams(org.opensearch.ml.common.parameter.KMeansParams)

Example 14 with KMeansParams

use of org.opensearch.ml.common.parameter.KMeansParams in project ml-commons by opensearch-project.

the class SecureMLRestIT method testPredictWithReadOnlyMLAccess.

public void testPredictWithReadOnlyMLAccess() throws IOException {
    exceptionRule.expect(ResponseException.class);
    exceptionRule.expectMessage("no permissions for [cluster:admin/opensearch/ml/predict]");
    KMeansParams kMeansParams = KMeansParams.builder().build();
    predict(mlReadOnlyClient, FunctionName.KMEANS, "modelId", irisIndex, kMeansParams, searchSourceBuilder, null);
}
Also used : KMeansParams(org.opensearch.ml.common.parameter.KMeansParams)

Aggregations

KMeansParams (org.opensearch.ml.common.parameter.KMeansParams)14 IOException (java.io.IOException)5 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 DataFrame (org.opensearch.ml.common.dataframe.DataFrame)3 ExpectedException (org.junit.rules.ExpectedException)2 ResponseException (org.opensearch.client.ResponseException)2 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)2 MLInputDataset (org.opensearch.ml.common.dataset.MLInputDataset)2 KMeansHelper.constructKMeansDataFrame (org.opensearch.ml.engine.helper.KMeansHelper.constructKMeansDataFrame)2 Map (java.util.Map)1 DataFrameInputDataset (org.opensearch.ml.common.dataset.DataFrameInputDataset)1 Input (org.opensearch.ml.common.parameter.Input)1 LocalSampleCalculatorInput (org.opensearch.ml.common.parameter.LocalSampleCalculatorInput)1 MLInput (org.opensearch.ml.common.parameter.MLInput)1 MLPredictionOutput (org.opensearch.ml.common.parameter.MLPredictionOutput)1 Model (org.opensearch.ml.common.parameter.Model)1 KMeans (org.opensearch.ml.engine.algorithms.clustering.KMeans)1 LinearRegressionHelper.constructLinearRegressionPredictionDataFrame (org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionPredictionDataFrame)1 LinearRegressionHelper.constructLinearRegressionTrainDataFrame (org.opensearch.ml.engine.helper.LinearRegressionHelper.constructLinearRegressionTrainDataFrame)1