Skip to content

Commit

Permalink
style(elb): reduce branch conditions and increase readability
Browse files Browse the repository at this point in the history
  • Loading branch information
lanej committed Mar 14, 2018
1 parent 199cbf8 commit 770629e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
7 changes: 3 additions & 4 deletions lib/fog/aws/models/elb/listeners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ def all
end

def get(lb_port)
all.find{|listener| listener.lb_port == lb_port}
all.find { |listener| listener.lb_port == lb_port }
end

private

# Munge an array of ListenerDescription hashes like:
# {'Listener' => listener, 'PolicyNames' => []}
# to an array of listeners with a PolicyNames key
def munged_data
data.map {|description|
description['Listener'].merge('PolicyNames' => description['PolicyNames'])
}
data.map { |description| description['Listener'].merge('PolicyNames' => description['PolicyNames']) }
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/fog/aws/models/elb/load_balancers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class LoadBalancers < Fog::Collection
model Fog::AWS::ELB::LoadBalancer

# Creates a new load balancer
def initialize(attributes={})
def initialize(attributes = {})
super
end

def all
result = []
marker = nil
finished = false
while !finished
until finished
data = service.describe_load_balancers('Marker' => marker).body
result.concat(data['DescribeLoadBalancersResult']['LoadBalancerDescriptions'])
marker = data['DescribeLoadBalancersResult']['NextMarker']
Expand All @@ -24,10 +24,9 @@ def all
end

def get(identity)
if identity
data = service.describe_load_balancers('LoadBalancerNames' => identity).body['DescribeLoadBalancersResult']['LoadBalancerDescriptions'].first
new(data)
end
return unless identity
data = service.describe_load_balancers('LoadBalancerNames' => identity).body['DescribeLoadBalancersResult']['LoadBalancerDescriptions'].first
new(data)
rescue Fog::AWS::ELB::NotFound
nil
end
Expand Down
45 changes: 24 additions & 21 deletions lib/fog/aws/requests/elb/create_load_balancer_listeners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,37 @@ def create_load_balancer_listeners(lb_name, listeners)

class Mock
def create_load_balancer_listeners(lb_name, listeners)
if load_balancer = self.data[:load_balancers][lb_name]
response = Excon::Response.new
load_balancer = data[:load_balancers][lb_name]
raise Fog::AWS::ELB::NotFound unless load_balancer
response = Excon::Response.new

certificate_ids = Fog::AWS::IAM::Mock.data[@aws_access_key_id][:server_certificates].map {|n, c| c['Arn'] }
certificate_ids = Fog::AWS::IAM::Mock.data[@aws_access_key_id][:server_certificates].map { |_n, c| c['Arn'] }

listeners.each do |listener|
if listener['SSLCertificateId'] and !certificate_ids.include? listener['SSLCertificateId']
raise Fog::AWS::IAM::NotFound.new('CertificateNotFound')
end
listeners.each do |listener|
if listener['SSLCertificateId'] && !certificate_ids.include?(listener['SSLCertificateId'])
raise Fog::AWS::IAM::NotFound, 'CertificateNotFound'
end

if (%w( HTTP HTTPS).include?(listener['Protocol']) && !%w( HTTP HTTPS ).include?(listener['InstanceProtocol'])) ||
(%w( TCP SSL).include?(listener['Protocol']) && !%w( TCP SSL ).include?(listener['InstanceProtocol']))
raise Fog::AWS::ELB::ValidationError
end if listener['Protocol'] && listener['InstanceProtocol']
load_balancer['ListenerDescriptions'] << {'Listener' => listener, 'PolicyNames' => []}
if listener['Protocol'] && listener['InstanceProtocol']
if (
%w[HTTP HTTPS].include?(listener['Protocol']) && !%w[HTTP HTTPS].include?(listener['InstanceProtocol'])
) || (
%w[TCP SSL].include?(listener['Protocol']) && !%w[TCP SSL].include?(listener['InstanceProtocol'])
)
raise Fog::AWS::ELB::ValidationError
end
end
load_balancer['ListenerDescriptions'] << { 'Listener' => listener, 'PolicyNames' => [] }
end

response.status = 200
response.body = {
'ResponseMetadata' => {
'RequestId' => Fog::AWS::Mock.request_id
}
response.status = 200
response.body = {
'ResponseMetadata' => {
'RequestId' => Fog::AWS::Mock.request_id
}
}

response
else
raise Fog::AWS::ELB::NotFound
end
response
end
end
end
Expand Down

0 comments on commit 770629e

Please sign in to comment.