From aa42a0eb23b713b5715ac37e4987a4ff184873d6 Mon Sep 17 00:00:00 2001 From: Soufiane Echkem Date: Thu, 24 Oct 2024 16:11:21 +0100 Subject: [PATCH] feat(engine/gRPC): GetContactGroup return information about a contact group. --- engine/enginerpc/engine.proto | 7 +++ engine/enginerpc/engine_impl.cc | 45 +++++++++++++++++++ engine/inc/com/centreon/engine/engine_impl.hh | 3 ++ 3 files changed, 55 insertions(+) diff --git a/engine/enginerpc/engine.proto b/engine/enginerpc/engine.proto index 8d8e6e63a2e..d5203a8ac48 100644 --- a/engine/enginerpc/engine.proto +++ b/engine/enginerpc/engine.proto @@ -37,6 +37,7 @@ service Engine { rpc GetService(ServiceIdentifier) returns (EngineService) {} rpc GetHostGroup(NameIdentifier) returns (EngineHostGroup) {} rpc GetServiceGroup(NameIdentifier) returns (EngineServiceGroup) {} + rpc GetContactGroup(NameIdentifier) returns (EngineContactGroup) {} rpc GetHostsCount(google.protobuf.Empty) returns (GenericValue) {} rpc GetContactsCount(google.protobuf.Empty) returns (GenericValue) {} rpc GetServicesCount(google.protobuf.Empty) returns (GenericValue) {} @@ -697,6 +698,12 @@ message EngineServiceGroup { repeated string members = 7; } +message EngineContactGroup { + string name = 1; + string alias = 2; + repeated string members = 3; +} + message EngineComment { string host_name = 1; string svc_desc = 2; diff --git a/engine/enginerpc/engine_impl.cc b/engine/enginerpc/engine_impl.cc index b67cf951912..809eeee5e86 100644 --- a/engine/enginerpc/engine_impl.cc +++ b/engine/enginerpc/engine_impl.cc @@ -896,6 +896,51 @@ grpc::Status engine_impl::GetServiceGroup(grpc::ServerContext* context return grpc::Status(grpc::INVALID_ARGUMENT, err); } +/** + * @brief Return ContactGroup informations. + * + * @param context gRPC context + * @param request ContactGroup's identifier (by ContactGroup name) + * @param response The filled fields + * + *@return Status::OK + */ +grpc::Status engine_impl::GetContactGroup(grpc::ServerContext* context + [[maybe_unused]], + const NameIdentifier* request, + EngineContactGroup* response) { + std::string err; + auto fn = std::packaged_task([&err, request, + contactgroup = + response]() -> int32_t { + std::shared_ptr selectedcontactgroup; + auto itcontactgroup = contactgroup::contactgroups.find(request->name()); + if (itcontactgroup != contactgroup::contactgroups.end()) + selectedcontactgroup = itcontactgroup->second; + else { + err = fmt::format("could not find contactgroup '{}'", request->name()); + return 1; + } + + contactgroup->set_name(selectedcontactgroup->get_name()); + contactgroup->set_alias(selectedcontactgroup->get_alias()); + + if (!selectedcontactgroup->get_members().empty()) + for (const auto& [key, _] : selectedcontactgroup->get_members()) + contactgroup->add_members(key); + + return 0; + }); + + std::future result = fn.get_future(); + command_manager::instance().enqueue(std::move(fn)); + + if (result.get() == 0) + return grpc::Status::OK; + else + return grpc::Status(grpc::INVALID_ARGUMENT, err); +} + /** * @brief Return the total number of hosts. * diff --git a/engine/inc/com/centreon/engine/engine_impl.hh b/engine/inc/com/centreon/engine/engine_impl.hh index 0114f0d84fb..35db9fd1945 100644 --- a/engine/inc/com/centreon/engine/engine_impl.hh +++ b/engine/inc/com/centreon/engine/engine_impl.hh @@ -63,6 +63,9 @@ class engine_impl final : public Engine::Service { grpc::Status GetServiceGroup(grpc::ServerContext* context, const NameIdentifier* request, EngineServiceGroup* response) override; + grpc::Status GetContactGroup(grpc::ServerContext* context, + const NameIdentifier* request, + EngineContactGroup* response) override; grpc::Status AddHostComment(grpc::ServerContext* context, const EngineComment* request, CommandSuccess* response) override;