Class: Zone

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

Instance Method Summary collapse

Methods included from ConfigReader

#config_dir, #config_file, #mkdir, #param

Methods included from CommandProc

#getopt, #help, included

Constructor Details

#initialize(name) ⇒ Zone

Returns a new instance of Zone.



9
10
11
12
13
14
# File 'lib/zone.rb', line 9

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

Instance Method Details

#add(args) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/zone.rb', line 170

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

  end
end

#get_zone(forceRest = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zone.rb', line 106

def get_zone(forceRest = false)
  zonedir = mkdir("zones")
  zone = nil
  id, name = id_and_name
  if @zname
    if ! forceRest
      if id.nil?
        r = name + '\.\d+'
      else
        r = '.*\.' + id
      end
      regex = Regexp.new(r)
      Dir.new(zonedir).each { |file|
        if file.full_match regex
          path = File.join(zonedir, file)
          zone = JSON.parse(File.open(path).read)
        end
      }
    end
    if (zone.nil?)
      zone = Route.do.get_zone(id, name)
      Route.do.translate_zone(zone)
    end
  end
  zone
end

#id_and_name ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/zone.rb', line 95

def id_and_name
  id = name = nil
  if  @zname.full_match /\d+/
    id = @zname
  else
    name = @zname
  end
  name = name.sub(/\.\Z/, "") if ! name.nil?
  return id, name
end

#list(args) ⇒ Object



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

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

#match_key(abbrev, struct) ⇒ Object

let user lowercase and abbreviate key; abbreviation must match one key



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zone.rb', line 30

def match_key(abbrev, struct)
  abbrev = abbrev.downcase
  key = nil
  struct.keys.each { |fullkey|
    testkey = fullkey.downcase
    return fullkey if abbrev == testkey
    if fullkey.downcase.start_with?(abbrev)
      if (! key)
        key = fullkey
      else
        return nil
      end
    end
  }
  key
end


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/zone.rb', line 151

def print(args)
  if @zname
    zone = get_zone
    if (zone.nil?)
      puts "zone empty"
      return
    end
    if (args.length == 1 && args[0] == "-l")
      puts JSON.pretty_generate(zone)
    else
      @table = nil
      structure_print(zone, args, true)
      @table.print_table if @table
    end
  else
    puts "need zone name or id"
  end
end

#pull(args) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/zone.rb', line 140

def pull(args)
  raise("Illegal arguments:" + args.to_s)  if args.length != 0
  raise("Must specify zone") if @zname.nil?
  zone = get_zone(true)
  file_name = zone["DomainName"].sub(/\.\Z/, '')
  file_name = file_name + '.' + zone["ZoneId"].to_s
  file_name = config_file("zones", file_name)
  File.delete(file_name) if File.exists?(file_name)
  File.open(file_name, "w").write(JSON.pretty_generate zone)
end

#push(args) ⇒ Object



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

def push(args)
  zone = get_zone
  Route.do.untranslate_zone(zone)
  File.open("untranslated.zone","w").write(JSON.pretty_generate zone)
  Route.do.update_zone(zone)
end

#structure_print(struct, args, header) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zone.rb', line 47

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)
        @table = TablePrint.new(*struct.keys)
      end
      values = []
      struct.keys.each { |key|
        value = struct[key]
        if (value.nil?)
          value = "null"
        elsif (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
      }
      @table.append(*values)
    else
      args = args.clone
      key = args.shift
      fullkey = match_key(key, struct)
      raise "no (unique) key matches " + key if ! fullkey
      structure_print(struct[fullkey], args, header)
    end
  else
    if (args.length == 0)
      puts struct.to_s
    else
      puts "unknown class <" + struct.class.to_s + ">"
    end
  end
end