Class: Aptible::CLI::Helpers::Vhost::OptionSetBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aptible/cli/helpers/vhost/option_set_builder.rb

Constant Summary collapse

FLAGS =
%i(
  environment
  app
  database
  create
  tls
  ports
  port
  alb
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ OptionSetBuilder

Returns a new instance of OptionSetBuilder.



17
18
19
20
# File 'lib/aptible/cli/helpers/vhost/option_set_builder.rb', line 17

def initialize(&block)
  FLAGS.each { |f| instance_variable_set("@#{f}", false) }
  instance_exec(&block) if block
end

Instance Method Details

#declare_options(thor) ⇒ Object



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
# File 'lib/aptible/cli/helpers/vhost/option_set_builder.rb', line 22

def declare_options(thor)
  thor.instance_exec(self) do |builder|
    option :environment, aliases: '--env'

    if builder.database?
      option :database
    elsif builder.app?
      app_options

      if builder.create?
        option(
          :default_domain,
          type: :boolean,
          desc: 'Enable Default Domain on this Endpoint'
        )

      end

      if builder.ports?
        option(
          :ports,
          type: :array,
          desc: 'A list of ports to expose on this Endpoint'
        )
      end

      if builder.port?
        option(
          :port,
          type: :numeric,
          desc: 'A port to expose on this Endpoint'
        )
      end

      if builder.alb?
        option(
          :load_balancing_algorithm_type,
          type: :string,
          desc: 'The load balancing algorithm for this Endpoint. ' \
                'Valid options are round_robin, ' \
                'least_outstanding_requests, and ' \
                'weighted_random'
        )

        option(
          :shared,
          type: :boolean,
          desc: "Share this Endpoint's load balancer with other " \
                'Endpoints'
        )
      end
    end

    if builder.create?
      option(
        :internal,
        type: :boolean,
        desc: 'Restrict this Endpoint to internal traffic'
      )
    end

    option(
      :ip_whitelist,
      type: :array,
      desc: 'A list of IPv4 sources (addresses or CIDRs) to ' \
            'which to restrict traffic to this Endpoint'
    )

    unless builder.create?
      # Yes, it has to be a dash...
      # See: https://github.com/erikhuda/thor/pull/551
      option(
        :'no-ip_whitelist',
        type: :boolean,
        desc: 'Disable IP Whitelist'
      )
    end

    if builder.tls?
      option(
        :certificate_file,
        type: :string,
        desc: 'A file containing a certificate to use on this ' \
              'Endpoint'
      )
      option(
        :private_key_file,
        type: :string,
        desc: 'A file containing a private key to use on this ' \
              'Endpoint'
      )

      option(
        :managed_tls,
        type: :boolean,
        desc: 'Enable Managed TLS on this Endpoint'
      )

      option(
        :managed_tls_domain,
        desc: 'A domain to use for Managed TLS'
      )

      option(
        :certificate_fingerprint,
        type: :string,
        desc: 'The fingerprint of an existing Certificate to use ' \
              'on this Endpoint'
      )
    end
  end
end

#prepare(account, options) ⇒ Object



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
# File 'lib/aptible/cli/helpers/vhost/option_set_builder.rb', line 135

def prepare(, options)
  options = options.dup # We're going to delete keys here
  verify_option_conflicts(options)

  params = {}

  params[:ip_whitelist] = options.delete(:ip_whitelist) do
    create? ? [] : nil
  end

  if options.delete(:'no-ip_whitelist') { false }
    params[:ip_whitelist] = []
  end

  params[:container_port] = options.delete(:port) if port?

  if ports?
    raw_ports = options.delete(:ports) do
      create? ? [] : nil
    end

    if raw_ports
      params[:container_ports] = raw_ports.map do |p|
        begin
          Integer(p)
        rescue ArgumentError
          m = "Invalid port: #{p}"
          raise Thor::Error, m
        end
      end
    end
  end

  if app?
    params[:internal] = options.delete(:internal) do
      create? ? false : nil
    end

    params[:default] = options.delete(:default_domain) do
      create? ? false : nil
    end

    options.delete(:app)
  elsif database?
    params[:internal] = options.delete(:internal) do
      create? ? false : nil
    end

    options.delete(:database)
  else
    params[:internal] = false
  end

  process_tls(, options, params) if tls?

  if alb?
    lba_type = options.delete(:load_balancing_algorithm_type)
    if lba_type
      valid_types = %w(round_robin least_outstanding_requests
                       weighted_random)
      unless valid_types.include?(lba_type)
        e = "Invalid load balancing algorithm type: #{lba_type}. " \
            "Valid options are: #{valid_types.join(', ')}"
        raise Thor::Error, e
      end
      params[:load_balancing_algorithm_type] = lba_type
    end

    params[:shared] = options.delete(:shared)
  end

  options.delete(:environment)

  # NOTE: This is here to ensure that specs don't test for options
  # that are not declared. This is not expected to happen when using
  # this.
  raise "Unexpected options: #{options}" if options.any?

  params.delete_if { |_, v| v.nil? }
end