Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 1 | package databases |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 6 | ) |
| 7 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 8 | // CreateOptsBuilder builds create options |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 9 | type CreateOptsBuilder interface { |
| 10 | ToDBCreateMap() (map[string]interface{}, error) |
| 11 | } |
| 12 | |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 13 | // CreateOpts is the struct responsible for configuring a database; often in |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 14 | // the context of an instance. |
| 15 | type CreateOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 16 | // Specifies the name of the database. Valid names can be composed |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 17 | // 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 Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 22 | Name string `json:"name" required:"true"` |
| 23 | // Set of symbols and encodings. The default character set is |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 24 | // "utf8". See http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for |
| 25 | // supported character sets. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 26 | CharSet string `json:"character_set,omitempty"` |
| 27 | // Set of rules for comparing characters in a character set. The |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 28 | // 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 Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 31 | Collate string `json:"collate,omitempty"` |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 34 | // ToMap is a helper function to convert individual DB create opt structures |
| 35 | // into sub-maps. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 36 | func (opts CreateOpts) ToMap() (map[string]interface{}, error) { |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 37 | if len(opts.Name) > 64 { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 38 | err := gophercloud.ErrInvalidInput{} |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 39 | 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 Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 43 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 44 | return gophercloud.BuildRequestBody(opts, "") |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 47 | // BatchCreateOpts allows for multiple databases to created and modified. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 48 | type BatchCreateOpts []CreateOpts |
| 49 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 50 | // ToDBCreateMap renders a JSON map for creating DBs. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 51 | func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | dbs := make([]map[string]interface{}, len(opts)) |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 53 | for i, db := range opts { |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 54 | dbMap, err := db.ToMap() |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 58 | dbs[i] = dbMap |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 59 | } |
| 60 | return map[string]interface{}{"databases": dbs}, nil |
| 61 | } |
| 62 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 63 | // 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 Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 65 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 66 | b, err := opts.ToDBCreateMap() |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 67 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 68 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 69 | return |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 70 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 71 | _, r.Err = client.Post(baseURL(client, instanceID), &b, nil, nil) |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 74 | // 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 Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 77 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 78 | return pagination.NewPager(client, baseURL(client, instanceID), func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 79 | return DBPage{pagination.LinkedPageBase{PageResult: r}} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 80 | }) |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 83 | // Delete will permanently delete the database within a specified instance. |
| 84 | // All contained data inside the database will also be permanently deleted. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 85 | func Delete(client *gophercloud.ServiceClient, instanceID, dbName string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 86 | _, r.Err = client.Delete(dbURL(client, instanceID, dbName), nil) |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 87 | } |