lib/foreman_hooks/host_rename.rb
def execute_hook_action
      @rename = false
      name = @rec['host']['name']
      id = @rec['host']['id']
    
      case @action
      when 'create'
        sql = "insert into host (id, name) values (?, ?)"
        params = [id, name]
      when 'update'
        # Check if we are renaming the host
        @old_name = @db.get_first_row('select name from host where id = ?', id)
        @old_name = @old_name[0] unless @old_name.nil?
        if @old_name.nil?
          warn 'received an update for a non-existent host'
        else
          @rename = @old_name != name
        end
        debug "checking for a rename: old=#{@old_name} new=#{name} rename?=#{@rename}"
    
        sql = 'update host set name = ? where id = ?'
        params = [name, id]
      when 'destroy'
        sql = 'delete from host where id = ?'
        params = [id]
      else
        raise ArgumentError, "unsupported action: #{ARGV[0]}"
      end
      debug "updating database; id=#{id} name=#{name} sql=#{sql}"
      stm = @db.prepare sql
      stm.bind_params *params
      stm.execute
    end
def execute_hook_action
      @rename = false
      name = @rec['host']['name']
      id = @rec['host']['id']
    
      case @action
      when 'create'
        sql = "insert into host (id, name) values (?, ?)"
        params = [id, name]
      when 'update'
        # Check if we are renaming the host
        @old_name = @db.get_first_row('select name from host where id = ?', id)
        @old_name = @old_name[0] unless @old_name.nil?
        if @old_name.nil?
          warn 'received an update for a non-existent host'
        else
          @rename = @old_name != name
        end
        debug "checking for a rename: old=#{@old_name} new=#{name} rename?=#{@rename}"
    
        sql = 'update host set name = ? where id = ?'
        params = [name, id]
      when 'destroy'
        sql = 'delete from host where id = ?'
        params = [id]
      else
        raise ArgumentError, "unsupported action: #{ARGV[0]}"
      end
      debug "updating database; id=#{id} name=#{name} sql=#{sql}"
      stm = @db.prepare sql
      stm.bind_params *params
      stm.execute
    end
Update the database based on the foreman_hook