Extract DB functionality out
diff --git a/openstack/db/v1/instances/pkg.go b/openstack/db/v1/instances/pkg.go
deleted file mode 100644
index 98a1bb3..0000000
--- a/openstack/db/v1/instances/pkg.go
+++ /dev/null
@@ -1 +0,0 @@
-package instances
diff --git a/openstack/db/v1/instances/requests.go b/openstack/db/v1/instances/requests.go
index b272ce3..08fa9b5 100644
--- a/openstack/db/v1/instances/requests.go
+++ b/openstack/db/v1/instances/requests.go
@@ -5,6 +5,7 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
+ db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
"github.com/rackspace/gophercloud/pagination"
)
@@ -13,48 +14,6 @@
ToInstanceCreateMap() (map[string]interface{}, error)
}
-// DatabaseOpts is the struct responsible for configuring a database; often in
-// the context of an instance.
-type DatabaseOpts struct {
- // Specifies the name of the database. Optional.
- Name string
-
- // Set of symbols and encodings. Optional; the default character set is utf8.
- CharSet string
-
- // Set of rules for comparing characters in a character set. Optional; the
- // default value for collate is utf8_general_ci.
- Collate string
-}
-
-func (opts DatabaseOpts) ToMap() (map[string]string, error) {
- db := map[string]string{}
- if opts.Name != "" {
- db["name"] = opts.Name
- }
- if opts.CharSet != "" {
- db["character_set"] = opts.CharSet
- }
- if opts.Collate != "" {
- db["collate"] = opts.Collate
- }
- return db, nil
-}
-
-type DatabasesOpts []DatabaseOpts
-
-func (opts DatabasesOpts) ToMap() ([]map[string]string, error) {
- var dbs []map[string]string
- for _, db := range opts {
- dbMap, err := db.ToMap()
- if err != nil {
- return dbs, err
- }
- dbs = append(dbs, dbMap)
- }
- return dbs, nil
-}
-
// UserOpts is the struct responsible for configuring a user; often in the
// context of an instance.
type UserOpts struct {
@@ -66,7 +25,7 @@
// An array of databases that this user will connect to. The `name` field is
// the only requirement for each option.
- Databases []DatabaseOpts
+ Databases []db.CreateOpts
// Specifies the host from which a user is allowed to connect to the database.
// Possible values are a string containing an IPv4 address or "%" to allow
@@ -127,7 +86,7 @@
Name string
// A slice of database information options.
- Databases DatabasesOpts
+ Databases []db.CreateOpts
// A slice of user information options.
Users UsersOpts
@@ -150,11 +109,11 @@
instance["name"] = opts.Name
}
if len(opts.Databases) > 0 {
- dbs, err := opts.Databases.ToMap()
+ dbs, err := opts.Databases.ToDBCreateMap()
if err != nil {
return nil, err
}
- instance["databases"] = dbs
+ instance["databases"] = dbs["databases"]
}
if len(opts.Users) > 0 {
users, err := opts.Users.ToMap()
@@ -167,7 +126,14 @@
return map[string]interface{}{"instance": instance}, nil
}
-// Create will provision a new Database instance.
+// Create asynchronously provisions a new database instance. It requires the
+// user to specify a flavor and a volume size. The API service then provisions
+// the instance with the requested flavor and sets up a volume of the specified
+// size, which is the storage for the database instance.
+//
+// Although this call only allows the creation of 1 instance per request, you
+// can create an instance with multiple databases and users. The default
+// binding for a MySQL instance is port 3306.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
@@ -190,6 +156,7 @@
return res
}
+// List retrieves the status and information for all database instances.
func List(client *gophercloud.ServiceClient) pagination.Pager {
createPageFn := func(r pagination.PageResult) pagination.Page {
return InstancePage{pagination.LinkedPageBase{PageResult: r}}
@@ -198,6 +165,7 @@
return pagination.NewPager(client, baseURL(client), createPageFn)
}
+// Get retrieves the status and information for a specified database instance.
func Get(client *gophercloud.ServiceClient, id string) GetResult {
var res GetResult
@@ -213,6 +181,7 @@
return res
}
+// Delete permanently destroys the database instance.
func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
var res DeleteResult
@@ -227,6 +196,8 @@
return res
}
+// EnableRootUser enables the login from any host for the root user and
+// provides the user with a generated root password.
func EnableRootUser(client *gophercloud.ServiceClient, id string) UserRootResult {
var res UserRootResult
@@ -242,6 +213,9 @@
return res
}
+// IsRootEnabled checks an instance to see if root access is enabled. It returns
+// True if root user is enabled for the specified database instance or False
+// otherwise.
func IsRootEnabled(client *gophercloud.ServiceClient, id string) (bool, error) {
var res gophercloud.Result
@@ -254,6 +228,9 @@
return res.Body.(map[string]interface{})["rootEnabled"] == true, err
}
+// RestartService will restart only the MySQL Instance. Restarting MySQL will
+// erase any dynamic configuration settings that you have made within MySQL.
+// The MySQL service will be unavailable until the instance restarts.
func RestartService(client *gophercloud.ServiceClient, id string) ActionResult {
var res ActionResult
@@ -269,6 +246,8 @@
return res
}
+// ResizeInstance changes the memory size of the instance, assuming a valid
+// flavorRef is provided. It will also restart the MySQL service.
func ResizeInstance(client *gophercloud.ServiceClient, id, flavorRef string) ActionResult {
var res ActionResult
@@ -290,6 +269,9 @@
return res
}
+// ResizeVolume will resize 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.
func ResizeVolume(client *gophercloud.ServiceClient, id string, size int) ActionResult {
var res ActionResult
diff --git a/openstack/db/v1/instances/requests_test.go b/openstack/db/v1/instances/requests_test.go
index e079dd0..2319e20 100644
--- a/openstack/db/v1/instances/requests_test.go
+++ b/openstack/db/v1/instances/requests_test.go
@@ -4,6 +4,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
@@ -40,16 +41,16 @@
opts := CreateOpts{
Name: "json_rack_instance",
FlavorRef: "1",
- Databases: DatabasesOpts{
- DatabaseOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
- DatabaseOpts{Name: "nextround"},
+ Databases: []db.CreateOpts{
+ db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
+ db.CreateOpts{Name: "nextround"},
},
Users: UsersOpts{
UserOpts{
Name: "demouser",
Password: "demopassword",
- Databases: DatabasesOpts{
- DatabaseOpts{Name: "sampledb"},
+ Databases: []db.CreateOpts{
+ db.CreateOpts{Name: "sampledb"},
},
},
},
diff --git a/openstack/db/v1/instances/results.go b/openstack/db/v1/instances/results.go
index 48b930c..f211868 100644
--- a/openstack/db/v1/instances/results.go
+++ b/openstack/db/v1/instances/results.go
@@ -6,29 +6,62 @@
"github.com/rackspace/gophercloud/pagination"
)
+// Flavor represents information about a hardware flavor for a database instance.
type Flavor struct {
- ID string
+ // The unique identifier for a flavor.
+ ID string
+
+ // Various links which allow a user to reference the flavor.
Links []gophercloud.Link
}
+// Volume represents information about an attached volume for a database instance.
type Volume struct {
+ // The size in GB of the volume
Size int
}
+// Instance represents a remote MySQL instance.
type Instance struct {
- Created string //time.Time
- Updated string //time.Time
- Flavor Flavor
+ // Indicates the datetime that the instance was created
+ Created string //time.Time
+
+ // Indicates the most recent datetime that the instance was updated.
+ Updated string //time.Time
+
+ // Indicates the hardware flavor the instance uses.
+ Flavor Flavor
+
+ // A DNS-resolvable hostname associated with the database instance (rather
+ // than an IPv4 address). Since the hostname always resolves to the correct
+ // IP address of the database instance, this relieves the user from the task
+ // of maintaining the mapping. Note that although the IP address may likely
+ // change on resizing, migrating, and so forth, the hostname always resolves
+ // to the correct database instance.
Hostname string
- ID string
- Links []gophercloud.Link
- Name string
- Status string
- Volume Volume
+
+ // Indicates the unique identifier for the instance resource.
+ ID string
+
+ // Exposes various links that reference the instance resource.
+ Links []gophercloud.Link
+
+ // The human-readable name of the instance.
+ Name string
+
+ // The build status of the instance.
+ Status string
+
+ // Information about the attached volume of the instance.
+ Volume Volume
}
+// User represents a database user
type User struct {
- Name string
+ // The user name
+ Name string
+
+ // The user password
Password string
}
@@ -41,14 +74,17 @@
commonResult
}
+// GetResult represents the result of a Get operation.
type GetResult struct {
commonResult
}
+// DeleteResult represents the result of a Delete operation.
type DeleteResult struct {
gophercloud.ErrResult
}
+// Extract will extract an Instance from various result structs.
func (r commonResult) Extract() (*Instance, error) {
if r.Err != nil {
return nil, r.Err
@@ -63,10 +99,12 @@
return &response.Instance, err
}
+// InstancePage represents a single page of a paginated instance collection.
type InstancePage struct {
pagination.LinkedPageBase
}
+// IsEmpty checks to see whether the collection is empty.
func (page InstancePage) IsEmpty() (bool, error) {
instances, err := ExtractInstances(page)
if err != nil {
@@ -75,6 +113,7 @@
return len(instances) == 0, nil
}
+// NextPageURL will retrieve the next page URL.
func (page InstancePage) NextPageURL() (string, error) {
type resp struct {
Links []gophercloud.Link `mapstructure:"instances_links"`
@@ -89,6 +128,8 @@
return gophercloud.ExtractNextURL(r.Links)
}
+// ExtractInstances will convert a generic pagination struct into a more
+// relevant slice of Instance structs.
func ExtractInstances(page pagination.Page) ([]Instance, error) {
casted := page.(InstancePage).Body
@@ -101,10 +142,12 @@
return response.Instances, err
}
+// UserRootResult represents the result of an operation to enable the root user.
type UserRootResult struct {
gophercloud.Result
}
+// Extract will extract root user information from a UserRootResult.
func (r UserRootResult) Extract() (*User, error) {
if r.Err != nil {
return nil, r.Err
@@ -119,6 +162,9 @@
return &response.User, err
}
+// ActionResult represents the result of action requests, such as: restarting
+// an instance service, resizing its memory allocation, and resizing its
+// attached volume size.
type ActionResult struct {
gophercloud.ErrResult
}