Class: Fog::AWS::ELB::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/elb.rb,
lib/fog/aws/elb/policy_types.rb,
lib/fog/aws/requests/elb/create_load_balancer.rb,
lib/fog/aws/requests/elb/delete_load_balancer.rb,
lib/fog/aws/requests/elb/configure_health_check.rb,
lib/fog/aws/requests/elb/describe_load_balancers.rb,
lib/fog/aws/requests/elb/describe_instance_health.rb,
lib/fog/aws/requests/elb/create_load_balancer_policy.rb,
lib/fog/aws/requests/elb/delete_load_balancer_policy.rb,
lib/fog/aws/requests/elb/create_load_balancer_listeners.rb,
lib/fog/aws/requests/elb/delete_load_balancer_listeners.rb,
lib/fog/aws/requests/elb/describe_load_balancer_policies.rb,
lib/fog/aws/requests/elb/create_lb_cookie_stickiness_policy.rb,
lib/fog/aws/requests/elb/create_app_cookie_stickiness_policy.rb,
lib/fog/aws/requests/elb/describe_load_balancer_policy_types.rb,
lib/fog/aws/requests/elb/register_instances_with_load_balancer.rb,
lib/fog/aws/requests/elb/set_load_balancer_policies_of_listener.rb,
lib/fog/aws/requests/elb/deregister_instances_from_load_balancer.rb,
lib/fog/aws/requests/elb/set_load_balancer_listener_ssl_certificate.rb,
lib/fog/aws/requests/elb/enable_availability_zones_for_load_balancer.rb,
lib/fog/aws/requests/elb/disable_availability_zones_for_load_balancer.rb

Constant Summary collapse

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"
}]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



74
75
76
77
78
79
80
81
82
# File 'lib/fog/aws/elb.rb', line 74

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]

  @region = options[:region] || 'us-east-1'

  unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
    raise ArgumentError, "Unknown region: #{@region.inspect}"
  end
end

Class Method Details

.dataObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/aws/elb.rb', line 53

def self.data
  @data ||= Hash.new do |hash, region|
    owner_id = Fog::AWS::Mock.owner_id
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :owner_id => owner_id,
        :load_balancers => {},
        :policy_types => Fog::AWS::ELB::Mock::POLICY_TYPES
      }
    end
  end
end

.dns_name(name, region) ⇒ Object



66
67
68
# File 'lib/fog/aws/elb.rb', line 66

def self.dns_name(name, region)
  "#{name}-#{Fog::Mock.random_hex(8)}.#{region}.elb.amazonaws.com"
end

.resetObject



70
71
72
# File 'lib/fog/aws/elb.rb', line 70

def self.reset
  @data = nil
end

Instance Method Details

#configure_health_check(lb_name, health_check) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aws/requests/elb/configure_health_check.rb', line 41

def configure_health_check(lb_name, health_check)
  if load_balancer = self.data[:load_balancers][lb_name]
    response = Excon::Response.new
    response.status = 200

    load_balancer['HealthCheck'] = health_check

    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      },
      'ConfigureHealthCheckResult' => {
        'HealthCheck' => load_balancer['HealthCheck']
      }
    }

    response
  else
    raise Fog::AWS::ELB::NotFound
  end
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/aws/requests/elb/create_app_cookie_stickiness_policy.rb', line 33

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 }

    create_load_balancer_policy(lb_name, policy_name, 'AppCookieStickinessPolicyType', {'CookieName' => cookie_name})

    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      }
    }

    response
  else
    raise Fog::AWS::ELB::NotFound
  end
end


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/aws/requests/elb/create_lb_cookie_stickiness_policy.rb', line 35

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'] << { 'CookieExpirationPeriod' => cookie_expiration_period, 'PolicyName' => policy_name }

    create_load_balancer_policy(lb_name, policy_name, 'LBCookieStickinessPolicyType', {'CookieExpirationPeriod' => cookie_expiration_period})

    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      }
    }

    response
  else
    raise Fog::AWS::ELB::NotFound
  end
end

#create_load_balancer(availability_zones, lb_name, listeners = []) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fog/aws/requests/elb/create_load_balancer.rb', line 57

def create_load_balancer(availability_zones, lb_name, listeners = [])
  response = Excon::Response.new
  response.status = 200

  raise Fog::AWS::ELB::IdentifierTaken if self.data[:load_balancers].has_key? lb_name

  certificate_ids = Fog::AWS::IAM::Mock.data[@aws_access_key_id][:server_certificates].map {|n, c| c['Arn'] }

  listeners = [*listeners].map do |listener|
    if listener['SSLCertificateId'] and !certificate_ids.include? listener['SSLCertificateId']
      raise Fog::AWS::IAM::NotFound.new('CertificateNotFound')
    end
    {'Listener' => listener, 'PolicyNames' => []}
  end

  dns_name = Fog::AWS::ELB::Mock.dns_name(lb_name, @region)
  self.data[:load_balancers][lb_name] = {
    'AvailabilityZones' => availability_zones,
    'CanonicalHostedZoneName' => '',
    'CanonicalHostedZoneNameID' => '',
    'CreatedTime' => Time.now,
    'DNSName' => dns_name,
    'HealthCheck' => {
      'HealthyThreshold' => 10,
      'Timeout' => 5,
      'UnhealthyThreshold' => 2,
      'Interval' => 30,
      'Target' => 'TCP:80'
    },
    'Instances' => [],
    'ListenerDescriptions' => listeners,
    'LoadBalancerName' => lb_name,
    'Policies' => {
      'AppCookieStickinessPolicies' => [],
      'LBCookieStickinessPolicies' => [],
      'Proper' => []
    },
    'SourceSecurityGroup' => {
      'GroupName' => '',
      'OwnerAlias' => ''
    }
  }

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'CreateLoadBalancerResult' => {
      'DNSName' => dns_name
    }
  }

  response
end

#create_load_balancer_listeners(lb_name, listeners) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/aws/requests/elb/create_load_balancer_listeners.rb', line 55

def create_load_balancer_listeners(lb_name, listeners)
  if load_balancer = self.data[:load_balancers][lb_name]
    response = Excon::Response.new

    certificate_ids = Fog::AWS::IAM::Mock.data[@aws_access_key_id][:server_certificates].map {|n, c| c['Arn'] }

    listeners.each do |listener|
      if listener['SSLCertificateId'] and !certificate_ids.include? listener['SSLCertificateId']
        raise Fog::AWS::IAM::NotFound.new('CertificateNotFound')
      end

      if (%w( HTTP HTTPS).include?(listener['Protocol']) && !%w( HTTP HTTPS ).include?(listener['InstanceProtocol'])) ||
          (%w( TCP SSL).include?(listener['Protocol']) && !%w( TCP SSL ).include?(listener['InstanceProtocol']))
        raise Fog::AWS::ELB::ValidationError
      end if listener['Protocol'] && listener['InstanceProtocol']
      load_balancer['ListenerDescriptions'] << {'Listener' => listener, 'PolicyNames' => []}
    end

    response.status = 200
    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      }
    }

    response
  else
    raise Fog::AWS::ELB::NotFound
  end
end

#create_load_balancer_policy(lb_name, name, type_name, attributes = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/aws/requests/elb/create_load_balancer_policy.rb', line 47

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']['Proper'].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 |key, value|
      {"AttributeName" => key, "AttributeValue" => value.to_s}
    end

    load_balancer['Policies']['Proper'] << {
      '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

#dataObject



84
85
86
# File 'lib/fog/aws/elb.rb', line 84

def data
  self.class.data[@region][@aws_access_key_id]
end

#delete_load_balancer(lb_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/elb/delete_load_balancer.rb', line 33

def delete_load_balancer(lb_name)
  response = Excon::Response.new
  response.status = 200

  self.data[:load_balancers].delete(lb_name)

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DeleteLoadBalancerResult' => nil
  }

  response
end

#delete_load_balancer_listeners(lb_name, load_balancer_ports) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/aws/requests/elb/delete_load_balancer_listeners.rb', line 31

def delete_load_balancer_listeners(lb_name, load_balancer_ports)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]

  response = Excon::Response.new
  response.status = 200

  load_balancer['ListenerDescriptions'].delete_if { |listener| load_balancer_ports.include? listener['Listener']['LoadBalancerPort'] }

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }

  response
end

#delete_load_balancer_policy(lb_name, policy_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/aws/requests/elb/delete_load_balancer_policy.rb', line 31

def delete_load_balancer_policy(lb_name, policy_name)
  if load_balancer = self.data[:load_balancers][lb_name]
    response = Excon::Response.new
    response.status = 200

    load_balancer['Policies'].each do |name, policies|
      policies.delete_if { |policy| policy['PolicyName'] == policy_name }
    end

    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      }
    }

    response
  else
    raise Fog::AWS::ELB::NotFound
  end
end

#deregister_instances_from_load_balancer(instance_ids, lb_name) ⇒ Object Also known as: deregister_instances



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/aws/requests/elb/deregister_instances_from_load_balancer.rb', line 36

def deregister_instances_from_load_balancer(instance_ids, lb_name)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
  instance_ids = [*instance_ids]
  instance_ids.each do |instance|
    raise Fog::AWS::ELB::InvalidInstance unless Fog::Compute::AWS::Mock.data[@region][@aws_access_key_id][:instances][instance]
  end

  response = Excon::Response.new
  response.status = 200

  load_balancer['Instances'].delete_if { |i| instance_ids.include? i['InstanceId'] }

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DeregisterInstancesFromLoadBalancerResult' => {
      'Instances' => load_balancer['Instances'].dup
    }
  }

  response
end

#describe_instance_health(lb_name, instance_ids = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/aws/requests/elb/describe_instance_health.rb', line 37

def describe_instance_health(lb_name, instance_ids = [])
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]

  instance_ids = [*instance_ids]
  instance_ids = load_balancer['Instances'].collect { |i| i['InstanceId'] } unless instance_ids.any?
  data = instance_ids.map do |id|
    unless Fog::Compute::AWS::Mock.data[@region][@aws_access_key_id][:instances][id]
      raise Fog::AWS::ELB::InvalidInstance
    end

    {
      'Description' => "",
      'InstanceId' => id,
      'ReasonCode' => "",
      'State' => 'OutOfService'
    }
  end

  response = Excon::Response.new
  response.status = 200

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DescribeInstanceHealthResult' => {
      'InstanceStates' => data
    }
  }

  response
end

#describe_load_balancer_policies(lb_name = nil, names = []) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/aws/requests/elb/describe_load_balancer_policies.rb', line 38

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']['Proper'].find { |p| p['PolicyName'] == name }
        policy.dup
      end.compact
    else
      load_balancer['Policies']['Proper']
    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

#describe_load_balancer_policy_types(type_names = []) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/aws/requests/elb/describe_load_balancer_policy_types.rb', line 39

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 { |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| policy_type.dup }
  end

  response = Excon::Response.new
  response.status = 200

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DescribeLoadBalancerPolicyTypesResult' => {
      'PolicyTypeDescriptions' => policy_types
    }
  }

  response
end

#describe_load_balancers(lb_names = []) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fog/aws/requests/elb/describe_load_balancers.rb', line 56

def describe_load_balancers(lb_names = [])
  lb_names = [*lb_names]
  load_balancers = if lb_names.any?
    lb_names.map do |lb_name|
      lb = self.data[:load_balancers].find { |name, data| name == lb_name }
      raise Fog::AWS::ELB::NotFound unless lb
      lb[1].dup
    end.compact
  else
    self.data[:load_balancers].map { |lb, values| values.dup }
  end

  response = Excon::Response.new
  response.status = 200

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DescribeLoadBalancersResult' => {
      'LoadBalancerDescriptions' => load_balancers.map do |lb|
        lb['Instances'] = lb['Instances'].map { |i| i['InstanceId'] }
        lb['Policies'] = lb['Policies'].reject { |name, policies| name == 'Proper' }
        lb
      end
    }
  }

  response
end

#disable_availability_zones_for_load_balancer(availability_zones, lb_name) ⇒ Object Also known as: disable_zones



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/requests/elb/disable_availability_zones_for_load_balancer.rb', line 35

def disable_availability_zones_for_load_balancer(availability_zones, lb_name)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]

  response = Excon::Response.new
  response.status = 200

  load_balancer['AvailabilityZones'].delete_if { |az| availability_zones.include? az }

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DisableAvailabilityZonesForLoadBalancerResult' => {
      'AvailabilityZones' => load_balancer['AvailabilityZones']
    }
  }

  response
end

#enable_availability_zones_for_load_balancer(availability_zones, lb_name) ⇒ Object Also known as: enable_zones



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/aws/requests/elb/enable_availability_zones_for_load_balancer.rb', line 36

def enable_availability_zones_for_load_balancer(availability_zones, lb_name)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]

  response = Excon::Response.new
  response.status = 200

  load_balancer['AvailabilityZones'] << availability_zones
  load_balancer['AvailabilityZones'].flatten!.uniq!

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'EnableAvailabilityZonesForLoadBalancerResult' => {
      'AvailabilityZones' => load_balancer['AvailabilityZones']
    }
  }

  response
end

#register_instances_with_load_balancer(instance_ids, lb_name) ⇒ Object Also known as: register_instances



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fog/aws/requests/elb/register_instances_with_load_balancer.rb', line 36

def register_instances_with_load_balancer(instance_ids, lb_name)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
  instance_ids = [*instance_ids]
  instances = instance_ids.map do |instance|
    raise Fog::AWS::ELB::InvalidInstance unless Fog::Compute::AWS::Mock.data[@region][@aws_access_key_id][:instances][instance]
    {'InstanceId' => instance}
  end

  response = Excon::Response.new
  response.status = 200

  load_balancer['Instances'] = load_balancer['Instances'] | instances.dup

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'RegisterInstancesWithLoadBalancerResult' => {
      'Instances' => instances
    }
  }

  response
end

#reset_dataObject



88
89
90
# File 'lib/fog/aws/elb.rb', line 88

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end

#set_load_balancer_listener_ssl_certificate(lb_name, load_balancer_port, ssl_certificate_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fog/aws/requests/elb/set_load_balancer_listener_ssl_certificate.rb', line 37

def set_load_balancer_listener_ssl_certificate(lb_name, load_balancer_port, ssl_certificate_id)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]

  certificate_ids = Fog::AWS::IAM::Mock.data[@aws_access_key_id][:server_certificates].map {|n, c| c['Arn'] }
  if !certificate_ids.include? ssl_certificate_id
    raise Fog::AWS::IAM::NotFound.new('CertificateNotFound')
  end

  response = Excon::Response.new

  unless listener = load_balancer['ListenerDescriptions'].find { |listener| listener['Listener']['LoadBalancerPort'] == load_balancer_port }
    response.status = 400
    response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>ListenerNotFound</Code><Message>LoadBalancer does not have a listnener configured at the given port.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestId></Response>"
    raise Excon::Errors.status_error({:expects => 200}, response)
  end

  listener['Listener']['SSLCertificateId'] = ssl_certificate_id

  response.status = 200
  response.body = {
    "ResponseMetadata" => {
      "RequestId" => Fog::AWS::Mock.request_id
    }
  }

  response
end

#set_load_balancer_policies_of_listener(lb_name, load_balancer_port, policy_names) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fog/aws/requests/elb/set_load_balancer_policies_of_listener.rb', line 43

def set_load_balancer_policies_of_listener(lb_name, load_balancer_port, policy_names)
  raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]

  policy_names = [*policy_names]
  response = Excon::Response.new
  if policy_names.size > 1
    response.status = 409
    response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>InvalidConfigurationRequest</Code><Message>Requested configuration change is invalid.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestId></Response>"
    raise Excon::Errors.status_error({:expects => 200}, response)
  end

  unless listener = load_balancer['ListenerDescriptions'].find { |listener| listener['Listener']['LoadBalancerPort'] == load_balancer_port }
    response.status = 400
    response.body = "<?xml version=\"1.0\"?><Response><Errors><Error><Code>ListenerNotFound</Code><Message>LoadBalancer does not have a listnener configured at the given port.</Message></Error></Errors><RequestID>#{Fog::AWS::Mock.request_id}</RequestId></Response>"
    raise Excon::Errors.status_error({:expects => 200}, response)
  end

  unless load_balancer['Policies']['Proper'].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)
  end if policy_names.any?

  listener['PolicyNames'] = policy_names

  response.status = 200
  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    }
  }

  response
end