8
8
require_relative 'base_command'
9
9
require_relative '../services/aws_service'
10
10
require_relative '../services/gcp_service'
11
+ require_relative '../services/ibm_service'
11
12
require_relative '../services/configuration_reader'
12
13
13
14
# Command shows list all active instances on Cloud Providers
@@ -20,7 +21,7 @@ def self.synopsis
20
21
21
22
def show_help
22
23
info = <<-HELP
23
- List cloud instances command shows a list of active machines on GCP and AWS providers and the time they were created.
24
+ List cloud instances command shows a list of active machines on GCP, AWS and IBM Cloud providers and the time they were created.
24
25
25
26
Add the --json flag for the list_cloud_instances command to show the machine readable text.
26
27
Add the --hours NUMBER_OF_HOURS flag for displaying the machine older than this hours.
@@ -45,7 +46,7 @@ def execute
45
46
def show_list
46
47
@hidden_instances = read_hidden_instances
47
48
@number_instances = 0
48
- print_lists ( generate_aws_list , generate_gcp_list )
49
+ print_lists ( generate_aws_list , generate_gcp_list , generate_ibm_list )
49
50
end
50
51
51
52
def generate_aws_list
@@ -90,6 +91,21 @@ def generate_gcp_list
90
91
Result . ok ( all_instances )
91
92
end
92
93
94
+ def generate_ibm_list
95
+ return Result . error ( 'IBM Cloud service is not configured' ) unless @env . ibm_service . configured?
96
+
97
+ all_instances = @env . ibm_service . instances_list_with_time
98
+ return Result . error ( 'No instances were found in IBM Cloud services' ) if all_instances . empty?
99
+
100
+ all_instances . each do |instance |
101
+ instance [ :launch_time ] = DateTime . parse ( instance [ :launch_time ] ) . new_offset ( 0.0 / 24 )
102
+ end
103
+ all_instances = select_by_time ( all_instances ) unless @env . hours . nil?
104
+ all_instances = time_to_string ( all_instances )
105
+ @number_instances += all_instances . size
106
+ Result . ok ( all_instances )
107
+ end
108
+
93
109
def select_by_time ( instances )
94
110
new_instances = instances
95
111
if @env . hours . to_i <= 0
@@ -108,26 +124,32 @@ def time_to_string(instances)
108
124
end
109
125
end
110
126
111
- def print_lists ( aws_list , gcp_list )
127
+ def print_lists ( aws_list , gcp_list , ibm_list )
112
128
if @env . json
113
- @ui . out ( in_json_format ( aws_list , gcp_list ) )
129
+ @ui . out ( in_json_format ( aws_list , gcp_list , ibm_list ) )
114
130
else
115
131
if aws_list . success?
116
132
@ui . info ( 'List all active instances on AWS:' )
117
- @ui . info ( "\n " + in_table_format ( aws_list . value , false ) )
133
+ @ui . info ( "\n " + in_table_format ( aws_list . value , false , 'aws' ) )
118
134
else
119
135
@ui . info ( aws_list . error )
120
136
end
121
137
if gcp_list . success?
122
138
@ui . info ( 'List all active instances on GCP:' )
123
- @ui . info ( "\n " + in_table_format ( gcp_list . value , true ) )
139
+ @ui . info ( "\n " + in_table_format ( gcp_list . value , true , 'gcp' ) )
124
140
else
125
141
@ui . info ( gcp_list . error )
126
142
end
143
+ if ibm_list . success?
144
+ @ui . info ( 'List all active instances on IBM Cloud:' )
145
+ @ui . info ( "\n " + in_table_format ( ibm_list . value , false , 'ibm' ) )
146
+ else
147
+ @ui . info ( ibm_list . error )
148
+ end
127
149
end
128
150
end
129
151
130
- def in_json_format ( aws_list , gcp_list )
152
+ def in_json_format ( aws_list , gcp_list , ibm_list )
131
153
aws_list_json = if aws_list . error?
132
154
{ aws : aws_list . error }
133
155
else
@@ -138,18 +160,25 @@ def in_json_format(aws_list, gcp_list)
138
160
else
139
161
{ gcp : gcp_list . value }
140
162
end
141
- JSON . generate ( aws_list_json . merge ( gcp_list_json ) )
163
+ ibm_list_json = if ibm_list . error?
164
+ { ibm : ibm_list . error }
165
+ else
166
+ { ibm : ibm_list . value }
167
+ end
168
+ JSON . generate ( aws_list_json . merge ( gcp_list_json , ibm_list_json ) )
142
169
end
143
170
144
- def in_table_format ( list , with_user_info )
171
+ def in_table_format ( list , with_user_info , provider )
145
172
return 'List empty' if list . empty?
146
173
147
174
header = [ 'Launch time' , 'Node name' ]
175
+ header . concat ( [ 'PVM Instance ID' ] ) if provider == 'ibm'
148
176
header . concat ( [ 'Zone' , 'Path' , 'User' ] ) if with_user_info
149
177
table = TTY ::Table . new ( header : header )
150
178
list . each do |instance |
151
179
info = [ instance [ :launch_time ] , instance [ :node_name ] ]
152
180
info . concat ( [ instance [ :zone ] , instance [ :path ] , instance [ :username ] ] ) if with_user_info
181
+ info . concat ( [ instance [ :instance_id ] ] ) if provider == 'ibm'
153
182
table << info
154
183
end
155
184
table . render ( :unicode )
0 commit comments