Class: Zone

Inherits:
Object
  • Object
show all
Includes:
CommandProc
Defined in:
lib/zone.rb

Instance Method Summary collapse

Methods included from CommandProc

#help, included

Constructor Details

#initialize(name) ⇒ Zone

Returns a new instance of Zone.



6
7
8
9
10
11
12
# File 'lib/zone.rb', line 6

def initialize(name)
  @lines = nil
  @zname = name
  @usage = { :list => "list zones"}
  @argflows = [ {:name => "list", :usage => "list zones", :args=>[]},
               {:name => "add", :usage => "", :args=>[]}]      
end

Instance Method Details

#add(args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zone.rb', line 88

def add(args)
  if @zname 
    puts "unimplemented"
  else
   @zone =  {
      :Comment => "",
      :DomainName => @zname,
      :FailoverGroups => [ ],
      :LoadBalancingGroups => [ ],
      :Records => null,
      :Status => 1,
      :ZoneType => 1
    }

  end
end

#char_repeat(n, char) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/zone.rb', line 105

def char_repeat(n, char)
  result = ""
  (1..n).each { |i|
    result = result + char
  }
  result
end

#list(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zone.rb', line 14

def list(args)
  if @zname
    puts "unimplemented"
  else
    zones = Route.do.get_all_zones
    pp_table_start("ZONE", "ID", "STATUS")
    zones.each {|zone|
      pp_table_append(zone["DomainName"],  zone["ZoneId"].to_s, Route.do.xlate_id(:zone_status, zone["Status"]))
    }
    pp_table
  end
end

#pp_table ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/zone.rb', line 139

def pp_table
  if @lines.nil?
    return
  end
  ncols = @lines[0].length
  maxwidths = Array.new(ncols, 0)
  @lines.each { |line|
    line.each_with_index { |col, index|
      maxwidths[index] = [col.to_s.length, maxwidths[index]].max
    }
  }
  @lines.each { |line|
    output = ""
    line.each_with_index {|col, index|
      output = output + col.to_s + spaces(maxwidths[index] - col.to_s.length + 2)
    }
    puts output
  }
  @lines = nil
end

#pp_table_append(*columns) ⇒ Object



135
136
137
# File 'lib/zone.rb', line 135

def pp_table_append(*columns)
  @lines << columns
end

#pp_table_append_cols(columns) ⇒ Object



131
132
133
# File 'lib/zone.rb', line 131

def pp_table_append_cols(columns)
  @lines << columns
end

#pp_table_start(*titles) ⇒ Object



127
128
129
# File 'lib/zone.rb', line 127

def pp_table_start(*titles)
  pp_table_start_cols(titles)
end

#pp_table_start_cols(titles) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/zone.rb', line 117

def pp_table_start_cols(titles)
  @lines = []
  @lines << titles
  separator = []
  titles.each { |title|
    separator << char_repeat(title.length, '-')
  }
  @lines << separator
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/zone.rb', line 70

def print(args)
  if @zname
    if  @zname =~ /\d+/
      zone = Route.do.get_zone(@zname, "")
    else
      zone = Route.do.get_zone("", @zname)
    end
    if (args.length > 0 && args[0] == "-l")
      puts JSON.pretty_generate(zone)
    else
      structure_print(zone, args, true)
      pp_table
    end
  else
    puts "pull command needs zone name or id"
  end
end

#spaces(n) ⇒ Object



113
114
115
# File 'lib/zone.rb', line 113

def spaces(n)
  char_repeat(n, ' ')
end

#structure_print(struct, args, header) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zone.rb', line 27

def structure_print(struct, args, header)
  if (struct.class == Array)
    # puts "printing array of " + struct.length.to_s + " elts"
    struct.each { |elt|
      # puts "calling struct_print with header = " + header.to_s + ", and elt = " + elt.to_s
      structure_print(elt, args, header)
      header = false
    }
  elsif (struct.class == Hash)
    if (args.length == 0)
      # print top level elements of hash
      if (header)
        pp_table_start_cols struct.keys
      end
      values = []
      struct.keys.each { |key|
        value = struct[key]
        if (value.class == Array)
          value = value.length.to_s + " elements"
        elsif (value.class == String)
        elsif (value.class == Fixnum)
          value = value.to_s
        elsif (value.class == Hash)
          value = "object"
        else
          value = "<" + value.class.to_s + ">"
        end
        values << value
      }
      pp_table_append_cols(values)
    else
      key = args.shift
      structure_print(struct[key], args, header)
    end
  else
    if (args.length == 0)
      puts struct.to_s
    else
      puts "unknown class <" + struct.class.to_s + ">"
    end
  end
end