Class: Fog::Rackspace::Databases::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/databases.rb,
lib/fog/rackspace/requests/databases/get_flavor.rb,
lib/fog/rackspace/requests/databases/list_users.rb,
lib/fog/rackspace/requests/databases/create_user.rb,
lib/fog/rackspace/requests/databases/delete_user.rb,
lib/fog/rackspace/requests/databases/get_instance.rb,
lib/fog/rackspace/requests/databases/list_flavors.rb,
lib/fog/rackspace/requests/databases/list_databases.rb,
lib/fog/rackspace/requests/databases/list_instances.rb,
lib/fog/rackspace/requests/databases/check_root_user.rb,
lib/fog/rackspace/requests/databases/create_database.rb,
lib/fog/rackspace/requests/databases/create_instance.rb,
lib/fog/rackspace/requests/databases/delete_database.rb,
lib/fog/rackspace/requests/databases/delete_instance.rb,
lib/fog/rackspace/requests/databases/resize_instance.rb,
lib/fog/rackspace/requests/databases/enable_root_user.rb,
lib/fog/rackspace/requests/databases/restart_instance.rb,
lib/fog/rackspace/requests/databases/resize_instance_volume.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/rackspace/databases.rb', line 59

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

Instance Method Details

#check_root_user(instance_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/check_root_user.rb', line 5

def check_root_user(instance_id)
  request(
    :expects => 200,
    :method => 'GET',
    :path => "instances/#{instance_id}/root"
  )
end

#create_database(instance_id, name, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/rackspace/requests/databases/create_database.rb', line 5

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

#create_instance(name, flavor_id, volume_size, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/rackspace/requests/databases/create_instance.rb', line 5

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

#create_user(instance_id, name, password, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/rackspace/requests/databases/create_user.rb', line 5

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

#delete_database(instance_id, name) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/delete_database.rb', line 5

def delete_database(instance_id, name)
  request(
    :expects => 202,
    :method => 'DELETE',
    :path => "instances/#{instance_id}/databases/#{name}"
  )
end

#delete_instance(instance_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/delete_instance.rb', line 5

def delete_instance(instance_id)
  request(
    :expects => 202,
    :method => 'DELETE',
    :path => "instances/#{instance_id}"
  )
end

#delete_user(instance_id, name) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/delete_user.rb', line 5

def delete_user(instance_id, name)
  request(
    :expects => 202,
    :method => 'DELETE',
    :path => "instances/#{instance_id}/users/#{name}"
  )
end

#enable_root_user(instance_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/enable_root_user.rb', line 5

def enable_root_user(instance_id)
  request(
    :expects => 200,
    :method => 'POST',
    :path => "instances/#{instance_id}/root"
  )
end

#get_flavor(flavor_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/get_flavor.rb', line 5

def get_flavor(flavor_id)
  request(
    :expects => 200,
    :method => 'GET',
    :path => "flavors/#{flavor_id}"
  )
end

#get_instance(instance_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/get_instance.rb', line 5

def get_instance(instance_id)
  request(
    :expects => 200,
    :method => 'GET',
    :path => "instances/#{instance_id}"
  )
end

#list_databases(instance_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/list_databases.rb', line 5

def list_databases(instance_id)
  request(
    :expects => 200,
    :method => 'GET',
    :path => "instances/#{instance_id}/databases"
  )
end

#list_flavorsObject



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/list_flavors.rb', line 5

def list_flavors()
  request(
    :expects => 200,
    :method => 'GET',
    :path => 'flavors'
  )
end

#list_instancesObject



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/list_instances.rb', line 5

def list_instances()
  request(
    :expects => 200,
    :method => 'GET',
    :path => 'instances'
  )
end

#list_users(instance_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/rackspace/requests/databases/list_users.rb', line 5

def list_users(instance_id)
  request(
    :expects => 200,
    :method => 'GET',
    :path => "instances/#{instance_id}/users"
  )
end

#request(params) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fog/rackspace/databases.rb', line 80

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

#resize_instance(instance_id, flavor_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/databases/resize_instance.rb', line 5

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

#resize_instance_volume(instance_id, volume_size) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/rackspace/requests/databases/resize_instance_volume.rb', line 5

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

#restart_instance(instance_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/rackspace/requests/databases/restart_instance.rb', line 5

def restart_instance(instance_id)
  data = {
    'restart' => {}
  }

  request(
    :body => Fog::JSON.encode(data),
    :expects => 202,
    :method => 'POST',
    :path => "instances/#{instance_id}/action"
  )
end