From a08d1c2d0690ef783bc3da9f2b7def2213862c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20S=2E=20Montero?= Date: Sun, 20 Jul 2008 22:41:20 +0000 Subject: [PATCH] Adds a new attribute (from the POLL string) to the VM template. It replaces the attribute if it already exists. git-svn-id: http://svn.opennebula.org/trunk@65 3034c82b-c49b-4eb3-8279-a7acafdc01c0 --- include/TemplateSQL.h | 10 ++++- include/VirtualMachine.h | 16 +++++++ include/VirtualMachinePool.h | 16 +++++++ src/template/TemplateSQL.cc | 61 ++++++++++++++++++++++++++ src/vmm/VirtualMachineManagerDriver.cc | 11 ++++- 5 files changed, 111 insertions(+), 3 deletions(-) diff --git a/include/TemplateSQL.h b/include/TemplateSQL.h index cee9757fc5..9c784a3b1a 100644 --- a/include/TemplateSQL.h +++ b/include/TemplateSQL.h @@ -84,7 +84,15 @@ protected: * Removes the template from the DB * @param db pointer to the database. */ - int drop(SqliteDB *db); + int drop(SqliteDB *db); + + /** + * Removes a template attribute from the DB (ONLY SINGLE ATTRIBUTES) + * @param db pointer to the database. + * @param name of the attribute. + * @param value of the new attribute. + */ + int replace_attribute(SqliteDB * db, string& name, string& value); }; /* -------------------------------------------------------------------------- */ diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 251bd5dd2f..1a8b74acb9 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -750,6 +750,22 @@ private: return -1; }; + /** + * Updates the template of a VM, adding a new attribute (replacing it if + * already defined), the vm's mutex SHOULD be locked + * @param vm pointer to the virtual machine object + * @param name of the new attribute + * @param value of the new attribute + * @return 0 on success + */ + int update_template_attribute( + SqliteDB * db, + string& name, + string& value) + { + return vm_template.replace_attribute(db,name,value); + } + protected: //************************************************************************** diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index 49d7f1af72..4b631450ec 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -86,6 +86,22 @@ public: // Virtual Machine DB access functions //-------------------------------------------------------------------------- + /** + * Updates the template of a VM, adding a new attribute (replacing it if + * already defined), the vm's mutex SHOULD be locked + * @param vm pointer to the virtual machine object + * @param name of the new attribute + * @param value of the new attribute + * @return 0 on success + */ + int update_template_attribute( + VirtualMachine * vm, + string& name, + string& value) + { + return vm->update_template_attribute(db,name,value); + } + /** * Updates the history record of a VM, the vm's mutex SHOULD be locked * @param vm pointer to the virtual machine object diff --git a/src/template/TemplateSQL.cc b/src/template/TemplateSQL.cc index 340be054aa..eec67cae7a 100644 --- a/src/template/TemplateSQL.cc +++ b/src/template/TemplateSQL.cc @@ -241,3 +241,64 @@ int TemplateSQL::drop(SqliteDB * db) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int TemplateSQL::replace_attribute( + SqliteDB * db, + string& name, + string& value) +{ + ostringstream oss; + int rc; + + multimap::const_iterator i; + Attribute * attribute; + + if ( id == -1 || name.empty() || name.empty() ) + { + return -1; + } + + i = attributes.find(name); + + if ( i != attributes.end() ) //attribute exists + { + string * attr = i->second->marshall(); + + if ( attr != 0 ) + { + oss << "DELETE FROM " << table << " WHERE id=" << id + << " AND name='" << name << "' AND value='" << *attr << "'"; + + delete attr; + } + else + { + oss << "DELETE FROM " << table << " WHERE id=" << id + << " AND name='" << name << "'"; + } + + rc = db->exec(oss); + + if ( rc != 0 ) + { + return rc; + } + + attributes.erase(name); + } + + attribute = new SingleAttribute(name,value); + + attributes.insert(make_pair(attribute->name(),attribute)); + + oss.str(""); + + oss << "INSERT INTO " << table << " " << db_names + << " VALUES (" << id << ",'" << name << "'," << + Attribute::SIMPLE <<",'" << value << "')"; + + return db->exec(oss); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/vmm/VirtualMachineManagerDriver.cc b/src/vmm/VirtualMachineManagerDriver.cc index f3e11d767a..9e129b15a8 100644 --- a/src/vmm/VirtualMachineManagerDriver.cc +++ b/src/vmm/VirtualMachineManagerDriver.cc @@ -467,10 +467,17 @@ void VirtualMachineManagerDriver::protocol( } else { + string val; + os.str(""); - os << "Unknown monitoring attribute (ignored): " << tmp; + os << "Unknown monitoring attribute (adding/updating" + << " template): " << tmp; - vm->log("VMM",Log::WARNING,os); + vm->log("VMM",Log::WARNING,os); + + tiss >> val; + + vmpool->update_template_attribute(vm,var,val); } }