Sha256: 2bfee773385d063f34c964f55c171beec981912b956f5bfaece51426df75324f

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

module Rubix
  
  class Template < Model

    #
    # == Properties & Finding ==
    #

    attr_accessor :name
    
    def initialize properties={}
      super(properties)
      @name     = properties[:name]
      
      self.host_ids = properties[:host_ids]
      self.hosts    = properties[:hosts]

      self.host_group_ids = properties[:host_group_ids]
      self.host_groups    = properties[:host_groups]
    end

    def self.find_request options={}
      params = {'select_groups' => 'refer', 'select_hosts' => 'refer', 'output' => 'extend'}
      case
      when options[:id]
        params['templateids'] = [options[:id]]
      when options[:name]
        params['filter'] = { 'host' => options[:name] }
      end
      request('template.get', params)
    end

    def self.build template
      new({
            :id       => (template['templateid'] || template['hostid']).to_i,
            :name     => template['host'],
            :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
            :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
          })
    end

    def self.id_field
      'templateid'
    end

    #
    # == Validation ==
    #

    def validate
      raise ValidationError.new("A template must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
      true
    end
    
    #
    # == Associations ==
    #

    include Associations::HasManyHosts
    include Associations::HasManyHostGroups

    #
    # == CRUD ==
    #
    
    def create_request
      request('template.create', {'host' => name, 'groups' => host_group_params})
    end
    
    def update_request
      request('template.update', [{'host' => name, 'templateid' => id, 'groups' => host_group_params}])
    end

    def destroy_request
      request('template.delete', [{'templateid' => id}])
    end
    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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