From aa42a0eb23b713b5715ac37e4987a4ff184873d6 Mon Sep 17 00:00:00 2001 From: Soufiane Echkem Date: Thu, 24 Oct 2024 16:11:21 +0100 Subject: [PATCH 1/3] 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; From b6d337a4fbfc883642104dc239eb46050cd52ea1 Mon Sep 17 00:00:00 2001 From: Soufiane Echkem Date: Thu, 24 Oct 2024 16:13:08 +0100 Subject: [PATCH 2/3] feat(engine): Add inheritance test HostGroup --- tests/engine/contactgroup_inheritance.robot | 318 ++++++++++++++++++++ tests/resources/Engine.py | 28 ++ 2 files changed, 346 insertions(+) create mode 100644 tests/engine/contactgroup_inheritance.robot diff --git a/tests/engine/contactgroup_inheritance.robot b/tests/engine/contactgroup_inheritance.robot new file mode 100644 index 00000000000..9ce0e38ca96 --- /dev/null +++ b/tests/engine/contactgroup_inheritance.robot @@ -0,0 +1,318 @@ +*** Settings *** +Documentation Centreon Broker and Engine Verify contactgroup inheritance. + +Resource ../resources/import.resource + +Suite Setup Ctn Clean Before Suite +Suite Teardown Ctn Clean After Suite +Test Setup Ctn Stop Processes +Test Teardown Ctn Save Logs If Failed + + +*** Test Cases *** +EBSN5 + [Documentation] Verify contactgroup inheritance : contactgroup(empty) inherit from template (full) , on Start Engine + [Tags] engine contactgroup MON-151622 + Ctn Config Engine ${1} ${5} ${5} + Ctn Config Broker rrd + Ctn Config Broker central + Ctn Config Broker module + Ctn Config BBDO3 1 + Ctn Clear Retention + + # Create files : + Ctn Create Template File ${0} contactgroup alias ["contactgroup_template_1_alias"] + + # Delete unnecessary fields in templates: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 active_checks_enabled contactgroupTemplates.cfg + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 passive_checks_enabled contactgroupTemplates.cfg + + # Add Command : + Ctn Engine Config Add Command 0 command_notif /usr/bin/true + Ctn Engine Config Set Value In Contacts 0 John_Doe host_notification_commands command_notif + Ctn Engine Config Set Value In Contacts 0 John_Doe service_notification_commands command_notif + + # Add necessarily files : + Ctn Config Engine Add Cfg File ${0} contacts.cfg + Ctn Config Engine Add Cfg File ${0} contactgroups.cfg + Ctn Config Engine Add Cfg File ${0} contactgroupTemplates.cfg + + # Create contact groups + Ctn Add Contact Group ${0} ${1} ["John_Doe"] + Ctn Add Contact Group ${0} ${2} ["U1"] + Ctn Add Contact Group ${0} ${3} ["U2"] + Ctn Add Contact Group ${0} ${4} ["U3","U4"] + + # Delete unnecessary fields in contactgroup: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_1 alias contactgroups.cfg + Ctn Engine Config Delete Key In Cfg 0 contactgroup_1 members contactgroups.cfg + + # Set contactgroup_1 to use contactgroup_template_1 + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 use contactgroup_template_1 contactgroups.cfg + + # Operation in contactTemplates + ${config_values} Create Dictionary +... contactgroup_members contactgroup_3 +... members U1 + + FOR ${key} ${value} IN &{config_values} + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg + END + + ${start} Get Current Date + Ctn Clear Retention + Ctn Start Broker + Ctn Start Engine + Ctn Wait For Engine To Be Ready ${start} ${1} + + ${output} Ctn Get Contactgroup Info Grpc contactgroup_1 + + Should Be Equal As Strings ${output}[name] contactgroup_1 + Should Be Equal As Strings ${output}[alias] contactgroup_template_1_alias + Should Not Contain ${output}[members] John_Doe + Should Contain ${output}[members] U1 + Should Contain ${output}[members] U2 + + Ctn Stop Engine + Ctn Kindly Stop Broker + +EBSN6 + [Documentation] Verify contactgroup inheritance : contactgroup(full) inherit from template (full) , on Start Engine + [Tags] engine contactgroup MON-151622 + Ctn Config Engine ${1} ${5} ${5} + Ctn Config Broker rrd + Ctn Config Broker central + Ctn Config Broker module + Ctn Config BBDO3 1 + Ctn Clear Retention + + # Create files : + Ctn Create Template File ${0} contactgroup alias ["contactgroup_template_1_alias"] + + # Delete unnecessary fields in templates: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 active_checks_enabled contactgroupTemplates.cfg + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 passive_checks_enabled contactgroupTemplates.cfg + + # Add Command : + Ctn Engine Config Add Command 0 command_notif /usr/bin/true + Ctn Engine Config Set Value In Contacts 0 John_Doe host_notification_commands command_notif + Ctn Engine Config Set Value In Contacts 0 John_Doe service_notification_commands command_notif + + # Add necessarily files : + Ctn Config Engine Add Cfg File ${0} contacts.cfg + Ctn Config Engine Add Cfg File ${0} contactgroups.cfg + Ctn Config Engine Add Cfg File ${0} contactgroupTemplates.cfg + + # Operation in contact group + Ctn Add Contact Group ${0} ${1} ["John_Doe"] + Ctn Add Contact Group ${0} ${2} ["U1"] + Ctn Add Contact Group ${0} ${3} ["U2"] + Ctn Add Contact Group ${0} ${4} ["U3","U4"] + + # Contactgroup_1 to use contactgroup_template_1 + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 use contactgroup_template_1 contactgroups.cfg + + # Operation in contactTemplates + ${config_values} Create Dictionary +... contactgroup_members contactgroup_2 + + FOR ${key} ${value} IN &{config_values} + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 ${key} ${value} contactgroups.cfg + END + + ${config_values_tmp} Create Dictionary +... contactgroup_members contactgroup_4 +... members U2 + + FOR ${key} ${value} IN &{config_values_tmp} + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg + END + + ${start} Get Current Date + Ctn Clear Retention + Ctn Start Broker + Ctn Start Engine + Ctn Wait For Engine To Be Ready ${start} ${1} + + ${output} Ctn Get Contactgroup Info Grpc contactgroup_1 + + Should Be Equal As Strings ${output}[name] contactgroup_1 + Should Be Equal As Strings ${output}[alias] contactgroup_1 + Should Contain ${output}[members] John_Doe + Should Contain ${output}[members] U1 + Should Not Contain Any ${output}[members] U2 U3 U4 + + Ctn Stop Engine + Ctn Kindly Stop Broker + +EBSN7 + [Documentation] Verify contactgroup inheritance : contactgroup(empty) inherit from template (full) , on Reload Engine + [Tags] engine contactgroup MON-151622 + Ctn Config Engine ${1} ${5} ${5} + Ctn Config Broker rrd + Ctn Config Broker central + Ctn Config Broker module + Ctn Config BBDO3 1 + Ctn Clear Retention + + # Create files : + Ctn Create Template File ${0} contactgroup alias ["contactgroup_template_1_alias"] + + # Delete unnecessary fields in templates: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 active_checks_enabled contactgroupTemplates.cfg + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 passive_checks_enabled contactgroupTemplates.cfg + + # Add Command : + Ctn Engine Config Add Command 0 command_notif /usr/bin/true + Ctn Engine Config Set Value In Contacts 0 John_Doe host_notification_commands command_notif + Ctn Engine Config Set Value In Contacts 0 John_Doe service_notification_commands command_notif + + # Add necessarily files : + Ctn Config Engine Add Cfg File ${0} contacts.cfg + Ctn Config Engine Add Cfg File ${0} contactgroups.cfg + Ctn Config Engine Add Cfg File ${0} contactgroupTemplates.cfg + + # Operation in contact group + Ctn Add Contact Group ${0} ${1} ["John_Doe"] + + ${start} Get Current Date + Ctn Clear Retention + Ctn Start Broker + Ctn Start Engine + Ctn Wait For Engine To Be Ready ${start} ${1} + + # Operation in contact group + Ctn Add Contact Group ${0} ${2} ["U1"] + Ctn Add Contact Group ${0} ${3} ["U2"] + Ctn Add Contact Group ${0} ${4} ["U3","U4"] + + # Delete unnecessary fields in contactgroup: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_1 alias contactgroups.cfg + Ctn Engine Config Delete Key In Cfg 0 contactgroup_1 members contactgroups.cfg + + # Contactgroup_1 to use contactgroup_template_1 + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 use contactgroup_template_1 contactgroups.cfg + + # Operation in contactTemplates + ${config_values} Create Dictionary +... contactgroup_members contactgroup_3 +... members U1 + + FOR ${key} ${value} IN &{config_values} + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg + END + + # Reload engine + ${start} Get Current Date + Ctn Reload Engine + Ctn Wait For Engine To Be Ready ${start} ${1} + ${content} Create List Reload configuration finished + ${result} Ctn Find In Log With Timeout + ... ${ENGINE_LOG}/config0/centengine.log + ... ${start} + ... ${content} + ... 60 + ... verbose=False + Should Be True ${result} Engine is Not Ready after 60s!! + + ${output} Ctn Get Contactgroup Info Grpc contactgroup_1 + + Should Be Equal As Strings ${output}[name] contactgroup_1 + Should Be Equal As Strings ${output}[alias] contactgroup_template_1_alias + Should Not Contain ${output}[members] John_Doe + Should Contain ${output}[members] U1 + Should Contain ${output}[members] U2 + + Ctn Stop Engine + Ctn Kindly Stop Broker + +EBSN8 + [Documentation] Verify contactgroup inheritance : contactgroup(full) inherit from template (full) , on Reload Engine + [Tags] engine contactgroup MON-151622 + Ctn Config Engine ${1} ${5} ${5} + Ctn Config Broker rrd + Ctn Config Broker central + Ctn Config Broker module + Ctn Config BBDO3 1 + Ctn Clear Retention + + # Create files : + Ctn Create Template File ${0} contactgroup alias ["contactgroup_template_1_alias"] + + # Delete unnecessary fields in templates: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 active_checks_enabled contactgroupTemplates.cfg + Ctn Engine Config Delete Key In Cfg 0 contactgroup_template_1 passive_checks_enabled contactgroupTemplates.cfg + + # Add Command : + Ctn Engine Config Add Command 0 command_notif /usr/bin/true + Ctn Engine Config Set Value In Contacts 0 John_Doe host_notification_commands command_notif + Ctn Engine Config Set Value In Contacts 0 John_Doe service_notification_commands command_notif + + # Add necessarily files : + Ctn Config Engine Add Cfg File ${0} contacts.cfg + Ctn Config Engine Add Cfg File ${0} contactgroups.cfg + Ctn Config Engine Add Cfg File ${0} contactgroupTemplates.cfg + + # Operation in contact group + Ctn Add Contact Group ${0} ${1} ["John_Doe"] + + ${start} Get Current Date + Ctn Clear Retention + Ctn Start Broker + Ctn Start Engine + Ctn Wait For Engine To Be Ready ${start} ${1} + + # Operation in contact group + Ctn Add Contact Group ${0} ${2} ["U1"] + Ctn Add Contact Group ${0} ${3} ["U2"] + Ctn Add Contact Group ${0} ${4} ["U3","U4"] + + # Delete unnecessary fields in contactgroup: + Ctn Engine Config Delete Key In Cfg 0 contactgroup_1 members contactgroups.cfg + + # Contactgroup_1 to use contactgroup_template_1 + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 use contactgroup_template_1 contactgroups.cfg + + # Operation in contactgroup + ${config_values} Create Dictionary +... contactgroup_members contactgroup_3 +... members U1 + + FOR ${key} ${value} IN &{config_values} + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 ${key} ${value} contactgroups.cfg + END + + # Operation in contactgroupTemplates + ${config_values_tmp} Create Dictionary +... contactgroup_members contactgroup_4 +... members John_Doe + + FOR ${key} ${value} IN &{config_values_tmp} + Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg + END + + # Reload engine + ${start} Get Current Date + Ctn Reload Engine + Ctn Wait For Engine To Be Ready ${start} ${1} + ${content} Create List Reload configuration finished + ${result} Ctn Find In Log With Timeout + ... ${ENGINE_LOG}/config0/centengine.log + ... ${start} + ... ${content} + ... 60 + ... verbose=False + Should Be True ${result} Engine is Not Ready after 60s!! + + ${output} Ctn Get Contactgroup Info Grpc contactgroup_1 + + Log To Console ${output} + + Should Be Equal As Strings ${output}[name] contactgroup_1 + Should Be Equal As Strings ${output}[alias] contactgroup_1 + Should Not Contain Any ${output}[members] John_Doe U3 U4 + Should Contain ${output}[members] U1 + Should Contain ${output}[members] U2 + + Ctn Stop Engine + Ctn Kindly Stop Broker \ No newline at end of file diff --git a/tests/resources/Engine.py b/tests/resources/Engine.py index 09c778d8283..2ec604687f7 100755 --- a/tests/resources/Engine.py +++ b/tests/resources/Engine.py @@ -1206,6 +1206,8 @@ def ctn_engine_config_set_key_value_in_cfg(idx: int, desc: str, key: str, value: r = re.compile(r"^\s*hostgroup_name\s+" + desc + "\s*$") elif file == "servicegroups.cfg": r = re.compile(r"^\s*servicegroup_name\s+" + desc + "\s*$") + elif file == "contactgroups.cfg": + r = re.compile(r"^\s*contactgroup_name\s+" + desc + "\s*$") elif len(file) > 13 and file[-13:] == "Templates.cfg": r = re.compile(r"^\s*name\s+" + desc + "\s*$") else: @@ -1242,6 +1244,8 @@ def ctn_engine_config_delete_key_in_cfg(idx: int, desc: str, key: str, file): r = re.compile(r"^\s*hostgroup_name\s+" + desc + "\s*$") elif file == "servicegroups.cfg": r = re.compile(r"^\s*servicegroup_name\s+" + desc + "\s*$") + elif file == "contactgroups.cfg": + r = re.compile(r"^\s*contactgroup_name\s+" + desc + "\s*$") elif len(file) > 13 and file[-13:] == "Templates.cfg": if file[-13:] == "Templates.cfg": r = re.compile(r"^\s*name\s+" + desc + "\s*$") @@ -4033,6 +4037,30 @@ def ctn_get_servicegroup_info_grpc(name:str): logger.console(f"gRPC server not ready {e}") return {} +def ctn_get_contactgroup_info_grpc(name:str): + """ + Retrieve contact group information via a gRPC call. + + Args: + name: The name of the contact group to retrieve. + + Returns: + A dictionary containing the contact group information, if successfully retrieved. + """ + limit = time.time() + 30 + while time.time() < limit: + time.sleep(1) + with grpc.insecure_channel("127.0.0.1:50001") as channel: + stub = engine_pb2_grpc.EngineStub(channel) + request = engine_pb2.NameIdentifier(name=name) + try: + cg = stub.GetContactGroup(request) + cg_dict = MessageToDict(cg, always_print_fields_with_no_presence=True) + return cg_dict + except Exception as e: + logger.console(f"gRPC server not ready {e}") + return {} + def ctn_check_key_value_existence(data_list, key, value): """ Check if a specific key-value pair exists in a list of data strings. From af3b2d6a6aae4322c0638fa73ab288b476a33436 Mon Sep 17 00:00:00 2001 From: Soufiane Echkem Date: Mon, 4 Nov 2024 10:19:24 +0100 Subject: [PATCH 3/3] refactor(test): review --- tests/engine/contactgroup_inheritance.robot | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/engine/contactgroup_inheritance.robot b/tests/engine/contactgroup_inheritance.robot index 9ce0e38ca96..c060371f2f2 100644 --- a/tests/engine/contactgroup_inheritance.robot +++ b/tests/engine/contactgroup_inheritance.robot @@ -52,8 +52,8 @@ EBSN5 # Operation in contactTemplates ${config_values} Create Dictionary -... contactgroup_members contactgroup_3 -... members U1 + ... contactgroup_members contactgroup_3 + ... members U1 FOR ${key} ${value} IN &{config_values} Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg @@ -98,7 +98,7 @@ EBSN6 Ctn Engine Config Set Value In Contacts 0 John_Doe host_notification_commands command_notif Ctn Engine Config Set Value In Contacts 0 John_Doe service_notification_commands command_notif - # Add necessarily files : + # Add the necessarily files : Ctn Config Engine Add Cfg File ${0} contacts.cfg Ctn Config Engine Add Cfg File ${0} contactgroups.cfg Ctn Config Engine Add Cfg File ${0} contactgroupTemplates.cfg @@ -114,15 +114,15 @@ EBSN6 # Operation in contactTemplates ${config_values} Create Dictionary -... contactgroup_members contactgroup_2 + ... contactgroup_members contactgroup_2 FOR ${key} ${value} IN &{config_values} Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 ${key} ${value} contactgroups.cfg END ${config_values_tmp} Create Dictionary -... contactgroup_members contactgroup_4 -... members U2 + ... contactgroup_members contactgroup_4 + ... members U2 FOR ${key} ${value} IN &{config_values_tmp} Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg @@ -195,8 +195,8 @@ EBSN7 # Operation in contactTemplates ${config_values} Create Dictionary -... contactgroup_members contactgroup_3 -... members U1 + ... contactgroup_members contactgroup_3 + ... members U1 FOR ${key} ${value} IN &{config_values} Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg @@ -275,8 +275,8 @@ EBSN8 # Operation in contactgroup ${config_values} Create Dictionary -... contactgroup_members contactgroup_3 -... members U1 + ... contactgroup_members contactgroup_3 + ... members U1 FOR ${key} ${value} IN &{config_values} Ctn Engine Config Set Key Value In Cfg 0 contactgroup_1 ${key} ${value} contactgroups.cfg @@ -284,8 +284,8 @@ EBSN8 # Operation in contactgroupTemplates ${config_values_tmp} Create Dictionary -... contactgroup_members contactgroup_4 -... members John_Doe + ... contactgroup_members contactgroup_4 + ... members John_Doe FOR ${key} ${value} IN &{config_values_tmp} Ctn Engine Config Set Key Value In Cfg 0 contactgroup_template_1 ${key} ${value} contactgroupTemplates.cfg