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

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/elb.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/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/create_lb_cookie_stickiness_policy.rb,
lib/fog/aws/requests/elb/create_app_cookie_stickiness_policy.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



62
63
64
65
66
67
68
69
70
# File 'lib/fog/aws/elb.rb', line 62

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'].include?(@region)
    raise ArgumentError, "Unknown region: #{@region.inspect}"
  end
end

Class Method Details

.dataObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/aws/elb.rb', line 42

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 => {}
      }
    end
  end
end

.dns_name(name, region) ⇒ Object



54
55
56
# File 'lib/fog/aws/elb.rb', line 54

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

.resetObject



58
59
60
# File 'lib/fog/aws/elb.rb', line 58

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
# 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 }

    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
# 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'] << { 'PolicyName' => policy_name, '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



53
54
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fog/aws/requests/elb/create_load_balancer.rb', line 53

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' => {
      'LBCookieStickinessPolicies' => [],
      'AppCookieStickinessPolicies' => []
    },
    '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



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_listeners.rb', line 51

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
      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

#dataObject



72
73
74
# File 'lib/fog/aws/elb.rb', line 72

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 { |p| p['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_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
# 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]
    end.compact
  else
    self.data[:load_balancers].values
  end

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

  response.body = {
    'ResponseMetadata' => {
      'RequestId' => Fog::AWS::Mock.request_id
    },
    'DescribeLoadBalancersResult' => {
      'LoadBalancerDescriptions' => load_balancers
    }
  }

  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'] = instances.dup

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

  response
end

#reset_dataObject



76
77
78
# File 'lib/fog/aws/elb.rb', line 76

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
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'].find { |name, policies| 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)
  end if policy_names.any?

  listener['PolicyNames'] = policy_names

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

  response
end