Title: [opsview] [13295] Merge of 12827 from DEV-professional

Property changes on: trunk
___________________________________________________________________
Modified: svn:mergeinfo
   - /branches/DEV-capside/release-3.9.0:6365-6370
/branches/DEV-extra-notification-data:6734-6746
/branches/DEV-odw-calculations:9016-9160
/branches/DEV-restapi-monitoringservers:9778
/branches/DEV-snmp-multi:7324-7376
/branches/US187:9403-9479
/branches/US198b:8875-8994
/branches/US310:9851-9873
/branches/US539:12178-12182
/branches/enterprise/BRAN-3.14:6874,6882,6956
/branches/enterprise/DEV-professional:6494-8475,8477-8478,8486,8490,8492,8494,8499-8500,8515,8518-8519,8524,8527-8529,8531-8532,8536,8538,8540-8544,8547-8552,8554,8557,8600-8601,8637,8749,8772,8897,8917,8944-8945,8947,8950-8952,8967,8969,8979,8982,8987,8998,9006,9008,9011,9013,9026,9033-9036,9038,9040,9042,9044,9048,9050-9053,9062,9064-9065,9069,9078,9081,9088,9096,9099-9101,9103,9105,9115,9118,9347,9353-9354,9411,9432,9445,9486,9538-9550,9569-9571,9620,9635,9637-9638,9645-9647,9693,9699,9721,9814,9825,9857,9879,9881,9939,10002,10029,10038-10041,10070,10086,10093,10345,10347,10388,10407,10481,10549,10560-10562,10565,10568,10571,10577,10622,10932,11025,11035,11360-11612,11798-11799,11920,12075-12076,12096,12189,12207,12232,12266,12288,12322,12334,12355,12372,12401-12406,12428,12491,12523,12539-12540,12544,12546,12555,12580,12583,12599-12600,12609,12611,12614-12615,12618,12624,12630-12631,12633,12643,12645-12646,12649,12653-12654,12657-12658,12660,12662-12663,12666,12668,12678-
 12679,12681-12682,12685,12687,12691-12693,12697,12703,12721,12724-12725,12731-12732,12737-12739,12741,12743-12744,12746,12750,12754-12756,12759,12764-12765,12770,12772-12776,12784,12787-12788,12795,12797,12799-12800,12806,12810,12814,12816-12817,12822-12823,13126-13127,13211
/branches/nagvis-iframe:9764-9786
/branches/opsview2runtime:10960-11258
/branches/opsview2runtime_xs:11279-11303
/branches/us168:8759,8762-8763,8766-8768,8770,8774-8777,8794,8798-8799,8801-8803,8821-8822,8834,8837-8838,8932,8937,8947,8969,8977,8981,9004,9007,9009-9010,9012,9024,9049
/commercial/branches/BRAN-4.2:10944,11017
/commercial/branches/BRAN-4.3:12423-12426
/commercial/branches/US306-slaves:9804-9823
/commercial/branches/US307-network-map:9685-9837
   + /branches/DEV-capside/release-3.9.0:6365-6370
/branches/DEV-extra-notification-data:6734-6746
/branches/DEV-odw-calculations:9016-9160
/branches/DEV-restapi-monitoringservers:9778
/branches/DEV-snmp-multi:7324-7376
/branches/US187:9403-9479
/branches/US198b:8875-8994
/branches/US310:9851-9873
/branches/US539:12178-12182
/branches/enterprise/BRAN-3.14:6874,6882,6956
/branches/enterprise/DEV-professional:6494-8475,8477-8478,8486,8490,8492,8494,8499-8500,8515,8518-8519,8524,8527-8529,8531-8532,8536,8538,8540-8544,8547-8552,8554,8557,8600-8601,8637,8749,8772,8897,8917,8944-8945,8947,8950-8952,8967,8969,8979,8982,8987,8998,9006,9008,9011,9013,9026,9033-9036,9038,9040,9042,9044,9048,9050-9053,9062,9064-9065,9069,9078,9081,9088,9096,9099-9101,9103,9105,9115,9118,9347,9353-9354,9411,9432,9445,9486,9538-9550,9569-9571,9620,9635,9637-9638,9645-9647,9693,9699,9721,9814,9825,9857,9879,9881,9939,10002,10029,10038-10041,10070,10086,10093,10345,10347,10388,10407,10481,10549,10560-10562,10565,10568,10571,10577,10622,10932,11025,11035,11360-11612,11798-11799,11920,12075-12076,12096,12189,12207,12232,12266,12288,12322,12334,12355,12372,12401-12406,12428,12491,12523,12539-12540,12544,12546,12555,12580,12583,12599-12600,12609,12611,12614-12615,12618,12624,12630-12631,12633,12643,12645-12646,12649,12653-12654,12657-12658,12660,12662-12663,12666,12668,12678-
 12679,12681-12682,12685,12687,12691-12693,12697,12703,12721,12724-12725,12731-12732,12737-12739,12741,12743-12744,12746,12750,12754-12756,12759,12764-12765,12770,12772-12776,12784,12787-12788,12795,12797,12799-12800,12806,12810,12814,12816-12817,12822-12823,12827,13126-13127,13211
/branches/nagvis-iframe:9764-9786
/branches/opsview2runtime:10960-11258
/branches/opsview2runtime_xs:11279-11303
/branches/us168:8759,8762-8763,8766-8768,8770,8774-8777,8794,8798-8799,8801-8803,8821-8822,8834,8837-8838,8932,8937,8947,8969,8977,8981,9004,9007,9009-9010,9012,9024,9049
/commercial/branches/BRAN-4.2:10944,11017
/commercial/branches/BRAN-4.3:12423-12426
/commercial/branches/US306-slaves:9804-9823
/commercial/branches/US307-network-map:9685-9837

Modified: trunk/opsview-core/lib/Opsview/ResultSet/Hostgroups.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/ResultSet/Hostgroups.pm	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-core/lib/Opsview/ResultSet/Hostgroups.pm	2013-08-15 19:41:58 UTC (rev 13295)
@@ -84,6 +84,33 @@
     return $self->search( { rgt => \"= lft+1" } );
 }
 
+sub non_leaf {
+    my ($self) = @_;
+    return $self->search( { rgt => \"!= lft+1" } );
+}
+
+sub order_by_depth_last {
+    my ($self) = @_;
+    return $self->search( {}, { order_by => "matpath" } );
+}
+
+sub search_leaves_without_hosts {
+    my $self = shift;
+
+    # The CONCAT trick was seen on MySQL site http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
+    # by Chris Stubben to count number of comma separators
+    return $self->search(
+        {},
+        {
+            join      => "hosts",
+            '+select' => [ \"COUNT(hosts.id) AS hosts_count" ],
+            "+as"     => ['hosts_count'],
+            having    => { hosts_count => 0 },
+            group_by  => "id",
+        }
+    );
+}
+
 # This is quite horrible. We need to do this so that when synchronisations occur, we search first by name
 # then by matpath. DBIx::Class::ResultSet quite rightly doesn't do this because name is not a unique column
 sub find {
@@ -146,12 +173,43 @@
 # there is only ever one host group with parent=undef
 sub synchronise {
     my ( $self, $original_attrs, $info ) = @_;
+
+    my $get_highest_hostgroup_parent_res;
     if ( exists $original_attrs->{parent}
         && !defined $original_attrs->{parent} )
     {
         $original_attrs->{id} = 1;
     }
+    elsif ($info->{create_only}
+        && !exists $original_attrs->{parent}
+        && $info->{user} )
+    {
+
+        # When creating a new host group and no parent is specified, we find the most appropriate
+        # one and then after the host group is created, we add it to the access for this user's
+        # role. This seems reasonable as the user created it
+        $get_highest_hostgroup_parent_res =
+          $info->{user}->get_highest_hostgroup_parent;
+
+        $original_attrs->{parent} =
+          $get_highest_hostgroup_parent_res->{hostgroup}->id;
+    }
+
     my $obj = $self->next::method( $original_attrs, $info );
+
+    if ($get_highest_hostgroup_parent_res) {
+        my $role = $info->{user}->role;
+
+        # Tick in roles -> status objects page
+        unless ( $role->all_hostgroups ) {
+            $role->add_to_access_hostgroups($obj);
+        }
+
+        # Add to access control to see host group, but only if this was a non-leaf
+        if ( $get_highest_hostgroup_parent_res->{used_parent} ) {
+            $role->add_to_hostgroups($obj);
+        }
+    }
     $info->{seen_ids}->{ $obj->id }++ if exists $info->{seen_ids};
     $obj;
 }
@@ -171,14 +229,20 @@
         return $self->$action($search_args);
     }
     else {
-        return $self->find_by_name_matpath(
-            $action,
-            {
-                name    => $attrs->{name},
-                matpath => $attrs->{matpath}
-            },
-            $info
-        );
+        if ( $action eq "new" ) {
+            return $self->new($attrs);
+        }
+        else {
+
+            return $self->find_by_name_matpath(
+                $action,
+                {
+                    name    => $attrs->{name},
+                    matpath => $attrs->{matpath}
+                },
+                $info
+            );
+        }
     }
 }
 

Modified: trunk/opsview-core/lib/Opsview/ResultSet.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/ResultSet.pm	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-core/lib/Opsview/ResultSet.pm	2013-08-15 19:41:58 UTC (rev 13295)
@@ -345,6 +345,7 @@
 
         # Set attributes
         foreach my $attr ( keys %$attrs ) {
+            next if $attr =~ /^\./;
             if ( $object->has_column($attr) ) {
                 $object->$attr( $attrs->{$attr} );
             }

Modified: trunk/opsview-core/lib/Opsview/Schema/Contacts.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/Schema/Contacts.pm	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-core/lib/Opsview/Schema/Contacts.pm	2013-08-15 19:41:58 UTC (rev 13295)
@@ -394,6 +394,36 @@
     return @cgs;
 }
 
+# This works out what the best host group parent is to be used when adding a new host group
+# The rules are:
+#   - use the highest non-leaf host group the user has access to, or
+#   - find a leaf without any hosts, or
+#   - for the first leaf, pick the same parent
+sub get_highest_hostgroup_parent {
+    my ($self) = @_;
+    my $res    = {};
+    my $role   = $self->role;
+    my $hg     = $role->hostgroups->non_leaf->order_by_depth_last->first;
+    if ($hg) {
+        return {
+            hostgroup   => $hg,
+            used_parent => 0
+        };
+    }
+    $hg = $role->hostgroups->search_leaves_without_hosts->first;
+    if ($hg) {
+        return {
+            hostgroup   => $hg,
+            used_parent => 0
+        };
+    }
+    $hg = $role->hostgroups->leaves->first;
+    return {
+        hostgroup   => $hg->parent,
+        used_parent => 1
+    };
+}
+
 __PACKAGE__->validation_auto(1);
 __PACKAGE__->validation_filter(0);
 __PACKAGE__->validation_module( "Data::FormValidator" );

Modified: trunk/opsview-core/t/940contacts.t
===================================================================
--- trunk/opsview-core/t/940contacts.t	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-core/t/940contacts.t	2013-08-15 19:41:58 UTC (rev 13295)
@@ -57,6 +57,22 @@
 is( $adminnoc->language, "en" );
 isnt( $adminnoc->encrypted_password, "secret" );
 
+sub is_highest_hostgroup_parent {
+    my ($user, $expected) = @_;
+    my @caller = caller;
+    my $res = $user->get_highest_hostgroup_parent;
+
+    # Change to just id, otherwise Data::Dump::dump will return massive output
+    my $got = { %$res,
+        hostgroup => $res->{hostgroup}->id,
+    };
+    $got->{hostgroup} = $res->{hostgroup}->id;
+    is_deeply( $got, $expected ) or diag "line $caller[2], hostgroup name=".$res->{hostgroup}->name ." with ". Data::Dump::dump($got);
+}
+
+is_highest_hostgroup_parent( $admin, {hostgroup => 1, used_parent => 0} );
+is_highest_hostgroup_parent( $adminnoc, {hostgroup => 3, used_parent => 1 } );
+
 $expected = {
     description => "rah",
     id => 12,

Modified: trunk/opsview-core/t/944hostgroups.t
===================================================================
--- trunk/opsview-core/t/944hostgroups.t	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-core/t/944hostgroups.t	2013-08-15 19:41:58 UTC (rev 13295)
@@ -21,6 +21,12 @@
 
 is( $rs->count, 10 );
 
+is(
+    $rs->search_leaves_without_hosts->first->id,
+    9,
+    "Finds the first host group without any hosts in, for adding a new host group into"
+);
+
 # Count number of hosts
 is(
     $rs->find( { name => "leaf" } )->count_related("hosts"),

Modified: trunk/opsview-web/lib/Opsview/Web/Controller/Admin/Hostgroup.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/Controller/Admin/Hostgroup.pm	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-web/lib/Opsview/Web/Controller/Admin/Hostgroup.pm	2013-08-15 19:41:58 UTC (rev 13295)
@@ -32,6 +32,7 @@
     page_search_box        => 1,
     page_add_new           => 1,
     acl                    => 'CONFIGUREHOSTGROUPS',
+    use_synchronise        => {},
 );
 
 before 'auto' => sub {

Modified: trunk/opsview-web/lib/Opsview/Web/ControllerBase/Admin.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/ControllerBase/Admin.pm	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-web/lib/Opsview/Web/ControllerBase/Admin.pm	2013-08-15 19:41:58 UTC (rev 13295)
@@ -543,7 +543,10 @@
     my ( $self, $c, $attrs ) = @_;
 
     if ( $c->debug ) { warn( Data::Dump::dump($attrs) ) }
-    my $info = { username => $c->user->username, };
+    my $info = {
+        username => $c->user->username,
+        user     => $c->user
+    };
     if ( $c->stash->{create_only} ) {
         $info->{create_only} = 1;
     }

Modified: trunk/opsview-web/lib/Opsview/Web/ControllerBase/Config.pm
===================================================================
--- trunk/opsview-web/lib/Opsview/Web/ControllerBase/Config.pm	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-web/lib/Opsview/Web/ControllerBase/Config.pm	2013-08-15 19:41:58 UTC (rev 13295)
@@ -339,6 +339,7 @@
     my ( $self, $c, $data, $options ) = @_;
     $options ||= {};
     $options->{username} = $c->user->username;
+    $options->{user}     = $c->user;
     my $new_object;
     my $list_object_number = $c->stash->{list_object_number};
     try {

Modified: trunk/opsview-web/t/815hostgroup.t
===================================================================
--- trunk/opsview-web/t/815hostgroup.t	2013-08-15 19:39:32 UTC (rev 13294)
+++ trunk/opsview-web/t/815hostgroup.t	2013-08-15 19:41:58 UTC (rev 13295)
@@ -10,6 +10,8 @@
 
 use Opsview::Test qw(opsview);
 use Test::WWW::Mechanize::Catalyst qw(Opsview::Web);
+
+#use Test::WWW::Mechanize;
 use HTTP::Cookies;
 use Sys::Hostname;
 
@@ -23,11 +25,48 @@
 use Test::More qw(no_plan);
 use Test::Deep;
 
-my $ua = Test::WWW::Mechanize::Catalyst->new( autocheck => 0 );
-
 my $hostname = Sys::Hostname::hostname;
 $hostname = "localhost:3000";
 
+my $adminnoc = Test::WWW::Mechanize::Catalyst->new( autocheck => 0 );
+
+#my $adminnoc = Test::WWW::Mechanize->new( autocheck => 0 );
+log_in( $adminnoc, "adminnoc", "adminnoc" );
+
+$adminnoc->get_ok( "/admin/hostgroup" );
+$adminnoc->content_lacks( "newhostgroup" );
+
+$adminnoc->get_ok( "/admin/role/edit/16",
+    "Checking role whether host groups listed"
+);
+$adminnoc->form_name( "main_form" );
+$adminnoc->content_lacks( "newhostgroup" );
+$adminnoc->untick( "all_hostgroups", 1 );
+map { $_->disabled(0) }
+  ( $adminnoc->find_all_inputs( name => "access_hostgroups" ) );
+$adminnoc->tick( "access_hostgroups", 4 );
+$adminnoc->tick( "access_keywords",   9 );
+$adminnoc->submit;
+is( $adminnoc->status, 200, "Changed role to include Leaf host groups" );
+
+$adminnoc->get_ok( "/admin/hostgroup/new" );
+$adminnoc->form_name( "main_form" );
+$adminnoc->field( 'name', 'aaanewhostgroup' );
+$adminnoc->submit;
+is( $adminnoc->status, 200, "Submitted new host group" );
+
+$adminnoc->get_ok( "/admin/hostgroup" );
+$adminnoc->content_contains( "newhostgroup" );
+
+$adminnoc->get_ok( "/admin/role/edit/16",
+    "Checking role whether host groups listed"
+);
+$adminnoc->content_contains( "newhostgroup" );
+$adminnoc->form_name( "main_form" );
+is( $adminnoc->value( "access_hostgroups", 1 ), 11, "Host group is set" );
+
+my $ua = Test::WWW::Mechanize::Catalyst->new( autocheck => 0 );
+
 # No cookies here - should get login page
 log_in( $ua, "admin", "initial" );
 

_______________________________________________
Opsview-checkins mailing list
Opsview-checkins@lists.opsview.org
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to