Module: Aptible::CLI::ResourceFormatter

Extended by:
Helpers::DateHelpers
Defined in:
lib/aptible/cli/resource_formatter.rb

Constant Summary collapse

NO_NESTING =
Object.new.freeze

Class Method Summary collapse

Methods included from Helpers::DateHelpers

utc_date, utc_datetime, utc_string

Class Method Details

.inject_account(node, account, include_stack = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aptible/cli/resource_formatter.rb', line 70

def (node, , include_stack = false)
  node.value('id', .id)
  node.value('handle', .handle)
  node.value('created_at', .created_at)

  if include_stack && .stack
    node.keyed_object('stack', 'name') do |n|
      n.value('name', .stack.name)
      n.value('id', .stack.id)
      n.value('region', .stack.region)
      n.value(
        'outbound_ip_addresses',
        .stack.outbound_ip_addresses
      )
    end
  end
end

.inject_app(node, app, account, setting = nil, include_services: true) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/aptible/cli/resource_formatter.rb', line 98

def inject_app(node, app, , setting = nil,
               include_services: true)
  node.value('id', app.id)
  node.value('handle', app.handle)
  node.value('created_at', app.created_at)

  (node, )

  node.value('status', app.status)
  node.value('git_remote', app.git_repo)

  if app.last_deploy_operation
    node.keyed_object('last_deploy_operation', 'id') do |n|
      inject_operation(n, app.last_deploy_operation)
    end
  end

  if include_services
    node.list('services') do |services_list|
      app.each_service do |service|
        services_list.object do |n|
          inject_service(n, service, NO_NESTING)
        end
      end
    end
  end

  unless setting.nil?
    node.value('docker_image',
               setting.settings['APTIBLE_DOCKER_IMAGE'])
    node.value('private_registry_username',
               setting.sensitive_settings['APTIBLE_PRIVATE_REGISTRY_USERNAME'])
    node.value('private_registry_password',
               setting.sensitive_settings['APTIBLE_PRIVATE_REGISTRY_PASSWORD'])
  end
end

.inject_backup(node, backup, include_db: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aptible/cli/resource_formatter.rb', line 9

def inject_backup(node, backup, include_db: false)
  bu_operation = begin
                   backup.created_from_operation
                 rescue HyperResource::ClientError
                   nil
                 end

  origin = if backup.manual && !backup.copied_from
             if bu_operation
               "manual, #{bu_operation.user_email}"
             else
               'manual, unknown'
             end
           elsif backup.manual && backup.copied_from
             'manual, copy'
           elsif backup.copied_from
             'automatic, copy'
           else
             'automatic'
           end
  description = "#{backup.id}: #{backup.created_at}, " \
                "#{backup.aws_region}, #{origin}"

  if include_db
    db = backup.database_with_deleted
    node.keyed_object('database', 'id') do |n|
      inject_deleted_database(n, db, backup.)
    end

    description = "#{description}, " \
                  "#{db.handle} deleted at #{db.deleted_at}"
  end

  node.value('id', backup.id)
  node.value('description', description)
  node.value('created_at', backup.created_at)
  node.value('region', backup.aws_region)
  node.value('size', backup.size)
  node.value('manual', backup.manual)

  if backup.copied_from
    node.keyed_object('copied_from', 'description') do |n|
      inject_backup(n, backup.copied_from)
    end
  end

  if bu_operation && !backup.copied_from
    node.keyed_object('created_from_operation', 'id') do |n|
      inject_operation(n, bu_operation)
    end
  end
end

.inject_backup_retention_policy(node, policy, account) ⇒ Object



318
319
320
321
322
323
324
325
326
327
# File 'lib/aptible/cli/resource_formatter.rb', line 318

def inject_backup_retention_policy(node, policy, )
  node.value('id', policy.id)
  node.value('daily', policy.daily)
  node.value('monthly', policy.monthly)
  node.value('yearly', policy.yearly)
  node.value('make_copy', policy.make_copy)
  node.value('keep_final', policy.keep_final)

  (node, )
end

.inject_credential(node, credential) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/aptible/cli/resource_formatter.rb', line 181

def inject_credential(node, credential)
  # TODO: Should this accept a DB for nesting? Maybe if we have any
  # callers that could benefit from it.
  node.value('type', credential.type)
  node.value('connection_url', credential.connection_url)
  node.value('default', credential.default)
end

.inject_database(node, database, account) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/aptible/cli/resource_formatter.rb', line 142

def inject_database(node, database, )
  # Some callers pass a database object with sensitive attributes already, others do not.
  # Avoid creating extra 'show' activity if we already have the needed info
  database = with_sensitive(database) if database.objects[:database_credentials].nil?

  node.value('id', database.id)
  node.value('handle', database.handle)
  node.value('created_at', database.created_at)

  node.value('type', database.type)
  node.value('version', database.database_image.version)
  node.value('status', database.status)

  node.value('connection_url', database.connection_url)

  node.list('credentials') do |creds_list|
    database.database_credentials.each do |cred|
      creds_list.object { |n| inject_credential(n, cred) }
    end
  end
  (node, )

  if database.disk
    node.value('disk_type', database.disk.ebs_volume_type)
    node.value('disk_size', database.disk.size)
    node.value('disk_modification_progress',
               database.disk.modification_progress)
    node.value('disk_modification_status', database.disk.status)
    node.value('disk_provisioned_iops', database.disk.baseline_iops)
  end

  if database.service
    node.value('container_size', \
               database.service.container_memory_limit_mb)
    node.value('container_profile', \
               database.service.instance_class.to_s[/[a-z]/])
  end
end

.inject_database_minimal(node, database, account) ⇒ Object



135
136
137
138
139
140
# File 'lib/aptible/cli/resource_formatter.rb', line 135

def inject_database_minimal(node, database, )
  node.value('id', database.id)
  node.value('handle', database.handle)
  node.value('created_at', database.created_at)
  (node, )
end

.inject_deleted_database(node, database, account) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/aptible/cli/resource_formatter.rb', line 62

def inject_deleted_database(node, database, )
  node.value('id', database.id)
  node.value('handle', database.handle)
  node.value('type', database.type)
  node.value('deleted_at', database.deleted_at)
  (node, )
end

.inject_log_drain(node, log_drain, account) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/aptible/cli/resource_formatter.rb', line 257

def inject_log_drain(node, log_drain, )
  node.value('id', log_drain.id)
  node.value('handle', log_drain.handle)
  node.value('drain_type', log_drain.drain_type)
  node.value('created_at', log_drain.created_at)
  node.value('drain_apps', log_drain.drain_apps)
  node.value('drain_databases', log_drain.drain_databases)
  node.value('drain_ephemeral_sessions',
             log_drain.drain_ephemeral_sessions)
  node.value('drain_proxies', log_drain.drain_proxies)

  # These can be either optional for the drain type,
  # or sensitive attributes we don't need to worry about
  # in text output
  optional_attrs = %w(drain_username drain_host drain_port url)
  optional_attrs.each do |attr|
    value = log_drain.attributes[attr]
    node.value(attr, value) unless value.nil?
  end

  (node, )
end

.inject_maintenance(node, command_prefix, maintenance_resource, account) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/aptible/cli/resource_formatter.rb', line 296

def inject_maintenance(
  node,
  command_prefix,
  maintenance_resource,
  
)
  node.value('id', maintenance_resource.id)
  raw_start, raw_end = maintenance_resource.maintenance_deadline
  window_start = utc_string(raw_start)
  window_end = utc_string(raw_end)
  label = "#{maintenance_resource.handle} between #{window_start} " \
          "and #{window_end}"
  restart_command = "#{command_prefix}" \
                    " #{maintenance_resource.handle}" \
                    " --environment #{.handle}"
  node.value('label', label)
  node.value('handle', maintenance_resource.handle)
  node.value('restart_command', restart_command)

  (node, )
end

.inject_metric_drain(node, metric_drain, account) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/aptible/cli/resource_formatter.rb', line 280

def inject_metric_drain(node, metric_drain, )
  node.value('id', metric_drain.id)
  node.value('handle', metric_drain.handle)
  node.value('drain_type', metric_drain.drain_type)
  node.value('created_at', metric_drain.created_at)

  # Sensitive attributes we don't need to worry about being missing in text output
  optional_attrs = %w(drain_configuration)
  optional_attrs.each do |attr|
    value = metric_drain.attributes[attr]
    node.value(attr, value) unless value.nil?
  end

  (node, )
end

.inject_operation(node, operation) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/aptible/cli/resource_formatter.rb', line 88

def inject_operation(node, operation)
  node.value('id', operation.id)
  node.value('status', operation.status)
  if %w(deploy rebuild).include?(operation.type)
    node.value('git_ref', operation.git_ref)
  end
  node.value('user_email', operation.user_email)
  node.value('created_at', operation.created_at)
end

.inject_service(node, service, app) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/aptible/cli/resource_formatter.rb', line 189

def inject_service(node, service, app)
  node.value('id', service.id)
  node.value('service', service.process_type)
  node.value('created_at', service.created_at)

  node.value('command', service.command || 'CMD')
  node.value('container_count', service.container_count)
  node.value('container_size', service.container_memory_limit_mb)
  node.value('container_profile', service.instance_class.to_s[/[a-z]/])

  attach_app(node, app)
end

.inject_service_sizing_policy(node, policy, service) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/aptible/cli/resource_formatter.rb', line 329

def inject_service_sizing_policy(node, policy, service)
  node.value('autoscaling_type', policy.autoscaling)
  node.value('metric_lookback_seconds', policy.metric_lookback_seconds)
  node.value('percentile', policy.percentile)
  node.value('post_scale_up_cooldown_seconds',
             policy.post_scale_up_cooldown_seconds)
  node.value('post_scale_down_cooldown_seconds',
             policy.post_scale_down_cooldown_seconds)
  node.value('post_release_cooldown_seconds',
             policy.post_release_cooldown_seconds)
  node.value('mem_cpu_ratio_r_threshold',
             policy.mem_cpu_ratio_r_threshold)
  node.value('mem_cpu_ratio_c_threshold',
             policy.mem_cpu_ratio_c_threshold)
  node.value('mem_scale_up_threshold', policy.mem_scale_up_threshold)
  node.value('mem_scale_down_threshold',
             policy.mem_scale_down_threshold)
  node.value('minimum_memory', policy.minimum_memory)
  node.value('maximum_memory', policy.maximum_memory)
  node.value('min_cpu_threshold', policy.min_cpu_threshold)
  node.value('max_cpu_threshold', policy.max_cpu_threshold)
  node.value('min_containers', policy.min_containers)
  node.value('max_containers', policy.max_containers)
  node.value('scale_up_step', policy.scale_up_step)
  node.value('scale_down_step', policy.scale_down_step)
  node.value('restart_free_scale', policy.use_horizontal_scale)

  attach_service(node, service)
end

.inject_vhost(node, vhost, service) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/aptible/cli/resource_formatter.rb', line 202

def inject_vhost(node, vhost, service)
  node.value('id', vhost.id)
  node.value('hostname', vhost.external_host)
  node.value('status', vhost.status)
  node.value('created_at', vhost.created_at)

  case vhost.type
  when 'tcp', 'tls'
    ports = if vhost.container_ports.any?
              vhost.container_ports.map(&:to_s).join(' ')
            else
              'all'
            end
    node.value('type', vhost.type)
    node.value('ports', ports)
  when 'http', 'http_proxy_protocol'
    port = vhost.container_port ? vhost.container_port : 'default'
    node.value('type', 'https')
    node.value('port', port)
    node.value('load_balancing_algorithm_type', vhost
                .load_balancing_algorithm_type || 'round_robin')
    node.value('shared', vhost.shared || 'false')
  end

  node.value('internal', vhost.internal)

  unless vhost.current_setting.nil?
    vhost.current_setting.settings.each do |k, v|
      node.value(k.downcase, v)
    end
  end

  ip_whitelist = if vhost.ip_whitelist.any?
                   vhost.ip_whitelist.join(' ')
                 else
                   'all traffic'
                 end
  node.value('ip_whitelist', ip_whitelist)

  node.value('default_domain_enabled', vhost.default)
  node.value('default_domain', vhost.virtual_domain) if vhost.default

  node.value('managed_tls_enabled', vhost.acme)
  if vhost.acme
    node.value('managed_tls_domain', vhost.user_domain)
    node.value(
      'managed_tls_dns_challenge_hostname',
      vhost.acme_dns_challenge_host
    )
    node.value('managed_tls_status', vhost.acme_status)
  end

  attach_service(node, service)
end