Class: EnfCli::Cmd::Xdns

Inherits:
EnfThor
  • Object
show all
Defined in:
lib/enfcli/commands/xdns.rb

Constant Summary collapse

DnsRecordType_AAAA =
"AAAA"
DnsRecordType_TXT =
"TXT"
DnsRecordType_SRV =
"SRV"
DnsRecordType_CNAME =
"CNAME"
DnsRecordTypes =
[DnsRecordType_AAAA, DnsRecordType_CNAME, DnsRecordType_SRV, DnsRecordType_TXT]

Instance Method Summary collapse

Methods inherited from EnfThor

capture_stdout, command_help, handle_argument_error, help

Instance Method Details

#add_networks_to_zoneObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/enfcli/commands/xdns.rb', line 193

def add_networks_to_zone
  try_with_rescue_in_session do
    ## gather parameters
    zone_id = options[:'zone-id']
    networks = array_option_to_string(options[:networks]).split(",").map { |x| x.strip }

    ## add networks request
    add_networks_req = {
      :networks => networks,
    }

    ## call api
    data = EnfApi::Dns.instance.add_networks_to_zone zone_id, add_networks_req
    networks = data[:data]

    ## display data
    say "Added the following networks to zone with id: #{zone_id}", :green
    display_networks_table networks
  end
end

#create_recordObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/enfcli/commands/xdns.rb', line 297

def create_record
  try_with_rescue_in_session do
    ## gather parameters
    zone_id = options[:'zone-id']
    name = options[:name]
    type = options[:type]
    ttl = options[:ttl]
    value = array_option_to_string(options.value)

    ## get value
    case type
    when DnsRecordType_AAAA
      ipv6 = EnfCli::IPV6.new(value).to_s
      value = { :ipv6 => ipv6 }
    when DnsRecordType_CNAME
      value = { :dname => value }
    when DnsRecordType_SRV
      raise "Invalid value for #{DnsRecordType_SRV} record" unless options.value.length == 4
      value = { :priority => Integer(options.value[0]),
                :weight => Integer(options.value[1]),
                :port => Integer(options.value[2]),
                :target => options.value[3] }
    when DnsRecordType_TXT
      value = { :txt => value }
    end

    ## create request hash
    new_record = {
      :type => type,
      :ttl => ttl,
      :value => value,
    }

    ## optionally add name to request hash
    new_record[:name] = name if name

    ## call api
    data = EnfApi::Dns.instance.create_dns_record zone_id, new_record
    records = data[:data]

    ## display table
    say "Created new DNS record!", :green
    display_records_table records
  end
end

#create_zoneObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/enfcli/commands/xdns.rb', line 87

def create_zone
  try_with_rescue_in_session do
    ## session
    session = EnfCli::CTX.instance.session

    ## Gather parameters
    zone_domain_name = options["zone-domain-name"]
    description = array_option_to_string(options.description) if options.description
    if EnfCli::CTX.instance.xaptum_admin?
      enf_domain = options["enf-domain"]
      raise "No value provided for required options '--enf-domain'" unless enf_domain
    else
      enf_domain = session[:domain]
    end

    enf_network = options["enf-network"]
    unless EnfCli::CTX.instance.edit_domain_role?
      raise "No value provided for required option '--enf-network'" unless enf_network
    end

    ## create request hash
    new_zone = {
      :zone_domain_name => zone_domain_name,
      :description => description,
      :enf_domain => enf_domain,
    }

    ## add enf_network to request if present
    new_zone[:enf_network] = enf_network if enf_network

    ## call api
    data = EnfApi::Dns.instance.create_dns_zone new_zone
    zones = data[:data]

    ## display success
    say "Created DNS zone #{zone_domain_name}!", :green

    display_zones_table zones
  end
end

#delete_networks_from_zoneObject



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/enfcli/commands/xdns.rb', line 235

def delete_networks_from_zone
  try_with_rescue_in_session do
    ## gather parameters
    zone_id = options[:'zone-id']
    networks = array_option_to_string(options[:networks])

    ## call api
    EnfApi::Dns.instance.delete_networks_from_zone zone_id, networks

    ## print success
    say "Deleted networks from DNS zone!", :green
  end
end

#delete_recordObject



384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/enfcli/commands/xdns.rb', line 384

def delete_record
  try_with_rescue_in_session do
    ## gather parameters
    id = options[:id]

    ## call api
    EnfApi::Dns.instance.delete_dns_record id

    ## print success
    say "Deleted DNS record!", :green
  end
end

#delete_serverObject



444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/enfcli/commands/xdns.rb', line 444

def delete_server
  try_with_rescue_in_session do
    ## gather parameters
    network = options[:network]
    ipv6 = options[:ipv6]

    ## call api
    EnfApi::Dns.instance.delete_server network, ipv6

    ## print success
    say "Delete DNS server with ipv6 #{ipv6} in #{network}!", :green
  end
end

#delete_zoneObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/enfcli/commands/xdns.rb', line 155

def delete_zone
  try_with_rescue_in_session do
    zone_id = options[:'zone-id']
    ## call api
    EnfApi::Dns.instance.delete_dns_zone zone_id

    ## print success
    say "Deleted DNS Zone with id: #{zone_id}", :green
  end
end

#list_networks_in_zoneObject



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/enfcli/commands/xdns.rb', line 217

def list_networks_in_zone
  try_with_rescue_in_session do
    ## gather parameters
    zone_id = options[:'zone-id']

    ## call api
    data = EnfApi::Dns.instance.list_networks_in_zone zone_id
    networks = data[:data]

    ## display data
    display_networks_table networks
  end
end

#list_recordsObject



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/enfcli/commands/xdns.rb', line 346

def list_records
  try_with_rescue_in_session do
    ## gather parameters
    zone_id = options[:'zone-id']

    ## call api
    data = EnfApi::Dns.instance.list_dns_records zone_id
    records = data[:data]

    ## display table
    display_records_table records
  end
end

#list_serversObject



426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/enfcli/commands/xdns.rb', line 426

def list_servers
  try_with_rescue_in_session do
    ## gather parameters
    network = options[:network]

    ## call api
    data = EnfApi::Dns.instance.list_servers network
    servers = data[:data]

    ## display resutls
    display_servers_table servers
  end
end

#list_zonesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/enfcli/commands/xdns.rb', line 131

def list_zones
  try_with_rescue_in_session do
    ## session
    session = EnfCli::CTX.instance.session

    if EnfCli::CTX.instance.xaptum_admin?
      enf_domain = options["enf-domain"]
      raise "No value provided for required options '--enf-domain'" unless enf_domain
    else
      enf_domain = session[:domain]
    end

    ## call api
    data = EnfApi::Dns.instance.list_zones enf_domain
    zones = data[:data]

    ## display table
    display_zones_table zones
  end
end

#list_zones_in_networkObject



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/enfcli/commands/xdns.rb', line 275

def list_zones_in_network
  try_with_rescue_in_session do
    ## gather parameters
    network = options[:network]

    ## call api
    data = EnfApi::Dns.instance.list_zones_in_network network
    zones = data[:data]

    ## display data
    display_zones_table zones
  end
end

#provision_serverObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/enfcli/commands/xdns.rb', line 402

def provision_server
  try_with_rescue_in_session do
    ## gather parameters
    network = options[:network]
    description = array_option_to_string(options.description) if options.description
    ipv6 = options[:ipv6]

    new_server = {
      :ipv6 => ipv6,
      :description => description,
    }

    ## call API
    data = EnfApi::Dns.instance.provision_server network, new_server
    servers = data[:data]

    ## display results
    display_servers_table servers
  end
end

#queryObject



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/enfcli/commands/xdns.rb', line 365

def query
  try_with_rescue_in_session do
    ## gather parameters
    network = options[:network]
    name = options[:name]
    type = options[:type]

    ## call api
    data = EnfApi::Dns.instance.query network, type, name
    records = data[:data]

    ## display table
    display_records_table records
  end
end

#replace_networks_in_zoneObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/enfcli/commands/xdns.rb', line 253

def replace_networks_in_zone
  try_with_rescue_in_session do
    ## gather parameters
    zone_id = options[:'zone-id']
    networks = array_option_to_string(options[:networks]).split(",").map { |x| x.strip }

    ## replace networks request
    replace_networks_req = {
      :networks => networks,
    }

    ## call api
    EnfApi::Dns.instance.replace_networks_in_zone zone_id, replace_networks_req

    ## print success
    say "Replaced networks in DNS zone!", :green
  end
end

#update_zoneObject



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

def update_zone
  try_with_rescue_in_session do
    ## get parameters
    description = array_option_to_string(options.description) if options.description

    ## update request
    update_zone_req = {
      :description => description,
    }

    ## call api
    data = EnfApi::Dns.instance.update_dns_zone options[:'zone-id'], update_zone_req
    zones = data[:data]

    ## display updated result
    display_zones_table zones
  end
end