From c68ae344b3341761946e38d4838e8f5be11378d7 Mon Sep 17 00:00:00 2001 From: Aaron Suggs Date: Mon, 2 May 2011 18:17:19 -0400 Subject: [PATCH] [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 --- lib/fog/aws/elb.rb | 2 + lib/fog/aws/elb/models/load_balancer.rb | 21 ++-------- lib/fog/aws/elb/models/policies.rb | 39 +++++++++++++++++++ lib/fog/aws/elb/models/policy.rb | 52 +++++++++++++++++++++++++ tests/aws/requests/elb/model_tests.rb | 43 ++++++++++++-------- 5 files changed, 124 insertions(+), 33 deletions(-) create mode 100644 lib/fog/aws/elb/models/policies.rb create mode 100644 lib/fog/aws/elb/models/policy.rb diff --git a/lib/fog/aws/elb.rb b/lib/fog/aws/elb.rb index 7eeb7d544..7d0afacf9 100644 --- a/lib/fog/aws/elb.rb +++ b/lib/fog/aws/elb.rb @@ -30,6 +30,8 @@ module Fog model_path 'fog/aws/elb/models' model :load_balancer collection :load_balancers + model :policy + collection :policies class Mock diff --git a/lib/fog/aws/elb/models/load_balancer.rb b/lib/fog/aws/elb/models/load_balancer.rb index bcba38b45..de5e985ee 100644 --- a/lib/fog/aws/elb/models/load_balancer.rb +++ b/lib/fog/aws/elb/models/load_balancer.rb @@ -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 diff --git a/lib/fog/aws/elb/models/policies.rb b/lib/fog/aws/elb/models/policies.rb new file mode 100644 index 000000000..eee1f37e6 --- /dev/null +++ b/lib/fog/aws/elb/models/policies.rb @@ -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 + diff --git a/lib/fog/aws/elb/models/policy.rb b/lib/fog/aws/elb/models/policy.rb new file mode 100644 index 000000000..9ba10c1a1 --- /dev/null +++ b/lib/fog/aws/elb/models/policy.rb @@ -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 diff --git a/tests/aws/requests/elb/model_tests.rb b/tests/aws/requests/elb/model_tests.rb index 43ebf86ae..45d815ab7 100644 --- a/tests/aws/requests/elb/model_tests.rb +++ b/tests/aws/requests/elb/model_tests.rb @@ -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