NConf::DB::Modify::updateValue

User / Community contributed content: modifications, patches, extensions, modules, scripts etc.

NConf::DB::Modify::updateValue

Postby benediktkoeppel » Sat Dec 10, 2011 15:53

Hi

Attached a little patch to support NConf::DB::Modify::updateValue. updateValue can update an existing value of an object.

I have added another option to the insertValue method, to switch it from INSERT into UPDATE mode. Further down, I added UPDATE SQLs where the library is doing INSERT at the moment. insertValue decides based on the "update" parameter, if it should use INSERT or UPDATE SQL.
Then, the updateValue method was added, which is basically just a wrapper for insertValue.

Code: Select all
diff --git a/lib/NConf/DB/Modify.pm b/lib/NConf/DB/Modify.pm
index 201e4df..fa74533 100644
--- a/lib/NConf/DB/Modify.pm
+++ b/lib/NConf/DB/Modify.pm
@@ -29,7 +29,7 @@ BEGIN {
     use vars qw(@ISA @EXPORT @EXPORT_OK);
 
     @ISA         = qw(NConf::DB);
-    @EXPORT      = qw(@NConf::DB::EXPORT linkItems addItem insertValue addHistory queryExecModify);
+    @EXPORT      = qw(@NConf::DB::EXPORT linkItems addItem insertValue updateValue addHistory queryExecModify);
     @EXPORT_OK   = qw(@NConf::DB::EXPORT_OK);
 }
 
@@ -441,6 +441,37 @@ sub addItem {
 
 ##############################################################################
 
+sub updateValue {
+    &logger(5,"Entered updateValue()");
+
+    # SUB use: Update the value of a single attribute (any datatype)
+
+    # SUB specs: ###################
+
+    # Expected arguments:
+    # 0: class name
+    # 1: ID of item
+    # 2: attribute name
+    # 3: attribute value
+
+    # Return values:
+    # 0: 'true' on operation success,
+    #     undef on failure
+
+    ################################
+   
+    # read arguments passed
+    my $class_name = shift;
+    my $id_item    = shift;
+    my $attr       = shift;
+    my $attr_value = shift; # value can be "0"
+
+    &logger(4,"Calling insertValue with update option");
+    return insertValue( $class_name, $id_item, $attr, $attr_value, 1 );
+}
+
+##############################################################################
+
 sub insertValue {
     &logger(5,"Entered insertValue()");
 
@@ -453,6 +484,7 @@ sub insertValue {
     # 1: ID of item
     # 2: attribute name
     # 3: attribute value
+    # 4: update attribute: 0: insert (default), 1: update
 
     # Return values:
     # 0: 'true' on operation success,
@@ -465,11 +497,14 @@ sub insertValue {
     my $id_item    = shift;
     my $attr       = shift;
     my $attr_value = shift; # value can be "0"
+    my $update     = shift;
     $class_name = lc($class_name);
 
     unless($class_name && $attr){&logger(1,"insertValue(): Missing argument(s). Aborting.")}
     if($attr_value eq ""){&logger(1,"insertValue(): Missing argument(s). Aborting.")}
 
+    unless(defined $update){$update=0}
+
     # get a list of all class attrs plus their properties (datatype, maxlength, mandatory etc.)
     # list datastructure: $class_attrs_hash{'class name'}->{'attr name'}->{'property'}
     my %class_attrs_hash = &getConfigAttrs();
@@ -528,7 +563,7 @@ sub insertValue {
                           ."linking $class_name '$group' to $class_attrs_hash{$class_name}->{$attr}->{'assign_to_class'} '$item_name'.");
 
             # recursive call to insertValue()
-            &insertValue($class_name,$id_group,$attr,$item_name);
+            &insertValue($class_name,$id_group,$attr,$item_name, $update);
         }
         return "true";
     }
@@ -554,8 +589,15 @@ sub insertValue {
         my $id_attr = &getAttrId($attr, $class_name);
         $attr_value = &NConf::DB::dbQuote($attr_value);
 
-        my $sql  = "INSERT INTO ConfigValues (fk_id_attr, fk_id_item, attr_value)
-                    VALUES ($id_attr,$id_item,$attr_value)";
+   my $sql;
+        unless($update) {
+            $sql  = "INSERT INTO ConfigValues (fk_id_attr, fk_id_item, attr_value)
+                        VALUES ($id_attr,$id_item,$attr_value)";
+   } else {
+       $sql  = "UPDATE ConfigValues
+                   SET attr_value=$attr_value
+         WHERE fk_id_attr=$id_attr AND fk_id_item=$id_item";
+   }
 
         my $qres = &queryExecModify($sql,"Adding attr '$attr' to $class_name");
 
@@ -604,8 +646,15 @@ sub insertValue {
         my $id_attr = &getAttrId($attr, $class_name);
         $attr_value = &NConf::DB::dbQuote($attr_value);
 
-        my $sql  = "INSERT INTO ConfigValues (fk_id_attr, fk_id_item, attr_value)
-                    VALUES ($id_attr,$id_item,$attr_value)";
+   my $sql;
+        unless($update) {
+            $sql  = "INSERT INTO ConfigValues (fk_id_attr, fk_id_item, attr_value)
+                        VALUES ($id_attr,$id_item,$attr_value)";
+   } else {
+       $sql  = "UPDATE ConfigValues
+                   SET attr_value=$attr_value
+         WHERE fk_id_attr=$id_attr AND fk_id_item=$id_item";
+   }
 
         my $qres = &queryExecModify($sql,"Adding attr '$attr' to $class_name");


Regards,
Benedikt
benediktkoeppel
 

Re: NConf::DB::Modify::updateValue

Postby agargiulo » Sat Dec 10, 2011 17:24

Hi. Very interesting. This might be a very useful feature for release 1.3.1. I have added it to our bugtracker.

Thanks,
Angelo
User avatar
agargiulo
NConf developer
NConf developer
 
Posts: 725
Joined: Fri Mar 06, 2009 17:50
Location: Zurich, Switzerland

Re: NConf::DB::Modify::updateValue

Postby xkilian » Mon Feb 20, 2012 21:31

Is it still planned to permit updating existing entries in 1.3.1.

For automating updating data associated with hosts and services from an external CMDB or discovery script the update functions is really needed.

I converted the almost famous Cricket configuration generator genDevConfig to ouput in Nagios format, but I might also support sending it to nconf via the API in addition to raw Nagios/Icinga/Shinken format. genDevConfig does SNMP discovery of a host(typicaly a network switch, load balancer, router) and creates the host and associated services configuration. Great for keeping network devices updated with the correct interface descriptions, speeds, states, modules, serial numbers, etc.

Cheers,

X
xkilian
starter
starter
 
Posts: 2
Joined: Mon Feb 20, 2012 20:45

Re: NConf::DB::Modify::updateValue

Postby agargiulo » Fri Feb 24, 2012 00:33

Hi. I don't know if this feature will be in 1.3.1 but we will definitely look into it soon.
User avatar
agargiulo
NConf developer
NConf developer
 
Posts: 725
Joined: Fri Mar 06, 2009 17:50
Location: Zurich, Switzerland


Return to Contributions

Who is online

Users browsing this forum: No registered users and 1 guest

cron