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

[aws|elb] Create Policies collection and model

Adds methods like:

LoadBalancer#policies
LoadBalancer#policies.all
LoadBalancer#policies.get(id)
LoadBalancer#policies.create(attrs)
Policy#destroy
This commit is contained in:
Aaron Suggs 2011-05-02 18:17:19 -04:00
parent 0f820c0953
commit c68ae344b3
5 changed files with 124 additions and 33 deletions

View file

@ -30,6 +30,8 @@ module Fog
model_path 'fog/aws/elb/models'
model :load_balancer
collection :load_balancers
model :policy
collection :policies
class Mock

View file

@ -12,7 +12,7 @@ module Fog
attribute :availability_zones, :aliases => 'AvailabilityZones'
attribute :instances, :aliases => 'Instances'
attribute :listener_descriptions, :aliases => 'ListenerDescriptions'
attribute :policies, :aliases => 'Policies'
#attribute :policies, :aliases => 'Policies'
attr_accessor :listeners
@ -81,22 +81,8 @@ module Fog
reload
end
def create_app_policy(policy_name, cookie_name)
requires :id
connection.create_app_cookie_stickiness_policy(id, policy_name, cookie_name)
reload
end
def create_lb_policy(policy_name, expiration=nil)
requires :id
connection.create_lb_cookie_stickiness_policy(id, policy_name, expiration)
reload
end
def destroy_policy(policy_name)
requires :id
connection.delete_load_balancer_policy(id, policy_name)
reload
def policies
Fog::AWS::ELB::Policies.new(:data => attributes['Policies'], :connection => connection, :load_balancer => self)
end
def set_listener_policy(port, policy_name)
@ -131,6 +117,7 @@ module Fog
def reload
super
@instance_health, @listeners = nil
self
end
def destroy

View file

@ -0,0 +1,39 @@
require 'fog/aws/elb/models/policy'
module Fog
module AWS
class ELB
class Policies < Fog::Collection
model Fog::AWS::ELB::Policy
attr_accessor :data, :load_balancer
def all
load(munged_data)
end
def get(id)
all.detect{|policy| id == policy.id}
end
private
# Munge a hash like:
# {'LBCookieStickinessPolicies' => [policies...],
# 'AppCookieStickinessPolicies' => [policies...]}
# to a single array of policies with a cookie_stickiness value
def munged_data
munged_data = []
data['LBCookieStickinessPolicies'].each do |policy|
munged_data << policy.merge(:cookie_stickiness => :lb)
end
data['AppCookieStickinessPolicies'].each do |policy|
munged_data << policy.merge(:cookie_stickiness => :app)
end
munged_data
end
end
end
end
end

View file

@ -0,0 +1,52 @@
require 'fog/core/model'
module Fog
module AWS
class ELB
class Policy < Fog::Model
identity :id, :aliases => 'PolicyName'
attribute :cookie, :aliases => 'CookieName'
attribute :expiration, :aliases => 'CookieExpirationPeriod'
attr_accessor :cookie_stickiness # Either :app or :lb
def save
requires :id, :load_balancer, :cookie_stickiness
connection_method = nil
args = [load_balancer.id, id]
case cookie_stickiness
when :app
requires :cookie
method = :create_app_cookie_stickiness_policy
args << cookie
when :lb
method = :create_lb_cookie_stickiness_policy
args << expiration if expiration
else
raise ArgumentError.new('cookie_stickiness must be :app or :lb')
end
connection.send(method, *args)
reload
end
def destroy
requires :id, :load_balancer
connection.delete_load_balancer_policy(load_balancer.id, id)
reload
end
def reload
load_balancer.reload
end
def load_balancer
collection.load_balancer
end
end
end
end
end

View file

@ -17,6 +17,7 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
elb = AWS[:elb].load_balancers.create(:id => elb_id)
tests("dns names is set").returns(true) { elb.dns_name.is_a?(String) }
tests("created_at is set").returns(true) { (Time.now-10..Time.now).include? elb.created_at }
tests("policies is empty").returns([]) { elb.policies }
end
tests('all') do
@ -122,39 +123,49 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
end
tests('policies') do
app_policy = 'my-app-policy'
default_policies = {"LBCookieStickinessPolicies"=>[], "AppCookieStickinessPolicies"=>[]}
returns(default_policies) { elb.policies }
app_policy_id = 'my-app-policy'
tests 'are empty' do
returns([]) { elb.policies.to_a }
end
tests('#all') do
returns([]) { elb.policies.all.to_a }
end
tests('create app policy') do
elb.create_app_policy(app_policy, 'my-app-cookie')
returns([{'PolicyName' => app_policy, 'CookieName' => 'my-app-cookie'}]) do
elb.policies['AppCookieStickinessPolicies']
end
elb.policies.create(:id => app_policy_id, :cookie => 'my-app-cookie', :cookie_stickiness => :app)
returns(app_policy_id) { elb.policies.first.id }
end
tests('get policy') do
returns(app_policy_id) { elb.policies.get(app_policy_id).id }
end
tests('destroy app policy') do
elb.destroy_policy(app_policy)
returns(default_policies) { elb.policies }
elb.policies.first.destroy
returns([]) { elb.policies.to_a }
end
lb_policy = 'my-lb-policy'
lb_policy_id = 'my-lb-policy'
tests('create lb policy') do
elb.create_lb_policy(lb_policy, 600)
returns([{'PolicyName' => lb_policy, 'CookieExpirationPeriod' => 600}]) do
elb.policies['LBCookieStickinessPolicies']
end
elb.policies.create(:id => lb_policy_id, :expiration => 600, :cookie_stickiness => :lb)
returns(lb_policy_id) { elb.policies.first.id }
end
tests('setting a listener policy') do
elb.set_listener_policy(80, lb_policy)
returns([lb_policy]) { elb.listener_descriptions.first['PolicyNames'] }
elb.set_listener_policy(80, lb_policy_id)
returns([lb_policy_id]) { elb.listener_descriptions.first['PolicyNames'] }
end
tests('unsetting a listener policy') do
elb.unset_listener_policy(80)
returns([]) { elb.listener_descriptions.first['PolicyNames'] }
end
tests('a malformed policy') do
raises(ArgumentError) { elb.policies.create(:id => 'foo', :cookie_stickiness => 'invalid stickiness') }
end
end
tests('destroy') do