Class: CloudDB::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/clouddb/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id) ⇒ Instance

Creates a new CloudDB::Instance object representing a database instance.



19
20
21
22
23
24
25
26
27
28
# File 'lib/clouddb/instance.rb', line 19

def initialize(connection,id)
  @connection    = connection
  @id            = id
  @dbmgmthost   = connection.dbmgmthost
  @dbmgmtpath   = connection.dbmgmtpath
  @dbmgmtport   = connection.dbmgmtport
  @dbmgmtscheme = connection.dbmgmtscheme
  populate
  self
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/clouddb/instance.rb', line 4

def connection
  @connection
end

#createdObject (readonly)

Returns the value of attribute created.



14
15
16
# File 'lib/clouddb/instance.rb', line 14

def created
  @created
end

#flavor_idObject (readonly)

Returns the value of attribute flavor_id.



9
10
11
# File 'lib/clouddb/instance.rb', line 9

def flavor_id
  @flavor_id
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



8
9
10
# File 'lib/clouddb/instance.rb', line 8

def hostname
  @hostname
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/clouddb/instance.rb', line 6

def id
  @id
end

Returns the value of attribute links.



16
17
18
# File 'lib/clouddb/instance.rb', line 16

def links
  @links
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/clouddb/instance.rb', line 7

def name
  @name
end

#root_enabledObject (readonly)

Returns the value of attribute root_enabled.



10
11
12
# File 'lib/clouddb/instance.rb', line 10

def root_enabled
  @root_enabled
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/clouddb/instance.rb', line 13

def status
  @status
end

#updatedObject (readonly)

Returns the value of attribute updated.



15
16
17
# File 'lib/clouddb/instance.rb', line 15

def updated
  @updated
end

#volume_sizeObject (readonly)

Returns the value of attribute volume_size.



12
13
14
# File 'lib/clouddb/instance.rb', line 12

def volume_size
  @volume_size
end

#volume_usedObject (readonly)

Returns the value of attribute volume_used.



11
12
13
# File 'lib/clouddb/instance.rb', line 11

def volume_used
  @volume_used
end

Instance Method Details

#create_database(options = {}) ⇒ Object

Creates a brand new database and associates it with the current instance. Returns true if successful.

Options:

:name - Specifies the database name for creating the database. *required*
:character_set - Set of symbols and encodings. The default character set is utf8.
:collate - Set of rules for comparing characters in a character set. The default value for collate is
           utf8_general_ci.


102
103
104
105
106
# File 'lib/clouddb/instance.rb', line 102

def create_database(options={})
  new_databases = Array.new
  new_databases << options
  create_databases new_databases
end

#create_databases(databases) ⇒ Object

Creates brand new databases and associates them with the current instance. Returns true if successful.

Options for each database in the array:

:name - Specifies the database name for creating the database. *required*
:character_set - Set of symbols and encodings. The default character set is utf8.
:collate - Set of rules for comparing characters in a character set. The default value for collate is
           utf8_general_ci.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clouddb/instance.rb', line 74

def create_databases(databases)
  (raise CloudDB::Exception::Syntax, "Must provide at least one database in the array") if (!databases.is_a?(Array) || databases.size < 1)

  body = Hash.new
  body[:databases] = Array.new

  for database in databases
    new_database = Hash.new
    new_database[:name]          = database[:name] or raise CloudDB::Exception::MissingArgument, "Must provide a name for each database"
    new_database[:character_set] = database[:character_set] || 'utf8'
    new_database[:collate]       = database[:collate] || 'utf8_general_ci'
    (raise CloudDB::Exception::Syntax, "Database names must be 64 characters or less") if database[:name].size > 64

    body[:databases] << new_database
  end

  response = @connection.dbreq("POST", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/databases", @dbmgmtport, @dbmgmtscheme, {}, body.to_json)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  true
end

#create_user(options = {}) ⇒ Object

Creates a brand new user and associates it with the current instance. Returns true if successful.

Options:

:name - Name of the user for the database(s). *required*
:password - User password for database access. *required*
:databases - An array of databases with at least one database. *required*


159
160
161
162
163
# File 'lib/clouddb/instance.rb', line 159

def create_user(options={})
  new_users = Array.new
  new_users << options
  create_users new_users
end

#create_users(users) ⇒ Object

Creates brand new users and associates them with the current instance. Returns true if successful.

Options for each user in the array:

:name - Name of the user for the database(s). *required*
:password - User password for database access. *required*
:databases - An array of databases with at least one database. *required*


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/clouddb/instance.rb', line 131

def create_users(users)
  (raise CloudDB::Exception::Syntax, "Must provide at least one user in the array") if (!users.is_a?(Array) || users.size < 1)

  body = Hash.new
  body[:users] = Array.new

  for user in users
    new_user = Hash.new
    new_user[:name]      = user[:name] or raise CloudDB::Exception::MissingArgument, "Must provide a name for each user"
    new_user[:password]  = user[:password] or raise CloudDB::Exception::MissingArgument, "Must provide a password for each user"
    new_user[:databases] = user[:databases]
    (raise CloudDB::Exception::Syntax, "User names must be 16 characters or less") if user[:name].size > 16
    (raise CloudDB::Exception::Syntax, "Must provide at least one database in each user :databases array") if (!user[:databases].is_a?(Array) || user[:databases].size < 1)

    body[:users] << new_user
  end

  response = @connection.dbreq("POST", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/users", @dbmgmtport, @dbmgmtscheme, {}, body.to_json)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  true
end

#destroy!Object

Deletes the current instance object. Returns true if successful, raises an exception otherwise.



237
238
239
240
241
# File 'lib/clouddb/instance.rb', line 237

def destroy!
  response = @connection.dbreq("DELETE", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}", @dbmgmtport, @dbmgmtscheme)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^202$/)
  true
end

#enable_rootObject

Enables the root user for the specified database instance and returns the root password.

Example:

i.enable_root
=> true


170
171
172
173
174
175
176
# File 'lib/clouddb/instance.rb', line 170

def enable_root()
  response = @connection.dbreq("POST", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/root", @dbmgmtport, @dbmgmtscheme, {})
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  @root_enabled = true
  body = JSON.parse(response.body)['user']
  return body
end

#get_database(name) ⇒ Object Also known as: database

Returns a CloudDB::Database object for the given database name.



62
63
64
# File 'lib/clouddb/instance.rb', line 62

def get_database(name)
  CloudDB::Database.new(self, name)
end

#get_user(name) ⇒ Object Also known as: user

Returns a CloudDB::User object for the given user name.



120
121
122
# File 'lib/clouddb/instance.rb', line 120

def get_user(name)
  CloudDB::User.new(self, name)
end

#list_databasesObject Also known as: databases

Lists the databases associated with this instance

Example:

i.list_databases


54
55
56
57
58
# File 'lib/clouddb/instance.rb', line 54

def list_databases
  response = @connection.dbreq("GET", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/databases", @dbmgmtport, @dbmgmtscheme)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  CloudDB.symbolize_keys(JSON.parse(response.body)["databases"])
end

#list_usersObject Also known as: users

Lists the users associated with the current Instance

Example:

i.list_users


112
113
114
115
116
# File 'lib/clouddb/instance.rb', line 112

def list_users
  response = @connection.dbreq("GET", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/users", @dbmgmtport, @dbmgmtscheme)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  CloudDB.symbolize_keys(JSON.parse(response.body)["users"])
end

#populateObject Also known as: refresh

Updates the information about the current instance object by making an API call.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clouddb/instance.rb', line 31

def populate
  response = @connection.dbreq("GET", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}", @dbmgmtport, @dbmgmtscheme)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  data = JSON.parse(response.body)['instance']
  @id           = data["id"]
  @name         = data["name"]
  @hostname     = data["hostname"]
  @flavor_id    = data["flavor"]["id"] if data["flavor"]
  @root_enabled = data["rootEnabled"]
  @volume_used  = data["volume"]["used"] if data["volume"]
  @volume_size  = data["volume"]["size"] if data["volume"]
  @status       = data["status"]
  @created      = data["created"]
  @updated      = data["updated"]
  @links        = data["links"]
  true
end

#resize(options = {}) ⇒ Object

This operation changes the memory size of the instance, assuming a valid flavorRef is provided. Restarts MySQL in the process.

Options:

:flavor_ref - reference to a flavor as specified in the response from the List Flavors API call. *required*


195
196
197
198
199
200
201
202
203
204
# File 'lib/clouddb/instance.rb', line 195

def resize(options={})
  body = Hash.new
  body[:resize] = Hash.new

  body[:resize][:flavorRef]  = options[:flavor_ref] or raise CloudDB::Exception::MissingArgument, "Must provide a flavor to create an instance"

  response = @connection.dbreq("POST", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/action", @dbmgmtport, @dbmgmtscheme, {}, body.to_json)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  true
end

#resize_volume(options = {}) ⇒ Object

This operation supports resizing the attached volume for an instance. It supports only increasing the volume size and does not support decreasing the size. The volume size is in gigabytes (GB) and must be an integer.

Options:

:size - specifies the volume size in gigabytes (GB). The value specified must be between 1 and 10. *required*


211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/clouddb/instance.rb', line 211

def resize_volume(options={})
  body = Hash.new
  body[:resize] = Hash.new
  volume = Hash.new
  body[:resize][:volume] = volume

  volume[:size] = options[:size] or raise CloudDB::Exception::MissingArgument, "Must provide a volume size"
  (raise CloudDB::Exception::Syntax, "Volume size must be a value between 1 and 10") if !options[:size].is_a?(Integer) || options[:size] < 1 || options[:size] > 10

  response = @connection.dbreq("POST", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/action", @dbmgmtport, @dbmgmtscheme, {}, body.to_json)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  true
end

#restartObject

The restart operation will restart only the MySQL Instance. Restarting MySQL will erase any dynamic configuration settings that you have made within MySQL.



227
228
229
230
231
232
233
234
# File 'lib/clouddb/instance.rb', line 227

def restart()
  body = Hash.new
  body[:restart] = Hash.new

  response = @connection.dbreq("POST", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/action", @dbmgmtport, @dbmgmtscheme, {}, body.to_json)
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  true
end

#root_enabled?Boolean

Returns true if root user is enabled for the specified database instance or false otherwise.

Example:

i.root_enabled?
=> true

Returns:

  • (Boolean)


183
184
185
186
187
188
# File 'lib/clouddb/instance.rb', line 183

def root_enabled?()
  response = @connection.dbreq("GET", @dbmgmthost, "#{@dbmgmtpath}/instances/#{CloudDB.escape(@id.to_s)}/root", @dbmgmtport, @dbmgmtscheme, {})
  CloudDB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/)
  @root_enabled = JSON.parse(response.body)['rootEnabled']
  return @root_enabled
end