mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-27 09:00:04 -04:00
Make TransportPutDatabaseConfigurationAction project aware (#130063)
This commit is contained in:
parent
3200abc4ce
commit
6cb7b2d2f2
2 changed files with 24 additions and 15 deletions
|
@ -20,7 +20,9 @@ import org.elasticsearch.cluster.ClusterStateTaskListener;
|
|||
import org.elasticsearch.cluster.SimpleBatchedExecutor;
|
||||
import org.elasticsearch.cluster.block.ClusterBlockException;
|
||||
import org.elasticsearch.cluster.block.ClusterBlockLevel;
|
||||
import org.elasticsearch.cluster.metadata.ProjectId;
|
||||
import org.elasticsearch.cluster.metadata.ProjectMetadata;
|
||||
import org.elasticsearch.cluster.project.ProjectResolver;
|
||||
import org.elasticsearch.cluster.service.ClusterService;
|
||||
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
|
||||
import org.elasticsearch.common.Priority;
|
||||
|
@ -58,13 +60,15 @@ public class TransportPutDatabaseConfigurationAction extends TransportMasterNode
|
|||
};
|
||||
|
||||
private final MasterServiceTaskQueue<UpdateDatabaseConfigurationTask> updateDatabaseConfigurationTaskQueue;
|
||||
private final ProjectResolver projectResolver;
|
||||
|
||||
@Inject
|
||||
public TransportPutDatabaseConfigurationAction(
|
||||
TransportService transportService,
|
||||
ClusterService clusterService,
|
||||
ThreadPool threadPool,
|
||||
ActionFilters actionFilters
|
||||
ActionFilters actionFilters,
|
||||
ProjectResolver projectResolver
|
||||
) {
|
||||
super(
|
||||
PutDatabaseConfigurationAction.NAME,
|
||||
|
@ -81,6 +85,7 @@ public class TransportPutDatabaseConfigurationAction extends TransportMasterNode
|
|||
Priority.NORMAL,
|
||||
UPDATE_TASK_EXECUTOR
|
||||
);
|
||||
this.projectResolver = projectResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -89,7 +94,7 @@ public class TransportPutDatabaseConfigurationAction extends TransportMasterNode
|
|||
|
||||
updateDatabaseConfigurationTaskQueue.submitTask(
|
||||
Strings.format("update-geoip-database-configuration-[%s]", id),
|
||||
new UpdateDatabaseConfigurationTask(listener, request.getDatabase()),
|
||||
new UpdateDatabaseConfigurationTask(projectResolver.getProjectId(), listener, request.getDatabase()),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
@ -105,9 +110,9 @@ public class TransportPutDatabaseConfigurationAction extends TransportMasterNode
|
|||
}
|
||||
}
|
||||
|
||||
static void validatePrerequisites(DatabaseConfiguration database, ClusterState state) {
|
||||
static void validatePrerequisites(ProjectId projectId, DatabaseConfiguration database, ClusterState state) {
|
||||
// we need to verify that the database represents a unique file (name) among the various databases for this same provider
|
||||
IngestGeoIpMetadata geoIpMeta = state.metadata().getProject().custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
|
||||
IngestGeoIpMetadata geoIpMeta = state.metadata().getProject(projectId).custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
|
||||
|
||||
Optional<DatabaseConfiguration> sameName = geoIpMeta.getDatabases()
|
||||
.values()
|
||||
|
@ -125,12 +130,14 @@ public class TransportPutDatabaseConfigurationAction extends TransportMasterNode
|
|||
});
|
||||
}
|
||||
|
||||
private record UpdateDatabaseConfigurationTask(ActionListener<AcknowledgedResponse> listener, DatabaseConfiguration database)
|
||||
implements
|
||||
ClusterStateTaskListener {
|
||||
private record UpdateDatabaseConfigurationTask(
|
||||
ProjectId projectId,
|
||||
ActionListener<AcknowledgedResponse> listener,
|
||||
DatabaseConfiguration database
|
||||
) implements ClusterStateTaskListener {
|
||||
|
||||
ClusterState execute(ClusterState currentState) throws Exception {
|
||||
final var project = currentState.metadata().getProject();
|
||||
final var project = currentState.metadata().getProject(projectId);
|
||||
IngestGeoIpMetadata geoIpMeta = project.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
|
||||
|
||||
String id = database.id();
|
||||
|
@ -140,7 +147,7 @@ public class TransportPutDatabaseConfigurationAction extends TransportMasterNode
|
|||
return currentState;
|
||||
}
|
||||
|
||||
validatePrerequisites(database, currentState);
|
||||
validatePrerequisites(projectId, database, currentState);
|
||||
|
||||
Map<String, DatabaseConfigurationMetadata> databases = new HashMap<>(geoIpMeta.getDatabases());
|
||||
databases.put(
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
package org.elasticsearch.ingest.geoip.direct;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.Metadata;
|
||||
import org.elasticsearch.cluster.metadata.ProjectId;
|
||||
import org.elasticsearch.cluster.metadata.ProjectMetadata;
|
||||
import org.elasticsearch.ingest.geoip.IngestGeoIpMetadata;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
|
@ -20,25 +21,26 @@ import java.util.Map;
|
|||
public class TransportPutDatabaseConfigurationActionTests extends ESTestCase {
|
||||
|
||||
public void testValidatePrerequisites() {
|
||||
ProjectId projectId = randomProjectIdOrDefault();
|
||||
// Test that we reject two configurations with the same database name but different ids:
|
||||
String name = randomAlphaOfLengthBetween(1, 50);
|
||||
IngestGeoIpMetadata ingestGeoIpMetadata = randomIngestGeoIpMetadata(name);
|
||||
ClusterState state = ClusterState.builder(ClusterState.EMPTY_STATE)
|
||||
.metadata(Metadata.builder(Metadata.EMPTY_METADATA).putCustom(IngestGeoIpMetadata.TYPE, ingestGeoIpMetadata))
|
||||
.putProjectMetadata(ProjectMetadata.builder(projectId).putCustom(IngestGeoIpMetadata.TYPE, ingestGeoIpMetadata).build())
|
||||
.build();
|
||||
DatabaseConfiguration databaseConfiguration = randomDatabaseConfiguration(randomIdentifier(), name);
|
||||
expectThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> TransportPutDatabaseConfigurationAction.validatePrerequisites(databaseConfiguration, state)
|
||||
() -> TransportPutDatabaseConfigurationAction.validatePrerequisites(projectId, databaseConfiguration, state)
|
||||
);
|
||||
|
||||
// Test that we do not reject two configurations with different database names:
|
||||
String differentName = randomValueOtherThan(name, () -> randomAlphaOfLengthBetween(1, 50));
|
||||
DatabaseConfiguration databaseConfigurationForDifferentName = randomDatabaseConfiguration(randomIdentifier(), differentName);
|
||||
TransportPutDatabaseConfigurationAction.validatePrerequisites(databaseConfigurationForDifferentName, state);
|
||||
TransportPutDatabaseConfigurationAction.validatePrerequisites(projectId, databaseConfigurationForDifferentName, state);
|
||||
|
||||
// Test that we do not reject a configuration if none already exists:
|
||||
TransportPutDatabaseConfigurationAction.validatePrerequisites(databaseConfiguration, ClusterState.EMPTY_STATE);
|
||||
TransportPutDatabaseConfigurationAction.validatePrerequisites(projectId, databaseConfiguration, ClusterState.EMPTY_STATE);
|
||||
|
||||
// Test that we do not reject a configuration if one with the same database name AND id already exists:
|
||||
DatabaseConfiguration databaseConfigurationSameNameSameId = ingestGeoIpMetadata.getDatabases()
|
||||
|
@ -46,7 +48,7 @@ public class TransportPutDatabaseConfigurationActionTests extends ESTestCase {
|
|||
.iterator()
|
||||
.next()
|
||||
.database();
|
||||
TransportPutDatabaseConfigurationAction.validatePrerequisites(databaseConfigurationSameNameSameId, state);
|
||||
TransportPutDatabaseConfigurationAction.validatePrerequisites(projectId, databaseConfigurationSameNameSameId, state);
|
||||
}
|
||||
|
||||
private IngestGeoIpMetadata randomIngestGeoIpMetadata(String name) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue