2009-03-21 04:23:35 +03:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2009-03-21 04:23:35 +03:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef HOOK_H_
# define HOOK_H_
# include <string>
2019-09-09 15:43:51 +03:00
# include "Template.h"
# include "PoolObjectSQL.h"
# include "NebulaUtil.h"
2009-03-21 04:23:35 +03:00
2019-09-09 15:43:51 +03:00
class HookImplementation ;
2009-03-21 04:23:35 +03:00
2019-09-09 15:43:51 +03:00
class Hook : public PoolObjectSQL
2009-03-21 04:23:35 +03:00
{
public :
2009-03-30 01:34:37 +04:00
2020-09-10 10:08:29 +03:00
~ Hook ( ) ;
2009-03-30 01:34:37 +04:00
/**
2009-04-09 04:58:43 +04:00
* Defines the hook type , so a whole hook class can be masked
2009-03-30 01:34:37 +04:00
*/
enum HookType
{
2019-09-09 15:43:51 +03:00
STATE = 0x01 ,
API = 0x02 ,
UNDEFINED = 0x04
2009-03-30 01:34:37 +04:00
} ;
2020-07-02 23:42:10 +03:00
static std : : string hook_type_to_str ( HookType ht )
2019-09-09 15:43:51 +03:00
{
switch ( ht )
{
case STATE : return " state " ;
case API : return " api " ;
case UNDEFINED : return " " ;
}
return " " ;
} ;
static HookType str_to_hook_type ( std : : string & ht )
{
one_util : : tolower ( ht ) ;
if ( ht = = " state " )
{
return STATE ;
}
else if ( ht = = " api " )
{
return API ;
}
return UNDEFINED ;
}
2009-03-30 01:34:37 +04:00
/**
2019-09-09 15:43:51 +03:00
* Function to print the Hook object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
2009-03-30 01:34:37 +04:00
*/
2019-09-09 15:43:51 +03:00
std : : string & to_xml ( std : : string & xml ) const
{
return _to_xml ( xml , false ) ;
}
2009-03-21 04:23:35 +03:00
/**
2019-09-09 15:43:51 +03:00
* Include exection records for the hook
2009-03-21 04:23:35 +03:00
*/
2019-09-09 15:43:51 +03:00
std : : string & to_xml_extended ( std : : string & xml ) const
{
return _to_xml ( xml , true ) ;
}
private :
friend class HookPool ;
// *************************************************************************
// Constructor/Destructor
// *************************************************************************
2020-06-29 13:14:00 +03:00
Hook ( Template * tmpl ) ;
2019-09-09 15:43:51 +03:00
2012-10-08 01:20:28 +04:00
/**
2019-09-09 15:43:51 +03:00
* Set hook implementation attribute depending of the hook type .
*/
int set_hook ( HookType hook_type , std : : string & error ) ;
2009-04-04 03:34:33 +04:00
/**
2019-09-09 15:43:51 +03:00
* Factory method for Hook templates
2009-04-04 03:34:33 +04:00
*/
2019-09-09 15:43:51 +03:00
Template * get_new_template ( ) const
{
return new Template ;
}
2009-04-09 04:58:43 +04:00
2009-03-21 04:23:35 +03:00
/**
2019-09-09 15:43:51 +03:00
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
2009-03-21 04:23:35 +03:00
*/
2019-09-09 15:43:51 +03:00
int from_xml ( const std : : string & xml_str ) ;
/* Checks the mandatory templates attrbutes
* @ param error string describing the error if any
* @ return 0 on success
*/
int post_update_template ( std : : string & error ) ;
// -------------------------------------------------------------------------
// Hook Attributes
// -------------------------------------------------------------------------
2009-03-21 04:23:35 +03:00
/**
2019-09-09 15:43:51 +03:00
* The hook type
2009-03-21 04:23:35 +03:00
*/
2019-09-09 15:43:51 +03:00
HookType type ;
2009-03-21 04:23:35 +03:00
2009-03-30 01:34:37 +04:00
/**
2019-09-09 15:43:51 +03:00
* The command to be executed
2009-03-30 01:34:37 +04:00
*/
2020-07-02 23:42:10 +03:00
std : : string cmd ;
2009-04-09 04:58:43 +04:00
2009-03-21 04:23:35 +03:00
/**
* True if the command is to be executed remotely
*/
2009-03-30 01:34:37 +04:00
bool remote ;
2009-03-21 04:23:35 +03:00
2019-09-09 15:43:51 +03:00
/**
* Object which implement type dependent methods .
*/
HookImplementation * _hook ;
2012-10-08 01:20:28 +04:00
2019-09-09 15:43:51 +03:00
// *************************************************************************
// Database implementation
// *************************************************************************
2012-10-08 01:20:28 +04:00
2009-03-21 04:23:35 +03:00
/**
2019-09-09 15:43:51 +03:00
* Construct the XML representation of the hook
* @ param xml the resulting XML string
* @ param log to include the execution log
*
* @ return a reference to the generated string
2009-03-21 04:23:35 +03:00
*/
2019-09-09 15:43:51 +03:00
std : : string & _to_xml ( std : : string & xml , bool log ) const ;
2009-03-21 04:23:35 +03:00
/**
2019-09-09 15:43:51 +03:00
* Bootstraps the database table ( s ) associated to the Host
* @ return 0 on success
2009-03-21 04:23:35 +03:00
*/
2020-06-29 13:14:00 +03:00
static int bootstrap ( SqlDB * db ) ;
2009-03-21 04:23:35 +03:00
/**
2019-09-09 15:43:51 +03:00
* Writes / updates the Hosts data fields in the database .
* @ param db pointer to the db
* @ return 0 on success
2009-03-21 04:23:35 +03:00
*/
2019-09-20 10:38:16 +03:00
int update ( SqlDB * db ) override
2009-03-21 04:23:35 +03:00
{
2020-07-02 23:42:10 +03:00
std : : string error_str ;
2019-09-09 15:43:51 +03:00
return insert_replace ( db , true , error_str ) ;
2009-03-21 04:23:35 +03:00
} ;
/**
2019-09-09 15:43:51 +03:00
* Writes the Hook in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
int insert ( SqlDB * db , std : : string & error_str ) override ;
2019-09-09 15:43:51 +03:00
/**
* Execute an INSERT or REPLACE Sql query .
* @ param db The SQL DB
* @ param replace Execute an INSERT or a REPLACE
* @ param error_str Returns the error reason , if any
* @ return 0 one success
2009-03-21 04:23:35 +03:00
*/
2020-07-02 23:42:10 +03:00
int insert_replace ( SqlDB * db , bool replace , std : : string & error_str ) ;
2019-09-20 10:38:16 +03:00
/**
* Drops object from the database
* @ param db pointer to the db
* @ return 0 on success
*/
int drop ( SqlDB * db ) override ;
2009-03-21 04:23:35 +03:00
} ;
2019-09-09 15:43:51 +03:00
# endif /*HOOK_H_*/