Skip to content

Commit 910f209

Browse files
nazarovdoamvasilyev
authored andcommitted
Add IBM Cloud resources support to list-cloud-resources command (refs FRUC-211)
1 parent ae7d38c commit 910f209

File tree

2 files changed

+116
-21
lines changed

2 files changed

+116
-21
lines changed

core/commands/list_cloud_resources_command.rb

+83-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.synopsis
1515

1616
def show_help
1717
info = <<-HELP
18-
The command shows a list of active resources: instances, disks (volumes), security groups and key pairs on GCP and AWS providers and the time they were created.
18+
The command shows a list of active resources: instances, disks (volumes), security groups, public networks and key pairs on GCP, AWS and IBM Cloud providers and the time they were created.
1919
2020
Add the --json flag to show the machine readable text.
2121
Add the --hours NUMBER_OF_HOURS flag to display the resources older than this hours.
@@ -60,32 +60,40 @@ def execute
6060

6161
# Outputs the resources in a table format
6262
def show_in_table_format(resources)
63-
@ui.info("AWS Instances:\n#{instances_table(resources[:instances][:aws])}")
64-
@ui.info("GCP Instances:\n#{instances_table(resources[:instances][:gcp])}")
63+
@ui.info("AWS Instances:\n#{instances_table(resources[:instances][:aws], 'aws')}")
64+
@ui.info("GCP Instances:\n#{instances_table(resources[:instances][:gcp], 'gcp')}")
65+
@ui.info("IBM Cloud Instances:\n#{instances_table(resources[:instances][:ibm], 'ibm')}")
6566
@ui.info("Disks (volumes):\n#{disks_table(resources[:disks])}")
66-
@ui.info("AWS key pairs:\n#{key_pairs_table(resources[:key_pairs])}")
67+
@ui.info("AWS key pairs:\n#{key_pairs_table(resources[:aws_key_pairs], 'aws')}")
6768
@ui.info("AWS security groups:\n#{security_groups_table(resources[:security_groups])}")
69+
@ui.info("IBM Cloud public networks:\n#{public_network_table(resources[:ibm_public_networks])}")
70+
@ui.info("IBM Cloud key pairs:\n#{key_pairs_table(resources[:ibm_key_pairs], 'ibm')}")
6871
end
6972

7073
# Fetches the list of resources and generates their description in format
7174
# { instances: { gcp: Array, aws: Array }, disks: { gcp: Array, aws: Array }, key_pairs: Array, security_groups: Array }
7275
def list_resources
73-
key_pairs = @filter_unused ? @env.aws_service.list_unused_key_pairs(@resource_expiration_threshold) : @env.aws_service.key_pairs_list
76+
aws_key_pairs = @filter_unused ? @env.aws_service.list_unused_key_pairs(@resource_expiration_threshold) : @env.aws_service.key_pairs_list
77+
ibm_key_pairs = list_ibm_ssh_keys
7478
security_groups = @filter_unused ? @env.aws_service.list_unused_security_groups(@resource_expiration_threshold) : @env.aws_service.security_group_list
75-
@resources_count += key_pairs.length + security_groups.length
79+
ibm_public_networks = @env.ibm_service.public_networks_list
80+
@resources_count += aws_key_pairs.length + ibm_key_pairs.length + security_groups.length + ibm_public_networks.length
7681
{
7782
instances: list_instances,
7883
disks: list_disks,
79-
key_pairs: key_pairs,
80-
security_groups: security_groups
84+
aws_key_pairs: aws_key_pairs,
85+
ibm_key_pairs: ibm_key_pairs,
86+
security_groups: security_groups,
87+
ibm_public_networks: ibm_public_networks
8188
}
8289
end
8390

8491
def list_instances
8592
@hidden_instances = read_hidden_instances
8693
{
8794
aws: list_aws_instances,
88-
gcp: list_gcp_instances
95+
gcp: list_gcp_instances,
96+
ibm: list_ibm_instances
8997
}
9098
end
9199

@@ -98,6 +106,16 @@ def list_disks
98106
}
99107
end
100108

109+
def list_ibm_ssh_keys
110+
all_key_pairs = @env.ibm_service.ssh_keys_list
111+
all_key_pairs.each do |key_pair|
112+
key_pair[:launch_time] = DateTime.parse(key_pair[:launch_time]).new_offset(0.0 / 24)
113+
end
114+
all_key_pairs = select_by_time(all_key_pairs) unless @env.hours.nil?
115+
all_key_pairs = time_to_string(all_key_pairs)
116+
all_key_pairs
117+
end
118+
101119
# Renders a table with the disks that are not attached to any instance
102120
def disks_table(disks)
103121
header = ['Disk name', 'Creation time', 'Provider', 'Zone']
@@ -112,18 +130,35 @@ def disks_table(disks)
112130
table.render(:unicode)
113131
end
114132

115-
# Renders a table with the key pairs that are not used by any instance
116-
def key_pairs_table(key_pairs)
133+
# Renders a table with the key pairs that are not used by any instance (all for IBM PVM instances)
134+
def key_pairs_table(key_pairs, provider)
117135
header = ['Key name', 'Creation time']
118136
table = TTY::Table.new(header: header)
119137
key_pairs.each do |key_pair|
120-
table << [key_pair[:name], key_pair[:creation_date]]
138+
case provider
139+
when 'ibm'
140+
table << [key_pair[:name], key_pair[:launch_time]]
141+
when 'aws'
142+
table << [key_pair[:name], key_pair[:creation_date]]
143+
end
121144
end
122145
return 'No key pairs found' if table.empty?
123146

124147
table.render(:unicode)
125148
end
126149

150+
# Renders a table with the public networks
151+
def public_network_table(networks)
152+
header = ['Public network name', 'Network ID']
153+
table = TTY::Table.new(header: header)
154+
networks.each do |network|
155+
table << [network[:name], network[:network_id]]
156+
end
157+
return 'No public networks found' if table.empty?
158+
159+
table.render(:unicode)
160+
end
161+
127162
# Renders a table with the security groups that are not used by any instance
128163
def security_groups_table(security_groups)
129164
header = ['Group ID', 'Configuration ID', 'Generation time']
@@ -194,6 +229,17 @@ def list_gcp_instances
194229
all_instances
195230
end
196231

232+
def list_ibm_instances
233+
all_instances = @env.ibm_service.instances_list_with_time
234+
all_instances.each do |instance|
235+
instance[:launch_time] = DateTime.parse(instance[:launch_time]).new_offset(0.0 / 24)
236+
end
237+
all_instances = select_by_time(all_instances) unless @env.hours.nil?
238+
all_instances = time_to_string(all_instances)
239+
@resources_count += all_instances.length
240+
all_instances
241+
end
242+
197243
def select_by_time(instances)
198244
instances.select do |instance|
199245
instance[:launch_time] < DateTime.now.new_offset(0.0 / 24) - (@env.hours.to_i / 24.0)
@@ -209,19 +255,35 @@ def time_to_string(instances)
209255

210256
# Renders the table with list of instances
211257
# @param list {Array} list of instances
212-
def instances_table(list)
258+
# @param provider {String} cloud provider
259+
def instances_table(list, provider)
213260
return 'No instances found' if list.empty?
214261

215-
header = ['Launch time', 'Node name', 'Zone', 'Path', 'User']
262+
header = ['Launch time', 'Node name']
263+
case provider
264+
when 'ibm'
265+
header.concat(['PVM Instance ID'])
266+
when 'gcp', 'aws'
267+
header.concat(['Zone', 'Path', 'User'])
268+
end
216269
table = TTY::Table.new(header: header)
217270
list.each do |instance|
218-
info = [
219-
instance[:launch_time],
220-
instance[:node_name],
221-
instance[:zone],
222-
instance[:path],
223-
instance[:username]
224-
]
271+
case provider
272+
when 'ibm'
273+
info = [
274+
instance[:launch_time],
275+
instance[:node_name],
276+
instance[:instance_id]
277+
]
278+
when 'gcp', 'aws'
279+
info = [
280+
instance[:launch_time],
281+
instance[:node_name],
282+
instance[:zone],
283+
instance[:path],
284+
instance[:username]
285+
]
286+
end
225287
table << info
226288
end
227289
table.render(:unicode)

core/services/ibm_service.rb

+33
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ def generate_instance_info(instance)
5252
}
5353
end
5454

55+
def public_networks_list
56+
return [] unless configured?
57+
58+
list_networks['networks'].map { |network| generate_public_network_info(network) }
59+
end
60+
61+
def generate_public_network_info(network)
62+
{
63+
name: network['name'],
64+
network_id: network['networkID']
65+
}
66+
end
67+
68+
def ssh_keys_list
69+
return [] unless configured?
70+
71+
list_ssh_keys['sshKeys']
72+
.map { |key_pair| generate_key_pair_info(key_pair) }
73+
.sort_by { |key_pair| DateTime.parse(key_pair[:launch_time]) }
74+
.reverse
75+
end
76+
77+
def generate_key_pair_info(key_pair)
78+
{
79+
name: key_pair['name'],
80+
launch_time: key_pair['creationDate']
81+
}
82+
end
5583

5684
def delete_ssh_key(key_pair_name)
5785
uri = URI("https://#{@ibm_region}.power-iaas.cloud.ibm.com/pcloud/v1/tenants/#{@ibm_tenant_id}/sshkeys/#{key_pair_name}")
@@ -88,6 +116,11 @@ def list_networks
88116
send_get_request(uri)
89117
end
90118

119+
def list_ssh_keys
120+
uri = URI("https://#{@ibm_region}.power-iaas.cloud.ibm.com/pcloud/v1/tenants/#{@ibm_tenant_id}/sshkeys")
121+
send_get_request(uri)
122+
end
123+
91124
def fetch_public_network_id(network_name)
92125
network_data = fetch_public_network_data(network_name)
93126
network_data['networkID']

0 commit comments

Comments
 (0)