Skip to content

Commit

Permalink
lib/server: provide access to raw data of firewalls
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Koch committed Jun 22, 2023
1 parent cdfb86c commit 8b22d07
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/hcloud/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

module Hcloud
class Future < Delegator
attr_reader :raw_data

# rubocop: disable Lint/MissingSuper
def initialize(client, target_class, id)
def initialize(client, target_class, id, raw_data: nil)
@target_class = target_class
@id = id
@raw_data = raw_data
@__client = client
end
# rubocop: enable Lint/MissingSuper
Expand Down
7 changes: 6 additions & 1 deletion lib/hcloud/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class Server
Future.new(client, PrimaryIP, data[:id]) if data.to_h[:id].is_a?(Integer)
end,
floating_ips: [FloatingIP],
firewalls: [{ id: Firewall }]
firewalls: [
lambda do |data, client|
Future.new(client, Firewall, data[:id], raw_data: data) if data.to_h[:id].is_a?(Integer)
end
]
# firewalls: [{ id: Firewall }]
},
volumes: [Volume]
)
Expand Down
24 changes: 19 additions & 5 deletions spec/hcloud/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,25 @@
expect(obj.public_net[:ipv6]).to be_a Hcloud::Future
expect(obj.public_net[:ipv6].__getobj__).to be_a Hcloud::PrimaryIP

# TODO: "firewalls" has a bit an inconvenient structure for us, I guess we do NOT want to
# have the loaded Firewall under the "id" key?
firewalls = obj.public_net[:firewalls].map { |firewall| firewall[:id] }
expect(firewalls).to all be_a Hcloud::Future
expect(firewalls.map(&:__getobj__)).to all be_a Hcloud::Firewall
expect(obj.public_net[:firewalls]).to all be_a Hcloud::Future
expect(obj.public_net[:firewalls].map(&:__getobj__)).to all be_a Hcloud::Firewall
end

it 'can access raw values for typed attributes' do
# "firewalls" info of a server contains an additional field "status" that we hide by
# presenting a Firewall object to the user
stub_item(:servers, server)

firewall_id = Faker::Number.number
server[:public_net][:firewalls] = [{ id: firewall_id, status: 'applied' }]

server[:public_net][:firewalls].map { |firewall| firewall[:id] }.each do |fwid|
stub_item(:firewalls, new_firewall(id: fwid))
end

obj = client.servers[server[:id]]

expect(obj.public_net[:firewalls][0].raw_data).to eq({ id: firewall_id, status: 'applied' })
end

it 'create new server, handle missing name' do
Expand Down

0 comments on commit 8b22d07

Please sign in to comment.