From 5bff32a2221b760413af062cd54fc49e737929a4 Mon Sep 17 00:00:00 2001 From: Dylan Egan Date: Mon, 19 Dec 2011 13:20:53 -0800 Subject: [PATCH] [aws|elb] start working on policies. :v: --- lib/fog/aws/elb.rb | 25 +++++-- .../describe_load_balancer_policy_types.rb | 69 +++++++++++++++++++ .../describe_load_balancer_policy_types.rb | 68 ++++++++++++++++++ tests/aws/requests/elb/helper.rb | 18 +++++ tests/aws/requests/elb/policy_tests.rb | 4 ++ 5 files changed, 177 insertions(+), 7 deletions(-) create mode 100644 lib/fog/aws/parsers/elb/describe_load_balancer_policy_types.rb create mode 100644 lib/fog/aws/requests/elb/describe_load_balancer_policy_types.rb diff --git a/lib/fog/aws/elb.rb b/lib/fog/aws/elb.rb index 1a79eba72f..7f771b5fb6 100644 --- a/lib/fog/aws/elb.rb +++ b/lib/fog/aws/elb.rb @@ -4,9 +4,12 @@ module Fog module AWS class ELB < Fog::Service - class IdentifierTaken < Fog::Errors::Error; end - class InvalidInstance < Fog::Errors::Error; end - class Throttled < Fog::Errors::Error; end + class DuplicatePolicyName < Fog::Errors::Error; end + class IdentifierTaken < Fog::Errors::Error; end + class InvalidInstance < Fog::Errors::Error; end + class PolicyTypeNotFound < Fog::Errors::Error; end + class Throttled < Fog::Errors::Error; end + class TooManyPolicies < Fog::Errors::Error; end requires :aws_access_key_id, :aws_secret_access_key recognizes :region, :host, :path, :port, :scheme, :persistent @@ -23,6 +26,7 @@ class Throttled < Fog::Errors::Error; end request :deregister_instances_from_load_balancer request :describe_instance_health request :describe_load_balancers + request :describe_load_balancer_policy_types request :disable_availability_zones_for_load_balancer request :enable_availability_zones_for_load_balancer request :register_instances_with_load_balancer @@ -45,7 +49,8 @@ def self.data hash[region] = Hash.new do |region_hash, key| region_hash[key] = { :owner_id => owner_id, - :load_balancers => {} + :load_balancers => {}, + :policy_types => {} } end end @@ -150,7 +155,7 @@ def request(params) :host => @host, :path => @path, :port => @port, - :version => '2011-04-05' + :version => '2011-11-15' } ) @@ -168,14 +173,20 @@ def request(params) case match[1] when 'CertificateNotFound' raise Fog::AWS::IAM::NotFound.slurp(error, match[2]) - when 'LoadBalancerNotFound' - raise Fog::AWS::ELB::NotFound.slurp(error, match[2]) when 'DuplicateLoadBalancerName' raise Fog::AWS::ELB::IdentifierTaken.slurp(error, match[2]) + when 'DuplicatePolicyName' + raise Fog::AWS::ELB::DuplicatePolicyName.slurp(error, match[2]) when 'InvalidInstance' raise Fog::AWS::ELB::InvalidInstance.slurp(error, match[2]) + when 'LoadBalancerNotFound' + raise Fog::AWS::ELB::NotFound.slurp(error, match[2]) + when 'PolicyTypeNotFound' + raise Fog::AWS::ELB::PolicyTypeNotFound.slurp(error, match[2]) when 'Throttling' raise Fog::AWS::ELB::Throttled.slurp(error, match[2]) + when 'TooManyPolicies' + raise Fog::AWS::ELB::TooManyPolicies.slurp(error, match[2]) else raise end diff --git a/lib/fog/aws/parsers/elb/describe_load_balancer_policy_types.rb b/lib/fog/aws/parsers/elb/describe_load_balancer_policy_types.rb new file mode 100644 index 0000000000..788bf20930 --- /dev/null +++ b/lib/fog/aws/parsers/elb/describe_load_balancer_policy_types.rb @@ -0,0 +1,69 @@ +module Fog + module Parsers + module AWS + module ELB + + class DescribeLoadBalancerPolicyTypes < Fog::Parsers::Base + + def reset + reset_policy_type + reset_policy_attribute_type_description + @results = { 'PolicyTypeDescriptions' => [] } + @response = { 'DescribeLoadBalancerPolicyTypesResult' => {}, 'ResponseMetadata' => {} } + end + + def reset_policy_type + @policy_type = { 'Description' => '', 'PolicyAttributeTypeDescriptions' => [], 'PolicyTypeName' => '' } + end + + def reset_policy_attribute_type_description + @policy_attribute_type_description = { 'AttributeName' => '', 'AttributeValue' => '', 'Cardinality' => '', 'DefaultValue' => '', 'Description' => '' } + end + + def start_element(name, attrs = []) + super + case name + when 'PolicyAttributeTypeDescriptions' + @in_policy_attribute_types = true + end + end + + def end_element(name) + case name + when 'member' + if @in_policy_attribute_types + @policy_type['PolicyAttributeTypeDescriptions'] << @policy_attribute_type_description + reset_policy_attribute_type_description + elsif !@in_policy_attribute_types + @results['PolicyTypeDescriptions'] << @policy_type + end + + when 'Description' + if @in_policy_attribute_types + @policy_attribute_type_description[name] = value + else + @policy_type[name] = value + end + when 'PolicyTypeName' + @policy_type[name] = value + + when 'PolicyAttributeTypeDescriptions' + @in_policy_attribute_types = false + + when 'AttributeName', 'AttributeValue', 'Cardinality', 'DefaultValue' + @policy_attribute_type_description[name] = value + + when 'RequestId' + @response['ResponseMetadata'][name] = value + + when 'DescribeLoadBalancerPolicyTypesResponse' + @response['DescribeLoadBalancerPolicyTypesResult'] = @results + end + end + + end + + end + end + end +end diff --git a/lib/fog/aws/requests/elb/describe_load_balancer_policy_types.rb b/lib/fog/aws/requests/elb/describe_load_balancer_policy_types.rb new file mode 100644 index 0000000000..41e745d3e8 --- /dev/null +++ b/lib/fog/aws/requests/elb/describe_load_balancer_policy_types.rb @@ -0,0 +1,68 @@ +module Fog + module AWS + class ELB + class Real + + require 'fog/aws/parsers/elb/describe_load_balancer_policy_types' + + # Describe all or specified load balancer policy types + # + # ==== Parameters + # * type_name<~Array> - Specifies the name of the policy types. If no names are specified, returns the description of all the policy types defined by Elastic Load Balancing service. + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'ResponseMetadata'<~Hash>: + # * 'RequestId'<~String> - Id of request + # * 'DescribeLoadBalancerPolicyTypesResult'<~Hash>: + # * 'PolicyTypeDescriptions'<~Array> + # * 'Description'<~String> - A human-readable description of the policy type. + # * 'PolicyAttributeTypeDescriptions'<~Array> + # * 'AttributeName'<~String> - The name of the attribute associated with the policy type. + # * 'AttributeValue'<~String> - The type of attribute. For example, Boolean, Integer, etc. + # * 'Cardinality'<~String> - The cardinality of the attribute. + # * 'DefaultValue'<~String> - The default value of the attribute, if applicable. + # * 'Description'<~String> - A human-readable description of the attribute. + # * 'PolicyTypeName'<~String> - The name of the policy type. + def describe_load_balancer_policy_types(type_names = []) + params = Fog::AWS.indexed_param('PolicyTypeNames.member', [*type_names]) + request({ + 'Action' => 'DescribeLoadBalancerPolicyTypes', + :parser => Fog::Parsers::AWS::ELB::DescribeLoadBalancerPolicyTypes.new + }.merge!(params)) + end + + end + + class Mock + def describe_load_balancer_policy_types(type_names = []) + type_names = [*type_names] + policy_types = if type_names.any? + type_names.map do |type_name| + policy_type = self.data[:policy_types].find { |name, data| name == type_name } + raise Fog::AWS::ELB::PolicyTypeNotFound unless policy_type + policy_type[1].dup + end.compact + else + self.data[:policy_types].map { |policy_type, values| values.dup } + end + + response = Excon::Response.new + response.status = 200 + + response.body = { + 'ResponseMetadata' => { + 'RequestId' => Fog::AWS::Mock.request_id + }, + 'DescribeLoadBalancerPolicyTypesResult' => { + 'PolicyTypeDescriptions' => policy_types + } + } + + response + end + end + end + end +end diff --git a/tests/aws/requests/elb/helper.rb b/tests/aws/requests/elb/helper.rb index 7ee8dcfb18..315771395c 100644 --- a/tests/aws/requests/elb/helper.rb +++ b/tests/aws/requests/elb/helper.rb @@ -28,6 +28,24 @@ module Formats 'DescribeLoadBalancersResult' => {'LoadBalancerDescriptions' => [LOAD_BALANCER]} }) + POLICY_ATTRIBUTE_TYPE_DESCRIPTION = { + "AttributeName" => String, + "AttributeValue" => String, + "Cardinality" => String, + "DefaultValue" => String, + "Description" => String + } + + POLICY_TYPE = { + "Description" => String, + "PolicyAttributeTypeDescriptions" => [POLICY_ATTRIBUTE_TYPE_DESCRIPTION], + "PolicyTypeName" => String + } + + DESCRIBE_LOAD_BALANCER_POLICY_TYPES = BASIC.merge({ + 'DescribeLoadBalancerPolicyTypesResult' => { 'PolicyTypeDescriptions' => [POLICY_TYPE] } + }) + CONFIGURE_HEALTH_CHECK = BASIC.merge({ 'ConfigureHealthCheckResult' => {'HealthCheck' => { 'Target' => String, diff --git a/tests/aws/requests/elb/policy_tests.rb b/tests/aws/requests/elb/policy_tests.rb index 9c5f279813..1d26082fa8 100644 --- a/tests/aws/requests/elb/policy_tests.rb +++ b/tests/aws/requests/elb/policy_tests.rb @@ -21,6 +21,10 @@ Fog::AWS[:elb].create_lb_cookie_stickiness_policy(@load_balancer_id, policy).body end + tests("#describe_load_balancer_policy_types").formats(AWS::ELB::Formats::DESCRIBE_LOAD_BALANCER_POLICY_TYPES) do + Fog::AWS[:elb].describe_load_balancer_policy_types.body + end + tests("#delete_load_balancer_policy").formats(AWS::ELB::Formats::BASIC) do policy = 'fog-lb-no-expiry' Fog::AWS[:elb].delete_load_balancer_policy(@load_balancer_id, policy).body