Class: R53z::Cli

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::Main
Defined in:
lib/r53z/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(options:, args:) ⇒ Cli

Returns a new instance of Cli.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
94
95
96
# File 'lib/r53z/cli.rb', line 8

def initialize(options:, args:)
  section = options[:section] || 'default'
  config_file = options[:credentials]
  creds = R53z::Config.new(config_file)
  @client = R53z::Client.new(section, creds)

  # XXX Dispatch table seems smarter...can't figure out how to call methods based
  # directly on hash keys at the moment.
  if options[:export]
    unless options[:export].is_a? String
      exit_now! "Export must have a valid directory path for dump files."
    end
    unless Dir.exists?(File.expand_path(options[:export]))
      exit_now! "Directory " + options[:export] + " does not exist."
    end
    export(:options => options, :args => args)
  end

  if options[:restore]
    unless Dir.exists?(File.expand_path(options[:restore]))
      exit_now! "Restore requires a directory containing zone files and optionally one or more zones to restore."
    end
    restore(:options => options, :args => args)
  end

  if options[:list]
    list(options: options, args: args)
  end

  if options[:create]
    create(options)
  end

  if options[:delete]
    if args.empty?
      exit_now! "Delete requires one or more zone names."
    end
    args.each do |name|
      if @client.list(name: name).any?
        @client.delete(name)
      else
        exit_now! "Could not locate zone named " + name
      end
    end
  end

  if options['list-delegation-sets']
    delegation_sets(args)
  end

  if options['create-delegation-sets']
    create_delegation(args)
  end

  if options['delete-delegation-sets']
    if args.empty?
      exit_now! "Deleting delegation sets requires one or more delegation set IDs."
    end
    args.each do |id|
      @client.delete_delegation_set(id: id)
    end
  end

  if options['record-sets']
    if args.empty?
      exit_now! "List record sets requires one or more zone names."
    end
    record_sets(args)
  end

  if options['name-servers']
    dset = @client.get_delegation_set(options['name-servers'])
    puts JSON.pretty_generate(dset.delegation_set[:name_servers])
  end

  if options['list-by-id']
    if args.empty?
      exit_now! "List by ID requires one or more zone IDs."
    end
    list_by_id(args)
  end

  if options['list-records-by-id']
    if args.empty?
      exit_now! "List records by ID requies on or more zone IDs."
    end
    list_records_by_id(args)
  end
end

Instance Method Details

#add_record(options, args) ⇒ Object



169
170
171
# File 'lib/r53z/cli.rb', line 169

def add_record(options, args)
  # Populate record set hash
end

#create(options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/r53z/cli.rb', line 155

def create(options)
  # Populate a zone hash
  zone_data = {:hosted_zone => { :hosted_zone_config => {}}, :delegation_set =>{}}
  zone_data[:hosted_zone][:name] = options[:create]
  if options[:comment]
    zone_data[:hosted_zone][:config] = {}
    zone_data[:hosted_zone][:config][:comment] = options[:comment]
  end
  if options['delegation-set']
    zone_data[:delegation_set][:id] = options['delegation-set']
  end
  @client.create(info: zone_data)
end

#create_delegation(args) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/r53z/cli.rb', line 193

def create_delegation(args)
  # further validation
  zones = []
  # No zones specified, create a new unassociated delegation set
  if args.empty?
    @client.create_delegation_set
  end

  args.each do |name|
    # Make sure at least one specified zone exists
    if @client.list(name: name)
      zones.push(name)
    else
      puts "Couldn't locate zone " + name unless @client.list(name)
    end
    if zones.empty?
      exit_now! "Couldn't find any of the specified zones."
    end
    zones.each do |zone|
      @client.create_delegation_set(@client.get_zone_id(zone))
    end
  end
end

#delegation_sets(args) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/r53z/cli.rb', line 173

def delegation_sets(args)
  # show them all
  if args.empty?
    sets = @client.list_delegation_sets
    sets.each do |set|
      puts JSON.pretty_generate(set.to_h)
    end
  else # only show specified zones
    args.each do |name|
      dset_id = @client.get_delegation_set_id(name)
      if dset_id
        dset = @client.get_delegation_set(dset_id)
        puts JSON.pretty_generate(dset.delegation_set.to_h)
      else
        exit_now!("Could not find a delegation set for " + name)
      end
    end
  end
end

#export(options:, args:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/r53z/cli.rb', line 98

def export(options:, args:)
  path = File.expand_path(options[:export])
  # If no zones, dump all zones
  zones = []
  # One zone, multiple, or all?
  if args.empty?
    @client.list(:delegation_set_id => options['delegation-set']).each do |zone|
      zones.push(zone[:name])
    end
  else
    if options['delegation-set']
      puts "--delegation-set is overridden when one or more zones are provided"
    end
    zones = args
  end

  zones.each do |name|
    @client.dump(path, name)
  end
end

#list(options:, args:) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/r53z/cli.rb', line 140

def list(options:, args:)
  if args.any?
    args.each do |name|
      puts JSON.pretty_generate(
        @client.list(
          :name => name,
          :delegation_set_id => options['delegation-set']))
    end
  else
    puts JSON.pretty_generate(
      @client.list(:delegation_set_id => options['delegation-set'])
    )
  end
end

#list_by_id(args) ⇒ Object



233
234
235
236
237
# File 'lib/r53z/cli.rb', line 233

def list_by_id(args)
  args.each do |id|
    puts JSON.pretty_generate(@client.list_by_id(id).to_hash)
  end
end

#list_records_by_id(args) ⇒ Object



239
240
241
242
243
# File 'lib/r53z/cli.rb', line 239

def list_records_by_id(args)
  args.each do |id|
    puts JSON.pretty_generate(@client.list_records(id))
  end
end

#record_sets(args) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/r53z/cli.rb', line 217

def record_sets(args)
  args.each do |name|
    zone_id = @client.get_zone_id(name)
    if zone_id
      sets = @client.list_records(@client.get_zone_id(name))
    else
      exit_now!("Could not locate zone " + name)
    end
    if sets
      puts JSON.pretty_generate(sets)
    else
      exit_now!("Could not locate record list for " + name)
    end
  end
end

#restore(options:, args:) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/r53z/cli.rb', line 119

def restore(options:, args:)
  path = File.expand_path(options[:restore])
  # If no zones, restore all zones in directory
  zones = []
  if args.empty?
    # derive list of zones from files in path
    zones = Dir[File.join(path, "*.json")].reject {|n| n.match("zoneinfo")}
  else
    # restore the ones specified
    args.each do |zone|
      zones.push(zone)
    end
  end

  delegation = options['delegation-set'] or nil

  zones.each do |zone|
    @client.restore(path, zone, delegation)
  end
end