1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[aws|elb] Create Listeners collection and model

This commit is contained in:
Aaron Suggs 2011-05-04 18:29:07 -04:00
parent c68ae344b3
commit 0154746576
6 changed files with 122 additions and 27 deletions

View file

@ -32,6 +32,8 @@ module Fog
collection :load_balancers collection :load_balancers
model :policy model :policy
collection :policies collection :policies
model :listener
collection :listeners
class Mock class Mock

View file

@ -0,0 +1,51 @@
require 'fog/core/model'
module Fog
module AWS
class ELB
class Listener < Fog::Model
attribute :policy_names, :aliases => 'PolicyNames'
attribute :instance_port, :aliases => 'InstancePort'
attribute :lb_port, :aliases => 'LoadBalancerPort'
attribute :protocol, :aliases => 'Protocol'
attribute :ssl_id, :aliases => 'SSLCertificateId'
def save
requires :load_balancer, :instance_port, :lb_port, :protocol
connection.create_load_balancer_listeners(load_balancer.id, [to_params])
reload
end
def destroy
requires :load_balancer, :lb_port
connection.delete_load_balancer_listeners(load_balancer.id, [lb_port])
reload
end
# Return the policy associated with this load balancer
def policy
load_balancer.policies.get(policy_names.first)
end
def reload
load_balancer.reload
end
def load_balancer
collection.load_balancer
end
def to_params
{
'InstancePort' => instance_port,
'LoadBalancerPort' => lb_port,
'Protocol' => protocol,
'SSLCertificateId' => ssl_id
}
end
end
end
end
end

View file

@ -0,0 +1,32 @@
require 'fog/aws/elb/models/listener'
module Fog
module AWS
class ELB
class Listeners < Fog::Collection
model Fog::AWS::ELB::Listener
attr_accessor :data, :load_balancer
def all
load(munged_data)
end
def get(lb_port)
all.detect{|listener| listener.lb_port == lb_port}
end
private
# Munge an array of ListernerDescription hashes like:
# {'Listener' => listener, 'PolicyNames' => []}
# to an array of listeners withi a PolicyNames key
def munged_data
data.map {|description|
description['Listener'].merge('PolicyNames' => description['PolicyNames'])
}
end
end
end
end
end

View file

@ -11,14 +11,14 @@ module Fog
attribute :dns_name, :aliases => 'DNSName' attribute :dns_name, :aliases => 'DNSName'
attribute :availability_zones, :aliases => 'AvailabilityZones' attribute :availability_zones, :aliases => 'AvailabilityZones'
attribute :instances, :aliases => 'Instances' attribute :instances, :aliases => 'Instances'
attribute :listener_descriptions, :aliases => 'ListenerDescriptions'
#attribute :policies, :aliases => 'Policies'
attr_accessor :listeners
def initialize(attributes={}) def initialize(attributes={})
attributes[:availability_zones] ||= %w(us-east-1a us-east-1b us-east-1c us-east-1d) attributes[:availability_zones] ||= %w(us-east-1a us-east-1b us-east-1c us-east-1d)
attributes[:listeners] ||= [{'LoadBalancerPort' => 80, 'InstancePort' => 80, 'Protocol' => 'http'}] attributes['ListenerDescriptions'] ||= [{
'Listener' => {'LoadBalancerPort' => 80, 'InstancePort' => 80, 'Protocol' => 'http'},
'PolicyNames' => []
}]
attributes['Policies'] ||= {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => []}
super super
end end
@ -67,22 +67,20 @@ module Fog
merge_attributes(:health_check => data) merge_attributes(:health_check => data)
end end
def create_listener(listener) def listeners
requires :id Fog::AWS::ELB::Listeners.new({
listener = [listener].flatten :data => attributes['ListenerDescriptions'],
connection.create_load_balancer_listeners(id, listener) :connection => connection,
reload :load_balancer => self
end })
def destroy_listener(port)
requires :id
port = [port].flatten
connection.delete_load_balancer_listeners(id, port)
reload
end end
def policies def policies
Fog::AWS::ELB::Policies.new(:data => attributes['Policies'], :connection => connection, :load_balancer => self) Fog::AWS::ELB::Policies.new({
:data => attributes['Policies'],
:connection => connection,
:load_balancer => self
})
end end
def set_listener_policy(port, policy_name) def set_listener_policy(port, policy_name)
@ -106,7 +104,7 @@ module Fog
requires :listeners requires :listeners
requires :availability_zones requires :availability_zones
connection.create_load_balancer(availability_zones, id, listeners) connection.create_load_balancer(availability_zones, id, listeners.map{|l| l.to_params})
# reload instead of merge attributes b/c some attrs (like HealthCheck) # reload instead of merge attributes b/c some attrs (like HealthCheck)
# may be set, but only the DNS name is returned in the create_load_balance # may be set, but only the DNS name is returned in the create_load_balance
@ -116,7 +114,7 @@ module Fog
def reload def reload
super super
@instance_health, @listeners = nil @instance_health = nil
self self
end end

View file

@ -3,6 +3,7 @@ module Fog
module AWS module AWS
class ELB class ELB
class Policies < Fog::Collection class Policies < Fog::Collection
model Fog::AWS::ELB::Policy model Fog::AWS::ELB::Policy
attr_accessor :data, :load_balancer attr_accessor :data, :load_balancer

View file

@ -108,17 +108,28 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
tests('listeners') do tests('listeners') do
default_listener_description = [{"Listener"=>{"InstancePort"=>80, "Protocol"=>"HTTP", "LoadBalancerPort"=>80}, "PolicyNames"=>[]}] default_listener_description = [{"Listener"=>{"InstancePort"=>80, "Protocol"=>"HTTP", "LoadBalancerPort"=>80}, "PolicyNames"=>[]}]
returns(default_listener_description) { elb.listener_descriptions } tests('default') do
returns(1) { elb.listeners.size }
listener = elb.listeners.first
returns([80,80,'HTTP', []]) { [listener.instance_port, listener.lb_port, listener.protocol, listener.policy_names] }
end
tests('#get') do
returns(80) { elb.listeners.get(80).lb_port }
end
tests('create') do tests('create') do
new_listener = { 'InstancePort' => 443, 'LoadBalancerPort' => 443, 'Protocol' => 'TCP'} new_listener = { 'InstancePort' => 443, 'LoadBalancerPort' => 443, 'Protocol' => 'TCP'}
elb.create_listener(new_listener) elb.listeners.create(:instance_port => 443, :lb_port => 443, :protocol => 'TCP')
returns(true) { elb.listener_descriptions.include?({'Listener' => new_listener, 'PolicyNames' => []}) } returns(2) { elb.listeners.size }
returns(443) { elb.listeners.get(443).lb_port }
end end
tests('destroy') do tests('destroy') do
elb.destroy_listener(443) elb.listeners.get(443).destroy
returns(default_listener_description) { elb.listener_descriptions } returns(nil) { elb.listeners.get(443) }
end end
end end
@ -155,12 +166,12 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
tests('setting a listener policy') do tests('setting a listener policy') do
elb.set_listener_policy(80, lb_policy_id) elb.set_listener_policy(80, lb_policy_id)
returns([lb_policy_id]) { elb.listener_descriptions.first['PolicyNames'] } returns([lb_policy_id]) { elb.listeners.get(80).policy_names }
end end
tests('unsetting a listener policy') do tests('unsetting a listener policy') do
elb.unset_listener_policy(80) elb.unset_listener_policy(80)
returns([]) { elb.listener_descriptions.first['PolicyNames'] } returns([]) { elb.listeners.get(80).policy_names }
end end
tests('a malformed policy') do tests('a malformed policy') do