blob: f441d123f7ecc17c47943d53161771676d33c3e9 [file] [log] [blame]
Jamie Hannaford1943b382015-02-12 11:50:02 +01001package databases
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford1943b382015-02-12 11:50:02 +01006)
7
Jamie Hannaford9793d942015-02-18 15:13:20 +01008// CreateOptsBuilder builds create options
Jamie Hannaford1943b382015-02-12 11:50:02 +01009type CreateOptsBuilder interface {
10 ToDBCreateMap() (map[string]interface{}, error)
11}
12
Jon Perritt763e5922016-03-07 03:21:18 -060013// CreateOpts is the struct responsible for configuring a database; often in
Jamie Hannaford1943b382015-02-12 11:50:02 +010014// the context of an instance.
15type CreateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060016 // Specifies the name of the database. Valid names can be composed
Jamie Hannaford9793d942015-02-18 15:13:20 +010017 // of the following characters: letters (either case); numbers; these
18 // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is
19 // permitted anywhere. Prohibited characters that are forbidden include:
20 // single quotes, double quotes, back quotes, semicolons, commas, backslashes,
21 // and forward slashes.
Jon Perrittdb0ae142016-03-13 00:33:41 -060022 Name string `json:"name" required:"true"`
23 // Set of symbols and encodings. The default character set is
Jamie Hannaford9793d942015-02-18 15:13:20 +010024 // "utf8". See http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for
25 // supported character sets.
Jon Perrittdb0ae142016-03-13 00:33:41 -060026 CharSet string `json:"character_set,omitempty"`
27 // Set of rules for comparing characters in a character set. The
Jamie Hannaford9793d942015-02-18 15:13:20 +010028 // default value for collate is "utf8_general_ci". See
29 // http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for supported
30 // collations.
Jon Perrittdb0ae142016-03-13 00:33:41 -060031 Collate string `json:"collate,omitempty"`
Jamie Hannaford1943b382015-02-12 11:50:02 +010032}
33
Jamie Hannaford9793d942015-02-18 15:13:20 +010034// ToMap is a helper function to convert individual DB create opt structures
35// into sub-maps.
Jon Perrittdb0ae142016-03-13 00:33:41 -060036func (opts CreateOpts) ToMap() (map[string]interface{}, error) {
Jamie Hannaford1943b382015-02-12 11:50:02 +010037 if len(opts.Name) > 64 {
Jon Perritt763e5922016-03-07 03:21:18 -060038 err := gophercloud.ErrInvalidInput{}
Jon Perritt763e5922016-03-07 03:21:18 -060039 err.Argument = "databases.CreateOpts.Name"
40 err.Value = opts.Name
41 err.Info = "Must be less than 64 chars long"
42 return nil, err
Jamie Hannaford1943b382015-02-12 11:50:02 +010043 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060044 return gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford1943b382015-02-12 11:50:02 +010045}
46
Jamie Hannaford9793d942015-02-18 15:13:20 +010047// BatchCreateOpts allows for multiple databases to created and modified.
Jamie Hannaford1943b382015-02-12 11:50:02 +010048type BatchCreateOpts []CreateOpts
49
Jamie Hannaford9793d942015-02-18 15:13:20 +010050// ToDBCreateMap renders a JSON map for creating DBs.
Jamie Hannaford1943b382015-02-12 11:50:02 +010051func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 dbs := make([]map[string]interface{}, len(opts))
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010053 for i, db := range opts {
Jamie Hannaford1943b382015-02-12 11:50:02 +010054 dbMap, err := db.ToMap()
55 if err != nil {
56 return nil, err
57 }
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010058 dbs[i] = dbMap
Jamie Hannaford1943b382015-02-12 11:50:02 +010059 }
60 return map[string]interface{}{"databases": dbs}, nil
61}
62
Jamie Hannaford9793d942015-02-18 15:13:20 +010063// Create will create a new database within the specified instance. If the
64// specified instance does not exist, a 404 error will be returned.
Jon Perritt3860b512016-03-29 12:01:48 -050065func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060066 b, err := opts.ToDBCreateMap()
Jamie Hannaford1943b382015-02-12 11:50:02 +010067 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060068 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050069 return
Jamie Hannaford1943b382015-02-12 11:50:02 +010070 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 _, r.Err = client.Post(baseURL(client, instanceID), &b, nil, nil)
Jamie Hannaford1943b382015-02-12 11:50:02 +010072}
73
Jamie Hannaford9793d942015-02-18 15:13:20 +010074// List will list all of the databases for a specified instance. Note: this
75// operation will only return user-defined databases; it will exclude system
76// databases like "mysql", "information_schema", "lost+found" etc.
Jamie Hannaford1943b382015-02-12 11:50:02 +010077func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -060078 return pagination.NewPager(client, baseURL(client, instanceID), func(r pagination.PageResult) pagination.Page {
Jamie Hannaford1943b382015-02-12 11:50:02 +010079 return DBPage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -060080 })
Jamie Hannaford1943b382015-02-12 11:50:02 +010081}
82
Jamie Hannaford9793d942015-02-18 15:13:20 +010083// Delete will permanently delete the database within a specified instance.
84// All contained data inside the database will also be permanently deleted.
Jon Perritt3860b512016-03-29 12:01:48 -050085func Delete(client *gophercloud.ServiceClient, instanceID, dbName string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060086 _, r.Err = client.Delete(dbURL(client, instanceID, dbName), nil)
Jamie Hannaford1943b382015-02-12 11:50:02 +010087}