Module: NexClient::Commands::Domains

Extended by:
Helpers
Defined in:
lib/nex_client/commands/domains.rb

Constant Summary collapse

DOMAINS_TITLE =
"Domains".colorize(:magenta)
DOMAINS_HEADERS =
['id','cname','origin'].map(&:upcase)

Constants included from Helpers

Helpers::LOG_COLORS

Class Method Summary collapse

Methods included from Helpers

display_logs, display_record_errors, error, success

Class Method Details

.create(args, opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nex_client/commands/domains.rb', line 26

def self.create(args,opts)
  domain_name,app_name = args
  app = NexClient::App.find(name: app_name).first
  app ||= NexClient::CubeInstance.find(name: app_name).first

  # Display error
  unless app
    error("Error! Could not find app: #{app_name}")
    return false
  end

  domain = NexClient::Domain.new(cname: domain_name)
  domain.relationships.attributes = { origin: { data: { type: app.type, id: app.id } } }
  domain.save

  # Display errors if any
  if domain.errors.any?
    display_record_errors(domain)
    return false
  end

  # Display domains
  self.display_domains(NexClient::Domain.includes(:origin).find(domain.id).first)
end

.destroy(args, opts) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nex_client/commands/domains.rb', line 51

def self.destroy(args,opts)
  name = args.first
  e = NexClient::Domain.find(cname: name).first

  # Display error
  unless e
    error("Error! Could not find domain: #{name}")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the name of this domain to confirm: ")
  unless answer == e.cname
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed domain: #{name}")
end

.display_domains(list) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/nex_client/commands/domains.rb', line 72

def self.display_domains(list)
  table = Terminal::Table.new title: DOMAINS_TITLE, headings: DOMAINS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end

.format_origin(record) ⇒ Object



91
92
93
94
# File 'lib/nex_client/commands/domains.rb', line 91

def self.format_origin(record)
  return "-" unless record.origin
  record.origin.name
end

.format_record(record) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/nex_client/commands/domains.rb', line 82

def self.format_record(record)
  origin = self.format_origin(record)
  [
    record.id,
    record.cname,
    origin
  ]
end

.list(args, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nex_client/commands/domains.rb', line 10

def self.list(args,opts)
  filters = {}
  filters[:'origin.name'] = args.first if args.first.present?

  # Create table
  list = NexClient::Domain.includes(:origin).where(filters).order('cname')
  self.display_domains(list)

  # Loop through results
  while (list.pages.links||{})['next']
    ask("Press enter for next page")
    list = list.pages.next
    self.display_domains(list)
  end
end