Class: PI::Cli::Command::Dns

Inherits:
Base show all
Includes:
PI::Cli::ChooseHelper, InteractHelper
Defined in:
lib/cli/commands/dns.rb

Constant Summary

Constants included from InteractHelper

InteractHelper::ESCAPES, InteractHelper::EVENTS

Constants included from PI::Cli::ChooseHelper

PI::Cli::ChooseHelper::DEFAULTS, PI::Cli::ChooseHelper::NO_SET, PI::Cli::ChooseHelper::YES_SET

Instance Method Summary collapse

Methods included from InteractHelper

#asks, #read_char, #read_event, #read_line

Methods included from PI::Cli::ChooseHelper

#check_envname_valid?, #check_envvalue_valid?, #check_name_valid?, #check_status, #check_version_name_valid?, #choose_app, #choose_app_help, #choose_app_help_target_not_all, #choose_dnsid, #choose_project, #choose_serviceid, #choose_target, #get_graceful, #select_apps, #total_opts

Methods inherited from Base

#auth_token, #client, #initialize, #target_url

Constructor Details

This class inherits a constructor from PI::Cli::Command::Base

Instance Method Details

#app_dns(appid_or_appname = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cli/commands/dns.rb', line 6

def app_dns(appid_or_appname=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  app, appid = choose_app_help_target_not_all(appid_or_appname)
  appid = (appid == nil ? app[:id] : appid)

  dns = client.app_dns(appid)
  return display JSON.pretty_generate(dns) if @options[:json]
  return display "No DNS!" if dns.nil? || dns.empty?
  display "Mapped DNS Name:"
  dns.each do |s|
    display "#{s}"
  end
end

#create_dns(dnsname = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cli/commands/dns.rb', line 128

def create_dns(dnsname=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(projectid|desc|zoneid)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  projectid = @options[:projectid]
  description = @options[:desc]
  zoneid = @options[:zoneid]
  if dnsname              
    if not check_name_valid?(dnsname)
      err "Invalid dns name '#{dnsname}'." 
    end
  end
  dns_zone = client.dns_zone_list
  err"No dns zone" if dns_zone.nil? || dns_zone.empty?
  if zoneid
    zoneid_flag = false
    dns_zone.each do |d|
      zoneid_flag = true if d[:id].to_s == zoneid
    end
    err"The dns zone id is not exist!" if zoneid_flag != true
  end
  
  projects = client.projects  
  if projectid
    projectid_flag = false
    projects.each do |p|
      projectid_flag = true if p[:id].to_s == projectid
    end
    err"Invalid project id: #{projectid}" if projectid_flag != true
  else
    projectname = ask"Project name", :choices => projects.collect { |p| p[:name] }, :indexed => true  
    projects.each do |p|
      projectid = p[:id] if p[:name] == projectname
    end 
  end           
    
  unless zoneid
    zonename = ask"Select dns zone", :choices => dns_zone.collect { |d| d[:name] }, :indexed => true
    dns_zone.each do |d|
      zoneid = d[:id] if d[:name] == zonename
    end
    err"Invalid zone" if zoneid.nil?
  end
  
  loop{
    dnsname = ask"DNS name" unless dnsname
    tmp = client.verify_dns(dnsname, zoneid)
    if not check_name_valid?(dnsname)
      display "Invalid dns name '#{dnsname}'." 
      dnsname =nil
      next
    else if tmp[1] == "false"
      display "DNS '#{dnsname}' already exists." 
      dnsname =nil
      next
    else
      break
      end
    end
  }   
  # dnsname = dnsname.concat(".#{zone}")        
  description = ask"Description", :default => nil unless description
  manifest = {
    :name        => dnsname,
    :zoneId      => zoneid,
    :projectId   => projectid,
    :bak         => description
  }
  client.create_dns(manifest)
  display"OK".green    
end

#delete_dns(dnsid = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cli/commands/dns.rb', line 203

def delete_dns(dnsid=nil)
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  unless dnsid
    dnsid = choose_dnsid
  end
  client.delete_dns(dnsid)
  display"OK".green
end

#deregister_dns(dnsid = nil) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/cli/commands/dns.rb', line 274

def deregister_dns(dnsid=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(recordid)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  recordid = @options[:recordid]
  unless dnsid
    dnsid = choose_dnsid
  end
  unless recordid
    targets = client.usable_dns_target(dnsid)
    target = ask"Select target", :choices => targets.collect { |t| t[:targetName] }, :indexed => true
    record = Array.new
    targets.each do |t|
      record = t[:records] if t[:targetName] == target
    end
    err"No record" if record.nil? || record.empty?
    view = ask"Select record", :choices => record.collect { |t| t[:view] }, :indexed => true
    recordid = nil
    record.each do |r|
      recordid = r[:id] if r[:view] == view
    end
  end
  client.deregister_dns(recordid)
  display "OK".green
end

#dnsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cli/commands/dns.rb', line 91

def dns
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  dns = client.dns
  return display "No Services" if dns.nil? || dns.empty?        
  return display JSON.pretty_generate(dns) if @options[:json]
  dns.sort! {|a, b| a[:name] <=> b[:name] }
  dns_table = table do |t|
    t.headings = 'ID', 'DNS Name', 'Project Name', 'Description'
    dns.each do |s|
      t << [s[:id],s[:name], s[:projectName], s[:bak]]
    end
  end
  display dns_table
end

#dns_records(dnsid = nil) ⇒ Object



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
# File 'lib/cli/commands/dns.rb', line 303

def dns_records(dnsid=nil)
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  unless dnsid
    dnsid = choose_dnsid
  end
  record_info = Array.new
  targets = client.usable_dns_target(dnsid)
  targets.each do |t|
    t[:records].each do |v|
      record_info.push({ :id => v[:id],:targetName => t[:targetName],:view => v[:view]})
    end
  end
  if record_info.empty?
    err"No dns record"
  else
    record_info_table = table do |t|
    t.headings = 'Record ID', 'Target Name', 'View Name'
    record_info.each do |s|
      t << [s[:id],s[:targetName], s[:view]]
    end
  end
  display record_info_table
  end      
end

#map_dns(appid_or_appname = nil) ⇒ Object



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
# File 'lib/cli/commands/dns.rb', line 25

def map_dns(appid_or_appname=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|dns)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  dnsname = @options[:dns]
  if dnsname
    dns_list = client.dns
    dnsnames = dns_list.collect { |d| d[:name] }
    err "Invalid dns name" unless dnsnames.include?(dnsname)
  end
  app, appid = choose_app_help_target_not_all(appid_or_appname)
  appid = (appid == nil ? app[:id] : appid)
  dns = client.usable_dns(appid)
  err "No usable dns!" if dns.nil? || dns.empty?
  if dnsname        
    usable_dnsnames = dns.collect { |d| d[:name] }
    err "Invalid dns name" unless usable_dnsnames.include?(dnsname)        
    tmp = dnsname.split(",")
    manifest = Array.new
    tmp.each do |t|
      manifest.push(t)
    end
  else
    manifest = asks "Select dns, indexed list?", :choices => dns.collect { |d| d[:name] }, :indexed => true
  end      
  display "Mapping dns: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.map_dns(appid,manifest)
  Thread.kill(t)
  display "OK".green   
end

#register_dns(dnsid = nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/cli/commands/dns.rb', line 216

def register_dns(dnsid=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|continent|country)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  target = @options[:target]
  continent = @options[:continent]
  if continent
    continents = client.dns_continents
    err "Invalid continent" unless continents.has_key?(continent.to_sym)
  end
  country = @options[:country]
  if country && continent
    countries = client.dns_country(continent)
    err "Invalid country" unless countries.has_key?(country.to_sym)
  end
  unless dnsid
    dnsid = choose_dnsid
  end
  unless target
    targets = client.usable_dns_target(dnsid)
    target = ask"Select target", :choices => targets.collect { |t| t[:targetName] }, :indexed => true
  end
  unless continent
    continents = client.dns_continents
    err "No continents" if continents.nil? || continents.empty?
    tmp = continents.values
    tmp.sort! {|a, b| a <=> b }
    continent = ask"Select continent", :choices => tmp.collect { |t| t }, :indexed => true
    display "Select continent: #{continent}"
    continent = continents.invert["#{continent}"]
  end
  if continent.to_s == "DEFAULT" 
    manifest = {
    :continentCode => continent,
    :countryCode   => "DEFAULT"
  }
  else
    unless country
      countries = client.dns_country(continent)
      err "No countries" if countries.nil? || countries.empty?
      tmp = countries.values
      tmp.sort! {|a, b| a <=> b }
      country = ask"Select country", :choices => tmp.collect { |t| t }, :indexed => true
      display "Select country: #{country}"
      country = countries.invert["#{country}"]
    end
    manifest = {
    :continentCode => continent,
    :countryCode   => country
  }
  end     
  client.register_dns(dnsid, target, manifest)
  display "OK".green
end

#unmap_dns(appid_or_appname = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cli/commands/dns.rb', line 68

def unmap_dns(appid_or_appname=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|dns)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  dnsname = @options[:dns]
  app, appid = choose_app_help_target_not_all(appid_or_appname)
  appid = (appid == nil ? app[:id] : appid)
  dns = client.app_dns(appid)
  err "No mapped dns!" if dns.nil? || dns.empty? 
  if dnsname 
    urls = dnsname.split(",")
  else
    urls = asks "Select DNS", :choices => dns, :indexed => true
  end
  
  urls.each do |url|
    do_unmap_dns(appid, url)
  end  
end

#zonesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cli/commands/dns.rb', line 110

def zones
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  zones = client.dns_zone_list
  return display JSON.pretty_generate(zones) if @options[:json]
  return display "No DNS!" if zones.nil? || zones.empty?
  zones_table = table do |t|
    t.headings = 'ID', 'Name'
    zones.each do |s|
      t << [s[:id], s[:name]]
    end
  end
  display zones_table
end