Module: Aptible::CLI::Subcommands::Endpoints

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

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



8
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
# File 'lib/aptible/cli/subcommands/endpoints.rb', line 8

def self.included(thor)
  thor.class_eval do
    include Helpers::Operation
    include Helpers::AppOrDatabase
    include Helpers::Vhost

    database_create_flags = Helpers::Vhost::OptionSetBuilder.new do
      create!
      database!
    end

    desc 'endpoints:database:create DATABASE',
         'Create a Database Endpoint'
    database_create_flags.declare_options(self)
    define_method 'endpoints:database:create' do |handle|
      database = ensure_database(options.merge(db: handle))
      service = database.service
      raise Thor::Error, 'Database is not provisioned' if service.nil?

      vhost = service.create_vhost!(
        type: 'tcp',
        platform: 'elb',
        **database_create_flags.prepare(database., options)
      )

      provision_vhost_and_explain(service, vhost)
    end

    database_modify_flags = Helpers::Vhost::OptionSetBuilder.new do
      database!
    end

    desc 'endpoints:database:modify --database DATABASE ' \
         'ENDPOINT_HOSTNAME',
         'Modify a Database Endpoint'
    database_modify_flags.declare_options(self)
    define_method 'endpoints:database:modify' do |hostname|
      database = ensure_database(options.merge(db: options[:database]))
      vhost = find_vhost(each_service(database), hostname)
      vhost.update!(**database_modify_flags.prepare(database.,
                                                    options))
      provision_vhost_and_explain(vhost.service, vhost)
    end

    tcp_create_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      create!
      ports!
    end

    desc 'endpoints:tcp:create [--app APP] SERVICE',
         'Create an App TCP Endpoint'
    tcp_create_flags.declare_options(self)
    define_method 'endpoints:tcp:create' do |type|
      create_app_vhost(
        tcp_create_flags, options, type,
        type: 'tcp', platform: 'elb'
      )
    end

    tcp_modify_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      ports!
    end

    desc 'endpoints:tcp:modify [--app APP] ENDPOINT_HOSTNAME',
         'Modify an App TCP Endpoint'
    tcp_modify_flags.declare_options(self)
    define_method 'endpoints:tcp:modify' do |hostname|
      modify_app_vhost(tcp_modify_flags, options, hostname)
    end

    tls_create_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      create!
      ports!
      tls!
    end

    desc 'endpoints:tls:create [--app APP] SERVICE',
         'Create an App TLS Endpoint'
    tls_create_flags.declare_options(self)
    define_method 'endpoints:tls:create' do |type|
      create_app_vhost(
        tls_create_flags, options, type,
        type: 'tls', platform: 'elb'
      )
    end

    tls_modify_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      ports!
      tls!
    end

    desc 'endpoints:tls:modify [--app APP] ENDPOINT_HOSTNAME',
         'Modify an App TLS Endpoint'
    tls_modify_flags.declare_options(self)
    define_method 'endpoints:tls:modify' do |hostname|
      modify_app_vhost(tls_modify_flags, options, hostname)
    end

    grpc_create_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      create!
      port!
      tls!
    end

    desc 'endpoints:grpc:create [--app APP] SERVICE',
         'Create an App gRPC Endpoint'
    grpc_create_flags.declare_options(self)
    define_method 'endpoints:grpc:create' do |type|
      create_app_vhost(
        grpc_create_flags, options, type,
        type: 'grpc', platform: 'elb'
      )
    end

    grpc_modify_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      port!
      tls!
    end

    desc 'endpoints:grpc:modify [--app APP] ENDPOINT_HOSTNAME',
         'Modify an App gRPC Endpoint'
    grpc_modify_flags.declare_options(self)
    define_method 'endpoints:grpc:modify' do |hostname|
      modify_app_vhost(grpc_modify_flags, options, hostname)
    end

    https_create_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      create!
      port!
      tls!
    end

    desc 'endpoints:https:create [--app APP] SERVICE',
         'Create an App HTTPS Endpoint'
    https_create_flags.declare_options(self)
    define_method 'endpoints:https:create' do |type|
      create_app_vhost(
        https_create_flags, options, type,
        type: 'http', platform: 'alb'
      )
    end

    https_modify_flags = Helpers::Vhost::OptionSetBuilder.new do
      app!
      port!
      tls!
    end

    desc 'endpoints:https:modify [--app APP] ENDPOINT_HOSTNAME',
         'Modify an App HTTPS Endpoint'
    https_modify_flags.declare_options(self)
    define_method 'endpoints:https:modify' do |hostname|
      modify_app_vhost(https_modify_flags, options, hostname)
    end

    desc 'endpoints:list [--app APP | --database DATABASE]',
         'List Endpoints for an App or Database'
    app_or_database_options
    define_method 'endpoints:list' do
      resource = ensure_app_or_database(options)

      Formatter.render(Renderer.current) do |root|
        root.list do |list|
          each_service(resource) do |service|
            service.each_vhost do |vhost|
              list.object do |node|
                ResourceFormatter.inject_vhost(node, vhost, service)
              end
            end
          end
        end
      end
    end

    desc 'endpoints:deprovision [--app APP | --database DATABASE] ' \
         'ENDPOINT_HOSTNAME', \
         'Deprovision an App or Database Endpoint'
    app_or_database_options
    define_method 'endpoints:deprovision' do |hostname|
      resource = ensure_app_or_database(options)
      vhost = find_vhost(each_service(resource), hostname)
      op = vhost.create_operation!(type: 'deprovision')
      attach_to_operation_logs(op)
    end

    desc 'endpoints:renew [--app APP] ENDPOINT_HOSTNAME', \
         'Renew an App Managed TLS Endpoint'
    long_desc <<-LONGDESC
      Use this command for the initial activation of an App Managed
      TLS Endpoint.

      WARNING: review the documentation on rate limits before using
      this command automatically
      (https://www.aptible.com/docs/core-concepts/apps/connecting-to-apps/app-endpoints/managed-tls#rate-limits).
    LONGDESC
    app_options
    define_method 'endpoints:renew' do |hostname|
      app = ensure_app(options)
      vhost = find_vhost(app.each_service, hostname)
      op = vhost.create_operation!(type: 'renew')
      attach_to_operation_logs(op)
    end

    no_commands do
      def create_app_vhost(flags, options, process_type, **attrs)
        service = ensure_service(options, process_type)
        vhost = service.create_vhost!(
          **flags.prepare(service., options),
          **attrs
        )
        provision_vhost_and_explain(service, vhost)
      end

      def modify_app_vhost(flags, options, hostname)
        app = ensure_app(options)
        vhost = find_vhost(each_service(app), hostname)
        vhost.update!(**flags.prepare(vhost.service., options))
        provision_vhost_and_explain(vhost.service, vhost)
      end
    end
  end
end