Source

iscsi-tools / lib / iscsi / context.rb

Full commit
require 'runner'

module ISCSI
  class Context
    class Category
      def initialize
        @map = {}
      end
      def access id, &producer
        @map[id] || @map[id]=producer.call
      end
    end
    
    attr_reader :runner
    
    def initialize verbose=false
      @categories = Hash.new { |h,k| h[k] = Category.new }
      @runner = ::Runner.new(verbose)
    end
    def cat category
      @categories[category]
    end
    
    def target name
      targets.
        find { |t| t.name == name }
    end
    def targets
      ArrayEnum.new(stmf_targets) { |node| 
        cat(:target).access(node.value) { Target.new(self, node) } }
    end
    def stmf_targets
      parser = HashParser.new
      parser.parse runner.shell('stmfadm list-target -v')
    end

    def target_groups
      ArrayEnum.new(stmf_target_groups) { |node| TargetGroup.new(self, node) }
    end
    def stmf_target_groups
      parser = HashParser.new
      parser.parse runner.shell('stmfadm list-tg -v')
    end
  end
end