Module: NexClient::Commands::SslCertificates

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

Constant Summary collapse

DOMAINS_TITLE =
"SSL Certificates".colorize(:light_yellow)
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
50
51
52
53
54
55
56
57
58
59
# File 'lib/nex_client/commands/ssl_certificates.rb', line 26

def self.create(args,opts)
  cert_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

  # Load or ask certificate content from specified files
  pubcert = opts.cert.present? ? File.read(opts.cert) : ask("Copy/paste your certificate below:") { |q| q.gather = "" }
  bundle = opts.bundle.present? ? File.read(opts.bundle) : ask("Copy/paste your cert bundle below:") { |q| q.gather = "" }
  privkey = opts.privkey.present? ? File.read(opts.privkey) : ask("Copy/paste your cert private key below:") { |q| q.gather = "" }

  cert = NexClient::SslCertificate.new(
    cname: cert_name,
    public_cert: pubcert,
    cert_bundle: bundle,
    private_key: privkey
  )
  cert.relationships.attributes = { origin: { data: { type: app.type, id: app.id } } }
  cert.save

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

  # Display certs
  self.display_certs(NexClient::SslCertificate.includes(:origin).find(cert.id).first)
end

.destroy(args, opts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nex_client/commands/ssl_certificates.rb', line 61

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

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

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

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

.display_certs(list) ⇒ Object



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

def self.display_certs(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



101
102
103
104
# File 'lib/nex_client/commands/ssl_certificates.rb', line 101

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

.format_record(record) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/nex_client/commands/ssl_certificates.rb', line 92

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/ssl_certificates.rb', line 10

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

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

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