Commits

Kaspar Schiess  committed 586f0d4

LU listing

With the aid of metaprogramming, that wasn't so bad.

  • Participants
  • Parent commits c692479

Comments (0)

Files changed (5)

File lib/iscsi.rb

 end
 
 require 'iscsi/array_enum'
-require 'iscsi/context'
 require 'iscsi/entity'
 require 'iscsi/target_group'
 require 'iscsi/target'
-require 'iscsi/hash_parser'
+require 'iscsi/lu'
+require 'iscsi/hash_parser'
+require 'iscsi/context'

File lib/iscsi/context.rb

       @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
-      @stmf_targets ||= begin
+    def self.define_category singular, plural, klass, command
+      cached_command = "stmf_#{plural}"
+
+      define_method singular do |name|
+        self.send(plural).
+          find { |e| e.name == name }
+      end
+      define_method plural do
+        ArrayEnum.new(self.send(cached_command)) { |node| 
+          cat(singular.to_sym).access(node.value) { 
+            klass.new(self, node) } }
+      end
+      define_method cached_command do
         parser = HashParser.new
-        parser.parse runner.shell('stmfadm list-target -v')
+        parser.parse runner.shell(command)
       end
     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
+    
+    define_category :target, :targets, Target, 'stmfadm list-target -v'
+    define_category :target_group, :target_groups, TargetGroup, 'stmfadm list-tg -v'
+    define_category :lu, :lus, LU, 'stmfadm list-lu -v'
   end
 end

File lib/iscsi/lu.rb

+module ISCSI
+  class LU < Entity
+  end
+end

File spec/fixture/lus.txt

+LU Name: 600144F085698100000050CB43400009
+    Operational Status: Online
+    Provider Name     : sbd
+    Alias             : /dev/zvol/rdsk/rpool/vol1
+    View Entry Count  : 1
+    Data File         : /dev/zvol/rdsk/rpool/vol1
+    Meta File         : not set
+    Size              : 536870912000
+    Block Size        : 512
+    Management URL    : not set
+    Vendor ID         : SUN     
+    Product ID        : COMSTAR         
+    Serial Num        : not set
+    Write Protect     : Disabled
+    Writeback Cache   : Enabled
+    Access State      : Active
+LU Name: 600144F085698100000050CB47E2000A
+    Operational Status: Online
+    Provider Name     : sbd
+    Alias             : /dev/zvol/rdsk/rpool/vol2
+    View Entry Count  : 1
+    Data File         : /dev/zvol/rdsk/rpool/vol2
+    Meta File         : not set
+    Size              : 536870912000
+    Block Size        : 512
+    Management URL    : not set
+    Vendor ID         : SUN     
+    Product ID        : COMSTAR         
+    Serial Num        : not set
+    Write Protect     : Disabled
+    Writeback Cache   : Enabled
+    Access State      : Active

File spec/lib/iscsi/context_spec.rb

   
   let(:fake_runner) { FakeRunner.new(
     /^stmfadm list-target/ => fixture('target_list.txt'), 
-    /^stmfadm list-tg/     => fixture('target_groups.txt')) }
+    /^stmfadm list-tg/     => fixture('target_groups.txt'), 
+    /^stmfadm list-lu/     => fixture('lus.txt')) }
   before(:each) { 
     flexmock(ctx, runner: fake_runner) }
   
       end 
     end
   end
+  describe '#lus' do
+    slet(:lus) { ctx.lus }
+
+    describe 'lus[0]' do
+      slet(:lu) { lus[0] }
+      
+      it "has a name" do
+        lu.name.should == '600144F085698100000050CB43400009'
+      end 
+    end
+  end
 end