module NexClient::Commands::SslCertificates

Constants

DOMAINS_HEADERS
DOMAINS_TITLE

Public Class Methods

create(args,opts) click to toggle source
# File lib/nex_client/commands/ssl_certificates.rb, line 26
def self.create(args,opts)
  cert_name,origin_name = args
  origin = NexClient::App.find(name: origin_name).first
  origin ||= NexClient::CubeInstance.find(name: origin_name).first
  origin ||= NexClient::Organization.find(handle: origin_name).first

  # Display error
  unless origin
    error("Error! Could not find origin: #{origin_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: origin.type, id: origin.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) click to toggle source
# File lib/nex_client/commands/ssl_certificates.rb, line 62
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) click to toggle source
# File lib/nex_client/commands/ssl_certificates.rb, line 83
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) click to toggle source
# File lib/nex_client/commands/ssl_certificates.rb, line 102
def self.format_origin(record)
  return '-' unless o = record.origin
  name = o.respond_to?(:handle) ? o.handle : o.name
  "#{o.type[0..2]}:#{name}"
end
format_record(record) click to toggle source
# File lib/nex_client/commands/ssl_certificates.rb, line 93
def self.format_record(record)
  origin = self.format_origin(record)
  [
    record.id,
    record.cname,
    origin
  ]
end
list(args,opts) click to toggle source
# 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']
    return true if ask("Press enter for next page ('q' to quit)") =~ /q/
    list = list.pages.next
    self.display_certs(list)
  end
end