Module: Aptible::CLI::Helpers::LogDrain

Includes:
Token
Defined in:
lib/aptible/cli/helpers/log_drain.rb

Constant Summary

Constants included from Token

Token::TOKEN_ENV_VAR

Instance Method Summary collapse

Methods included from Token

#current_token_hash, #fetch_token, #save_token, #token_file

Methods included from ConfigPath

#aptible_config_path

Instance Method Details

#create_https_based_log_drain(handle, options, url_format_msg: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aptible/cli/helpers/log_drain.rb', line 21

def create_https_based_log_drain(handle, options, url_format_msg: nil)
   = ensure_environment(options)
  url = ensure_url(options, url_format_msg: url_format_msg)

  opts = {
    handle: handle,
    url: url,
    drain_apps: options[:drain_apps],
    drain_databases: options[:drain_databases],
    drain_ephemeral_sessions: options[:drain_ephemeral_sessions],
    drain_proxies: options[:drain_proxies],
    drain_type: :https_post
  }
  create_log_drain(, opts)
end

#create_log_drain(account, drain_opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/aptible/cli/helpers/log_drain.rb', line 7

def create_log_drain(, drain_opts)
  drain = .create_log_drain!(drain_opts)
  op = drain.create_operation(type: :provision)

  if op.errors.any?
    # NOTE: If we fail to provision the log drain, we should try and
    # clean it up immediately.
    drain.create_operation(type: :deprovision)
    raise Thor::Error, op.errors.full_messages.first
  end

  attach_to_operation_logs(op)
end

#create_syslog_based_log_drain(handle, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aptible/cli/helpers/log_drain.rb', line 37

def create_syslog_based_log_drain(handle, options)
   = ensure_environment(options)

  opts = {
    handle: handle,
    drain_host: options[:host],
    drain_port: options[:port],
    logging_token: options[:token],
    drain_apps: options[:drain_apps],
    drain_databases: options[:drain_databases],
    drain_ephemeral_sessions: options[:drain_ephemeral_sessions],
    drain_proxies: options[:drain_proxies],
    drain_type: :syslog_tls_tcp
  }
  create_log_drain(, opts)
end

#ensure_log_drain(account, handle) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aptible/cli/helpers/log_drain.rb', line 67

def ensure_log_drain(, handle)
  drains = .log_drains.select { |d| d.handle == handle }

  if drains.empty?
    raise Thor::Error, "No drain found with handle #{handle}"
  end

  # Log Drain handles are globally unique, so this is excessive
  unless drains.length == 1
    raise Thor::Error, "#{drains.length} drains found with handle "\
                       "#{handle}"
  end

  drains.first
end

#ensure_url(options, url_format_msg: nil) ⇒ Object

Raises:

  • (Thor::Error)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/aptible/cli/helpers/log_drain.rb', line 54

def ensure_url(options, url_format_msg: nil)
  msg = '--url is required.'
  msg = "#{msg} #{url_format_msg}" unless url_format_msg.nil?

  url = options[:url]
  raise Thor::Error, msg if url.nil?

  # API already does url validation, so I'm not going
  # to duplicate that logic here, even if it would
  # get us an error faster
  url
end