@@ -15,7 +15,7 @@ def self.synopsis
15
15
16
16
def show_help
17
17
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.
19
19
20
20
Add the --json flag to show the machine readable text.
21
21
Add the --hours NUMBER_OF_HOURS flag to display the resources older than this hours.
@@ -60,32 +60,40 @@ def execute
60
60
61
61
# Outputs the resources in a table format
62
62
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' ) } " )
65
66
@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' ) } " )
67
68
@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' ) } " )
68
71
end
69
72
70
73
# Fetches the list of resources and generates their description in format
71
74
# { instances: { gcp: Array, aws: Array }, disks: { gcp: Array, aws: Array }, key_pairs: Array, security_groups: Array }
72
75
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
74
78
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
76
81
{
77
82
instances : list_instances ,
78
83
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
81
88
}
82
89
end
83
90
84
91
def list_instances
85
92
@hidden_instances = read_hidden_instances
86
93
{
87
94
aws : list_aws_instances ,
88
- gcp : list_gcp_instances
95
+ gcp : list_gcp_instances ,
96
+ ibm : list_ibm_instances
89
97
}
90
98
end
91
99
@@ -98,6 +106,16 @@ def list_disks
98
106
}
99
107
end
100
108
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
+
101
119
# Renders a table with the disks that are not attached to any instance
102
120
def disks_table ( disks )
103
121
header = [ 'Disk name' , 'Creation time' , 'Provider' , 'Zone' ]
@@ -112,18 +130,35 @@ def disks_table(disks)
112
130
table . render ( :unicode )
113
131
end
114
132
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 )
117
135
header = [ 'Key name' , 'Creation time' ]
118
136
table = TTY ::Table . new ( header : header )
119
137
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
121
144
end
122
145
return 'No key pairs found' if table . empty?
123
146
124
147
table . render ( :unicode )
125
148
end
126
149
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
+
127
162
# Renders a table with the security groups that are not used by any instance
128
163
def security_groups_table ( security_groups )
129
164
header = [ 'Group ID' , 'Configuration ID' , 'Generation time' ]
@@ -194,6 +229,17 @@ def list_gcp_instances
194
229
all_instances
195
230
end
196
231
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
+
197
243
def select_by_time ( instances )
198
244
instances . select do |instance |
199
245
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)
209
255
210
256
# Renders the table with list of instances
211
257
# @param list {Array} list of instances
212
- def instances_table ( list )
258
+ # @param provider {String} cloud provider
259
+ def instances_table ( list , provider )
213
260
return 'No instances found' if list . empty?
214
261
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
216
269
table = TTY ::Table . new ( header : header )
217
270
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
225
287
table << info
226
288
end
227
289
table . render ( :unicode )
0 commit comments