Module: Aptible::CLI::Subcommands::MetricDrain

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/metric_drain.rb

Constant Summary collapse

SITES =
{
  'US1' => 'https://app.datadoghq.com',
  'US3' => 'https://us3.datadoghq.com',
  'US5' => 'https://us5.datadoghq.com',
  'EU1' => 'https://app.datadoghq.eu',
  'US1-FED' => 'https://app.ddog-gov.com'
}.freeze
PATH =
'/api/v1/series'.freeze

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
134
135
136
137
138
139
140
141
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
# File 'lib/aptible/cli/subcommands/metric_drain.rb', line 14

def self.included(thor)
  thor.class_eval do
    include Helpers::Token
    include Helpers::Database
    include Helpers::MetricDrain

    desc 'metric_drain:list', 'List all Metric Drains'
    option :environment
    define_method 'metric_drain:list' do
      Formatter.render(Renderer.current) do |root|
        root.grouped_keyed_list(
          { 'environment' => 'handle' },
          'handle'
        ) do |node|
          scoped_environments(options).each do ||
            .metric_drains.each do |drain|
              node.object do |n|
                ResourceFormatter.inject_metric_drain(n, drain, )
              end
            end
          end
        end
      end
    end

    desc 'metric_drain:create:influxdb HANDLE '\
         '--db DATABASE_HANDLE --environment ENVIRONMENT',
         'Create an InfluxDB Metric Drain'
    option :db, type: :string
    option :environment

    define_method 'metric_drain:create:influxdb' do |handle|
       = ensure_environment(options)
      database = ensure_database(options)

      opts = {
        handle: handle,
        database_id: database.id,
        drain_type: :influxdb_database
      }

      create_metric_drain(, opts)
    end

    desc 'metric_drain:create:influxdb:custom HANDLE '\
         '--username USERNAME --password PASSWORD ' \
         '--url URL_INCLUDING_PORT ' \
         '--db INFLUX_DATABASE_NAME ' \
         '--environment ENVIRONMENT',
         'Create an InfluxDB Metric Drain'
    option :db, type: :string
    option :username, type: :string
    option :password, type: :string
    option :url, type: :string
    option :db, type: :string
    option :environment
    define_method 'metric_drain:create:influxdb:custom' do |handle|
       = ensure_environment(options)

      config = {
        address: options[:url],
        username: options[:username],
        password: options[:password],
        database: options[:db]
      }
      opts = {
        handle: handle,
        drain_configuration: config,
        drain_type: :influxdb
      }

      create_metric_drain(, opts)
    end

    desc 'metric_drain:create:influxdb:customv2 HANDLE '\
           '--org ORGANIZATION --token INFLUX_TOKEN ' \
           '--url URL_INCLUDING_PORT ' \
           '--bucket INFLUX_BUCKET_NAME ' \
           '--environment ENVIRONMENT',
         'Create an InfluxDB v2 Metric Drain'
    option :bucket, type: :string
    option :org, type: :string
    option :token, type: :string
    option :url, type: :string
    option :environment
    define_method 'metric_drain:create:influxdb:customv2' do |handle|
       = ensure_environment(options)

      config = {
        address: options[:url],
        org: options[:org],
        authToken: options[:token],
        bucket: options[:bucket]
      }
      opts = {
        handle: handle,
        drain_configuration: config,
        drain_type: :influxdb2
      }

      create_metric_drain(, opts)
    end

    desc 'metric_drain:create:datadog HANDLE '\
         '--api_key DATADOG_API_KEY '\
         '--site DATADOG_SITE ' \
         '--environment ENVIRONMENT',
         'Create a Datadog Metric Drain'
    option :api_key, type: :string
    option :site, type: :string
    option :environment
    define_method 'metric_drain:create:datadog' do |handle|
       = ensure_environment(options)

      config = {
        api_key: options[:api_key]
      }
      unless options[:site].nil?
        site = SITES[options[:site]]

        unless site
          sites = SITES.keys.join(', ')
          raise Thor::Error, 'Invalid Datadog site. ' \
                             "Valid options are #{sites}"
        end

        config[:series_url] = site + PATH
      end
      opts = {
        handle: handle,
        drain_type: :datadog,
        drain_configuration: config
      }

      create_metric_drain(, opts)
    end

    desc 'metric_drain:deprovision HANDLE --environment ENVIRONMENT',
         'Deprovisions a Metric Drain'
    option :environment
    define_method 'metric_drain:deprovision' do |handle|
       = ensure_environment(options)
      drain = ensure_metric_drain(, handle)
      op = drain.create_operation(type: :deprovision)
      begin
        attach_to_operation_logs(op)
      rescue HyperResource::ClientError => e
        # A 404 here means that the operation completed successfully,
        # and was removed faster than attach_to_operation_logs
        # could attach to the logs.
        raise if e.response.status != 404
      end
    end
  end
end