[icinga-checkins] icinga.org: icinga2/feature/configconvert-2743: configconvert: detect additive inheritance for *groups and use += on export

git at icinga.org git at icinga.org
Tue May 28 19:38:12 CEST 2013


Module: icinga2
Branch: feature/configconvert-2743
Commit: fccc07ae0fd0b72a6d04c4e167bc71f3f88ce5d9
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=fccc07ae0fd0b72a6d04c4e167bc71f3f88ce5d9

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Tue May 28 19:31:32 2013 +0200

configconvert: detect additive inheritance for *groups and use += on export

i guess this can be used for host/service contact(groups) and other foo
as well, so let's see.

refs #4006

---

 contrib/configconvert/Icinga2/Convert.pm          |   18 +++++++++++++++
 contrib/configconvert/Icinga2/ExportIcinga2Cfg.pm |   24 +++++++++++++++++---
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index 5ffc014..b0c73e4 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -738,6 +738,11 @@ sub convert_2x {
             # debug # @{$cfg_obj_2x->{'service'}->{$service_cnt}->{'servicegroups'}} = ();
         
             if(defined($obj_1x_service->{'servicegroups'})) {
+                # check if there's additive inheritance required, and save a flag
+                if ($obj_1x_service->{'servicegroups'} =~ /^\+/) {
+                    $cfg_obj_2x->{'service'}->{$service_cnt}->{'__I2_CONVERT_SG_ADD'} = 1;
+                    $obj_1x_service->{'servicegroups'} =~ s/^\+//;
+                }
                 # convert comma seperated list to array
                 push @{$cfg_obj_2x->{'service'}->{$service_cnt}->{'servicegroups'}}, Icinga2::Utils::str2arr_by_delim_without_excludes($obj_1x_service->{'servicegroups'}, ',', 1);
                 #print "DEBUG: servicegroups " . join (" ", @{$cfg_obj_2x->{'service'}->{$service_cnt}->{'servicegroups'}});
@@ -917,6 +922,12 @@ sub convert_2x {
         delete($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'hostgroups'});
 
         if(defined($obj_1x_host->{'hostgroups'})) {
+            # check if there's additive inheritance required, and save a flag
+            if ($obj_1x_host->{'hostgroups'} =~ /^\+/) {
+                $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'__I2_CONVERT_HG_ADD'} = 1;
+                $obj_1x_host->{'hostgroups'} =~ s/^\+//;
+            }
+
             # convert comma seperated list to array
             push @{$cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'hostgroups'}}, Icinga2::Utils::str2arr_by_delim_without_excludes($obj_1x_host->{'hostgroups'}, ',', 1);
             #print "DEBUG: hostgroups " . join (" ", @{$cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'hostgroups'}});
@@ -1041,6 +1052,13 @@ sub convert_2x {
         
         delete($cfg_obj_2x->{'user'}->{$contact_obj_1x_key}->{'usergroups'});
         if(defined($obj_1x_contact->{'contactgroups'})) {
+
+            # check if there's additive inheritance required, and save a flag
+            if ($obj_1x_contact->{'contactgroups'} =~ /^\+/) {
+                $cfg_obj_2x->{'user'}->{$contact_obj_1x_key}->{'__I2_CONVERT_UG_ADD'} = 1;
+                $obj_1x_contact->{'contactgroups'} =~ s/^\+//;
+            }
+
             push @{$cfg_obj_2x->{'user'}->{$contact_obj_1x_key}->{'usergroups'}}, Icinga2::Utils::str2arr_by_delim_without_excludes($obj_1x_contact->{'contactgroups'}, ',', 1);
             #print "DEBUG: usergroups " . join (" ", @{$cfg_obj_2x->{'user'}->{$contact_obj_1x_key}->{'usergroups'}});
         }
diff --git a/contrib/configconvert/Icinga2/ExportIcinga2Cfg.pm b/contrib/configconvert/Icinga2/ExportIcinga2Cfg.pm
index c74beb2..ab5007b 100644
--- a/contrib/configconvert/Icinga2/ExportIcinga2Cfg.pm
+++ b/contrib/configconvert/Icinga2/ExportIcinga2Cfg.pm
@@ -283,7 +283,11 @@ sub dump_service_2x {
     ####################################################
     if(defined($service_2x->{'servicegroups'})) {
         my $servicegroups = join '", "', @{$service_2x->{'servicegroups'}};
-        dump_config_line($icinga2_cfg, "\tservicegroups = [ \"$servicegroups\" ],");
+        if ($service_2x->{'__I2_CONVERT_SG_ADD'} == 1) {
+            dump_config_line($icinga2_cfg, "\tservicegroups += [ \"$servicegroups\" ],");
+        } else {
+            dump_config_line($icinga2_cfg, "\tservicegroups = [ \"$servicegroups\" ],");
+        }
     }
 
     ####################################################
@@ -432,7 +436,11 @@ sub dump_host_2x {
     ####################################################
     if(defined($host_2x->{'hostgroups'})) {
         my $hostgroups = join '", "', @{$host_2x->{'hostgroups'}};
-        dump_config_line($icinga2_cfg, "\thostgroups = [ \"$hostgroups\" ],");
+        if ($host_2x->{'__I2_CONVERT_HG_ADD'} == 1) {
+            dump_config_line($icinga2_cfg, "\thostgroups += [ \"$hostgroups\" ],");
+        } else {
+            dump_config_line($icinga2_cfg, "\thostgroups = [ \"$hostgroups\" ],");
+        }
     }
 
     ####################################################
@@ -579,7 +587,11 @@ sub dump_host_2x {
         if(defined($service_2x->{'servicegroups'})) {
             #say Dumper($service_2x->{'servicegroups'});
             my $servicegroups = join '", "', @{$service_2x->{'servicegroups'}};
-            dump_config_line($icinga2_cfg, "\t\tservicegroups = [ \"$servicegroups\" ],");
+            if ($service_2x->{'__I2_CONVERT_SG_ADD'} == 1) {
+                dump_config_line($icinga2_cfg, "\t\tservicegroups += [ \"$servicegroups\" ],");
+            } else {
+                dump_config_line($icinga2_cfg, "\t\tservicegroups = [ \"$servicegroups\" ],");
+            }
         }
 
         ####################################################
@@ -697,7 +709,11 @@ sub dump_user_2x {
     if(defined($user_2x->{'usergroups'})) {
         #say Dumper($user_2x->{'usergroups'});
         my $usergroups = join '", "', @{$user_2x->{'usergroups'}};
-        dump_config_line($icinga2_cfg, "\tgroups = [ \"$usergroups\" ],");
+        if ($user_2x->{'__I2_CONVERT_UG_ADD'} == 1) {
+            dump_config_line($icinga2_cfg, "\tgroups += [ \"$usergroups\" ],");
+        } else {
+            dump_config_line($icinga2_cfg, "\tgroups = [ \"$usergroups\" ],");
+        }
     }
 
     dump_config_line($icinga2_cfg, "");





More information about the icinga-checkins mailing list