Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CXXCBC-431: Fix history retention bucket capability check #505

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions core/cluster.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,38 @@ class cluster_impl : public std::enable_shared_from_this<cluster_impl>
}
}

template<class Request,
class Handler,
typename std::enable_if_t<std::is_same_v<typename Request::encoded_request_type, io::http_request>, int> = 0>
void execute_with_bucket_capability_check(Request request, bucket_capability cap, Handler&& handler)
{
auto bucket_name = request.bucket_name;
return open_bucket(
bucket_name,
[self = shared_from_this(), bucket_name, cap, request = std::move(request), handler = std::forward<Handler>(handler)](
std::error_code ec) mutable {
if (ec) {
handler(request.make_response({ ec }, {}));
return;
}
return self->with_bucket_configuration(
bucket_name,
[self = std::move(self), cap, request = std::move(request), handler = std::forward<Handler>(handler)](
std::error_code ec, topology::configuration config) mutable {
if (ec) {
handler(request.make_response({ ec }, {}));
return;
}
auto bucket_caps = config.bucket_capabilities;
if (bucket_caps.find(cap) == bucket_caps.end()) {
handler(request.make_response({ errc::common::feature_not_available }, {}));
return;
}
return self->execute(std::move(request), std::forward<Handler>(handler));
});
});
}

std::shared_ptr<bucket> find_bucket_by_name(const std::string& name)
{
std::scoped_lock lock(buckets_mutex_);
Expand Down Expand Up @@ -1074,13 +1106,19 @@ void
cluster::execute(operations::management::collection_create_request request,
utils::movable_function<void(operations::management::collection_create_response)>&& handler) const
{
if (request.history.has_value()) {
return impl_->execute_with_bucket_capability_check(std::move(request), bucket_capability::non_deduped_history, std::move(handler));
}
return impl_->execute(std::move(request), std::move(handler));
}

void
cluster::execute(operations::management::collection_update_request request,
utils::movable_function<void(operations::management::collection_update_response)>&& handler) const
{
if (request.history.has_value()) {
return impl_->execute_with_bucket_capability_check(std::move(request), bucket_capability::non_deduped_history, std::move(handler));
}
return impl_->execute(std::move(request), std::move(handler));
}

Expand Down
7 changes: 1 addition & 6 deletions core/operations/management/collection_create.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace couchbase::core::operations::management
{
std::error_code
collection_create_request::encode_to(encoded_request_type& encoded, http_context& context) const
collection_create_request::encode_to(encoded_request_type& encoded, http_context& /*context*/) const
{
encoded.method = "POST";
encoded.path = fmt::format("/pools/default/buckets/{}/scopes/{}/collections", bucket_name, scope_name);
Expand All @@ -42,11 +42,6 @@ collection_create_request::encode_to(encoded_request_type& encoded, http_context
return couchbase::errc::common::invalid_argument;
}
if (history.has_value()) {
auto bucket_caps = context.config.bucket_capabilities;
if (bucket_caps.find(bucket_capability::non_deduped_history) == bucket_caps.end()) {
return errc::common::feature_not_available;
}

encoded.body.append(fmt::format("&history={}", history.value()));
}
return {};
Expand Down
7 changes: 1 addition & 6 deletions core/operations/management/collection_update.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace couchbase::core::operations::management
{
std::error_code
collection_update_request::encode_to(encoded_request_type& encoded, http_context& context) const
collection_update_request::encode_to(encoded_request_type& encoded, http_context& /*context*/) const
{
encoded.method = "PATCH";
encoded.path = fmt::format("/pools/default/buckets/{}/scopes/{}/collections/{}", bucket_name, scope_name, collection_name);
Expand All @@ -42,11 +42,6 @@ collection_update_request::encode_to(encoded_request_type& encoded, http_context
}
}
if (history.has_value()) {
auto bucket_caps = context.config.bucket_capabilities;
if (bucket_caps.find(bucket_capability::non_deduped_history) == bucket_caps.end()) {
return errc::common::feature_not_available;
}

values["history"] = history.value() ? "true" : "false";
}
encoded.body = utils::string_codec::v2::form_encode(values);
Expand Down
59 changes: 43 additions & 16 deletions test/test_integration_management.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1557,33 +1557,60 @@ TEST_CASE("integration: collection management history retention not supported in

SECTION("create collection")
{
couchbase::core::operations::management::collection_create_request req{
integration.ctx.bucket,
scope_name,
collection_name,
};
SECTION("core API")
{
couchbase::core::operations::management::collection_create_request req{
integration.ctx.bucket,
scope_name,
collection_name,
};
req.history = true;

req.history = true;
auto resp = test::utils::execute(integration.cluster, req);
REQUIRE(resp.ctx.ec == couchbase::errc::common::feature_not_available);
}

auto resp = test::utils::execute(integration.cluster, req);
REQUIRE(resp.ctx.ec == couchbase::errc::common::feature_not_available);
SECTION("public API")
{
auto manager = couchbase::cluster(integration.cluster).bucket(integration.ctx.bucket).collections();

couchbase::create_collection_settings settings{};
settings.history = true;

auto ctx = manager.create_collection(scope_name, collection_name, settings).get();
REQUIRE(ctx.ec() == couchbase::errc::common::feature_not_available);
}
}

SECTION("update collection")
{
auto ec = create_collection(integration.cluster, integration.ctx.bucket, scope_name, collection_name);
REQUIRE_SUCCESS(ec);

couchbase::core::operations::management::collection_update_request req{
integration.ctx.bucket,
scope_name,
collection_name,
};
SECTION("core API")
{
couchbase::core::operations::management::collection_update_request req{
integration.ctx.bucket,
scope_name,
collection_name,
};

req.history = true;
req.history = true;

auto resp = test::utils::execute(integration.cluster, req);
REQUIRE(resp.ctx.ec == couchbase::errc::common::feature_not_available);
auto resp = test::utils::execute(integration.cluster, req);
REQUIRE(resp.ctx.ec == couchbase::errc::common::feature_not_available);
}

SECTION("public API")
{
auto manager = couchbase::cluster(integration.cluster).bucket(integration.ctx.bucket).collections();

couchbase::update_collection_settings settings{};
settings.history = true;

auto ctx = manager.update_collection(scope_name, collection_name, settings).get();
REQUIRE(ctx.ec() == couchbase::errc::common::feature_not_available);
}
}

// Clean up the collection that was created
Expand Down
Loading