mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #1020 from bradgignac/rackspace_cloud_database
Rackspace Cloud Database (Ready for Review)
This commit is contained in:
commit
7435b8d5d4
41 changed files with 1153 additions and 0 deletions
|
@ -15,6 +15,8 @@ class Rackspace < Fog::Bin
|
|||
Fog::DNS::Rackspace
|
||||
when :identity
|
||||
Fog::Rackspace::Identity
|
||||
when :databases
|
||||
Fog::Rackspace::Databases
|
||||
else
|
||||
raise ArgumentError, "Unrecognized service: #{key}"
|
||||
end
|
||||
|
|
|
@ -48,6 +48,7 @@ module Fog
|
|||
service(:storage, 'rackspace/storage', 'Storage')
|
||||
service(:load_balancers, 'rackspace/load_balancers', 'LoadBalancers')
|
||||
service(:identity, 'rackspace/identity', 'Identity')
|
||||
service(:databases, 'rackspace/databases', 'Databases')
|
||||
|
||||
def self.authenticate(options, connection_options = {})
|
||||
rackspace_auth_url = options[:rackspace_auth_url] || "auth.api.rackspacecloud.com"
|
||||
|
|
121
lib/fog/rackspace/databases.rb
Normal file
121
lib/fog/rackspace/databases.rb
Normal file
|
@ -0,0 +1,121 @@
|
|||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rackspace'))
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases < Fog::Service
|
||||
|
||||
class ServiceError < Fog::Rackspace::Errors::ServiceError; end
|
||||
class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end
|
||||
class BadRequest < Fog::Rackspace::Errors::BadRequest; end
|
||||
|
||||
DFW_ENDPOINT = 'https://dfw.databases.api.rackspacecloud.com/v1.0/'
|
||||
LON_ENDPOINT = 'https://lon.databases.api.rackspacecloud.com/v1.0/'
|
||||
ORD_ENDPOINT = 'https://ord.databases.api.rackspacecloud.com/v1.0/'
|
||||
|
||||
requires :rackspace_api_key, :rackspace_username
|
||||
recognizes :rackspace_auth_url
|
||||
recognizes :rackspace_auth_token
|
||||
recognizes :rackspace_endpoint
|
||||
|
||||
model_path 'fog/rackspace/models/databases'
|
||||
model :flavor
|
||||
collection :flavors
|
||||
model :instance
|
||||
collection :instances
|
||||
model :database
|
||||
collection :databases
|
||||
model :user
|
||||
collection :users
|
||||
|
||||
request_path 'fog/rackspace/requests/databases'
|
||||
request :list_flavors
|
||||
request :get_flavor
|
||||
|
||||
request :list_instances
|
||||
request :get_instance
|
||||
request :create_instance
|
||||
request :delete_instance
|
||||
request :check_root_user
|
||||
request :enable_root_user
|
||||
request :restart_instance
|
||||
request :resize_instance
|
||||
request :resize_instance_volume
|
||||
|
||||
request :list_databases
|
||||
request :create_database
|
||||
request :delete_database
|
||||
|
||||
request :list_users
|
||||
request :create_user
|
||||
request :delete_user
|
||||
|
||||
class Mock
|
||||
def request(params)
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
def initialize(options = {})
|
||||
@rackspace_api_key = options[:rackspace_api_key]
|
||||
@rackspace_username = options[:rackspace_username]
|
||||
@rackspace_auth_url = options[:rackspace_auth_url]
|
||||
@rackspace_must_reauthenticate = false
|
||||
@connection_options = options[:connection_options] || {}
|
||||
|
||||
endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT
|
||||
uri = URI.parse(endpoint)
|
||||
|
||||
@host = uri.host
|
||||
@persistent = options[:persistent] || false
|
||||
@path = uri.path
|
||||
@port = uri.port
|
||||
@scheme = uri.scheme
|
||||
|
||||
authenticate
|
||||
|
||||
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
|
||||
end
|
||||
|
||||
def request(params)
|
||||
begin
|
||||
response = @connection.request(params.merge!({
|
||||
:headers => {
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Auth-Token' => @auth_token
|
||||
}.merge!(params[:headers] || {}),
|
||||
:host => @host,
|
||||
:path => "#{@path}/#{params[:path]}"
|
||||
}))
|
||||
rescue Excon::Errors::NotFound => error
|
||||
raise NotFound.slurp error
|
||||
rescue Excon::Errors::BadRequest => error
|
||||
raise BadRequest.slurp error
|
||||
rescue Excon::Errors::InternalServerError => error
|
||||
raise InternalServerError.slurp error
|
||||
rescue Excon::Errors::HTTPStatusError => error
|
||||
raise ServiceError.slurp error
|
||||
end
|
||||
unless response.body.empty?
|
||||
response.body = Fog::JSON.decode(response.body)
|
||||
end
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authenticate
|
||||
options = {
|
||||
:rackspace_api_key => @rackspace_api_key,
|
||||
:rackspace_username => @rackspace_username,
|
||||
:rackspace_auth_url => @rackspace_auth_url
|
||||
}
|
||||
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
||||
@auth_token = credentials['X-Auth-Token']
|
||||
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
|
||||
@path = "#{@path}/#{account_id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/rackspace/models/databases/database.rb
Normal file
32
lib/fog/rackspace/models/databases/database.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Database < Fog::Model
|
||||
identity :name
|
||||
|
||||
attribute :character_set
|
||||
attribute :collate
|
||||
|
||||
def save
|
||||
requires :identity, :instance
|
||||
connection.create_database(instance.identity, identity, :character_set => character_set, :collate => collate)
|
||||
true
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :identity, :instance
|
||||
connection.delete_database(instance.identity, identity)
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def instance
|
||||
collection.instance
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
lib/fog/rackspace/models/databases/databases.rb
Normal file
31
lib/fog/rackspace/models/databases/databases.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/databases/database'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Databases < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Databases::Database
|
||||
|
||||
attr_accessor :instance
|
||||
|
||||
def all
|
||||
load(retrieve_databases)
|
||||
end
|
||||
|
||||
def get(database_name)
|
||||
data = retrieve_databases.find { |database| database['name'] == database_name }
|
||||
data && new(data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def retrieve_databases
|
||||
requires :instance
|
||||
data = connection.list_databases(instance.id).body['databases']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/models/databases/flavor.rb
Normal file
15
lib/fog/rackspace/models/databases/flavor.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Flavor < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :ram
|
||||
attribute :links
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
lib/fog/rackspace/models/databases/flavors.rb
Normal file
25
lib/fog/rackspace/models/databases/flavors.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/databases/flavor'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Flavors < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Databases::Flavor
|
||||
|
||||
def all
|
||||
data = connection.list_flavors.body['flavors']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(flavor_id)
|
||||
data = connection.get_flavor(flavor_id).body['flavor']
|
||||
new(data)
|
||||
rescue Fog::Rackspace::Databases::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
104
lib/fog/rackspace/models/databases/instance.rb
Normal file
104
lib/fog/rackspace/models/databases/instance.rb
Normal file
|
@ -0,0 +1,104 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Instance < Fog::Model
|
||||
# States
|
||||
ACTIVE = 'ACTIVE'
|
||||
BUILD = 'BUILD'
|
||||
BLOCKED = 'BLOCKED'
|
||||
REBOOT = 'REBOOT'
|
||||
RESIZE = 'RESIZE'
|
||||
SHUTDOWN = 'SHUTDOWN'
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :created
|
||||
attribute :updated
|
||||
attribute :state, :aliases => 'status'
|
||||
attribute :hostname
|
||||
attribute :links
|
||||
attribute :flavor_id, :aliases => 'flavor', :squash => 'id'
|
||||
attribute :volume_size, :aliases => 'volume', :squash => 'size'
|
||||
|
||||
attr_accessor :root_user, :root_password
|
||||
|
||||
def save
|
||||
requires :name, :flavor_id, :volume_size
|
||||
data = connection.create_instance(name, flavor_id, volume_size)
|
||||
merge_attributes(data.body['instance'])
|
||||
true
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :identity
|
||||
connection.delete_instance(identity)
|
||||
true
|
||||
end
|
||||
|
||||
def flavor
|
||||
requires :flavor_id
|
||||
@flavor ||= connection.flavors.get(flavor_id)
|
||||
end
|
||||
|
||||
def databases
|
||||
@databases ||= begin
|
||||
Fog::Rackspace::Databases::Databases.new({
|
||||
:connection => connection,
|
||||
:instance => self
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def users
|
||||
@users ||= begin
|
||||
Fog::Rackspace::Databases::Users.new({
|
||||
:connection => connection,
|
||||
:instance => self
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def ready?
|
||||
state == ACTIVE
|
||||
end
|
||||
|
||||
def root_user_enabled?
|
||||
requires :identity
|
||||
connection.check_root_user(identity).body['rootEnabled']
|
||||
end
|
||||
|
||||
def enable_root_user
|
||||
requires :identity
|
||||
data = connection.enable_root_user(identity).body['user']
|
||||
@root_user = data['name']
|
||||
@root_password = data['password']
|
||||
true
|
||||
end
|
||||
|
||||
def restart
|
||||
requires :identity
|
||||
connection.restart_instance(identity)
|
||||
self.state = REBOOT
|
||||
true
|
||||
end
|
||||
|
||||
def resize(flavor_id)
|
||||
requires :identity
|
||||
connection.resize_instance(identity, flavor_id)
|
||||
self.state = RESIZE
|
||||
true
|
||||
end
|
||||
|
||||
def resize_volume(volume_size)
|
||||
requires :identity
|
||||
connection.resize_instance_volume(identity, volume_size)
|
||||
self.state = RESIZE
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
lib/fog/rackspace/models/databases/instances.rb
Normal file
25
lib/fog/rackspace/models/databases/instances.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/databases/instance'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Instances < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Databases::Instance
|
||||
|
||||
def all
|
||||
data = connection.list_instances.body['instances']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(instance_id)
|
||||
data = connection.get_instance(instance_id).body['instance']
|
||||
new(data)
|
||||
rescue Fog::Rackspace::Databases::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/rackspace/models/databases/user.rb
Normal file
32
lib/fog/rackspace/models/databases/user.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class User < Fog::Model
|
||||
identity :name
|
||||
|
||||
attribute :password
|
||||
attribute :databases
|
||||
|
||||
def save
|
||||
requires :identity, :instance, :password
|
||||
connection.create_user(instance.identity, identity, password, :databases => databases)
|
||||
true
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :identity, :instance
|
||||
connection.delete_user(instance.identity, identity)
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def instance
|
||||
collection.instance
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
lib/fog/rackspace/models/databases/users.rb
Normal file
31
lib/fog/rackspace/models/databases/users.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/databases/user'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Users < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Databases::User
|
||||
|
||||
attr_accessor :instance
|
||||
|
||||
def all
|
||||
load(retrieve_users)
|
||||
end
|
||||
|
||||
def get(user_name)
|
||||
data = retrieve_users.find { |database| database['name'] == user_name }
|
||||
data && new(data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def retrieve_users
|
||||
requires :instance
|
||||
data = connection.list_users(instance.identity).body['users']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/check_root_user.rb
Normal file
15
lib/fog/rackspace/requests/databases/check_root_user.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def check_root_user(instance_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => "instances/#{instance_id}/root"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
24
lib/fog/rackspace/requests/databases/create_database.rb
Normal file
24
lib/fog/rackspace/requests/databases/create_database.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def create_database(instance_id, name, options = {})
|
||||
data = {
|
||||
'databases' => [{
|
||||
'name' => name,
|
||||
'character_set' => options[:character_set],
|
||||
'collate' => options[:collate]
|
||||
}]
|
||||
}
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 202,
|
||||
:method => 'POST',
|
||||
:path => "instances/#{instance_id}/databases"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/rackspace/requests/databases/create_instance.rb
Normal file
26
lib/fog/rackspace/requests/databases/create_instance.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def create_instance(name, flavor_id, volume_size, options = {})
|
||||
data = {
|
||||
'instance' => {
|
||||
'name' => name,
|
||||
'flavorRef' => flavor_id,
|
||||
'volume' => {
|
||||
'size' => volume_size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 200,
|
||||
:method => 'POST',
|
||||
:path => 'instances'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
24
lib/fog/rackspace/requests/databases/create_user.rb
Normal file
24
lib/fog/rackspace/requests/databases/create_user.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def create_user(instance_id, name, password, options = {})
|
||||
data = {
|
||||
'users' => [{
|
||||
'name' => name,
|
||||
'password' => password,
|
||||
'databases' => options[:databases]
|
||||
}]
|
||||
}
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 202,
|
||||
:method => 'POST',
|
||||
:path => "instances/#{instance_id}/users"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/delete_database.rb
Normal file
15
lib/fog/rackspace/requests/databases/delete_database.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def delete_database(instance_id, name)
|
||||
request(
|
||||
:expects => 202,
|
||||
:method => 'DELETE',
|
||||
:path => "instances/#{instance_id}/databases/#{name}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/delete_instance.rb
Normal file
15
lib/fog/rackspace/requests/databases/delete_instance.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def delete_instance(instance_id)
|
||||
request(
|
||||
:expects => 202,
|
||||
:method => 'DELETE',
|
||||
:path => "instances/#{instance_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/delete_user.rb
Normal file
15
lib/fog/rackspace/requests/databases/delete_user.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def delete_user(instance_id, name)
|
||||
request(
|
||||
:expects => 202,
|
||||
:method => 'DELETE',
|
||||
:path => "instances/#{instance_id}/users/#{name}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/enable_root_user.rb
Normal file
15
lib/fog/rackspace/requests/databases/enable_root_user.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def enable_root_user(instance_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'POST',
|
||||
:path => "instances/#{instance_id}/root"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/get_flavor.rb
Normal file
15
lib/fog/rackspace/requests/databases/get_flavor.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def get_flavor(flavor_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => "flavors/#{flavor_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/get_instance.rb
Normal file
15
lib/fog/rackspace/requests/databases/get_instance.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def get_instance(instance_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => "instances/#{instance_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/list_databases.rb
Normal file
15
lib/fog/rackspace/requests/databases/list_databases.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def list_databases(instance_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => "instances/#{instance_id}/databases"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/list_flavors.rb
Normal file
15
lib/fog/rackspace/requests/databases/list_flavors.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def list_flavors()
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => 'flavors'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/list_instances.rb
Normal file
15
lib/fog/rackspace/requests/databases/list_instances.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def list_instances()
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => 'instances'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/requests/databases/list_users.rb
Normal file
15
lib/fog/rackspace/requests/databases/list_users.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def list_users(instance_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => "instances/#{instance_id}/users"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
22
lib/fog/rackspace/requests/databases/resize_instance.rb
Normal file
22
lib/fog/rackspace/requests/databases/resize_instance.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def resize_instance(instance_id, flavor_id)
|
||||
data = {
|
||||
'resize' => {
|
||||
'flavorRef' => flavor_id
|
||||
}
|
||||
}
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 202,
|
||||
:method => 'POST',
|
||||
:path => "instances/#{instance_id}/action"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def resize_instance_volume(instance_id, volume_size)
|
||||
data = {
|
||||
'resize' => {
|
||||
'volume' => {
|
||||
'size' => volume_size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 202,
|
||||
:method => 'POST',
|
||||
:path => "instances/#{instance_id}/action"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
20
lib/fog/rackspace/requests/databases/restart_instance.rb
Normal file
20
lib/fog/rackspace/requests/databases/restart_instance.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Databases
|
||||
class Real
|
||||
def restart_instance(instance_id)
|
||||
data = {
|
||||
'restart' => {}
|
||||
}
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 202,
|
||||
:method => 'POST',
|
||||
:path => "instances/#{instance_id}/action"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
26
tests/rackspace/databases_tests.rb
Normal file
26
tests/rackspace/databases_tests.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases', ['rackspace']) do |variable|
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
@service = Fog::Rackspace::Databases.new
|
||||
|
||||
tests('#flavors').succeeds do
|
||||
data = @service.flavors
|
||||
returns(true) { data.is_a? Array }
|
||||
end
|
||||
|
||||
tests('#instances').succeeds do
|
||||
data = @service.instances
|
||||
returns(true) { data.is_a? Array }
|
||||
end
|
||||
|
||||
tests('#databases').succeeds do
|
||||
data = @service.databases
|
||||
returns(true) { data.is_a? Array }
|
||||
end
|
||||
|
||||
tests('#users').succeeds do
|
||||
data = @service.users
|
||||
returns(true) { data.is_a? Array }
|
||||
end
|
||||
end
|
17
tests/rackspace/models/databases/database_tests.rb
Normal file
17
tests/rackspace/models/databases/database_tests.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | database', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
instance = service.instances.create({
|
||||
:name => "fog_instance_#{Time.now.to_i.to_s}",
|
||||
:flavor_id => 1,
|
||||
:volume_size => 1
|
||||
})
|
||||
|
||||
instance.wait_for { ready? }
|
||||
|
||||
model_tests(instance.databases, { :name => "db_#{Time.now.to_i.to_s}" }, false)
|
||||
|
||||
instance.destroy
|
||||
end
|
17
tests/rackspace/models/databases/databases_tests.rb
Normal file
17
tests/rackspace/models/databases/databases_tests.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | databases', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
instance = service.instances.create({
|
||||
:name => "fog_instance_#{Time.now.to_i.to_s}",
|
||||
:flavor_id => 1,
|
||||
:volume_size => 1
|
||||
})
|
||||
|
||||
instance.wait_for { ready? }
|
||||
|
||||
collection_tests(instance.databases, { :name => "db_#{Time.now.to_i.to_s}" }, false)
|
||||
|
||||
instance.destroy
|
||||
end
|
20
tests/rackspace/models/databases/flavors_tests.rb
Normal file
20
tests/rackspace/models/databases/flavors_tests.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | flavors', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
|
||||
tests("success") do
|
||||
tests("#all").succeeds do
|
||||
service.flavors.all
|
||||
end
|
||||
|
||||
tests("#get").succeeds do
|
||||
service.flavors.get(1)
|
||||
end
|
||||
end
|
||||
|
||||
tests("failure").returns(nil) do
|
||||
service.flavors.get('some_random_identity')
|
||||
end
|
||||
end
|
43
tests/rackspace/models/databases/instance_tests.rb
Normal file
43
tests/rackspace/models/databases/instance_tests.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | instance', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
options = {
|
||||
:name => "fog_instance_#{Time.now.to_i.to_s}",
|
||||
:flavor_id => 1,
|
||||
:volume_size => 1
|
||||
}
|
||||
|
||||
model_tests(service.instances, options, false) do
|
||||
@instance.wait_for { ready? }
|
||||
tests('root_user_enabled before user is enabled').returns(false) do
|
||||
@instance.root_user_enabled?
|
||||
end
|
||||
|
||||
@instance.wait_for { ready? }
|
||||
tests('enable_root_user sets root user and password').succeeds do
|
||||
@instance.enable_root_user
|
||||
returns(false) { @instance.root_user.nil? }
|
||||
returns(false) { @instance.root_password.nil? }
|
||||
end
|
||||
|
||||
@instance.wait_for { ready? }
|
||||
tests('restarts instance').succeeds do
|
||||
@instance.restart
|
||||
returns('REBOOT') { @instance.state }
|
||||
end
|
||||
|
||||
@instance.wait_for { ready? }
|
||||
tests('resizes instance').succeeds do
|
||||
@instance.resize(2)
|
||||
returns('RESIZE') { @instance.state }
|
||||
end
|
||||
|
||||
@instance.wait_for { ready? }
|
||||
tests('restarts instance').succeeds do
|
||||
@instance.resize_volume(2)
|
||||
returns('RESIZE') { @instance.state }
|
||||
end
|
||||
end
|
||||
end
|
14
tests/rackspace/models/databases/instances_tests.rb
Normal file
14
tests/rackspace/models/databases/instances_tests.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | instances', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
options = {
|
||||
:name => "fog_instance_#{Time.now.to_i.to_s}",
|
||||
:flavor_id => 1,
|
||||
:volume_size => 1
|
||||
}
|
||||
collection_tests(service.instances, options, false) do
|
||||
@instance.wait_for { ready? }
|
||||
end
|
||||
end
|
21
tests/rackspace/models/databases/user_tests.rb
Normal file
21
tests/rackspace/models/databases/user_tests.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | user', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
instance = service.instances.create({
|
||||
:name => "fog_instance_#{Time.now.to_i.to_s}",
|
||||
:flavor_id => 1,
|
||||
:volume_size => 1
|
||||
})
|
||||
|
||||
instance.wait_for { ready? }
|
||||
|
||||
options = {
|
||||
:name => "user_#{Time.now.to_i.to_s}",
|
||||
:password => "fog_user"
|
||||
}
|
||||
model_tests(instance.users, options, false)
|
||||
|
||||
instance.destroy
|
||||
end
|
21
tests/rackspace/models/databases/users_tests.rb
Normal file
21
tests/rackspace/models/databases/users_tests.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
Shindo.tests('Fog::Rackspace::Databases | users', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
instance = service.instances.create({
|
||||
:name => "fog_instance_#{Time.now.to_i.to_s}",
|
||||
:flavor_id => 1,
|
||||
:volume_size => 1
|
||||
})
|
||||
|
||||
instance.wait_for { ready? }
|
||||
|
||||
options = {
|
||||
:name => "user_#{Time.now.to_i.to_s}",
|
||||
:password => "fog_user"
|
||||
}
|
||||
collection_tests(instance.users, options, false)
|
||||
|
||||
instance.destroy
|
||||
end
|
36
tests/rackspace/requests/databases/database_tests.rb
Normal file
36
tests/rackspace/requests/databases/database_tests.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
Shindo.tests('Fog::Rackspace::Database | database_tests', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
instance_name = 'fog' + Time.now.to_i.to_s
|
||||
instance_id = service.create_instance(instance_name, 1, 1).body['instance']['id']
|
||||
|
||||
until service.get_instance(instance_id).body["instance"]["status"] == 'ACTIVE'
|
||||
sleep 10
|
||||
end
|
||||
|
||||
tests('success') do
|
||||
database_name = 'fogdb' + Time.now.to_i.to_s
|
||||
|
||||
tests("#create_database(#{instance_id}, #{database_name})").succeeds do
|
||||
service.create_database(instance_id, database_name).body
|
||||
end
|
||||
|
||||
tests("#list_databases{#{instance_id})").formats(LIST_DATABASES_FORMAT) do
|
||||
service.list_databases(instance_id).body
|
||||
end
|
||||
|
||||
tests("#delete_database(#{instance_id}, #{database_name})").succeeds do
|
||||
service.delete_database(instance_id, database_name)
|
||||
end
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
tests("#create_database(#{instance_id}, '') => Invalid Create Critera").raises(Fog::Rackspace::Databases::BadRequest) do
|
||||
service.create_database(instance_id, '')
|
||||
end
|
||||
end
|
||||
|
||||
service.delete_instance(instance_id)
|
||||
end
|
16
tests/rackspace/requests/databases/flavor_tests.rb
Normal file
16
tests/rackspace/requests/databases/flavor_tests.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
Shindo.tests('Fog::Rackspace::Database | flavor_tests', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
|
||||
tests('success') do
|
||||
tests('#list_flavors_details').formats(LIST_FLAVORS_FORMAT) do
|
||||
service.list_flavors().body
|
||||
end
|
||||
|
||||
tests('#get_flavor(1)').formats(GET_FLAVOR_FORMAT) do
|
||||
service.get_flavor(1).body
|
||||
end
|
||||
end
|
||||
end
|
84
tests/rackspace/requests/databases/helper.rb
Normal file
84
tests/rackspace/requests/databases/helper.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
LINKS_FORMAT = [{
|
||||
'href' => String,
|
||||
'rel' => String
|
||||
}]
|
||||
|
||||
FLAVOR_FORMAT = {
|
||||
'id' => Integer,
|
||||
'name' => String,
|
||||
'ram' => Integer,
|
||||
'links' => LINKS_FORMAT
|
||||
}
|
||||
|
||||
GET_FLAVOR_FORMAT = {
|
||||
'flavor' => FLAVOR_FORMAT
|
||||
}
|
||||
|
||||
LIST_FLAVORS_FORMAT = {
|
||||
'flavors' => [FLAVOR_FORMAT]
|
||||
}
|
||||
|
||||
INSTANCE_FORMAT = {
|
||||
'id' => String,
|
||||
'name' => String,
|
||||
'status' => String,
|
||||
'links' => LINKS_FORMAT,
|
||||
'flavor' => {
|
||||
'id' => String,
|
||||
'links' => LINKS_FORMAT
|
||||
},
|
||||
'volume' => {
|
||||
'size' => Integer
|
||||
}
|
||||
}
|
||||
|
||||
INSTANCE_DETAILS_FORMAT = INSTANCE_FORMAT.merge({
|
||||
'created' => String,
|
||||
'updated' => String,
|
||||
'hostname' => String,
|
||||
})
|
||||
|
||||
CREATE_INSTANCE_FORMAT = {
|
||||
'instance' => INSTANCE_DETAILS_FORMAT
|
||||
}
|
||||
|
||||
GET_INSTANCE_FORMAT = {
|
||||
'instance' => INSTANCE_DETAILS_FORMAT.merge({
|
||||
'volume' => {
|
||||
'size' => Integer,
|
||||
'used' => Float
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
LIST_INSTANCES_FORMAT = {
|
||||
'instances' => [
|
||||
INSTANCE_FORMAT
|
||||
]
|
||||
}
|
||||
|
||||
CHECK_ROOT_USER_FORMAT = {
|
||||
'rootEnabled' => Fog::Boolean
|
||||
}
|
||||
|
||||
ENABLE_ROOT_USER_FORMAT = {
|
||||
'user' => {
|
||||
'name' => String,
|
||||
'password' => String
|
||||
}
|
||||
}
|
||||
|
||||
LIST_DATABASES_FORMAT = {
|
||||
'databases' => [{
|
||||
'name' => String
|
||||
}]
|
||||
}
|
||||
|
||||
LIST_USERS_FORMAT = {
|
||||
'users' => [{
|
||||
'name' => String,
|
||||
'databases' => [{
|
||||
'name' => String
|
||||
}]
|
||||
}]
|
||||
}
|
77
tests/rackspace/requests/databases/instance_tests.rb
Normal file
77
tests/rackspace/requests/databases/instance_tests.rb
Normal file
|
@ -0,0 +1,77 @@
|
|||
Shindo.tests('Fog::Rackspace::Database | instance_tests', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
|
||||
tests('success') do
|
||||
|
||||
instance_id = nil
|
||||
instance_name = 'fog' + Time.now.to_i.to_s
|
||||
|
||||
tests("#list_instances").formats(LIST_INSTANCES_FORMAT) do
|
||||
service.list_instances.body
|
||||
end
|
||||
|
||||
tests("#create_instance(#{instance_name}, 1, 1)").formats(CREATE_INSTANCE_FORMAT) do
|
||||
data = service.create_instance(instance_name, 1, 1).body
|
||||
instance_id = data['instance']['id']
|
||||
data
|
||||
end
|
||||
|
||||
until service.get_instance(instance_id).body["instance"]["status"] == 'ACTIVE'
|
||||
sleep 10
|
||||
end
|
||||
|
||||
tests("#get_instance(#{instance_id})").formats(GET_INSTANCE_FORMAT) do
|
||||
service.get_instance(instance_id).body
|
||||
end
|
||||
|
||||
tests("#check_root_user(#{instance_id})").formats(CHECK_ROOT_USER_FORMAT) do
|
||||
service.check_root_user(instance_id).body
|
||||
end
|
||||
|
||||
tests("#enable_root_user(#{instance_id})").formats(ENABLE_ROOT_USER_FORMAT) do
|
||||
service.enable_root_user(instance_id).body
|
||||
end
|
||||
|
||||
tests("#restart_instance(#{instance_id})").succeeds do
|
||||
service.restart_instance(instance_id)
|
||||
end
|
||||
|
||||
until service.get_instance(instance_id).body["instance"]["status"] == 'ACTIVE'
|
||||
sleep 10
|
||||
end
|
||||
|
||||
tests("#resize_instance(#{instance_id}, 2)").succeeds do
|
||||
service.resize_instance(instance_id, 2)
|
||||
end
|
||||
|
||||
until service.get_instance(instance_id).body["instance"]["status"] == 'ACTIVE'
|
||||
sleep 10
|
||||
end
|
||||
|
||||
tests("#resize_instance_volume(#{instance_id}, 2)").succeeds do
|
||||
service.resize_instance_volume(instance_id, 2)
|
||||
end
|
||||
|
||||
until service.get_instance(instance_id).body["instance"]["status"] == 'ACTIVE'
|
||||
sleep 10
|
||||
end
|
||||
|
||||
tests("#delete_instance(#{instance_id})").succeeds do
|
||||
service.delete_instance(instance_id)
|
||||
end
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
tests("#create_instance('', 0, 0) => Invalid Create Critera").raises(Fog::Rackspace::Databases::BadRequest) do
|
||||
service.create_instance('', 0, 0)
|
||||
end
|
||||
|
||||
tests("#get_instance('') => Does not exist").raises(Fog::Rackspace::Databases::NotFound) do
|
||||
service.get_instance('')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
37
tests/rackspace/requests/databases/user_tests.rb
Normal file
37
tests/rackspace/requests/databases/user_tests.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
Shindo.tests('Fog::Rackspace::Database | user_tests', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Databases.new
|
||||
instance_name = 'fog' + Time.now.to_i.to_s
|
||||
instance_id = service.create_instance(instance_name, 1, 1).body['instance']['id']
|
||||
|
||||
until service.get_instance(instance_id).body["instance"]["status"] == 'ACTIVE'
|
||||
sleep 10
|
||||
end
|
||||
|
||||
tests('success') do
|
||||
user_name = 'fog' + Time.now.to_i.to_s
|
||||
password = 'password1'
|
||||
|
||||
tests("#create_user(#{instance_id}, #{user_name}, #{password})").succeeds do
|
||||
service.create_user(instance_id, user_name, password).body
|
||||
end
|
||||
|
||||
tests("#list_users{#{instance_id})").formats(LIST_USERS_FORMAT) do
|
||||
service.list_users(instance_id).body
|
||||
end
|
||||
|
||||
tests("#delete_user(#{instance_id}, #{user_name})").succeeds do
|
||||
service.delete_user(instance_id, user_name)
|
||||
end
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
tests("#create_user(#{instance_id}, '', '') => Invalid Create Critera").raises(Fog::Rackspace::Databases::BadRequest) do
|
||||
service.create_user(instance_id, '', '')
|
||||
end
|
||||
end
|
||||
|
||||
service.delete_instance(instance_id)
|
||||
end
|
Loading…
Add table
Reference in a new issue