You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/08/23 14:18:34 UTC

[PATCH core 1/4] Core: Removed unused variables and trailing whitespace

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 client/lib/base_object.rb                             |    2 +-
 server/lib/deltacloud/drivers/google/google_driver.rb |    2 +-
 server/lib/deltacloud/helpers/deltacloud_helper.rb    |    2 --
 3 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
index 5035266..6a28d5a 100644
--- a/client/lib/base_object.rb
+++ b/client/lib/base_object.rb
@@ -102,7 +102,7 @@ module DeltaCloud
           :values => @providers
         }
       end
-      
+
 
       # This method define collection of text elements inside REST model
       # XML syntax: <addresses>
diff --git a/server/lib/deltacloud/drivers/google/google_driver.rb b/server/lib/deltacloud/drivers/google/google_driver.rb
index 18c4058..12b0bf0 100644
--- a/server/lib/deltacloud/drivers/google/google_driver.rb
+++ b/server/lib/deltacloud/drivers/google/google_driver.rb
@@ -196,7 +196,7 @@ class GoogleDriver < Deltacloud::BaseDriver
   def valid_credentials?(credentials)
     begin
       new_client(credentials).get_service && true
-    rescue => e
+    rescue
       return false
     end
     return true
diff --git a/server/lib/deltacloud/helpers/deltacloud_helper.rb b/server/lib/deltacloud/helpers/deltacloud_helper.rb
index 799478e..c31f89d 100644
--- a/server/lib/deltacloud/helpers/deltacloud_helper.rb
+++ b/server/lib/deltacloud/helpers/deltacloud_helper.rb
@@ -55,7 +55,6 @@ module Deltacloud::Helpers
       filter.merge!(:owner_id => params[:owner_id]) if params[:owner_id]
       filter.merge!(:state => params[:state]) if params[:state]
       filter = {} if filter.keys.size.eql?(0)
-      singular = model.to_s.singularize.to_sym
       begin
         @benchmark = Benchmark.measure do
           @elements = driver.send(model.to_sym, credentials, filter)
@@ -179,7 +178,6 @@ module Deltacloud::Helpers
     end
 
     def image_for_state(state)
-      state_img = "stopped" if (state!='RUNNING' or state!='PENDING')
       capture_haml do
         haml_tag :img, :src => "/images/#{state}" % state.downcase, :title => state
       end
-- 
1.7.10.2


[PATCH core 3/4] GoGrid: Updated supported API to 1.9 in client

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/drivers/gogrid/gogrid_client.rb |   15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb b/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb
index 76b326e..77fba97 100644
--- a/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb
+++ b/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb
@@ -17,6 +17,7 @@
 require 'digest/md5'
 require 'cgi'
 require 'open-uri'
+require 'json'
 
 module Kernel
   def suppress_warnings
@@ -44,7 +45,7 @@ class GoGridClient
                  apikey='YOUR API KEY',
                  secret='YOUR SHARED SECRET',
                  format='json',
-                 version='1.6')
+                 version='1.9')
     @server = server
     @secret = secret
     @default_params = {'format'=>format, 'v'=>version,'api_key' => apikey}
@@ -52,13 +53,13 @@ class GoGridClient
 
   def getRequestURL(method,params)
     requestURL = @server+'/'+method+'?'
-  	call_params = @default_params.merge(params)
-  	call_params['sig']=getSignature(@default_params['api_key'],@secret)
-  	requestURL = requestURL+encode_params(call_params)
+    call_params = @default_params.merge(params)
+    call_params['sig']=getSignature(@default_params['api_key'],@secret)
+    requestURL = requestURL+encode_params(call_params)
   end
 
   def getSignature(key,secret)
-    Digest::MD5.hexdigest(key+secret+"%.0f"%Time.new.to_f)
+    Digest::MD5.hexdigest(key+secret+"%.0f"%Time.now.to_f)
   end
 
   def sendAPIRequest(method,params={})
@@ -71,8 +72,8 @@ class GoGridClient
     if version
       @default_params['v'] = version
     else
-      @default_params['v'] = '1.5'
-   end
+      @default_params['v'] = '1.9'
+    end
     request = sendAPIRequest(method, params)
     JSON::parse(request)
   end
-- 
1.7.10.2


Re: [PATCH core 1/4] Core: Removed unused variables and trailing whitespace

Posted by Michal Fojtik <mf...@redhat.com>.
On Aug 24, 2012, at 1:34 AM, David Lutterkort <lu...@redhat.com> wrote:

> I do have a comment on 2/4: there's a lot of talk about the importance
> of setting time in tests/drivers/gogrid/common.rb, but then the call to
> Time.be is commented out .. what gives ?

Sorry for that, I just copied the boiler plate from EC2 tests.
I freezed the time in each test, since GoGrid is strict in the timestamp
(I can't be older than 5 minutes), thus freezing the time in common is
not possible.

I removed that comments.

> Also, 'rake test:drivers:gogrid' fails with
> 
>          1) Failure:
>        test_0004_must allow to retrieve single hardware_profile(GoGrid Hardware Profiles) [/homes/lutter/code/deltacloud/git/server/tests/drivers/gogrid/hardware_profiles_test.rb:46]:
>        Expected :memory (Symbol) to respond to #empty?.

Seems like an another funny MRI18 vs MRI19 issue:

In MRI18 this throw an exception:

:test.wont_be_empty

However it works under MRI19...

I fixed that by converting that symbol to String. That should work
on both platforms.

> ACK to the series once these two things are addressed.

Thanks. Corrected both and pushed :-)

  -- Michal

Michal Fojtik
http://deltacloud.org
mfojtik@redhat.com




Re: [PATCH core 1/4] Core: Removed unused variables and trailing whitespace

Posted by David Lutterkort <lu...@redhat.com>.
On Thu, 2012-08-23 at 14:25 +0200, Michal Fojtik wrote:
> On Aug 23, 2012, at 2:18 PM, mfojtik@redhat.com wrote:
> 
> The 2/4 is obviously missing here, because the fixtures have >1MB.
> However you can use our new tool to download this set:
> 
> $ gem install tracker-client
> 
> Save this YAML to your ~/.trackerrc
> 
> ---
> :url: http://tracker.deltacloud.org/
> :user:
> :password:
> 
> Then you can do:
> 
> $ git checkout master
> $ tracker download 20 -b gogrid_tests

Very nice.

I do have a comment on 2/4: there's a lot of talk about the importance
of setting time in tests/drivers/gogrid/common.rb, but then the call to
Time.be is commented out .. what gives ?

Also, 'rake test:drivers:gogrid' fails with

          1) Failure:
        test_0004_must allow to retrieve single hardware_profile(GoGrid Hardware Profiles) [/homes/lutter/code/deltacloud/git/server/tests/drivers/gogrid/hardware_profiles_test.rb:46]:
        Expected :memory (Symbol) to respond to #empty?.

ACK to the series once these two things are addressed.

David



Re: [PATCH core 1/4] Core: Removed unused variables and trailing whitespace

Posted by Michal Fojtik <mf...@redhat.com>.
On Aug 23, 2012, at 2:18 PM, mfojtik@redhat.com wrote:

The 2/4 is obviously missing here, because the fixtures have >1MB.
However you can use our new tool to download this set:

$ gem install tracker-client

Save this YAML to your ~/.trackerrc

---
:url: http://tracker.deltacloud.org/
:user:
:password:

Then you can do:

$ git checkout master
$ tracker download 20 -b gogrid_tests

Cheers,

  -- Michal


Michal Fojtik
http://deltacloud.org
mfojtik@redhat.com




[PATCH core 4/4] GoGrid: Fixed formatting and typos in GoGrid driver

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 .../lib/deltacloud/drivers/gogrid/gogrid_driver.rb |  838 ++++++++++----------
 1 file changed, 422 insertions(+), 416 deletions(-)

diff --git a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
index a145fc0..eb0047c 100644
--- a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
+++ b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
@@ -30,471 +30,477 @@ module Deltacloud
   module Drivers
     module Gogrid
 
-class GogridDriver < Deltacloud::BaseDriver
+      class GogridDriver < Deltacloud::BaseDriver
 
-  feature :instances, :user_name do
-    { :max_length => 20 }
-  end
-
-  feature :instances, :authentication_password
-  feature :instances, :sandboxing
-
-  define_hardware_profile 'default'
+        feature :instances, :user_name do
+          { :max_length => 20 }
+        end
 
-  USER_NAME_MAX = constraints(:collection => :instances, :feature => :user_name)[:max_length]
+        feature :instances, :authentication_password
+        feature :instances, :sandboxing
+
+        define_hardware_profile 'default'
+
+        USER_NAME_MAX = constraints(:collection => :instances, :feature => :user_name)[:max_length]
+
+        def hardware_profiles(credentials, opts={})
+          client = new_client(credentials)
+          safely do
+            server_types = client.request('common/lookup/list', { 'lookup' => 'server.type' })
+            server_rams = client.request('common/lookup/list', { 'lookup' => 'server.ram' })
+            @hardware_profiles = []
+            server_types['list'].each do |type|
+              memory_values = server_rams['list'].collect do |r|
+                r['name'] =~ /MB$/ ? r['name'].gsub(/MB$/, '').to_i : (r['name'].gsub(/(\w{2})$/, '')).to_i*1024
+              end
+              @hardware_profiles << ::Deltacloud::HardwareProfile.new(type['name'].tr(' ', '-').downcase) do
+                cpu 2
+                memory memory_values
+                storage 25
+              end
+            end
+          end
+          opts[:name] ||= opts[:id]
+          filter_on( @hardware_profiles, :name, opts )
+        end
 
-  def hardware_profiles(credentials, opts={})
-    client = new_client(credentials)
-    safely do
-      server_types = client.request('common/lookup/list', { 'lookup' => 'server.type' })
-      server_rams = client.request('common/lookup/list', { 'lookup' => 'server.ram' })
-      @hardware_profiles = []
-      server_types['list'].each do |type|
-        memory_values = server_rams['list'].collect do |r|
-            r['name'] =~ /MB$/ ? r['name'].gsub(/MB$/, '').to_i : (r['name'].gsub(/(\w{2})$/, '')).to_i*1024
+        def hardware_profile(credentials, opts={})
+          hardware_profiles(credentials, :id => opts[:id]).first
         end
-        @hardware_profiles << ::Deltacloud::HardwareProfile.new(type['name'].tr(' ', '-').downcase) do
-          cpu 2
-          memory memory_values
-          storage 25
+
+        def images(credentials, opts=nil)
+          imgs = []
+          if opts and opts[:id]
+            safely do
+              begin
+                imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
+              rescue => e
+                raise e unless e.message =~ /Bad Request/
+                imgs = []
+              end
+            end
+          else
+            safely do
+              imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
+                convert_image(image, credentials.user)
+              end
+            end
+          end
+          imgs = filter_on( imgs, :architecture, opts )
+          imgs.sort_by{|e| [e.owner_id, e.description]}
         end
-      end
-    end
-    @hardware_profiles
-  end
 
-  def images(credentials, opts=nil)
-    imgs = []
-    if opts and opts[:id]
-      safely do
-        imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
-      end
-    else
-      safely do
-        imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
-          convert_image(image, credentials.user)
+        def realms(credentials, opts={})
+          realms = []
+          client = new_client(credentials)
+          safely do
+            client.request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
+              realms << convert_realm(realm)
+            end
+          end
+          realms = filter_on(realms, :id, opts)
         end
-      end
-    end
-    imgs = filter_on( imgs, :architecture, opts )
-    imgs.sort_by{|e| [e.owner_id, e.description]}
-  end
 
-  def realms(credentials, opts=nil)
-    realms = []
-    client = new_client(credentials)
-    safely do
-        client.request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
-         realms << convert_realm(realm)
-      end
-    end
-    realms = filter_on(realms, :id, opts)
-  end
+        def create_instance(credentials, image_id, opts={})
+          server_ram = nil
+          if opts[:hwp_memory]
+            mem = opts[:hwp_memory].to_i
+            server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
+          else
+            server_ram = "512MB"
+          end
+
+          name = opts[:name]
+          if not name
+            name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
+          end
+
+          if name.length > USER_NAME_MAX
+            raise "Parameter name must be #{USER_NAME_MAX} characters or less"
+          end
+
+          client = new_client(credentials)
+          params = {
+            'name' => name,
+            'image' => image_id,
+            'server.ram' => server_ram,
+            'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
+          }
+          params.merge!('isSandbox' => 'true') if opts[:sandbox]
+          safely do
+            instance = client.request('grid/server/add', params)['list'].first
+            if instance
+              login_data = get_login_data(client, instance[:id])
+              if login_data['username'] and login_data['password']
+                instance['username'] = login_data['username']
+                instance['password'] = login_data['password']
+                inst = convert_instance(instance, credentials.user)
+              else
+                inst = convert_instance(instance, credentials.user)
+                inst.authn_error = "Unable to fetch password"
+              end
+              return inst
+            else
+              return nil
+            end
+          end
+        end
 
-  def create_instance(credentials, image_id, opts={})
-    server_ram = nil
-    if opts[:hwp_memory]
-      mem = opts[:hwp_memory].to_i
-      server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
-    else
-      server_ram = "512MB"
-    end
+        def run_on_instance(credentials, opts={})
+          target = instance(credentials, opts[:id])
+          param = {}
+          param[:credentials] = {
+            :username => target.username,
+            :password => target.password,
+          }
+          param[:credentials].merge!({ :password => opts[:password]}) if opts[:password].length>0
+          param[:port] = opts[:port] || '22'
+          param[:ip] = opts[:ip] || target.public_addresses.first.address
+          Deltacloud::Runner.execute(opts[:cmd], param)
+        end
 
-    name = opts[:name]
-    if not name
-      name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
-    end
+        def list_instances(credentials, id)
+          instances = []
+          safely do
+            new_client(credentials).request('grid/server/list')['list'].collect do |instance|
+              if id.nil? or instance['name'] == id
+                instances << convert_instance(instance, credentials.user)
+              end
+            end
+          end
+          instances
+        end
 
-    if name.length > USER_NAME_MAX
-      raise "Parameter name must be #{USER_NAME_MAX} characters or less"
-    end
+        def instances(credentials, opts=nil)
+          instances = []
+          if opts and opts[:id]
+            begin
+              client = new_client(credentials)
+              instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
+              login_data = get_login_data(client, instance['id'])
+              if login_data['username'] and login_data['password']
+                instance['username'] = login_data['username']
+                instance['password'] = login_data['password']
+                inst = convert_instance(instance, credentials.user)
+              else
+                inst = convert_instance(instance, credentials.user)
+                inst.authn_error = "Unable to fetch password"
+              end
+              instances = [inst]
+            rescue Exception => e
+              if e.message == "400 Bad Request"
+                # in the case of a VM that we just made, the grid/server/get method
+                # throws a "400 Bad Request error".  In this case we try again by
+                # getting a full listing a filtering on the id.  This could
+                # potentially take a long time, but I don't see another way to get
+                # information about a newly created instance
+                instances = list_instances(credentials, opts[:id])
+              end
+            end
+          else
+            instances = list_instances(credentials, nil)
+          end
+          instances = filter_on( instances, :state, opts )
+          instances
+        end
 
-    client = new_client(credentials)
-    params = {
-      'name' => name,
-      'image' => image_id,
-      'server.ram' => server_ram,
-      'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
-    }
-    params.merge!('isSandbox' => 'true') if opts[:sandbox]
-    safely do
-      instance = client.request('grid/server/add', params)['list'].first
-      if instance
-        login_data = get_login_data(client, instance[:id])
-        if login_data['username'] and login_data['password']
-          instance['username'] = login_data['username']
-          instance['password'] = login_data['password']
-          inst = convert_instance(instance, credentials.user)
-        else
-          inst = convert_instance(instance, credentials.user)
-          inst.authn_error = "Unable to fetch password"
-        end
-        return inst
-      else
-        return nil
-      end
-    end
-  end
+        def reboot_instance(credentials, id)
+          safely do
+            new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
+          end
+        end
 
-  def run_on_instance(credentials, opts={})
-    target = instance(credentials, opts[:id])
-    param = {}
-    param[:credentials] = {
-      :username => target.username,
-      :password => target.password,
-    }
-    param[:credentials].merge!({ :password => opts[:password]}) if opts[:password].length>0
-    param[:port] = opts[:port] || '22'
-    param[:ip] = opts[:ip] || target.public_addresses.first.address
-    Deltacloud::Runner.execute(opts[:cmd], param)
-  end
+        def destroy_instance(credentials, id)
+          safely do
+            new_client(credentials).request('grid/server/delete', { 'name' => id})
+          end
+        end
 
+        def stop_instance(credentials, id)
+          safely do
+            new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
+          end
+        end
 
+        def start_instance(credentials, id)
+          safely do
+            new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
+          end
+        end
 
-  def list_instances(credentials, id)
-    instances = []
-    safely do
-      new_client(credentials).request('grid/server/list')['list'].collect do |instance|
-        if id.nil? or instance['name'] == id
-          instances << convert_instance(instance, credentials.user)
+        def create_load_balancer(credentials, opts={})
+          gogrid = new_client(credentials)
+          balancer, l_instance = nil, nil
+          safely do
+            virtip = get_free_ip_from_realm(credentials, opts['realm_id'])
+            if opts['instance_id']
+              l_instance = instance(credentials, :id => opts['instance_id'])
+              real_ip = {
+                'realiplist.0.port' => opts['listener_instance_port'].to_i,
+                'realiplist.0.ip' => l_instance ? l_instance.public_addresses.first : ""
+              }
+            else
+              real_ip = false
+            end
+            request = {
+              'name' => opts['name'],
+              'virtualip.ip' => virtip,
+              'virtualip.port' => opts['listener_balancer_port'].to_i,
+            }
+            request.merge!(real_ip) if real_ip
+            balancer = gogrid.request('grid/loadbalancer/add', request)['list'].first
+          end
+          balancer = convert_load_balancer(credentials, balancer)
+          balancer.instances = [l_instance] if l_instance
+          balancer
         end
-      end
-    end
-    instances
-  end
 
-  def instances(credentials, opts=nil)
-    instances = []
-    if opts and opts[:id]
-      begin
-        client = new_client(credentials)
-        instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
-        login_data = get_login_data(client, instance['id'])
-        if login_data['username'] and login_data['password']
-          instance['username'] = login_data['username']
-          instance['password'] = login_data['password']
-          inst = convert_instance(instance, credentials.user)
-        else
-          inst = convert_instance(instance, credentials.user)
-          inst.authn_error = "Unable to fetch password"
-        end
-        instances = [inst]
-      rescue Exception => e
-        if e.message == "400 Bad Request"
-          # in the case of a VM that we just made, the grid/server/get method
-          # throws a "400 Bad Request error".  In this case we try again by
-          # getting a full listing a filtering on the id.  This could
-          # potentially take a long time, but I don't see another way to get
-          # information about a newly created instance
-          instances = list_instances(credentials, opts[:id])
+        def destroy_load_balancer(credentials, id)
+          gogrid = new_client(credentials)
+          balancer = nil
+          safely do
+            balancer = gogrid.request('grid/loadbalancer/delete', { 'name' => id })
+          end
         end
-      end
-    else
-      instances = list_instances(credentials, nil)
-    end
-    instances = filter_on( instances, :state, opts )
-    instances
-  end
 
-  def reboot_instance(credentials, id)
-    safely do
-      new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
-    end
-  end
+        def load_balancers(credentials, opts={})
+          gogrid = new_client(credentials)
+          balancers = []
+          safely do
+            gogrid.request('grid/loadbalancer/list', opts || {})['list'].each do |b|
+              balancers << b
+            end
+          end
+          balancers.collect { |b| convert_load_balancer(credentials, b) }
+        end
 
-  def destroy_instance(credentials, id)
-    safely do
-      new_client(credentials).request('grid/server/delete', { 'name' => id})
-    end
-  end
+        def load_balancer(credentials, opts={})
+          gogrid = new_client(credentials)
+          balancer = nil
+          begin
+            balancer = gogrid.request('grid/loadbalancer/get', { 'name' => opts[:id] })['list'].first
+            balancer['instances'] = instances(credentials)
+            return convert_load_balancer(credentials, balancer)
+          rescue OpenURI::HTTPError
+            balancer = load_balancers(credentials, :id => opts[:id]).first
+          end
+        end
 
-  def stop_instance(credentials, id)
-    safely do
-      new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
-    end
-  end
 
-  def start_instance(credentials, id)
-    safely do
-      new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
-    end
-  end
+        def lb_register_instance(credentials, opts={})
+          client = new_client(credentials)
+          instance = instance(credentials, :id => opts[:instance_id])
+          balancer = client.request('grid/loadbalancer/get', { 'name' => opts[:id]})['list'].first
+          safely do
+            convert_load_balancer(credentials, client.request('grid/loadbalancer/edit', {
+              "id" => balancer['id'],
+              "realiplist.#{balancer['realiplist'].size}.ip" => instance.public_addresses.first,
+              "realiplist.#{balancer['realiplist'].size}.port" => balancer['virtualip']['port']
+            }))
+          end
+        end
 
- def create_load_balancer(credentials, opts={})
-    gogrid = new_client(credentials)
-    balancer, l_instance = nil, nil
-    safely do
-      virtip = get_free_ip_from_realm(credentials, opts['realm_id'])
-      if opts['instance_id']
-        l_instance = instance(credentials, :id => opts['instance_id'])
-        real_ip = {
-          'realiplist.0.port' => opts['listener_instance_port'].to_i,
-          'realiplist.0.ip' => l_instance ? l_instance.public_addresses.first : ""
-        }
-      else
-        real_ip = false
-      end
-      request = {
-        'name' => opts['name'],
-        'virtualip.ip' => virtip,
-        'virtualip.port' => opts['listener_balancer_port'].to_i,
-      }
-      request.merge!(real_ip) if real_ip
-      balancer = gogrid.request('grid/loadbalancer/add', request)['list'].first
-    end
-    balancer = convert_load_balancer(credentials, balancer)
-    balancer.instances = [l_instance] if l_instance
-    balancer
-  end
+        # Move this to capabilities
+        def lb_unregister_instance(credentials, opts={})
+          raise Deltacloud::BackendFeatureUnsupported.new('501',
+                                                          'Unregistering instances from load balancer is not supported in GoGrid')
+        end
 
-  def destroy_load_balancer(credentials, id)
-    gogrid = new_client(credentials)
-    balancer = nil
-    safely do
-      balancer = gogrid.request('grid/loadbalancer/delete', { 'name' => id })
-    end
-  end
+        def key(credentials, opts=nil)
+          keys(credentials, opts).first
+        end
 
-  def load_balancers(credentials, opts={})
-    gogrid = new_client(credentials)
-    balancers = []
-    safely do
-      balancer = gogrid.request('grid/loadbalancer/list', opts || {})['list'].each do |balancer|
-        balancers << balancer
-      end
-    end
-    balancers.collect { |b| convert_load_balancer(credentials, b) }
-  end
+        def keys(credentials, opts=nil)
+          gogrid = new_client( credentials )
+          creds = []
+          safely do
+            gogrid.request('support/password/list')['list'].each do |password|
+              creds << convert_key(password)
+            end
+          end
+          return creds
+        end
 
-  def load_balancer(credentials, opts={})
-    gogrid = new_client(credentials)
-    balancer = nil
-    begin
-      balancer = gogrid.request('grid/loadbalancer/get', { 'name' => opts[:id] })['list'].first
-      balancer['instances'] = instances(credentials)
-      return convert_load_balancer(credentials, balancer)
-    rescue OpenURI::HTTPError
-      balancer = load_balancers(credentials, :id => opts[:id]).first
-    end
-  end
+        def valid_credentials?(credentials)
+          # FIXME: We need to do this call to determine if
+          #        GoGrid is working with given credentials. There is no
+          #        other way to check, if given credentials are valid or not.
+          return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
+          true
+        end
 
+        define_instance_states do
+          start.to( :pending )         .automatically
+          pending.to( :running )       .automatically
+          running.to( :stopped )       .on( :stop )
+          stopped.to( :running )       .on( :start )
+          running.to( :finish )       .on( :destroy )
+          stopped.to( :finish )       .on( :destroy )
+        end
 
-  def lb_register_instance(credentials, opts={})
-    client = new_client(credentials)
-    instance = instance(credentials, :id => opts[:instance_id])
-    balancer = client.request('grid/loadbalancer/get', { 'name' => opts[:id]})['list'].first
-    safely do
-      convert_load_balancer(credentials, client.request('grid/loadbalancer/edit', {
-        "id" => balancer['id'],
-        "realiplist.#{balancer['realiplist'].size}.ip" => instance.public_addresses.first,
-        "realiplist.#{balancer['realiplist'].size}.port" => balancer['virtualip']['port']
-      }))
-    end
-  end
+        exceptions do
 
-  # Move this to capabilities
-  def lb_unregister_instance(credentials, opts={})
-      raise Deltacloud::BackendFeatureUnsupported.new('501',
-    'Unregistering instances from load balancer is not supported in GoGrid')
-  end
+          on /Forbidden/ do
+            status 401
+          end
 
-  def key(credentials, opts=nil)
-    keys(credentials, opts).first
-  end
+        end
 
-  def keys(credentials, opts=nil)
-    gogrid = new_client( credentials )
-    creds = []
-    safely do
-      gogrid.request('support/password/list')['list'].each do |password|
-        creds << convert_key(password)
-      end
-    end
-    return creds
-  end
+        private
 
-  def valid_credentials?(credentials)
-    client = new_client(credentials)
-    # FIXME: We need to do this call to determine if
-    #        GoGrid is working with given credentials. There is no
-    #        other way to check, if given credentials are valid or not.
-    return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
-    true
-  end
+        def new_client(credentials)
+          GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
+        end
 
-  define_instance_states do
-    start.to( :pending )         .automatically
-    pending.to( :running )       .automatically
-    running.to( :stopped )       .on( :stop )
-    stopped.to( :running )       .on( :start )
-    running.to( :finish )       .on( :destroy )
-    stopped.to( :finish )       .on( :destroy )
-  end
+        def convert_load_balancer(credentials, loadbalancer)
+          if loadbalancer['datacenter']
+            b_realm = realm(credentials, :id => loadbalancer['datacenter']['id'].to_s)
+          else
+            # Report first Realm until loadbalancer become ready
+            b_realm = realm(credentials, :id => 1)
+          end
+          balancer = LoadBalancer.new({
+            :id => loadbalancer['name'],
+            :realms => [b_realm]
+          })
+          balancer.public_addresses = [loadbalancer['virtualip']['ip']['ip']] if loadbalancer['virtualip'] and loadbalancer['virtualip']['ip']
+          balancer.listeners = []
+          balancer.instances = []
+          instance_ips = []
+          if loadbalancer['realiplist'].size > 0
+            loadbalancer['realiplist'].each do |instance_ip|
+              balancer.add_listener({
+                :protocol => 'TCP',
+                :load_balancer_port => loadbalancer['virtualip']['port'],
+                :instance_port => instance_ip['port']
+              })
+              instance_ips << instance_ip['ip']['ip']
+            end
+          else
+            balancer.add_listener({:protocol=>'TCP', :load_balancer_port =>loadbalancer['virtualip']['port'],
+                                   :instance_port => "unassigned"})
+          end
+          balancer.instances = get_load_balancer_instances(instance_ips, loadbalancer['instances']) if loadbalancer['instances']
+          return balancer
+        end
 
-  def catched_exceptions_list
-    {
-      :auth => [ /Forbidden/ ],
-      :error => [ /Error/ ],
-      :glob => [ /(.*)/ ]
-    }
-  end
+        def get_load_balancer_instances(instance_ips, instances)
+          instances.select { |i| instance_ips.include?(i.public_addresses.first) } if instances
+        end
 
-  private
 
-  def new_client(credentials)
-    GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
-  end
+        def get_login_data(client, instance_id)
+          login_data = {}
+          begin
+            client.request('support/password/list')['list'].each do |passwd|
+              next unless passwd['server']
+              if passwd['server']['id'] == instance_id
+                login_data['username'], login_data['password'] = passwd['username'], passwd['password']
+                break
+              end
+            end
+          rescue Exception => e
+            login_data[:error] = e.message
+          end
+          return login_data
+        end
 
-  def convert_load_balancer(credentials, loadbalancer)
-    if loadbalancer['datacenter']
-      b_realm = realm(credentials, :id => loadbalancer['datacenter']['id'].to_s)
-    else
-      # Report first Realm until loadbalancer become ready
-      b_realm = realm(credentials, :id => 1)
-    end
-    balancer = LoadBalancer.new({
-      :id => loadbalancer['name'],
-      :realms => [b_realm]
-    })
-    balancer.public_addresses = [loadbalancer['virtualip']['ip']['ip']] if loadbalancer['virtualip'] and loadbalancer['virtualip']['ip']
-    balancer.listeners = []
-    balancer.instances = []
-    instance_ips = []
-    if loadbalancer['realiplist'].size > 0
-      loadbalancer['realiplist'].each do |instance_ip|
-       balancer.add_listener({
-          :protocol => 'TCP',
-          :load_balancer_port => loadbalancer['virtualip']['port'],
-          :instance_port => instance_ip['port']
-        })
-        instance_ips << instance_ip['ip']['ip']
-      end
-    else
-      balancer.add_listener({:protocol=>'TCP', :load_balancer_port =>loadbalancer['virtualip']['port'],
-                             :instance_port => "unassigned"})
-    end
-    balancer.instances = get_load_balancer_instances(instance_ips, loadbalancer['instances']) if loadbalancer['instances']
-    return balancer
-  end
+        def convert_key(password)
+          Key.new({
+            :id => password['id'],
+            :username => password['username'],
+            :password => password['password'],
+            :credential_type => :password
+          })
+        end
 
-  def get_load_balancer_instances(instance_ips, instances)
-    instances.select { |i| instance_ips.include?(i.public_addresses.first) } if instances
-  end
+        def convert_image(gg_image, owner_id=nil)
+          Image.new( {
+            :id=>gg_image['id'].to_s,
+            :name => "#{gg_image['friendlyName']}",
+            :description=> convert_description(gg_image).to_s,
+            :owner_id=>gg_image['owner']['name'].to_s,
+            :architecture=>convert_arch(gg_image['description']),
+            :state => gg_image['state']['name'].upcase
+          } )
+        end
 
+        def convert_description(image)
+          if image['price'].eql?(0)
+            image['description']
+          else
+            "#{image['description']} (#{image['price']}$)"
+          end
+        end
 
-  def get_login_data(client, instance_id)
-    login_data = {}
-    begin
-      client.request('support/password/list')['list'].each do |passwd|
-        next unless passwd['server']
-        if passwd['server']['id'] == instance_id
-          login_data['username'], login_data['password'] = passwd['username'], passwd['password']
-          break
+        def convert_realm(realm)
+          Realm.new(
+            :id => realm['id'].to_s,
+            :name => realm['name'],
+            :state => :unlimited,
+            :storage => :unlimited
+          )
         end
-      end
-    rescue Exception => e
-      login_data[:error] = e.message
-    end
-    return login_data
-  end
 
-  def convert_key(password)
-    Key.new({
-      :id => password['id'],
-      :username => password['username'],
-      :password => password['password'],
-      :credential_type => :password
-    })
-  end
+        def convert_arch(description)
+          return 'i386' unless description
+          description.include?('64-bit') ? 'x86_64' : 'i386'
+        end
 
-  def convert_image(gg_image, owner_id=nil)
-    Image.new( {
-      :id=>gg_image['id'].to_s,
-      :name => "#{gg_image['friendlyName']}",
-      :description=> convert_description(gg_image).to_s,
-      :owner_id=>gg_image['owner']['name'].to_s,
-      :architecture=>convert_arch(gg_image['description']),
-      :state => gg_image['state']['name'].upcase
-    } )
-  end
+        def convert_instance(instance, owner_id)
+          opts = {}
+          unless instance['ram']['id'] == "1"
+            mem = instance['ram']['name']
+            if mem == "512MB"
+              opts[:hwp_memory] = "512"
+            else
+              opts[:hwp_memory] = (mem.to_i * 1024).to_s
+            end
+          end
+
+          prof = InstanceProfile.new("server", opts)
+          state = convert_server_state(instance['state']['name'], instance['id'])
+
+          Instance.new(
+            # note that we use 'name' as the id here, because newly created instances
+            # don't get a real ID until later on.  The name is good enough; from
+            # what I can tell, 'name' per user is unique, so it should be sufficient
+            # to uniquely identify this instance.
+            :id => instance['name'],
+            :owner_id => owner_id,
+            :image_id => "#{instance['image']['id']}",
+            :instance_profile => prof,
+            :name => instance['name'],
+            :realm_id => instance['ip']['datacenter']['id'],
+            :state => state,
+            :actions => instance_actions_for(state),
+            :public_addresses => [ InstanceAddress.new(instance['ip']['ip']) ],
+            :private_addresses => [],
+            :username => instance['username'],
+            :password => instance['password'],
+            :create_image => 'true'.eql?(instance['isSandbox'])
+          )
+        end
 
-  def convert_description(image)
-    if image['price'].eql?(0)
-      image['description']
-    else
-      "#{image['description']} (#{image['price']}$)"
-    end
-  end
+        def convert_server_state(state, id)
+          return 'PENDING' unless id
+          state.eql?('Off') ? 'STOPPED' : 'RUNNING'
+        end
 
-  def convert_realm(realm)
-    Realm.new(
-      :id => realm['id'].to_s,
-      :name => realm['name'],
-      :state => :unlimited,
-      :storage => :unlimited
-    )
-  end
+        def get_free_ip_from_realm(credentials, realm_id, ip_type = 1)
+          ip = ""
+          safely do
+            ip = new_client(credentials).request('grid/ip/list', {
+              'ip.type' => "#{ip_type}",
+              'ip.state' => '1',
+              'datacenter' => realm_id
+            })['list'].first['ip']
+          end
+          return ip
+        end
 
-  def convert_arch(description)
-    return 'i386' unless description
-    description.include?('64-bit') ? 'x86_64' : 'i386'
-  end
 
-  def convert_instance(instance, owner_id)
-    opts = {}
-    unless instance['ram']['id'] == "1"
-      mem = instance['ram']['name']
-      if mem == "512MB"
-        opts[:hwp_memory] = "512"
-      else
-        opts[:hwp_memory] = (mem.to_i * 1024).to_s
       end
-    end
-
-    prof = InstanceProfile.new("server", opts)
-    hwp_name = instance['image']['name']
-    state = convert_server_state(instance['state']['name'], instance['id'])
-
-    Instance.new(
-       # note that we use 'name' as the id here, because newly created instances
-       # don't get a real ID until later on.  The name is good enough; from
-       # what I can tell, 'name' per user is unique, so it should be sufficient
-       # to uniquely identify this instance.
-      :id => instance['name'],
-      :owner_id => owner_id,
-      :image_id => "#{instance['image']['id']}",
-      :instance_profile => prof,
-      :name => instance['name'],
-      :realm_id => instance['ip']['datacenter']['id'],
-      :state => state,
-      :actions => instance_actions_for(state),
-      :public_addresses => [ InstanceAddress.new(instance['ip']['ip']) ],
-      :private_addresses => [],
-      :username => instance['username'],
-      :password => instance['password'],
-      :create_image => 'true'.eql?(instance['isSandbox'])
-    )
-  end
-
-  def convert_server_state(state, id)
-    return 'PENDING' unless id
-    state.eql?('Off') ? 'STOPPED' : 'RUNNING'
-  end
-
-  def get_free_ip_from_realm(credentials, realm_id, ip_type = 1)
-    ip = ""
-    safely do
-      ip = new_client(credentials).request('grid/ip/list', {
-        'ip.type' => "#{ip_type}",
-        'ip.state' => '1',
-        'datacenter' => realm_id
-      })['list'].first['ip']
-    end
-    return ip
-  end
-
-
-end
 
     end
   end
-- 
1.7.10.2