mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[aws|elb] create policies, describe policies, fix old mocking and yup. ✌️
This commit is contained in:
parent
5bff32a222
commit
2ab2e22737
14 changed files with 293 additions and 46 deletions
|
@ -20,12 +20,14 @@ module Fog
|
|||
request :create_lb_cookie_stickiness_policy
|
||||
request :create_load_balancer
|
||||
request :create_load_balancer_listeners
|
||||
request :create_load_balancer_policy
|
||||
request :delete_load_balancer
|
||||
request :delete_load_balancer_listeners
|
||||
request :delete_load_balancer_policy
|
||||
request :deregister_instances_from_load_balancer
|
||||
request :describe_instance_health
|
||||
request :describe_load_balancers
|
||||
request :describe_load_balancer_policies
|
||||
request :describe_load_balancer_policy_types
|
||||
request :disable_availability_zones_for_load_balancer
|
||||
request :enable_availability_zones_for_load_balancer
|
||||
|
@ -43,6 +45,8 @@ module Fog
|
|||
|
||||
class Mock
|
||||
|
||||
require 'fog/aws/elb/policy_types'
|
||||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, region|
|
||||
owner_id = Fog::AWS::Mock.owner_id
|
||||
|
@ -50,7 +54,7 @@ module Fog
|
|||
region_hash[key] = {
|
||||
:owner_id => owner_id,
|
||||
:load_balancers => {},
|
||||
:policy_types => {}
|
||||
:policy_types => Fog::AWS::ELB::Mock::POLICY_TYPES
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
35
lib/fog/aws/elb/policy_types.rb
Normal file
35
lib/fog/aws/elb/policy_types.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
class Fog::AWS::ELB::Mock
|
||||
POLICY_TYPES = [{
|
||||
"Description" => "",
|
||||
"PolicyAttributeTypeDescriptions" => [{
|
||||
"AttributeName"=>"CookieName",
|
||||
"AttributeType"=>"String",
|
||||
"Cardinality"=>"ONE",
|
||||
"DefaultValue"=>"",
|
||||
"Description"=>""
|
||||
}],
|
||||
"PolicyTypeName"=>"AppCookieStickinessPolicyType"
|
||||
},
|
||||
{
|
||||
"Description" => "",
|
||||
"PolicyAttributeTypeDescriptions" => [{
|
||||
"AttributeName"=>"CookieExpirationPeriod",
|
||||
"AttributeType"=>"String",
|
||||
"Cardinality"=>"ONE",
|
||||
"DefaultValue"=>"",
|
||||
"Description"=>""
|
||||
}],
|
||||
"PolicyTypeName"=>"LBCookieStickinessPolicyType"
|
||||
},
|
||||
{
|
||||
"Description" => "Policy containing a list of public keys to accept when authenticating the back-end server(s). This policy cannot be applied directly to back-end servers or listeners but must be part of a BackendServerAuthenticationPolicyType.",
|
||||
"PolicyAttributeTypeDescriptions" => [{
|
||||
"AttributeName"=>"PublicKey",
|
||||
"AttributeType"=>"String",
|
||||
"Cardinality"=>"ONE",
|
||||
"DefaultValue"=>"",
|
||||
"Description"=>""
|
||||
}],
|
||||
"PolicyTypeName"=>"PublicKeyPolicyType"
|
||||
}]
|
||||
end
|
64
lib/fog/aws/parsers/elb/describe_load_balancer_policies.rb
Normal file
64
lib/fog/aws/parsers/elb/describe_load_balancer_policies.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module AWS
|
||||
module ELB
|
||||
|
||||
class DescribeLoadBalancerPolicies < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
reset_policy
|
||||
reset_policy_attribute_description
|
||||
@results = { 'PolicyDescriptions' => [] }
|
||||
@response = { 'DescribeLoadBalancerPoliciesResult' => {}, 'ResponseMetadata' => {} }
|
||||
end
|
||||
|
||||
def reset_policy
|
||||
@policy = { 'PolicyAttributeDescriptions' => [], 'PolicyName' => '', 'PolicyTypeName' => '' }
|
||||
end
|
||||
|
||||
def reset_policy_attribute_description
|
||||
@policy_attribute_description = { 'AttributeName' => '', 'AttributeValue' => '' }
|
||||
end
|
||||
|
||||
def start_element(name, attrs = [])
|
||||
super
|
||||
case name
|
||||
when 'PolicyAttributeDescriptions'
|
||||
@in_policy_attributes = true
|
||||
end
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'member'
|
||||
if @in_policy_attributes
|
||||
@policy['PolicyAttributeDescriptions'] << @policy_attribute_description
|
||||
reset_policy_attribute_description
|
||||
elsif !@in_policy_attributes
|
||||
@results['PolicyDescriptions'] << @policy
|
||||
reset_policy
|
||||
end
|
||||
|
||||
when 'PolicyName', 'PolicyTypeName'
|
||||
@policy[name] = value
|
||||
|
||||
when 'PolicyAttributeDescriptions'
|
||||
@in_policy_attributes = false
|
||||
|
||||
when 'AttributeName', 'AttributeValue'
|
||||
@policy_attribute_description[name] = value
|
||||
|
||||
when 'RequestId'
|
||||
@response['ResponseMetadata'][name] = value
|
||||
|
||||
when 'DescribeLoadBalancerPoliciesResponse'
|
||||
@response['DescribeLoadBalancerPoliciesResult'] = @results
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -17,7 +17,7 @@ module Fog
|
|||
end
|
||||
|
||||
def reset_policy_attribute_type_description
|
||||
@policy_attribute_type_description = { 'AttributeName' => '', 'AttributeValue' => '', 'Cardinality' => '', 'DefaultValue' => '', 'Description' => '' }
|
||||
@policy_attribute_type_description = { 'AttributeName' => '', 'AttributeType' => '', 'Cardinality' => '', 'DefaultValue' => '', 'Description' => '' }
|
||||
end
|
||||
|
||||
def start_element(name, attrs = [])
|
||||
|
@ -36,6 +36,7 @@ module Fog
|
|||
reset_policy_attribute_type_description
|
||||
elsif !@in_policy_attribute_types
|
||||
@results['PolicyTypeDescriptions'] << @policy_type
|
||||
reset_policy_type
|
||||
end
|
||||
|
||||
when 'Description'
|
||||
|
@ -50,7 +51,7 @@ module Fog
|
|||
when 'PolicyAttributeTypeDescriptions'
|
||||
@in_policy_attribute_types = false
|
||||
|
||||
when 'AttributeName', 'AttributeValue', 'Cardinality', 'DefaultValue'
|
||||
when 'AttributeName', 'AttributeType', 'Cardinality', 'DefaultValue'
|
||||
@policy_attribute_type_description[name] = value
|
||||
|
||||
when 'RequestId'
|
||||
|
|
|
@ -31,22 +31,7 @@ module Fog
|
|||
|
||||
class Mock
|
||||
def create_app_cookie_stickiness_policy(lb_name, policy_name, cookie_name)
|
||||
if load_balancer = self.data[:load_balancers][lb_name]
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['Policies']['AppCookieStickinessPolicies'] << { 'CookieName' => cookie_name, 'PolicyName' => policy_name }
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
else
|
||||
raise Fog::AWS::ELB::NotFound
|
||||
end
|
||||
create_load_balancer_policy(lb_name, policy_name, 'AppCookieStickinessPolicyType', {'CookieName' => cookie_name})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,22 +33,7 @@ module Fog
|
|||
|
||||
class Mock
|
||||
def create_lb_cookie_stickiness_policy(lb_name, policy_name, cookie_expiration_period=nil)
|
||||
if load_balancer = self.data[:load_balancers][lb_name]
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['Policies']['LBCookieStickinessPolicies'] << { 'PolicyName' => policy_name, 'CookieExpirationPeriod' => cookie_expiration_period }
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
else
|
||||
raise Fog::AWS::ELB::NotFound
|
||||
end
|
||||
create_load_balancer_policy(lb_name, policy_name, 'LBCookieStickinessPolicyType', {'CookieExpirationPeriod' => cookie_expiration_period})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -82,10 +82,7 @@ module Fog
|
|||
'Instances' => [],
|
||||
'ListenerDescriptions' => listeners,
|
||||
'LoadBalancerName' => lb_name,
|
||||
'Policies' => {
|
||||
'LBCookieStickinessPolicies' => [],
|
||||
'AppCookieStickinessPolicies' => []
|
||||
},
|
||||
'Policies' => [],
|
||||
'SourceSecurityGroup' => {
|
||||
'GroupName' => '',
|
||||
'OwnerAlias' => ''
|
||||
|
|
79
lib/fog/aws/requests/elb/create_load_balancer_policy.rb
Normal file
79
lib/fog/aws/requests/elb/create_load_balancer_policy.rb
Normal file
|
@ -0,0 +1,79 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class ELB
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/elb/empty'
|
||||
|
||||
# Create Elastic Load Balancer Policy
|
||||
#
|
||||
# ==== Parameters
|
||||
# * lb_name<~String> - The name associated with the LoadBalancer for which the policy is being created. This name must be unique within the client AWS account.
|
||||
# * attributes<~Hash> - A list of attributes associated with the policy being created.
|
||||
# * 'AttributeName'<~String> - The name of the attribute associated with the policy.
|
||||
# * 'AttributeValue'<~String> - The value of the attribute associated with the policy.
|
||||
# * name<~String> - The name of the LoadBalancer policy being created. The name must be unique within the set of policies for this LoadBalancer.
|
||||
# * type_name<~String> - The name of the base policy type being used to create this policy. To get the list of policy types, use the DescribeLoadBalancerPolicyTypes action.
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'ResponseMetadata'<~Hash>:
|
||||
# * 'RequestId'<~String> - Id of request
|
||||
def create_load_balancer_policy(lb_name, name, type_name, attributes = {})
|
||||
params = {}
|
||||
|
||||
attribute_name = []
|
||||
attribute_value = []
|
||||
attributes.each do |name, value|
|
||||
attribute_name.push(name)
|
||||
attribute_value.push(value)
|
||||
end
|
||||
|
||||
params.merge!(Fog::AWS.indexed_param('PolicyAttributes.member.%d.AttributeName', attribute_name))
|
||||
params.merge!(Fog::AWS.indexed_param('PolicyAttributes.member.%d.AttributeValue', attribute_value))
|
||||
|
||||
request({
|
||||
'Action' => 'CreateLoadBalancerPolicy',
|
||||
'LoadBalancerName' => lb_name,
|
||||
'PolicyName' => name,
|
||||
'PolicyTypeName' => type_name,
|
||||
:parser => Fog::Parsers::AWS::ELB::Empty.new
|
||||
}.merge!(params))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def create_load_balancer_policy(lb_name, name, type_name, attributes = {})
|
||||
if load_balancer = self.data[:load_balancers][lb_name]
|
||||
raise Fog::AWS::IAM::DuplicatePolicyName if policy = load_balancer['Policies'].find { |p| p['PolicyName'] == name }
|
||||
raise Fog::AWS::IAM::PolicyTypeNotFound unless policy_type = self.data[:policy_types].find { |pt| pt['PolicyTypeName'] == type_name }
|
||||
|
||||
response = Excon::Response.new
|
||||
|
||||
attributes = attributes.map do |name, value|
|
||||
{"AttributeName" => name, "AttributeValue" => value}
|
||||
end
|
||||
|
||||
load_balancer['Policies'] << {
|
||||
'PolicyAttributeDescriptions' => attributes,
|
||||
'PolicyName' => name,
|
||||
'PolicyTypeName' => type_name
|
||||
}
|
||||
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
else
|
||||
raise Fog::AWS::ELB::NotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -33,8 +33,8 @@ module Fog
|
|||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['Policies'].each do |name, policies|
|
||||
policies.delete_if { |p| p['PolicyName'] == policy_name }
|
||||
load_balancer['Policies'].delete_if do |policy|
|
||||
policy['PolicyName'] == policy_name
|
||||
end
|
||||
|
||||
response.body = {
|
||||
|
|
71
lib/fog/aws/requests/elb/describe_load_balancer_policies.rb
Normal file
71
lib/fog/aws/requests/elb/describe_load_balancer_policies.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class ELB
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/elb/describe_load_balancer_policies'
|
||||
|
||||
# Describe all or specified load balancer policies
|
||||
#
|
||||
# ==== Parameters
|
||||
# * lb_name<~String> - The mnemonic name associated with the LoadBalancer. If no name is specified, the operation returns the attributes of either all the sample policies pre-defined by Elastic Load Balancing or the specified sample polices.
|
||||
# * names<~Array> - The names of LoadBalancer policies you've created or Elastic Load Balancing sample policy names.
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'ResponseMetadata'<~Hash>:
|
||||
# * 'RequestId'<~String> - Id of request
|
||||
# * 'DescribeLoadBalancerPoliciesResult'<~Hash>:
|
||||
# * 'PolicyDescriptions'<~Array>
|
||||
# * 'PolicyAttributeDescriptions'<~Array>
|
||||
# * 'AttributeName'<~String> - The name of the attribute associated with the policy.
|
||||
# * 'AttributeValue'<~String> - The value of the attribute associated with the policy.
|
||||
# * 'PolicyName'<~String> - The name mof the policy associated with the LoadBalancer.
|
||||
# * 'PolicyTypeName'<~String> - The name of the policy type.
|
||||
def describe_load_balancer_policies(lb_name = nil, names = [])
|
||||
params = Fog::AWS.indexed_param('PolicyNames.member', [*names])
|
||||
request({
|
||||
'Action' => 'DescribeLoadBalancerPolicies',
|
||||
'LoadBalancerName' => lb_name,
|
||||
:parser => Fog::Parsers::AWS::ELB::DescribeLoadBalancerPolicies.new
|
||||
}.merge!(params))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def describe_load_balancer_policies(lb_name = nil, names = [])
|
||||
if lb_name
|
||||
raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
|
||||
names = [*names]
|
||||
policies = if names.any?
|
||||
names.map do |name|
|
||||
raise Fog::AWS::ELB::PolicyNotFound unless policy = load_balancer['Policies'].find { |p| p['PolicyName'] == name }
|
||||
policy.dup
|
||||
end.compact
|
||||
else
|
||||
load_balancer['Policies']
|
||||
end
|
||||
else
|
||||
policies = []
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'DescribeLoadBalancerPoliciesResult' => {
|
||||
'PolicyDescriptions' => policies
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -40,12 +40,12 @@ module Fog
|
|||
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 }
|
||||
policy_type = self.data[:policy_types].find { |pt| pt['PolicyTypeName'] == 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 }
|
||||
self.data[:policy_types].map { |policy_type| policy_type.dup }
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
|
|
|
@ -57,7 +57,7 @@ module Fog
|
|||
raise Excon::Errors.status_error({:expects => 200}, response)
|
||||
end
|
||||
|
||||
unless load_balancer['Policies'].find { |name, policies| policies.find { |policy| policy['PolicyName'] == policy_names.first } }
|
||||
unless load_balancer['Policies'].find { |policy| policy['PolicyName'] == policy_names.first }
|
||||
response.status = 400
|
||||
response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>PolicyNotFound</Code><Message>One or more specified policies were not found.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestId></Response>"
|
||||
raise Excon::Errors.status_error({:expects => 200}, response)
|
||||
|
|
|
@ -28,9 +28,24 @@ class AWS
|
|||
'DescribeLoadBalancersResult' => {'LoadBalancerDescriptions' => [LOAD_BALANCER]}
|
||||
})
|
||||
|
||||
POLICY_ATTRIBUTE_DESCRIPTION = {
|
||||
"AttributeName" => String,
|
||||
"AttributeValue" => String
|
||||
}
|
||||
|
||||
POLICY = {
|
||||
"PolicyAttributeDescriptions" => [POLICY_ATTRIBUTE_DESCRIPTION],
|
||||
"PolicyName" => String,
|
||||
"PolicyTypeName" => String
|
||||
}
|
||||
|
||||
DESCRIBE_LOAD_BALANCER_POLICIES = BASIC.merge({
|
||||
'DescribeLoadBalancerPoliciesResult' => { 'PolicyDescriptions' => [POLICY] }
|
||||
})
|
||||
|
||||
POLICY_ATTRIBUTE_TYPE_DESCRIPTION = {
|
||||
"AttributeName" => String,
|
||||
"AttributeValue" => String,
|
||||
"AttributeType" => String,
|
||||
"Cardinality" => String,
|
||||
"DefaultValue" => String,
|
||||
"Description" => String
|
||||
|
|
|
@ -22,7 +22,18 @@ Shindo.tests('AWS::ELB | policy_tests', ['aws', 'elb']) do
|
|||
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
|
||||
@policy_types = Fog::AWS[:elb].describe_load_balancer_policy_types.body
|
||||
end
|
||||
|
||||
# puts @public_key_policy = @policy_types['DescribeLoadBalancerPolicyTypesResult']['PolicyTypeDescriptions'].find { |pt| pt['PolicyTypeName'] == 'PublicKeyPolicyType' }.inspect
|
||||
|
||||
tests("#create_load_balancer_policy").formats(AWS::ELB::Formats::BASIC) do
|
||||
policy = 'fog-policy'
|
||||
Fog::AWS[:elb].create_load_balancer_policy(@load_balancer_id, policy, 'PublicKeyPolicyType', {'PublicKey' => AWS::IAM::SERVER_CERT_PUBLIC_KEY}).body
|
||||
end
|
||||
|
||||
tests("#describe_load_balancer_policies").formats(AWS::ELB::Formats::DESCRIBE_LOAD_BALANCER_POLICIES) do
|
||||
Fog::AWS[:elb].describe_load_balancer_policies(@load_balancer_id, 'fog-policy').body
|
||||
end
|
||||
|
||||
tests("#delete_load_balancer_policy").formats(AWS::ELB::Formats::BASIC) do
|
||||
|
|
Loading…
Reference in a new issue