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
# 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

    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

    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
      (http://go.aptible.com/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