Search in sources :

Example 36 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.

the class V20180212165000_AddDefaultCollectors method removeConfigPath.

private void removeConfigPath() {
    final FindIterable<Document> documentsWithConfigPath = collection.find(exists("configuration_path"));
    for (Document document : documentsWithConfigPath) {
        final ObjectId objectId = document.getObjectId("_id");
        document.remove("configuration_path");
        final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document);
        if (updateResult.wasAcknowledged()) {
            LOG.debug("Successfully updated document with ID <{}>", objectId);
        } else {
            LOG.error("Failed to update document with ID <{}>", objectId);
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 37 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.

the class V20180718155800_AddContentPackIdAndRev method upgrade.

@Override
public void upgrade() {
    final FindIterable<Document> documentsWithMissingFields = collection.find(or(not(exists(ContentPack.FIELD_META_ID)), not(exists(ContentPack.FIELD_META_REVISION))));
    for (Document document : documentsWithMissingFields) {
        final ObjectId objectId = document.getObjectId("_id");
        LOG.debug("Found document with missing \"id\" or \"rev\" field with ID <{}>", objectId);
        final String id = document.get("id", objectId.toHexString());
        final int rev = document.get("rev", 0);
        document.put("id", id);
        document.put("rev", rev);
        final UpdateResult updateResult = collection.replaceOne(eq("_id", objectId), document);
        if (updateResult.wasAcknowledged()) {
            LOG.debug("Successfully updated document with ID <{}>", objectId);
        } else {
            LOG.error("Failed to update document with ID <{}>", objectId);
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 38 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.

the class V20211221144300_GeoIpResolverConfigMigration method upgrade.

/**
 * This code change modifies {@link GeoIpResolverConfig} by removing the field <b>db_type</b> and adding the field <b>database_vendor_type</b>.
 *
 * <p>
 * The objective of this migration is to add the new field (with value {@link DatabaseVendorType#MAXMIND}) if not already present, and to remove the old field.
 * </p>
 */
@Override
public void upgrade() {
    MigrationCompletion completion = clusterConfigService.get(MigrationCompletion.class);
    if (completion != null) {
        LOG.debug("Migration was already completed");
        return;
    }
    final MongoCollection<Document> collection = mongoConnection.getMongoDatabase().getCollection(COLLECTION_NAME);
    LOG.info("Updating '{}' collection.", COLLECTION_NAME);
    Bson geoConfFilter = Filters.eq("type", GeoIpResolverConfig.class.getCanonicalName());
    Bson noColumnFilter = Filters.exists(FIELD_DB_VENDOR, false);
    // set default value for 'enforce_graylog_schema'
    Bson setEnforceSchema = Updates.set(FIELD_ENFORCE, false);
    // set blank asn db path
    Bson setAsnPath = Updates.set(FIELD_ASN_DB_PATH, "");
    // rename db type field to db vendor type
    Bson renameDbTypeToVendor = Updates.rename(FIELD_DB_TYPE, FIELD_DB_VENDOR);
    // rename existing db_path field to city_db_path
    Bson renameDbPath = Updates.rename(FIELD_DB_PATH, FIELD_CITY_DB_PATH);
    Bson updates = Updates.combine(setEnforceSchema, renameDbTypeToVendor, renameDbPath, setAsnPath);
    LOG.info("Planned Updates: {}", updates);
    final UpdateResult updateResult = collection.updateOne(Filters.and(geoConfFilter, noColumnFilter), updates);
    LOG.info("Update Result: {}", updateResult);
    Bson setDefaultVendor = Updates.set(FIELD_DB_VENDOR, DatabaseVendorType.MAXMIND.name());
    LOG.info("Setting default vendor: {}", setDefaultVendor);
    final UpdateResult updateVendorResult = collection.updateOne(geoConfFilter, setDefaultVendor);
    LOG.info("Default Vendor Update Result: {}", updateVendorResult);
    clusterConfigService.write(MigrationCompletion.create());
}
Also used : GeoIpResolverConfig(org.graylog.plugins.map.config.GeoIpResolverConfig) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Bson(org.bson.conversions.Bson)

Example 39 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.

the class V20200226181600_EncryptAccessTokensMigration method upgrade.

@Override
public void upgrade() {
    final MongoCollection<Document> collection = mongoConnection.getMongoDatabase().getCollection(AccessTokenImpl.COLLECTION_NAME);
    // If we should change the encryption method in the future, we need to adjust the query
    for (final Document document : collection.find(Filters.exists(AccessTokenImpl.TOKEN_TYPE, false))) {
        final String tokenId = document.getObjectId("_id").toHexString();
        final String tokenName = document.getString(AccessTokenImpl.NAME);
        final String tokenUsername = document.getString(AccessTokenImpl.USERNAME);
        final String tokenValue = document.getString(AccessTokenImpl.TOKEN);
        if (isNullOrEmpty(tokenValue)) {
            LOG.warn("Couldn't encrypt empty value for access token <{}/{}> of user <{}>", tokenId, tokenName, tokenUsername);
            continue;
        }
        final Bson query = Filters.eq("_id", document.getObjectId("_id"));
        final Bson updates = Updates.combine(Updates.set(AccessTokenImpl.TOKEN_TYPE, AccessTokenImpl.Type.AES_SIV.getIntValue()), Updates.set(AccessTokenImpl.TOKEN, accessTokenCipher.encrypt(tokenValue)));
        LOG.info("Encrypting access token <{}/{}> for user <{}>", tokenId, tokenName, tokenUsername);
        final UpdateResult result = collection.updateOne(query, updates);
        if (result.getModifiedCount() != 1) {
            LOG.warn("Expected to modify one access token, but <{}> have been updated", result.getModifiedCount());
        }
    }
}
Also used : Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Bson(org.bson.conversions.Bson)

Example 40 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.

the class V20200409083200_RemoveRootQueriesFromMigratedDashboards method upgrade.

@Override
public void upgrade() {
    if (clusterConfigService.get(MigrationCompleted.class) != null) {
        LOG.debug("Migration already completed.");
        return;
    }
    final UpdateResult updateResult = searchesCollection.updateMany(and(isDashboard(), atLeastOneQueryHasNonEmptyQueryString()), makeQueryStringEmpty(), forNonEmptyQueryStrings());
    writeMigrationCompleted(updateResult.getModifiedCount());
}
Also used : UpdateResult(com.mongodb.client.result.UpdateResult)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)75 Document (org.bson.Document)32 Bson (org.bson.conversions.Bson)17 Test (org.junit.Test)12 Update (org.springframework.data.mongodb.core.query.Update)12 Test (org.testng.annotations.Test)11 UpdateOptions (com.mongodb.client.model.UpdateOptions)10 Test (org.junit.jupiter.api.Test)10 BasicDBObject (com.mongodb.BasicDBObject)9 DeleteResult (com.mongodb.client.result.DeleteResult)8 FindOptions (dev.morphia.query.FindOptions)8 BsonDocument (org.bson.BsonDocument)8 Query (org.springframework.data.mongodb.core.query.Query)8 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)7 ArrayList (java.util.ArrayList)6 ObjectId (org.bson.types.ObjectId)6 UpdateOptions (dev.morphia.UpdateOptions)5 Map (java.util.Map)5 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5