Class: GoogleSslCert::CLI::Prune
Instance Method Summary
collapse
Methods inherited from Base
#initialize
Methods included from Logging
#logger
#global?
#region_ssl_certificates, #resource_manager, #secret_manager_service, #ssl_certificates
Instance Method Details
#base_options ⇒ Object
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
|
#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
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
|
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
|
#run ⇒ Object
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] || []
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_certs ⇒ Object
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_service ⇒ Object
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
|
#type ⇒ Object
47
48
49
|
# File 'lib/google_ssl_cert/cli/prune.rb', line 47
def type
global? ? "global" : "region"
end
|