Sha256: 8b395b6ca5c93b9bb8791dcfc54ce934a1ad6a82ddd8da3c40c9999b6fd44236

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

module Rubix
  
  class UserMacro < Model

    #
    # == Properties & Finding ==
    #
    
    attr_accessor :name, :value

    def initialize properties={}
      super(properties)
      @name  = properties[:name] || self.class.unmacro_name(properties[:macro])
      @value = properties[:value]

      self.host    = properties[:host]
      self.host_id = properties[:host_id]
    end

    def self.find_request options={}
      request('usermacro.get', 'hostids' => [options[:host_id]], 'filter' => {'macro' => macro_name(options[:name])}, "output" => "extend")
    end

    def self.build macro
      new({
            :id      => macro['hostmacroid'].to_i,
            :name    => unmacro_name(macro['macro']),
            :value   => macro['value'],
            :host_id => macro['hostid']
          })
    end
    
    def self.unmacro_name name
      (name || '').gsub(/^\{\$/, '').gsub(/\}$/, '').upcase
    end

    def self.macro_name name
      "{$#{name.upcase}}"
    end

    def macro_name
      self.class.macro_name(name)
    end
    
    def self.id_field
      'hostmacroid'
    end

    #
    # == Associations ==
    #
    
    include Associations::BelongsToHost

    #
    # == Validation ==
    #
    def validate
      raise ValidationError.new("A user macro must have both a 'name' and a 'value'") if name.nil? || name.strip.empty? || value.nil? || value.strip.empty?
      true
    end
    
    #
    # == CRUD ==
    #
    
    def create_request
      request('usermacro.massAdd', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
    end
    
    def update_request
      request('usermacro.massUpdate', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
    end

    def destroy_request
      request('usermacro.massRemove', 'hostids' => [host_id], 'macros' => [macro_name])
    end
    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubix-0.0.7 lib/rubix/models/user_macro.rb
rubix-0.0.6 lib/rubix/models/user_macro.rb
rubix-0.0.5 lib/rubix/models/user_macro.rb
rubix-0.0.4 lib/rubix/models/user_macro.rb
rubix-0.0.3 lib/rubix/models/user_macro.rb