Skip to content

Commit 417fb32

Browse files
committed
Refactor attributes and to_row
We should be using the superclass version of these rather than repeating the same reference repeatedly. Since `Api#attributes` returns an `IndifferentAccessHash` which the `hirb` table code fails with, it is better to use `#merge` to assemble the table `#to_row` contents.
1 parent bc5ac80 commit 417fb32

39 files changed

+470
-183
lines changed

lib/brightbox-cli/accounts.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ def lb_limit
1313
end
1414

1515
def to_row
16-
attributes.merge(:ram_free => ram_free, :cloud_ip_limit => cloud_ip_limit, :lb_limit => lb_limit)
16+
attributes.to_h.merge(
17+
:ram_free => ram_free,
18+
:cloud_ip_limit => cloud_ip_limit,
19+
:lb_limit => lb_limit
20+
)
1721
end
1822

1923
def self.all

lib/brightbox-cli/api.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def exists?
7272
false
7373
end
7474

75+
def to_row
76+
attributes.to_h
77+
end
78+
7579
def to_s
7680
@id
7781
end
@@ -175,7 +179,7 @@ def self.cached_get(id)
175179
if value
176180
value
177181
else
178-
Brightbox.config.cache_id id
182+
Brightbox.config.cache_id id if Brightbox.config.respond_to?(:cache_id)
179183
@cache[id] = get(id)
180184
end
181185
end

lib/brightbox-cli/cloud_ips.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ def self.format_translators_for_api(translators)
3333
end
3434

3535
def attributes
36-
a = fog_model.attributes
37-
a[:destination] = destination_id
38-
a
36+
fog_attributes.tap do |attrs|
37+
attrs[:destination] = destination_id
38+
end
3939
end
4040

4141
def to_row
42-
o = attributes
43-
o[:port_translators] = translators(o)
44-
o
42+
attributes.merge(
43+
port_translators: translators(attributes),
44+
).to_h
4545
end
4646

4747
def mapped?

lib/brightbox-cli/collaboration.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ def to_s
3131

3232
attr_reader :id
3333

34-
def attributes
35-
fog_model.attributes
36-
end
37-
3834
def to_row
39-
row_attributes = attributes
40-
row_attributes[:name] = invitee_name
41-
row_attributes
35+
attributes.merge(
36+
name: invitee_name
37+
).to_h
4238
end
4339

4440
def invitee_name

lib/brightbox-cli/database_server.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,34 @@ def self.detailed_fields
7070
end
7171

7272
def type_identifier
73-
return unless fog_model.attributes.key?("database_server_type")
73+
return unless fog_attributes.key?("database_server_type")
7474

75-
fog_model.attributes["database_server_type"]["id"]
75+
fog_attributes["database_server_type"]["id"]
7676
end
7777

7878
def zone_handle
79-
return unless fog_model.attributes.key?("zone")
79+
return unless fog_attributes.key?("zone")
8080

81-
fog_model.attributes["zone"]["handle"]
81+
fog_attributes["zone"]["handle"]
8282
end
8383

8484
def to_row
85-
a = fog_model.attributes
86-
a[:status] = fog_model.state
87-
a[:locked] = locked?
88-
a[:type] = type_identifier
89-
a[:db_engine] = engine_version
90-
a[:engine] = database_engine
91-
a[:version] = database_version
92-
a[:maintenance_weekday] = maintenance_weekday
93-
a[:maintenance_hour] = maintenance_hour
94-
a[:maintenance_window] = maintenance_window
95-
a[:zone] = zone_handle
96-
a[:created_on] = created_on
97-
a[:allow_access] = allow_access
98-
a[:cloud_ip_ids] = cloud_ip_ids
99-
a[:cloud_ips] = cloud_ip_addresses
100-
a
85+
attributes.merge(
86+
status: fog_model.state,
87+
locked: locked?,
88+
type: type_identifier,
89+
db_engine: engine_version,
90+
engine: database_engine,
91+
version: database_version,
92+
maintenance_weekday: maintenance_weekday,
93+
maintenance_hour: maintenance_hour,
94+
maintenance_window: maintenance_window,
95+
zone: zone_handle,
96+
created_on: created_on,
97+
allow_access: allow_access,
98+
cloud_ip_ids: cloud_ip_ids,
99+
cloud_ips: cloud_ip_addresses
100+
)
101101
end
102102

103103
def engine_version

lib/brightbox-cli/database_snapshot.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def self.default_field_order
2727
end
2828

2929
def to_row
30-
a = fog_model.attributes
31-
a[:status] = fog_model.state
32-
a[:locked] = locked?
33-
a[:created_on] = fog_model.created_at.strftime("%Y-%m-%d")
34-
a
30+
fog_attributes.merge(
31+
status: fog_model.state,
32+
locked: locked?,
33+
created_on: fog_model.created_at.strftime("%Y-%m-%d")
34+
)
3535
end
3636
end
3737
end

lib/brightbox-cli/database_type.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ class DatabaseType < Api
33
def self.require_account?; true; end
44

55
def attributes
6-
o = fog_model.attributes
7-
o[:ram] = ram
8-
o[:disk] = disk
9-
o
6+
fog_attributes.tap do |attrs|
7+
attrs[:ram] = ram
8+
attrs[:disk] = disk
9+
end
1010
end
1111

1212
def ram
@@ -17,10 +17,6 @@ def disk
1717
fog_model.disk.to_i
1818
end
1919

20-
def to_row
21-
attributes
22-
end
23-
2420
def self.all
2521
conn.database_types
2622
end

lib/brightbox-cli/detailed_server.rb

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
module Brightbox
22
class DetailedServer < Server
33
def to_row
4-
row_attributes = attributes
4+
attributes.tap do |attrs|
5+
attrs[:compatibility_mode] = attrs["compatibility_mode"]
56

6-
row_attributes[:compatibility_mode] = row_attributes["compatibility_mode"]
7-
8-
if server_type
9-
row_attributes[:type] = server_type["id"]
10-
row_attributes[:type_handle] = server_type["handle"]
11-
row_attributes[:type_name] = server_type["name"]
12-
row_attributes[:ram] = server_type["ram"]
13-
row_attributes[:cores] = server_type["cores"]
14-
row_attributes[:disk] = server_type["disk_size"].to_i
15-
end
16-
17-
if image
18-
row_attributes[:image_name] = image.name
19-
row_attributes[:arch] = image.arch
20-
row_attributes[:image_username] = image.username
21-
end
7+
if server_type
8+
attrs[:type] = server_type["id"]
9+
attrs[:type_handle] = server_type["handle"]
10+
attrs[:type_name] = server_type["name"]
11+
attrs[:ram] = server_type["ram"]
12+
attrs[:cores] = server_type["cores"]
13+
attrs[:disk] = server_type["disk_size"].to_i
14+
end
2215

23-
row_attributes[:private_ips] = interfaces.map { |i| i["ipv4_address"] }.join(", ")
24-
row_attributes[:ipv6_address] = interfaces.map { |i| i["ipv6_address"] }.join(", ")
16+
if image
17+
attrs[:image_name] = image.name
18+
attrs[:arch] = image.arch
19+
attrs[:image_username] = image.username
20+
end
2521

26-
row_attributes[:cloud_ip_ids] = cloud_ips.map { |i| i["id"] }.join(", ")
27-
row_attributes[:cloud_ipv4s] = cloud_ips.map { |i| i["public_ipv4"] }.join(", ")
28-
row_attributes[:cloud_ipv6s] = cloud_ips.map { |i| i["public_ipv6"] }.join(", ")
29-
row_attributes[:snapshots] = snapshots.map { |i| i["id"] }.join(", ")
22+
attrs[:private_ips] = interfaces.map { |i| i["ipv4_address"] }.join(", ")
23+
attrs[:ipv6_address] = interfaces.map { |i| i["ipv6_address"] }.join(", ")
3024

31-
if server_groups
32-
row_attributes[:server_groups] = server_groups.map { |sg| sg["id"] }.join(", ")
33-
end
25+
attrs[:cloud_ip_ids] = cloud_ips.map { |i| i["id"] }.join(", ")
26+
attrs[:cloud_ipv4s] = cloud_ips.map { |i| i["public_ipv4"] }.join(", ")
27+
attrs[:cloud_ipv6s] = cloud_ips.map { |i| i["public_ipv6"] }.join(", ")
28+
attrs[:snapshots] = snapshots.map { |i| i["id"] }.join(", ")
3429

35-
row_attributes[:volumes] = volume_ids if volumes
30+
if server_groups
31+
attrs[:server_groups] = server_groups.map { |sg| sg["id"] }.join(", ")
32+
end
3633

37-
row_attributes
34+
attrs[:volumes] = volume_ids if volumes
35+
end.to_h
3836
end
3937

4038
def volume_ids

lib/brightbox-cli/detailed_server_group.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Brightbox
22
class DetailedServerGroup < ServerGroup
33
def to_row
4-
row_attributes = super
4+
row_attributes = attributes
55
row_attributes[:firewall_policy] = firewall_policy && firewall_policy.id
6-
row_attributes
6+
row_attributes.to_h
77
end
88

99
def self.default_field_order

lib/brightbox-cli/firewall_policy.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ def self.all
1515
end
1616

1717
def attributes
18-
t = fog_model.attributes
19-
t[:name] = name
20-
t[:description] = description
21-
t[:server_group] = server_group_id
22-
t
23-
end
24-
25-
def to_row
26-
attributes
18+
fog_attributes.tap do |attrs|
19+
attrs[:name] = name
20+
attrs[:description] = description
21+
attrs[:server_group] = server_group_id
22+
end
2723
end
2824

2925
def self.default_field_order

0 commit comments

Comments
 (0)