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 { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 16 | // [REQUIRED] Specifies the name of the database. Valid names can be composed |
| 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. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 22 | Name string |
| 23 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 24 | // [OPTIONAL] Set of symbols and encodings. The default character set is |
| 25 | // "utf8". See http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for |
| 26 | // supported character sets. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 27 | CharSet string |
| 28 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 29 | // [OPTIONAL] Set of rules for comparing characters in a character set. The |
| 30 | // default value for collate is "utf8_general_ci". See |
| 31 | // http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for supported |
| 32 | // collations. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 33 | Collate string |
| 34 | } |
| 35 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 36 | // ToMap is a helper function to convert individual DB create opt structures |
| 37 | // into sub-maps. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 38 | func (opts CreateOpts) ToMap() (map[string]string, error) { |
| 39 | if opts.Name == "" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame^] | 40 | err := gophercloud.ErrMissingInput{} |
| 41 | err.Function = "databases.ToMap" |
| 42 | err.Argument = "databases.CreateOpts.Name" |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 43 | } |
| 44 | if len(opts.Name) > 64 { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame^] | 45 | err := gophercloud.ErrInvalidInput{} |
| 46 | err.Function = "databases.ToMap" |
| 47 | err.Argument = "databases.CreateOpts.Name" |
| 48 | err.Value = opts.Name |
| 49 | err.Info = "Must be less than 64 chars long" |
| 50 | return nil, err |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | db := map[string]string{"name": opts.Name} |
| 54 | |
| 55 | if opts.CharSet != "" { |
| 56 | db["character_set"] = opts.CharSet |
| 57 | } |
| 58 | if opts.Collate != "" { |
| 59 | db["collate"] = opts.Collate |
| 60 | } |
| 61 | return db, nil |
| 62 | } |
| 63 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 64 | // BatchCreateOpts allows for multiple databases to created and modified. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 65 | type BatchCreateOpts []CreateOpts |
| 66 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 67 | // ToDBCreateMap renders a JSON map for creating DBs. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 68 | func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 69 | dbs := make([]map[string]string, len(opts)) |
| 70 | for i, db := range opts { |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 71 | dbMap, err := db.ToMap() |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 75 | dbs[i] = dbMap |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 76 | } |
| 77 | return map[string]interface{}{"databases": dbs}, nil |
| 78 | } |
| 79 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 80 | // Create will create a new database within the specified instance. If the |
| 81 | // specified instance does not exist, a 404 error will be returned. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 82 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) CreateResult { |
| 83 | var res CreateResult |
| 84 | |
| 85 | reqBody, err := opts.ToDBCreateMap() |
| 86 | if err != nil { |
| 87 | res.Err = err |
| 88 | return res |
| 89 | } |
| 90 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 91 | _, res.Err = client.Request("POST", baseURL(client, instanceID), &gophercloud.RequestOpts{ |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 92 | JSONBody: &reqBody, |
| 93 | OkCodes: []int{202}, |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 94 | }) |
| 95 | |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 96 | return res |
| 97 | } |
| 98 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 99 | // List will list all of the databases for a specified instance. Note: this |
| 100 | // operation will only return user-defined databases; it will exclude system |
| 101 | // databases like "mysql", "information_schema", "lost+found" etc. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 102 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
| 103 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 104 | return DBPage{pagination.LinkedPageBase{PageResult: r}} |
| 105 | } |
| 106 | |
| 107 | return pagination.NewPager(client, baseURL(client, instanceID), createPageFn) |
| 108 | } |
| 109 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 110 | // Delete will permanently delete the database within a specified instance. |
| 111 | // All contained data inside the database will also be permanently deleted. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 112 | func Delete(client *gophercloud.ServiceClient, instanceID, dbName string) DeleteResult { |
| 113 | var res DeleteResult |
| 114 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 115 | _, res.Err = client.Request("DELETE", dbURL(client, instanceID, dbName), &gophercloud.RequestOpts{ |
Jamie Hannaford | d2b755f | 2015-10-07 14:01:57 +0200 | [diff] [blame] | 116 | OkCodes: []int{202}, |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 117 | }) |
| 118 | |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 119 | return res |
| 120 | } |