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

[rackspace|databases] Add write support for Rackspace Cloud Databases.

This commit is contained in:
Brad Gignac 2012-06-21 17:47:52 -04:00 committed by Brad Gignac
parent 2b6ced4f58
commit a36a6c9239
15 changed files with 311 additions and 6 deletions

View file

@ -10,6 +10,7 @@ module Fog
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
@ -24,11 +25,24 @@ module Fog
request_path 'fog/rackspace/requests/databases'
request :list_flavors_details
request :get_flavor
request :list_instances_details
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)
@ -44,7 +58,7 @@ module Fog
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
endpoint = options[:rackspace_databases_endpoint] || DFW_ENDPOINT
endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT
uri = URI.parse(endpoint)
@host = uri.host

View file

@ -5,6 +5,27 @@ module Fog
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

View file

@ -7,13 +7,33 @@ module Fog
identity :id
attribute :name
attribute :status
attribute :hostname
attribute :created
attribute :updated
attribute :state, :aliases => 'status'
attribute :hostname
attribute :links
attribute :volume
attribute :flavor
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
@ -37,6 +57,32 @@ module Fog
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)
true
end
def resize(flavor_id)
requires :identity
connection.resize_instance(identity, flavor_id)
true
end
def resize_volume(volume_size)
requires :identity
connection.resize_instance_volume(identity, volume_size)
true
end
end
end
end

View file

@ -5,6 +5,27 @@ module Fog
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

View file

@ -23,7 +23,7 @@ module Fog
def retrieve_users
requires :instance
data = connection.list_users(instance.id).body['users']
data = connection.list_users(instance.identity).body['users']
end
end
end

View 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

View file

@ -0,0 +1,29 @@
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
}
}
}
# Add databases
# Add users
request(
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'instances'
)
end
end
end
end
end

View 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

View 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

View 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

View 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

View 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

View 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

View file

@ -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

View 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