Class: Fog::HP::LB::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/hp/lb.rb,
lib/fog/hp/requests/lb/list_limits.rb,
lib/fog/hp/requests/lb/list_versions.rb,
lib/fog/hp/requests/lb/list_protocols.rb,
lib/fog/hp/requests/lb/list_algorithms.rb,
lib/fog/hp/requests/lb/get_load_balancer.rb,
lib/fog/hp/requests/lb/list_load_balancers.rb,
lib/fog/hp/requests/lb/create_load_balancer.rb,
lib/fog/hp/requests/lb/delete_load_balancer.rb,
lib/fog/hp/requests/lb/update_load_balancer.rb,
lib/fog/hp/requests/lb/get_load_balancer_node.rb,
lib/fog/hp/requests/lb/list_load_balancer_nodes.rb,
lib/fog/hp/requests/lb/create_load_balancer_node.rb,
lib/fog/hp/requests/lb/delete_load_balancer_node.rb,
lib/fog/hp/requests/lb/update_load_balancer_node.rb,
lib/fog/hp/requests/lb/list_load_balancer_virtual_ips.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



83
84
85
# File 'lib/fog/hp/lb.rb', line 83

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

Class Method Details

.dataObject



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
77
# File 'lib/fog/hp/lb.rb', line 46

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
        :versions => {
          "v1.1" => { "id" => "v1.1",
                      "links" => [{"href" => "http://api-docs.hpcloud.com", "rel" => "self"}],
                      "status" => "CURRENT",
                      "updated" => "2012-12-18T18:30:02.25Z"
          }
        },
        :limits => {
          "absolute" => {
            "values" => {
              "maxLoadBalancerNameLength" => 128,
              "maxLoadBalancers"          => 20,
              "maxNodesPerLoadBalancer"   => 5,
              "maxVIPsPerLoadBalancer"    => 1
            }
          }
        },
        :lbs => {},
        :protocols => {
          "HTTP" => { "name" => "HTTP", "port" => 80 },
          "TCP"  => { "name" => "TCP", "port"  => 443 }
        },
        :algorithms => {
          "ROUND_ROBIN"       => { "name" => "ROUND_ROBIN" },
          "LEAST_CONNECTIONS" => { "name" => "LEAST_CONNECTIONS"}
        }
    }
  end
end

.resetObject



79
80
81
# File 'lib/fog/hp/lb.rb', line 79

def self.reset
  @data = nil
end

Instance Method Details

#create_load_balancer(name, nodes, options = {}) ⇒ Object



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
# File 'lib/fog/hp/requests/lb/create_load_balancer.rb', line 71

def create_load_balancer(name, nodes, options={})
  ### Call: {"name" => "my-shiny-lb1", "nodes" => [{"address" => "15.185.1.1", "port" => "80"}]}
  response = Excon::Response.new
  response.status = 202
  # add the id, and other attributes that are added by the system
  nodes.each do |n|
    n['id']         = Fog::HP::Mock.uuid.to_s
    n['condition']  = 'ENABLED'
    n['status']     = 'ONLINE'
  end
  data = {
      'id'        => Fog::HP::Mock.uuid.to_s,
      'name'      => name,
      'protocol'  => options['protocol'] || 'HTTP',
      'port'      => options['port'] || '80',
      'algorithm' => options['algorithm'] || 'ROUND_ROBIN',
      'status'    => 'ACTIVE',
      'created'   => '2012-01-01T13:32:20Z',
      'updated'   => '2012-01-01T13:32:20Z',
      'nodes'     => nodes
  }
  if (!options['virtualIps'].nil? && !options['virtualIps'].empty?)
    data['virtualIps'] = options['virtualIps']
  else
    data['virtualIps'] = [{
                     'ipVersion' => 'IPV4',
                     'type' => 'PUBLIC',
                     'id' => Fog::HP::Mock.uuid.to_s,
                     'address' => Fog::HP::Mock.ip_address
                  }]
  end

  self.data[:lbs][data['id']] = data
  response.body = data
  response
end

#create_load_balancer_node(load_balancer_id, address, port, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/hp/requests/lb/create_load_balancer_node.rb', line 47

def create_load_balancer_node(load_balancer_id, address, port, options={})
  response = Excon::Response.new
  if get_load_balancer(load_balancer_id)
    response.status = 202

    data = {
        'id'        => Fog::HP::Mock.uuid.to_s,
        'address'   => address,
        'port'      => port,
        'condition' => options['condition'] || 'ENABLED',
        'status'    => 'ONLINE'
    }

    self.data[:lbs][load_balancer_id]['nodes'] << data
    response.body = {'nodes' => [data]}
    response
  else
    raise Fog::HP::LB::NotFound
  end

end

#dataObject



87
88
89
# File 'lib/fog/hp/lb.rb', line 87

def data
  self.class.data[@hp_access_key]
end

#delete_load_balancer(load_balancer_id) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/fog/hp/requests/lb/delete_load_balancer.rb', line 23

def delete_load_balancer(load_balancer_id)
  response = Excon::Response.new
  if list_load_balancers.body['loadBalancers'].detect { |_| _['id'] == load_balancer_id }
    response.status = 202
    response
  else
    raise Fog::HP::LB::NotFound
  end
end

#delete_load_balancer_node(load_balancer_id, node_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/hp/requests/lb/delete_load_balancer_node.rb', line 24

def delete_load_balancer_node(load_balancer_id, node_id)
  response = Excon::Response.new
  if get_load_balancer(load_balancer_id)
    if get_load_balancer_node(load_balancer_id, node_id)
      response.status = 202
      nodes = delete_node(load_balancer_id, node_id)
      self.data[:lbs][load_balancer_id]['nodes'] = nodes
      response
    else
      raise Fog::HP::LB::NotFound
    end
  else
    raise Fog::HP::LB::NotFound
  end
end

#delete_node(load_balancer_id, node_id) ⇒ Object



40
41
42
43
# File 'lib/fog/hp/requests/lb/delete_load_balancer_node.rb', line 40

def delete_node(load_balancer_id, node_id)
  nodes = list_load_balancer_nodes(load_balancer_id).body['nodes']
  nodes.reject {|n| n['id'] == node_id}
end

#find_node(load_balancer_id, node_id) ⇒ Object



46
47
48
49
# File 'lib/fog/hp/requests/lb/get_load_balancer_node.rb', line 46

def find_node(load_balancer_id, node_id)
  nodes = list_load_balancer_nodes(load_balancer_id).body['nodes']
  nodes.detect {|n| n['id'] == node_id}
end

#get_load_balancer(load_balancer_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/hp/requests/lb/get_load_balancer.rb', line 46

def get_load_balancer(load_balancer_id)
  response = Excon::Response.new
  if lb = list_load_balancers.body['loadBalancers'].detect { |_| _['id'] == load_balancer_id }
    response.status = 200
    response.body = lb
    response
  else
    raise Fog::HP::LB::NotFound
  end
end

#get_load_balancer_node(load_balancer_id, node_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/hp/requests/lb/get_load_balancer_node.rb', line 30

def get_load_balancer_node(load_balancer_id, node_id)
  response = Excon::Response.new
  if get_load_balancer(load_balancer_id)
   if node = find_node(load_balancer_id, node_id)
     response.status = 200
     response.body = node
     response
   else
     raise Fog::HP::LB::NotFound
   end
  else
    raise Fog::HP::LB::NotFound
  end

end

#list_algorithmsObject



22
23
24
25
26
27
28
# File 'lib/fog/hp/requests/lb/list_algorithms.rb', line 22

def list_algorithms
  response = Excon::Response.new
  algorithms  = self.data[:algorithms].values
  response.status = 200
  response.body = { 'algorithms' => algorithms }
  response
end

#list_limitsObject



29
30
31
32
33
34
35
# File 'lib/fog/hp/requests/lb/list_limits.rb', line 29

def list_limits
  response        = Excon::Response.new
  limits          = self.data[:limits].values
  response.status = 200
  response.body   = { 'limits' => limits }
  response
end

#list_load_balancer_nodes(load_balancer_id) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/hp/requests/lb/list_load_balancer_nodes.rb', line 27

def list_load_balancer_nodes(load_balancer_id)
  response = Excon::Response.new
  if lb = get_load_balancer(load_balancer_id).body
    response.status = 200
    response.body   = {'nodes' => lb['nodes']}
    response
  else
    raise Fog::HP::LB::NotFound
  end
end

#list_load_balancer_virtual_ips(load_balancer_id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/hp/requests/lb/list_load_balancer_virtual_ips.rb', line 29

def list_load_balancer_virtual_ips(load_balancer_id)
  response = Excon::Response.new
  if lb_data = get_load_balancer(load_balancer_id)
    virtual_ips = lb_data.body['virtualIps']
    response.status = 200
    response.body   = { 'virtualIps' => virtual_ips }
    response
  else
    raise Fog::HP::LB::NotFound
  end
end

#list_load_balancersObject



31
32
33
34
35
36
37
# File 'lib/fog/hp/requests/lb/list_load_balancers.rb', line 31

def list_load_balancers
  response = Excon::Response.new
  lbs = self.data[:lbs].values
  response.status = 200
  response.body = { 'loadBalancers' => lbs }
  response
end

#list_protocolsObject



24
25
26
27
28
29
30
# File 'lib/fog/hp/requests/lb/list_protocols.rb', line 24

def list_protocols
  response = Excon::Response.new
  protocols  = self.data[:protocols].values
  response.status = 200
  response.body   = { 'protocols' => protocols }
  response
end

#list_versionsObject



16
17
18
19
20
21
22
# File 'lib/fog/hp/requests/lb/list_versions.rb', line 16

def list_versions
  response        = Excon::Response.new
  versions       = self.data[:versions].values
  response.status = 200
  response.body   = { 'versions' => versions }
  response
end

#reset_dataObject



91
92
93
# File 'lib/fog/hp/lb.rb', line 91

def reset_data
  self.class.data.delete(@hp_access_key)
end

#update_load_balancer(load_balancer_id, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/hp/requests/lb/update_load_balancer.rb', line 30

def update_load_balancer(load_balancer_id, options={})
  response = Excon::Response.new
  if lb = list_load_balancers.body['loadBalancers'].detect { |_| _['id'] == load_balancer_id }

    lb['name']      = options['name']      if options['name']
    lb['algorithm'] = options['algorithm'] if options['algorithm']

    response.status = 202
    response.body   = ''
    response
  else
    raise Fog::HP::LB::NotFound
  end
end

#update_load_balancer_node(load_balancer_id, node_id, condition) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/hp/requests/lb/update_load_balancer_node.rb', line 28

def update_load_balancer_node(load_balancer_id, node_id, condition)
  response = Excon::Response.new
  if lb_data = get_load_balancer(load_balancer_id)
    if get_load_balancer_node(load_balancer_id, node_id)
      nodes = update_node(lb_data, node_id, condition)
      self.data[:lbs][load_balancer_id]['nodes'] = nodes
      response.status = 202
      response
    else
      raise Fog::HP::LB::NotFound
    end
  else
    raise Fog::HP::LB::NotFound
  end
end

#update_node(lb_data, node_id, condition) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/hp/requests/lb/update_load_balancer_node.rb', line 44

def update_node(lb_data, node_id, condition)
  nodes = lb_data.body['nodes']
  node = nodes.select {|n| n['id'] == node_id}.first
  # update the node attributes
  if node
    node['condition'] = condition
    node['status'] = condition == 'ENABLED' ? 'ONLINE' : 'OFFLINE'
  end
  new_nodes = nodes.reject {|n| n['id'] == node_id}
  new_nodes << node
end