Skip to content

Commit abdc151

Browse files
committed
[new_servers] first pass at basic functionality
1 parent 53ce447 commit abdc151

14 files changed

Lines changed: 396 additions & 12 deletions

File tree

‎bin/fog‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'fog')
33
require 'irb'
44
require 'yaml'
55
require File.join('fog', 'credentials')
6-
require File.join('fog', 'bin')
7-
8-
Fog.credential = ARGV.first ? :"#{ARGV.first}" : :default
6+
Fog.credential = ARGV.first ? :"#{ARGV.first}" : :default
97
unless Fog.credentials
108
exit
119
end
1210

13-
require File.join('fog', 'aws', 'bin')
14-
require File.join('fog', 'local', 'bin')
15-
require File.join('fog', 'rackspace', 'bin')
16-
require File.join('fog', 'slicehost', 'bin')
17-
require File.join('fog', 'terremark', 'bin')
18-
require File.join('fog', 'vcloud', 'bin')
19-
require File.join('fog', 'bluebox', 'bin')
11+
require 'fog/bin'
2012

2113
if ARGV.length > 1
2214
print(instance_eval(ARGV[1..-1].join(' ')).to_json)

‎lib/fog.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
require 'fog/aws'
3131
require 'fog/bluebox'
3232
require 'fog/local'
33+
require 'fog/new_servers'
3334
require 'fog/rackspace'
3435
require 'fog/slicehost'
3536
require 'fog/terremark'

‎lib/fog/bin.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
require File.join(File.dirname(__FILE__), 'credentials')
1+
require 'fog/aws/bin'
2+
require 'fog/local/bin'
3+
require 'fog/new_servers/bin'
4+
require 'fog/rackspace/bin'
5+
require 'fog/slicehost/bin'
6+
require 'fog/terremark/bin'
7+
require 'fog/vcloud/bin'
8+
require 'fog/bluebox/bin'
29

310
module Fog
411
class << self
512

613
def services
714
services = []
8-
[::AWS, ::Local, ::Rackspace, ::Slicehost, ::Terremark, ::Vcloud, ::Bluebox].each do |service|
15+
[::AWS, ::Local, ::NewServers, ::Rackspace, ::Slicehost, ::Terremark, ::Vcloud, ::Bluebox].each do |service|
916
if service.initialized?
1017
services << service
1118
end

‎lib/fog/credentials.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def credentials
3535
:bluebox_api_key: INTENTIONALLY_LEFT_BLANK
3636
:bluebox_customer_id: INTENTIONALLY_LEFT_BLANK
3737
:local_root: INTENTIONALLY_LEFT_BLANK
38+
:new_servers_password: INTENTIONALLY_LEFT_BLANK
39+
:new_servers_username: INTENTIONALLY_LEFT_BLANK
3840
:public_key_path: INTENTIONALLY_LEFT_BLANK
3941
:private_key_path: INTENTIONALLY_LEFT_BLANK
4042
:rackspace_api_key: INTENTIONALLY_LEFT_BLANK

‎lib/fog/new_servers.rb‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require 'fog/parser'
2+
3+
module Fog
4+
module NewServers
5+
extend Fog::Service
6+
7+
requires :new_servers_password
8+
requires :new_servers_username
9+
10+
model_path 'fog/new_servers/models'
11+
12+
request_path 'fog/new_servers/requests'
13+
request 'add_server'
14+
request 'cancel_server'
15+
request 'get_server'
16+
request 'list_images'
17+
request 'list_plans'
18+
request 'list_servers'
19+
request 'reboot_server'
20+
21+
class Mock
22+
include Collections
23+
24+
def self.data
25+
@data ||= Hash.new do |hash, key|
26+
hash[key] = {}
27+
end
28+
end
29+
30+
def self.reset_data(keys=data.keys)
31+
for key in [*keys]
32+
data.delete(key)
33+
end
34+
end
35+
36+
def initialize(options={})
37+
@new_server_username = options[:new_servers_username]
38+
@data = self.class.data[@new_server_username]
39+
end
40+
41+
end
42+
43+
class Real
44+
include Collections
45+
46+
def initialize(options={})
47+
@new_servers_password = options[:new_servers_password]
48+
@new_servers_username = options[:new_servers_username]
49+
@host = options[:host] || "noc.newservers.com"
50+
@port = options[:port] || 443
51+
@scheme = options[:scheme] || 'https'
52+
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
53+
end
54+
55+
def reload
56+
@connection.reset
57+
end
58+
59+
def request(params)
60+
params[:query] ||= {}
61+
params[:query].merge!({
62+
:password => @new_servers_password,
63+
:username => @new_servers_username
64+
})
65+
params[:headers] ||= {}
66+
case params[:method]
67+
when 'DELETE', 'GET', 'HEAD'
68+
params[:headers]['Accept'] = 'application/xml'
69+
when 'POST', 'PUT'
70+
params[:headers]['Content-Type'] = 'application/xml'
71+
end
72+
73+
begin
74+
response = @connection.request(params.merge!({:host => @host}))
75+
rescue Excon::Errors::Error => error
76+
raise case error
77+
when Excon::Errors::NotFound
78+
Fog::NewServers::NotFound.slurp(error)
79+
else
80+
error
81+
end
82+
end
83+
84+
response
85+
end
86+
87+
end
88+
end
89+
end

‎lib/fog/new_servers/bin.rb‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module NewServers
2+
class << self
3+
if Fog.credentials[:new_servers_password] && Fog.credentials[:new_servers_username]
4+
5+
def initialized?
6+
true
7+
end
8+
9+
def [](service)
10+
@@connections ||= Hash.new do |hash, key|
11+
credentials = Fog.credentials.reject do |k,v|
12+
![:new_servers_password, :new_servers_username].include?(k)
13+
end
14+
hash[key] = case key
15+
when :new_servers
16+
Fog::NewServers.new(credentials)
17+
end
18+
end
19+
@@connections[service]
20+
end
21+
22+
else
23+
24+
def initialized?
25+
false
26+
end
27+
28+
end
29+
end
30+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Fog
2+
module NewServers
3+
class Real
4+
5+
# Boot a new server
6+
#
7+
# ==== Parameters
8+
# * planId<~String> - The id of the plan to boot the server with
9+
# * options<~Hash>: optional extra arguments
10+
# * imageId<~String> - Optional image to boot server from
11+
# * name<~String> - Name to boot new server with
12+
#
13+
# ==== Returns
14+
# * response<~Excon::Response>:
15+
# * body<~Hash>:
16+
# * 'server'<~Hash>:
17+
# * 'id'<~String> - Id of the image
18+
#
19+
def add_server(plan_id, options = {})
20+
request(
21+
:expects => 200,
22+
:method => 'GET',
23+
:parser => Fog::ToHashDocument.new,
24+
:path => 'api/addServer',
25+
:query => {'planId' => plan_id}.merge!(options)
26+
)
27+
end
28+
29+
end
30+
31+
class Mock
32+
33+
def add_server(server_id)
34+
Fog::Mock.not_implemented
35+
end
36+
37+
end
38+
end
39+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Fog
2+
module NewServers
3+
class Real
4+
5+
# Shutdown a running server
6+
#
7+
# ==== Parameters
8+
# * serverId<~String> - The id of the server to shutdown
9+
#
10+
# ==== Returns
11+
# * response<~Excon::Response>:
12+
# * body<~Hash>:
13+
# * 'server'<~Hash>:
14+
# * 'id'<~String> - Id of the image
15+
#
16+
def cancel_server(server_id)
17+
request(
18+
:expects => 200,
19+
:method => 'GET',
20+
:parser => Fog::ToHashDocument.new,
21+
:path => 'api/cancelServer',
22+
:query => {'serverId' => server_id}
23+
)
24+
end
25+
26+
end
27+
28+
class Mock
29+
30+
def cancel_server(server_id)
31+
Fog::Mock.not_implemented
32+
end
33+
34+
end
35+
end
36+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Fog
2+
module NewServers
3+
class Real
4+
5+
# List servers
6+
#
7+
# ==== Returns
8+
# * response<~Excon::Response>:
9+
# * server<~Hash>:
10+
# * 'id'<~String> - Id of the server
11+
# * 'ip'<~Hash>:
12+
# * 'address'<~String> - Address of the ip
13+
# * 'name'<~String> - Name of the ip
14+
# * 'login'<~Hash>:
15+
# * 'name'<~String> - Name of the login
16+
# * 'password'<~String> - Password of the login
17+
# * 'username'<~String> - Username of the login
18+
# * 'name'<~String> - Name of the server
19+
# * 'notes'<~String> - Notes about the server
20+
# * 'state'<~String> - State of the server
21+
#
22+
def get_server(server_id)
23+
request(
24+
:expects => 200,
25+
:method => 'GET',
26+
:parser => Fog::ToHashDocument.new,
27+
:path => 'api/getServer',
28+
:query => {'serverId' => server_id}
29+
)
30+
end
31+
32+
end
33+
34+
class Mock
35+
36+
def get_server(server_id)
37+
Fog::Mock.not_implemented
38+
end
39+
40+
end
41+
end
42+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Fog
2+
module NewServers
3+
class Real
4+
5+
# List images
6+
#
7+
# ==== Returns
8+
# * response<~Excon::Response>:
9+
# * body<~Array>:
10+
# * 'id'<~String> - Id of the image
11+
# * 'name'<~String> - Name of the image
12+
# * 'size'<~String> - Size of the image
13+
#
14+
def list_images
15+
request(
16+
:expects => 200,
17+
:method => 'GET',
18+
:parser => Fog::ToHashDocument.new,
19+
:path => 'api/listImages'
20+
)
21+
end
22+
23+
end
24+
25+
class Mock
26+
27+
def list_images
28+
Fog::Mock.not_implemented
29+
end
30+
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)