Class: GoogleSslCert::CLI::Prune

Inherits:
Base
  • Object
show all
Includes:
Helpers::ProjectNumber
Defined in:
lib/google_ssl_cert/cli/prune.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Logging

#logger

Methods included from Helpers::Global

#global?

Methods included from GoogleServices

#region_ssl_certificates, #resource_manager, #secret_manager_service, #ssl_certificates

Constructor Details

This class inherits a constructor from GoogleSslCert::CLI::Base

Instance Method Details

#base_optionsObject



75
76
77
78
79
# File 'lib/google_ssl_cert/cli/prune.rb', line 75

def base_options
  options = { project: ENV['GOOGLE_PROJECT'] }
  options[:region] = ENV['GOOGLE_REGION'] unless global?
  options
end

#cert_base_nameObject



63
64
65
# File 'lib/google_ssl_cert/cli/prune.rb', line 63

def cert_base_name
  @cert_base_name = GoogleSslCert::Name.new(@options).base_name
end

#delete(cert) ⇒ Object



41
42
43
44
45
# File 'lib/google_ssl_cert/cli/prune.rb', line 41

def delete(cert)
  options = base_options.merge(ssl_certificate: cert.name)
  ssl_service.delete(options)
  logger.info "Deleted #{type} cert: #{cert.name}"
end

#match?(name) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/google_ssl_cert/cli/prune.rb', line 59

def match?(name)
  !!(name =~ Regexp.new("^#{cert_base_name}-\\d{14}$"))
end

#perform_delete(certs) ⇒ Object



35
36
37
38
39
# File 'lib/google_ssl_cert/cli/prune.rb', line 35

def perform_delete(certs)
  certs.each do |cert|
    delete(cert)
  end
end

#preview_delete(certs) ⇒ Object



28
29
30
31
32
33
# File 'lib/google_ssl_cert/cli/prune.rb', line 28

def preview_delete(certs)
  logger.info "Will delete the following #{type} certs:"
  certs.each do |cert|
    logger.info "  #{cert.name}"
  end
end

#runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/google_ssl_cert/cli/prune.rb', line 5

def run
  keep = @options[:keep] || 1
  right = -1 - keep
  certs = ssl_certs[0..right] || [] # delete all except the last cert

  if certs.empty?
    unless ssl_certs.empty?
      logger.info "Found certs:"
      ssl_certs.each do |cert|
        logger.info "  #{cert.name}"
      end
    end
    cert_word = keep == 1 ? "cert" : "certs"
    logger.info "Keeping #{keep} #{cert_word}."
    logger.info "No certs to prune with base cert name: #{cert_base_name}"
    return
  end

  preview_delete(certs) unless @options[:yes]
  sure?
  perform_delete(certs)
end

#ssl_certsObject

sadly the filter option doesnt support globs or regexp so will have to filter with ruby



52
53
54
55
56
57
# File 'lib/google_ssl_cert/cli/prune.rb', line 52

def ssl_certs
  resp = ssl_service.list(base_options)
  resp.select do |ssl|
    match?(ssl.name)
  end.sort_by(&:name)
end

#ssl_serviceObject



67
68
69
70
71
72
73
# File 'lib/google_ssl_cert/cli/prune.rb', line 67

def ssl_service
  if global?
    ssl_certificates
  else
    region_ssl_certificates
  end
end

#typeObject



47
48
49
# File 'lib/google_ssl_cert/cli/prune.rb', line 47

def type
  global? ? "global" : "region"
end