Class: Hugo::Aws::Elb

Inherits:
Object
  • Object
show all
Defined in:
lib/hugo/aws/elb.rb

Constant Summary collapse

ACCESS_KEY =
ENV['AMAZON_ACCESS_KEY_ID']
SECRET_KEY =
ENV['AMAZON_SECRET_ACCESS_KEY']
ZONES =
["us-east-1c"]
LISTENERS =
[{"InstancePort"=>"8080", "Protocol"=>"HTTP", "LoadBalancerPort"=>"80"}, 
{"InstancePort"=>"8443", "Protocol"=>"TCP", "LoadBalancerPort"=>"443"}]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Elb

Returns a new instance of Elb.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hugo/aws/elb.rb', line 13

def initialize(options = {} )
  @name = options[:name] || options["LoadBalancerName"]
    
  @uri = options["DNSName"] if options["DNSName"]
      
  if options["Instances"] and options["Instances"]["member"]
    @instances = options["Instances"]["member"].map { |i| i.InstanceId }
  else
    @instances = []
  end
  if options["AvailabilityZones"] and options["AvailabilityZones"]["member"]
    @zones = options["AvailabilityZones"]["member"]
  else
    @zones = options[:zones] || ZONES
  end
  if options["Listeners"] and options["Listeners"]["member"]
    @listeners = options["Listeners"]["member"]
  else
    @listeners = options[:listeners] || LISTENERS
  end
  if options["CreatedTime"]
    @create_time = options["CreatedTime"]
  end

  @aws_access_key_id = options[:aws_access_key_id] || ACCESS_KEY
  @aws_secret_access_key = options[:aws_secret_access_key] || SECRET_KEY
  
end

Instance Attribute Details

#aws_access_key_idObject

Returns the value of attribute aws_access_key_id.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def aws_access_key_id
  @aws_access_key_id
end

#aws_secret_access_keyObject

Returns the value of attribute aws_secret_access_key.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def aws_secret_access_key
  @aws_secret_access_key
end

#create_timeObject

Returns the value of attribute create_time.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def create_time
  @create_time
end

#instancesObject

Returns the value of attribute instances.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def instances
  @instances
end

#listenersObject

Returns the value of attribute listeners.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def listeners
  @listeners
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def name
  @name
end

#uriObject

Returns the value of attribute uri.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def uri
  @uri
end

#zonesObject

Returns the value of attribute zones.



11
12
13
# File 'lib/hugo/aws/elb.rb', line 11

def zones
  @zones
end

Class Method Details

.all(aws_access_key_id, aws_secret_access_key) ⇒ Object



76
77
78
79
80
# File 'lib/hugo/aws/elb.rb', line 76

def self.all(aws_access_key_id, aws_secret_access_key)
  @elb = AWS::ELB::Base.new(:access_key_id => aws_access_key_id, :secret_access_key => aws_secret_access_key)
  instances = @elb.describe_load_balancers().DescribeLoadBalancersResult.LoadBalancerDescriptions.member      
  instances.map { |i| self.new(i) }
end

.find(balancer, aws_access_key_id, aws_secret_access_key) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hugo/aws/elb.rb', line 82

def self.find(balancer, aws_access_key_id, aws_secret_access_key)
  @elb = AWS::ELB::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
  
  result = nil
    
  @elb.describe_load_balancers().DescribeLoadBalancersResult.LoadBalancerDescriptions.member.each do |m|
    result = self.new(m) if m.LoadBalancerName == balancer
  end
  
  result
rescue
  nil
end

.find_or_create(options) ⇒ Object



96
97
98
# File 'lib/hugo/aws/elb.rb', line 96

def self.find_or_create(options)
  self.find(options[:name], options[:aws_access_key_id], options[:aws_secret_access_key]) || self.new(options).create
end

Instance Method Details

#add(instance) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/hugo/aws/elb.rb', line 59

def add(instance)
  elb.register_instances_with_load_balancer(
    :instances => [instance],
    :load_balancer_name => @name)
  @instances << instance
  self
end

#createObject



42
43
44
45
46
47
48
49
# File 'lib/hugo/aws/elb.rb', line 42

def create
  elb.create_load_balancer(
    :load_balancer_name => self.name,
    :listeners => self.listeners,
    :availability_zones => self.zones
  ) unless self.create_time
  self
end

#destroyObject



51
52
53
# File 'lib/hugo/aws/elb.rb', line 51

def destroy
  elb.delete_load_balancer(:load_balancer_name => self.name)
end

#remove(instance) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/hugo/aws/elb.rb', line 67

def remove(instance)
  
  elb.deregister_instances_from_load_balancer(
    :instances => [instance],
    :load_balancer_name => @name)
  @instances = @instances - [instance]
  self
end

#saveObject



55
56
57
# File 'lib/hugo/aws/elb.rb', line 55

def save
  self.create
end