Skip to content

Commit e83e791

Browse files
Add load_balancing_algorithm_type configuration to HTTP(s) endpoints (#385)
* add load_balancing_algorithm_type * add load_balancing_algorithm_type * default round robin * add tests * don't default round_robin
1 parent a687233 commit e83e791

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

lib/aptible/cli/helpers/vhost/option_set_builder.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class OptionSetBuilder
1111
tls
1212
ports
1313
port
14+
alb
1415
).freeze
1516

1617
def initialize(&block)
@@ -51,6 +52,17 @@ def declare_options(thor)
5152
desc: 'A port to expose on this Endpoint'
5253
)
5354
end
55+
56+
if builder.alb?
57+
option(
58+
:load_balancing_algorithm_type,
59+
type: :string,
60+
desc: 'The load balancing algorithm for this Endpoint.' \
61+
'valid options are round_robin, ' \
62+
'least_outstanding_requests, and ' \
63+
'weighted_random'
64+
)
65+
end
5466
end
5567

5668
if builder.create?
@@ -168,6 +180,20 @@ def prepare(account, options)
168180

169181
process_tls(account, options, params) if tls?
170182

183+
if alb?
184+
lba_type = options.delete(:load_balancing_algorithm_type)
185+
if lba_type
186+
valid_types = %w(round_robin least_outstanding_requests
187+
weighted_random)
188+
unless valid_types.include?(lba_type)
189+
e = "Invalid load balancing algorithm type: #{lba_type}. " \
190+
"Valid options are: #{valid_types.join(', ')}"
191+
raise Thor::Error, e
192+
end
193+
params[:load_balancing_algorithm_type] = lba_type
194+
end
195+
end
196+
171197
options.delete(:environment)
172198

173199
# NOTE: This is here to ensure that specs don't test for options

lib/aptible/cli/resource_formatter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ def inject_vhost(node, vhost, service)
202202
port = vhost.container_port ? vhost.container_port : 'default'
203203
node.value('type', 'https')
204204
node.value('port', port)
205+
node.value('load_balancing_algorithm_type', vhost
206+
.load_balancing_algorithm_type)
205207
end
206208

207209
node.value('internal', vhost.internal)

lib/aptible/cli/subcommands/endpoints.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def self.included(thor)
159159
create!
160160
port!
161161
tls!
162+
alb!
162163
end
163164

164165
desc 'endpoints:https:create [--app APP] SERVICE',
@@ -177,6 +178,7 @@ def self.included(thor)
177178
app!
178179
port!
179180
tls!
181+
alb!
180182
end
181183

182184
desc 'endpoints:https:modify [--app APP] ENDPOINT_HOSTNAME',

spec/aptible/cli/resource_formatter_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def capture(m, *args)
2020
internal: false,
2121
ip_whitelist: [],
2222
default: false,
23-
acme: false
23+
acme: false,
24+
load_balancing_algorithm_type: 'least_outstanding_requests'
2425
)
2526

2627
expected = [
@@ -30,6 +31,7 @@ def capture(m, *args)
3031
"Created At: #{fmt_time(service.created_at)}",
3132
'Type: https',
3233
'Port: default',
34+
'Load Balancing Algorithm Type: least_outstanding_requests',
3335
'Internal: false',
3436
'IP Whitelist: all traffic',
3537
'Default Domain Enabled: false',

spec/aptible/cli/subcommands/endpoints_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ def stub_options(**opts)
486486
stub_options(port: 10)
487487
subject.send(m, 'web')
488488
end
489+
it 'creates an Endpoint with a load balancing algorithm' do
490+
expect_create_vhost(service,
491+
load_balancing_algorithm_type:
492+
'least_outstanding_requests')
493+
stub_options(load_balancing_algorithm_type:
494+
'least_outstanding_requests')
495+
subject.send(m, 'web')
496+
end
489497
end
490498

491499
describe 'endpoints:grpc:create' do
@@ -637,6 +645,14 @@ def stub_options(**opts)
637645
stub_options(port: 10)
638646
subject.send(m, v.external_host)
639647
end
648+
it 'allows updating the load balancing algorithm' do
649+
v = Fabricate(:vhost, service: service)
650+
expect_modify_vhost(v, load_balancing_algorithm_type:
651+
'least_outstanding_requests')
652+
stub_options(load_balancing_algorithm_type:
653+
'least_outstanding_requests')
654+
subject.send(m, v.external_host)
655+
end
640656
end
641657

642658
describe 'endpoints:grpc:modify' do

0 commit comments

Comments
 (0)