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

fix(engine/test): Test inheritance service group #1793

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
23 changes: 23 additions & 0 deletions common/engine_conf/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ void parser::_resolve_template(State* pb_config, error_cnt& err) {
_resolve_template(_pb_helper[&hg],
_pb_templates[message_helper::hostgroup]);

for (Servicegroup& sg : *pb_config->mutable_servicegroups())
_resolve_template(_pb_helper[&sg],
_pb_templates[message_helper::servicegroup]);

for (const Command& c : pb_config->commands())
_pb_helper.at(&c)->check_validity(err);

Expand Down Expand Up @@ -782,6 +786,25 @@ void parser::_merge(std::unique_ptr<message_helper>& msg_helper,
lst->add_data(v);
} else if (lst->data().empty())
*lst->mutable_data() = orig_lst->data();
} else if (d && d->name() == "PairStringSet") {
PairStringSet* orig_pair =
static_cast<PairStringSet*>(refl->MutableMessage(tmpl, f));
PairStringSet* pair =
static_cast<PairStringSet*>(refl->MutableMessage(msg, f));
if (pair->additive()) {
for (auto& v : orig_pair->data()) {
bool found = false;
for (auto& s : *pair->mutable_data()) {
if (s.first() == v.first() && s.second() == v.second()) {
found = true;
break;
}
}
if (!found)
pair->add_data()->CopyFrom(v);
}
} else if (pair->data().empty())
*pair->mutable_data() = orig_pair->data();
} else {
refl->MutableMessage(msg, f)->CopyFrom(
refl->GetMessage(*tmpl, f));
Expand Down
49 changes: 28 additions & 21 deletions engine/enginerpc/engine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ service Engine {
returns (com.centreon.common.pb_process_stat) {}
rpc GetVersion(google.protobuf.Empty) returns (Version) {}
rpc GetStats(GenericString) returns (Stats) {}
rpc GetHost(HostIdentifier) returns (EngineHost) {}
rpc GetContact(ContactIdentifier) returns (EngineContact) {}
rpc GetHost(NameOrIdIdentifier) returns (EngineHost) {}
rpc GetContact(NameIdentifier) returns (EngineContact) {}
rpc GetService(ServiceIdentifier) returns (EngineService) {}
rpc GetHostGroup(HostGroupIdentifier) returns (EngineHostGroup) {}
rpc GetHostGroup(NameIdentifier) returns (EngineHostGroup) {}
rpc GetServiceGroup(NameIdentifier) returns (EngineServiceGroup) {}
rpc GetHostsCount(google.protobuf.Empty) returns (GenericValue) {}
rpc GetContactsCount(google.protobuf.Empty) returns (GenericValue) {}
rpc GetServicesCount(google.protobuf.Empty) returns (GenericValue) {}
Expand All @@ -51,9 +52,9 @@ service Engine {
rpc AddHostComment(EngineComment) returns (CommandSuccess) {}
rpc AddServiceComment(EngineComment) returns (CommandSuccess) {}
rpc DeleteComment(GenericValue) returns (CommandSuccess) {}
rpc DeleteAllHostComments(HostIdentifier) returns (CommandSuccess) {}
rpc DeleteAllHostComments(NameOrIdIdentifier) returns (CommandSuccess) {}
rpc DeleteAllServiceComments(ServiceIdentifier) returns (CommandSuccess) {}
rpc RemoveHostAcknowledgement(HostIdentifier) returns (CommandSuccess) {}
rpc RemoveHostAcknowledgement(NameOrIdIdentifier) returns (CommandSuccess) {}
rpc RemoveServiceAcknowledgement(ServiceIdentifier) returns (CommandSuccess) {
}
rpc AcknowledgementHostProblem(EngineAcknowledgement)
Expand Down Expand Up @@ -109,12 +110,12 @@ service Engine {
rpc ChangeContactObjectCustomVar(ChangeObjectCustomVar)
returns (CommandSuccess) {}
rpc ShutdownProgram(google.protobuf.Empty) returns (google.protobuf.Empty) {}
rpc EnableHostAndChildNotifications(HostIdentifier) returns (CommandSuccess) {
rpc EnableHostAndChildNotifications(NameOrIdIdentifier) returns (CommandSuccess) {
}
rpc DisableHostAndChildNotifications(HostIdentifier)
rpc DisableHostAndChildNotifications(NameOrIdIdentifier)
returns (CommandSuccess) {}
rpc DisableHostNotifications(HostIdentifier) returns (CommandSuccess) {}
rpc EnableHostNotifications(HostIdentifier) returns (CommandSuccess) {}
rpc DisableHostNotifications(NameOrIdIdentifier) returns (CommandSuccess) {}
rpc EnableHostNotifications(NameOrIdIdentifier) returns (CommandSuccess) {}
rpc DisableNotifications(google.protobuf.Empty) returns (CommandSuccess) {}
rpc EnableNotifications(google.protobuf.Empty) returns (CommandSuccess) {}
rpc DisableServiceNotifications(ServiceIdentifier) returns (CommandSuccess) {}
Expand Down Expand Up @@ -338,7 +339,7 @@ message ThresholdsFile {
string filename = 1;
}

message HostIdentifier {
message NameOrIdIdentifier {
oneof identifier {
string name = 1;
uint32 id = 2;
Expand Down Expand Up @@ -479,7 +480,7 @@ message EngineHost {
repeated string custom_variables = 117;
}

message ContactIdentifier {
message NameIdentifier {
string name = 1;
}

Expand Down Expand Up @@ -518,20 +519,20 @@ message EngineContact {
repeated string custom_variables = 32;
}

message NameIdentifier {
message PairNamesIdentifier {
string host_name = 1;
string service_name = 2;
}

message IdIdentifier {
message PairIdsIdentifier {
uint32 host_id = 1;
uint32 service_id = 2;
}

message ServiceIdentifier {
oneof identifier {
NameIdentifier names = 1;
IdIdentifier ids = 2;
PairNamesIdentifier names = 1;
PairIdsIdentifier ids = 2;
}
}

Expand Down Expand Up @@ -676,10 +677,6 @@ message EngineService {
ServiceType service_type = 117;
}

message HostGroupIdentifier {
string name = 1;
}

message EngineHostGroup {
uint32 id = 1;
string name = 2;
Expand All @@ -690,6 +687,16 @@ message EngineHostGroup {
string action_url = 7;
}

message EngineServiceGroup {
uint64 id = 1;
string name = 2;
string alias = 3;
string notes = 4;
string notes_url = 5;
string action_url = 6;
repeated string members = 7;
}

message EngineComment {
string host_name = 1;
string svc_desc = 2;
Expand All @@ -709,8 +716,8 @@ message HostDelayIdentifier {

message ServiceDelayIdentifier {
oneof identifier {
NameIdentifier names = 1;
IdIdentifier ids = 2;
PairNamesIdentifier names = 1;
PairIdsIdentifier ids = 2;
}
uint32 delay_time = 3;
}
Expand Down
98 changes: 74 additions & 24 deletions engine/enginerpc/engine_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ using com::centreon::common::log_v2::log_v2;

namespace com::centreon::engine {

std::ostream& operator<<(std::ostream& str, const HostIdentifier& host_id) {
std::ostream& operator<<(std::ostream& str, const NameOrIdIdentifier& host_id) {
switch (host_id.identifier_case()) {
case HostIdentifier::kName:
case NameOrIdIdentifier::kName:
str << "host name=" << host_id.name();
break;
case HostIdentifier::kId:
case NameOrIdIdentifier::kId:
str << "host id=" << host_id.id();
break;
default:
Expand Down Expand Up @@ -87,7 +87,7 @@ std::ostream& operator<<(std::ostream& str, const ServiceIdentifier& serv_id) {

namespace fmt {
template <>
struct formatter<HostIdentifier> : ostream_formatter {};
struct formatter<NameOrIdIdentifier> : ostream_formatter {};
template <>
struct formatter<ServiceIdentifier> : ostream_formatter {};

Expand Down Expand Up @@ -211,7 +211,7 @@ grpc::Status engine_impl::NewThresholdsFile(grpc::ServerContext* context
*@return Status::OK
*/
grpc::Status engine_impl::GetHost(grpc::ServerContext* context [[maybe_unused]],
const HostIdentifier* request
const NameOrIdIdentifier* request
[[maybe_unused]],
EngineHost* response) {
std::string err;
Expand Down Expand Up @@ -435,7 +435,7 @@ grpc::Status engine_impl::GetHost(grpc::ServerContext* context [[maybe_unused]],
**/
grpc::Status engine_impl::GetContact(grpc::ServerContext* context
[[maybe_unused]],
const ContactIdentifier* request,
const NameIdentifier* request,
EngineContact* response) {
std::string err;
auto fn = std::packaged_task<int(void)>([&err, request,
Expand Down Expand Up @@ -807,7 +807,7 @@ grpc::Status engine_impl::GetService(grpc::ServerContext* context
*/
grpc::Status engine_impl::GetHostGroup(grpc::ServerContext* context
[[maybe_unused]],
const HostGroupIdentifier* request,
const NameIdentifier* request,
EngineHostGroup* response) {
std::string err;
auto fn = std::packaged_task<int(void)>(
Expand Down Expand Up @@ -845,6 +845,57 @@ grpc::Status engine_impl::GetHostGroup(grpc::ServerContext* context
return grpc::Status(grpc::INVALID_ARGUMENT, err);
}

/**
* @brief Return ServiceGroup informations.
*
* @param context gRPC context
* @param request ServiceGroup's identifier (by ServiceGroup name)
* @param response The filled fields
*
*@return Status::OK
*/
grpc::Status engine_impl::GetServiceGroup(grpc::ServerContext* context
[[maybe_unused]],
const NameIdentifier* request,
EngineServiceGroup* response) {
std::string err;
auto fn = std::packaged_task<int(void)>([&err, request,
servicegroup =
response]() -> int32_t {
std::shared_ptr<com::centreon::engine::servicegroup> selectedservicegroup;
auto itservicegroup = servicegroup::servicegroups.find(request->name());
if (itservicegroup != servicegroup::servicegroups.end())
selectedservicegroup = itservicegroup->second;
else {
err = fmt::format("could not find servicegroup '{}'", request->name());
return 1;
}
servicegroup->set_id(selectedservicegroup->get_id());
servicegroup->set_name(selectedservicegroup->get_group_name());
servicegroup->set_alias(selectedservicegroup->get_alias());

servicegroup->set_notes(selectedservicegroup->get_notes());
servicegroup->set_notes_url(selectedservicegroup->get_notes_url());
servicegroup->set_action_url(selectedservicegroup->get_action_url());

if (!selectedservicegroup->members.empty()) {
for (const auto& [host_serv_pair, _] : selectedservicegroup->members) {
servicegroup->add_members(
fmt::format("{},{}", host_serv_pair.first, host_serv_pair.second));
}
}
return 0;
});

std::future<int32_t> 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.
*
Expand Down Expand Up @@ -1193,11 +1244,10 @@ grpc::Status engine_impl::DeleteComment(grpc::ServerContext* context
*
* @return Status::OK
*/
grpc::Status engine_impl::DeleteAllHostComments(grpc::ServerContext* context
[[maybe_unused]],
const HostIdentifier* request,
CommandSuccess* response
[[maybe_unused]]) {
grpc::Status engine_impl::DeleteAllHostComments(
grpc::ServerContext* context [[maybe_unused]],
const NameOrIdIdentifier* request,
CommandSuccess* response [[maybe_unused]]) {
std::string err;
auto fn = std::packaged_task<int32_t(void)>([&err, request]() -> int32_t {
std::shared_ptr<engine::host> temp_host;
Expand Down Expand Up @@ -1264,7 +1314,7 @@ grpc::Status engine_impl::DeleteAllServiceComments(
*/
grpc::Status engine_impl::RemoveHostAcknowledgement(
grpc::ServerContext* context [[maybe_unused]],
const HostIdentifier* request,
const NameOrIdIdentifier* request,
CommandSuccess* response [[maybe_unused]]) {
std::string err;
auto fn = std::packaged_task<int32_t(void)>([&err, request]() -> int32_t {
Expand Down Expand Up @@ -2767,7 +2817,7 @@ grpc::Status engine_impl::DelayServiceNotification(

switch (request->identifier_case()) {
case ServiceDelayIdentifier::kNames: {
NameIdentifier names = request->names();
PairNamesIdentifier names = request->names();
auto it =
service::services.find({names.host_name(), names.service_name()});
if (it != service::services.end())
Expand All @@ -2779,7 +2829,7 @@ grpc::Status engine_impl::DelayServiceNotification(
}
} break;
case ServiceDelayIdentifier::kIds: {
IdIdentifier ids = request->ids();
PairIdsIdentifier ids = request->ids();
auto it =
service::services_by_id.find({ids.host_id(), ids.service_id()});
if (it != service::services_by_id.end())
Expand Down Expand Up @@ -3581,7 +3631,7 @@ grpc::Status engine_impl::ShutdownProgram(

::grpc::Status engine_impl::EnableHostAndChildNotifications(
::grpc::ServerContext* context [[maybe_unused]],
const ::com::centreon::engine::HostIdentifier* request,
const ::com::centreon::engine::NameOrIdIdentifier* request,
::com::centreon::engine::CommandSuccess* response [[maybe_unused]]) {
HOST_METHOD_BEGIN
commands::processing::wrapper_enable_host_and_child_notifications(
Expand All @@ -3591,7 +3641,7 @@ ::grpc::Status engine_impl::EnableHostAndChildNotifications(

::grpc::Status engine_impl::DisableHostAndChildNotifications(
::grpc::ServerContext* context [[maybe_unused]],
const ::com::centreon::engine::HostIdentifier* request,
const ::com::centreon::engine::NameOrIdIdentifier* request,
::com::centreon::engine::CommandSuccess* response [[maybe_unused]]) {
HOST_METHOD_BEGIN
commands::processing::wrapper_disable_host_and_child_notifications(
Expand All @@ -3601,7 +3651,7 @@ ::grpc::Status engine_impl::DisableHostAndChildNotifications(

::grpc::Status engine_impl::DisableHostNotifications(
::grpc::ServerContext* context [[maybe_unused]],
const ::com::centreon::engine::HostIdentifier* request,
const ::com::centreon::engine::NameOrIdIdentifier* request,
::com::centreon::engine::CommandSuccess* response [[maybe_unused]]) {
HOST_METHOD_BEGIN
disable_host_notifications(host_info.first.get());
Expand All @@ -3610,7 +3660,7 @@ ::grpc::Status engine_impl::DisableHostNotifications(

::grpc::Status engine_impl::EnableHostNotifications(
::grpc::ServerContext* context [[maybe_unused]],
const ::com::centreon::engine::HostIdentifier* request,
const ::com::centreon::engine::NameOrIdIdentifier* request,
::com::centreon::engine::CommandSuccess* response [[maybe_unused]]) {
HOST_METHOD_BEGIN
enable_host_notifications(host_info.first.get());
Expand Down Expand Up @@ -3697,10 +3747,10 @@ ::grpc::Status engine_impl::ChangeAnomalyDetectionSensitivity(
*/
std::pair<std::shared_ptr<com::centreon::engine::host>, std::string>
engine_impl::get_host(
const ::com::centreon::engine::HostIdentifier& host_info) {
const ::com::centreon::engine::NameOrIdIdentifier& host_info) {
std::string err;
switch (host_info.identifier_case()) {
case HostIdentifier::kName: {
case NameOrIdIdentifier::kName: {
/* get the host */
auto ithostname = host::hosts.find(host_info.name());
if (ithostname != host::hosts.end())
Expand All @@ -3711,7 +3761,7 @@ engine_impl::get_host(
err);
}
} break;
case HostIdentifier::kId: {
case NameOrIdIdentifier::kId: {
/* get the host */
auto ithostid = host::hosts_by_id.find(host_info.id());
if (ithostid != host::hosts_by_id.end())
Expand Down Expand Up @@ -3739,7 +3789,7 @@ engine_impl::get_serv(
/* checking identifier sesrname (by names or by ids) */
switch (serv_info.identifier_case()) {
case ServiceIdentifier::kNames: {
const NameIdentifier& names = serv_info.names();
const PairNamesIdentifier& names = serv_info.names();
/* get the service */
auto itservicenames = service::services.find(
std::make_pair(names.host_name(), names.service_name()));
Expand All @@ -3753,7 +3803,7 @@ engine_impl::get_serv(
}
} break;
case ServiceIdentifier::kIds: {
const IdIdentifier& ids = serv_info.ids();
const PairIdsIdentifier& ids = serv_info.ids();
/* get the service */
auto itserviceids = service::services_by_id.find(
std::make_pair(ids.host_id(), ids.service_id()));
Expand Down
Loading
Loading