Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/brightbox-cli/commands/lbs/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module Brightbox
deleted_at
policy
acme_domains
acme_cert_subjects
acme_cert_fingerprint
acme_cert_expires_at
acme_cert_issued_at
ssl_minimum_version
ssl_issuer
ssl_subject
Expand Down
36 changes: 28 additions & 8 deletions lib/brightbox-cli/load_balancers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,42 @@ def self.create(options)
new(conn.load_balancers.create(options))
end

def acme_domains
if attributes["acme"]
attributes["acme"]["domains"].map do |domain|
[domain["identifier"], domain["status"]].join(":")
end.join(",")
def acme_cert
if attributes[:acme] && attributes[:acme][:certificate]
OpenStruct.new(
:expires_at => attributes[:acme][:certificate][:expires_at],
:fingerprint => attributes[:acme][:certificate][:fingerprint],
:issued_at => attributes[:acme][:certificate][:issued_at],
:subjects => attributes[:acme][:certificate][:domains].join(",")
)
else
[]
OpenStruct.new(
:expires_at => "",
:fingerprint => "",
:issued_at => "",
:subjects => ""
)
end
end

def formatted_acme_domains
return "" unless attributes[:acme]

attributes[:acme][:domains].map do |domain|
[domain[:identifier], domain[:status]].join(":")
end.join(",")
rescue StandardError
[]
""
end

def to_row
attributes.merge(
:locked => locked?,
:acme_domains => acme_domains,
:acme_domains => formatted_acme_domains,
:acme_cert_expires_at => acme_cert.expires_at,
:acme_cert_fingerprint => acme_cert.fingerprint,
:acme_cert_issued_at => acme_cert.issued_at,
:acme_cert_subjects => acme_cert.subjects,
:ssl_minimum_version => ssl_minimum_version,
:ssl_issuer => certificate_issuer,
:ssl_subject => certificate_subject,
Expand Down
14 changes: 13 additions & 1 deletion spec/commands/lbs/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@
"identifier": "domain2.test",
"status": "verified"
}
]
],
"certificate": {
"domains": [
"domain.test"
],
"expires_at": "2025-12-31T23:59:59Z",
"fingerprint": "fingerprint",
"issued_at": "2025-01-01T00:00:00Z"
}
},
"nodes":[
{
Expand All @@ -94,6 +102,10 @@
expect(stdout).to include("created_at: 2012-03-05T12:00Z")
expect(stdout).to include("nodes: srv-12345")
expect(stdout).to include("acme_domains: domain.test:verified,domain2.test:verified")
expect(stdout).to include("acme_cert_subjects: domain.test")
expect(stdout).to include("acme_cert_expires_at: 2025-12-31T23:59:59Z")
expect(stdout).to include("acme_cert_fingerprint: fingerprint")
expect(stdout).to include("acme_cert_issued_at: 2025-01-01T00:00:00Z")
end
end
end
Expand Down
75 changes: 75 additions & 0 deletions spec/unit/brightbox/load_balancer/acme_cert_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require "spec_helper"

RSpec.describe Brightbox::LoadBalancer, "#acme_cert" do
subject(:acme_cert) { load_balancer.acme_cert }

let(:fog_model) do
double(
"Fog::Compute::Brightbox::LoadBalancer",
id: "lba-12345",
attributes: lba_attributes
)
end
let(:load_balancer) { Brightbox::LoadBalancer.new(fog_model) }

context "when ACME is not setup" do
let(:lba_attributes) { { acme: nil } }

it "returns an empty string" do
expect(acme_cert).not_to be_nil
end
end

context "when ACME is setup" do
context "without certificate" do
let(:lba_attributes) do
{ acme: { certificate: nil }}
end

it "returns an object without keys" do
expect(acme_cert).not_to be_nil

expect(acme_cert).to have_attributes(
expires_at: "",
fingerprint: "",
issued_at: "",
subjects: ""
)
end
end

context "with certificate" do
let(:expected_formatting) { "example.com:verified,example.net:pending" }
let(:lba_attributes) do
{
acme: {
domains: [
{ identifier: "domain.one.test", status: "verified" },
{ identifier: "domain.two.test", status: "verified" }
],
certificate: {
domains: [
"domain.one.test",
"domain.two.test"
],
expires_at: "2025-12-31T23:59:59Z",
fingerprint: "TLS-fingerprint",
issued_at: "2025-01-01T00:00:00Z"
}
}
}
end

it "returns an object with the correct keys" do
expect(acme_cert).not_to be_nil

expect(acme_cert).to have_attributes(
expires_at: "2025-12-31T23:59:59Z",
fingerprint: "TLS-fingerprint",
issued_at: "2025-01-01T00:00:00Z",
subjects: "domain.one.test,domain.two.test"
)
end
end
end
end
78 changes: 78 additions & 0 deletions spec/unit/brightbox/load_balancer/formatted_acme_domains_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require "spec_helper"

RSpec.describe Brightbox::LoadBalancer, "#formatted_acme_domains" do
subject(:load_balancer) { Brightbox::LoadBalancer.new(fog_model) }

let(:fog_model) do
double(
"Fog::Compute::Brightbox::LoadBalancer",
id: "lba-12345",
attributes: lba_attributes
)
end
let(:lba_attributes) do
# Encode/decode JSON to get final key/value forms
JSON.parse({
id: "lba-12345",
acme: acme_details
}.to_json)
end

context "when ACME is not setup" do
let(:acme_details) { nil }

it "returns an empty array" do
expect(load_balancer.formatted_acme_domains).to eq("")
end
end

context "when no domains are returned" do
let(:acme_details) do
{ domains: [] }
end

it "returns an empty array" do
expect(load_balancer.formatted_acme_domains).to eq("")
end
end

context "when multiple domains are returned" do
let(:acme_details) do
{
domains: [
{
identifier: "example.com",
status: "verified"
},
{
identifier: "example.net",
status: "pending"
}
]
}
end
let(:expected_formatting) do
"example.com:verified,example.net:pending"
end

it "returns a comma separated list of domains and statuses" do
expect(load_balancer.formatted_acme_domains).to eq(expected_formatting)
end
end

context "when error occurs" do
let(:lba_attributes) do
# Encode/decode JSON to get final key/value forms
JSON.parse({
id: "lba-12345",
acme: {
domains: nil
}
}.to_json)
end

it "returns an empty array" do
expect(load_balancer.formatted_acme_domains).to eq("")
end
end
end
Loading