1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[Brightbox] Code style clean up

First pass at cleaning up the code to a better, more consistent style.
This commit is contained in:
Paul Thornthwaite 2013-11-27 17:16:07 +00:00
parent e1f98ae3bd
commit 77d8b11015
15 changed files with 62 additions and 79 deletions

View file

@ -166,7 +166,7 @@ module Fog
_request
end
private
private
def _request
raise Fog::Errors::MockNotImplemented
@ -236,7 +236,7 @@ module Fog
_wrapped_request(method, path, expected_responses, parameters)
end
private
private
# Wrapped request is the non-standard form of request introduced by mistake
#
@ -256,20 +256,18 @@ module Fog
# Select the account to scope for this request
account = scoped_account(parameters.fetch(:account_id, nil))
if account
request_options[:query] = { :account_id => account }
end
request_options[:query] = { :account_id => account } if account
request_options[:body] = Fog::JSON.encode(parameters) unless parameters.empty?
response = make_request(request_options)
# FIXME We should revert to returning the Excon::Request after a suitable
# FIXME: We should revert to returning the Excon::Request after a suitable
# configuration option is in place to switch back to this incorrect behaviour
unless response.body.empty?
Fog::JSON.decode(response.body)
else
if response.body.empty?
response
else
Fog::JSON.decode(response.body)
end
end

View file

@ -32,7 +32,7 @@ module Fog
img["official"] == true &&
img["arch"] == "i686" &&
img["name"] =~ /ubuntu/i
end.sort do |a,b|
end.sort do |a, b|
# Reverse sort so "raring" > "precise" and "13.10" > "13.04"
b["name"].downcase <=> a["name"].downcase
end.first["id"]

View file

@ -63,7 +63,7 @@ module Fog
# Request account can be changed at anytime and changes behaviour of future requests
@scoped_account = @configured_account
credential_options = {:username => username, :password => password}
credential_options = { :username => username, :password => password }
@credentials = CredentialSet.new(client_id, client_secret, credential_options)
# If existing tokens have been cached, allow continued use of them in the service
@ -175,7 +175,7 @@ module Fog
@default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image
end
private
private
# This makes a request of the API based on the configured setting for
# token management.
@ -200,13 +200,11 @@ module Fog
#
# @return [Excon::Response]
def managed_token_request(options)
begin
get_access_token unless access_token_available?
response = authenticated_request(options)
rescue Excon::Errors::Unauthorized
get_access_token
response = authenticated_request(options)
end
get_access_token unless access_token_available?
authenticated_request(options)
rescue Excon::Errors::Unauthorized
get_access_token
authenticated_request(options)
end
# This request makes an authenticated request of the API using currently
@ -221,7 +219,7 @@ module Fog
headers = options[:headers] || {}
headers.merge!("Authorization" => "OAuth #{@credentials.access_token}", "Content-Type" => "application/json")
options[:headers] = headers
# TODO This is just a wrapper around a call to Excon::Connection#request
# TODO: This is just a wrapper around a call to Excon::Connection#request
# so can be extracted from Compute by passing in the connection,
# credentials and options
@connection.request(options)

View file

@ -14,7 +14,7 @@ module Fog
options = {
:name => name,
:description => description
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_api_client(options)
merge_attributes(data)
true

View file

@ -3,9 +3,7 @@ require 'fog/core/model'
module Fog
module Compute
class Brightbox
class Application < Fog::Model
identity :id
attribute :url
attribute :name
@ -15,12 +13,11 @@ module Fog
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
options = {
:name => name
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_application(options)
merge_attributes(data)
true
end
end
end
end

View file

@ -43,9 +43,7 @@ module Fog
merge_attributes(data)
true
end
end
end
end
end

View file

@ -31,7 +31,7 @@ module Fog
def invite(email, role)
return nil if email.nil? || email == ""
return nil if role.nil? || role == ""
options = {:email => email, :role => role}
options = { :email => email, :role => role }
data = service.create_collaboration(options)
new(data)
end

View file

@ -26,7 +26,7 @@ module Fog
:server_group => server_group_id,
:name => name,
:description => description
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_firewall_policy(options)
merge_attributes(data)
true
@ -54,12 +54,10 @@ module Fog
def destroy
requires :identity
data = service.destroy_firewall_policy(identity)
service.destroy_firewall_policy(identity)
true
end
end
end
end
end

View file

@ -35,7 +35,7 @@ module Fog
:destination => destination,
:destination_port => destination_port,
:icmp_type_name => icmp_type_name
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_firewall_rule(options)
merge_attributes(data)
true

View file

@ -47,7 +47,7 @@ module Fog
:name => name,
:username => username,
:description => description
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_image(options)
merge_attributes(data)
true
@ -58,9 +58,7 @@ module Fog
service.destroy_image(identity)
true
end
end
end
end
end

View file

@ -52,7 +52,7 @@ module Fog
:name => name,
:certificate_pem => certificate_pem,
:certificate_private_key => certificate_private_key
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_load_balancer(options)
merge_attributes(data)
true
@ -94,16 +94,13 @@ module Fog
end
end
private
private
def time_or_original(value)
begin
Time.parse(value)
rescue TypeError, ArgumentError
value
end
Time.parse(value)
rescue TypeError, ArgumentError
value
end
end
end
end

View file

@ -37,7 +37,7 @@ module Fog
attribute :zone
attribute :server_type
def initialize(attributes={})
def initialize(attributes = {})
# Call super first to initialize the service object for our default image
super
self.image_id ||= service.default_image
@ -83,10 +83,10 @@ module Fog
def reboot(use_hard_reboot = true)
requires :identity
if ready?
unless use_hard_reboot
soft_reboot
else
if use_hard_reboot
hard_reboot
else
soft_reboot
end
else
# Not able to reboot if not ready in the first place
@ -137,23 +137,23 @@ module Fog
end
def private_ip_address
unless interfaces.empty?
interfaces.first["ipv4_address"]
else
if interfaces.empty?
nil
else
interfaces.first["ipv4_address"]
end
end
def public_ip_address
unless cloud_ips.empty?
cloud_ips.first["public_ip"]
else
if cloud_ips.empty?
nil
else
cloud_ips.first["public_ip"]
end
end
def ready?
self.state == 'active'
state == "active"
end
def activate_console
@ -171,10 +171,10 @@ module Fog
:zone => zone_id,
:user_data => user_data,
:server_groups => server_groups
}.delete_if {|k,v| v.nil? || v == "" }
unless flavor_id.nil? || flavor_id == ""
options.merge!(:server_type => flavor_id)
end
}.delete_if { |k, v| v.nil? || v == "" }
options.merge!(:server_type => flavor_id) unless flavor_id.nil? || flavor_id == ""
data = service.create_server(options)
merge_attributes(data)
true
@ -187,11 +187,12 @@ module Fog
interfaces.first["id"]
end
private
private
# Hard reboots are fast, avoiding the OS by doing a "power off"
def hard_reboot
stop
wait_for { ! ready? }
wait_for { !ready? }
start
end
@ -201,11 +202,11 @@ module Fog
# @todo Needs cleaner error handling when the OS times out
def soft_reboot
shutdown
# FIXME Using side effect of wait_for's (evaluated block) to detect timeouts
# FIXME: Using side effect of wait_for's (evaluated block) to detect timeouts
begin
wait_for(20) { ! ready? }
wait_for(20) { !ready? }
start
rescue Fog::Errors::Timeout => e
rescue Fog::Errors::Timeout
false
end
end

View file

@ -3,7 +3,6 @@ require 'fog/core/model'
module Fog
module Compute
class Brightbox
# A server group is a collection of servers
#
# Certain actions can accept a server group and affect all members
@ -24,15 +23,15 @@ module Fog
options = {
:name => name,
:description => description
}.delete_if {|k,v| v.nil? || v == "" }
}.delete_if { |k, v| v.nil? || v == "" }
data = service.create_server_group(options)
merge_attributes(data)
true
end
def servers
srv_ids = server_ids.collect {|srv| srv["id"]}
srv_ids.collect do |srv_id|
srv_ids = server_ids.map { |srv| srv["id"] }
srv_ids.map do |srv_id|
service.servers.get(srv_id)
end
end
@ -41,7 +40,7 @@ module Fog
#
# @param [Array] identifiers array of server identifier strings to add
# @return [Fog::Compute::ServerGroup]
def add_servers identifiers
def add_servers(identifiers)
requires :identity
options = {
:servers => server_references(identifiers)
@ -54,7 +53,7 @@ module Fog
#
# @param [Array] identifiers array of server identifier strings to remove
# @return [Fog::Compute::ServerGroup]
def remove_servers identifiers
def remove_servers(identifiers)
requires :identity
options = {
:servers => server_references(identifiers)
@ -68,7 +67,7 @@ module Fog
# @param [Array] identifiers array of server identifier strings to move
# @param [String] destination_group_id destination server group identifier
# @return [Fog::Compute::ServerGroup]
def move_servers identifiers, destination_group_id
def move_servers(identifiers, destination_group_id)
requires :identity
options = {
:servers => server_references(identifiers),
@ -84,12 +83,11 @@ module Fog
true
end
protected
protected
def server_references identifiers
identifiers.map {|id| {"server" => id} }
def server_references(identifiers)
identifiers.map { |id| { "server" => id } }
end
end
end
end

View file

@ -19,4 +19,4 @@ module Fog
end
end
end
end

View file

@ -18,7 +18,7 @@ module Fog::Brightbox::OAuth2
header_content = "#{credentials.client_id}:#{credentials.client_secret}"
encoded_credentials = Base64.encode64(header_content).chomp
connection.request({
connection.request(
:path => "/token",
:expects => 200,
:headers => {
@ -27,7 +27,7 @@ module Fog::Brightbox::OAuth2
},
:method => 'POST',
:body => Fog::JSON.encode(token_strategy.authorization_body_data)
})
)
end
# Encapsulates credentials required to request access tokens from the
@ -152,7 +152,7 @@ module Fog::Brightbox::OAuth2
end
end
private
private
# This updates the current credentials if passed a valid response
#