Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 1 | package databases |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 10 | // CreateOptsBuilder builds create options |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 11 | type CreateOptsBuilder interface { |
| 12 | ToDBCreateMap() (map[string]interface{}, error) |
| 13 | } |
| 14 | |
| 15 | // DatabaseOpts is the struct responsible for configuring a database; often in |
| 16 | // the context of an instance. |
| 17 | type CreateOpts struct { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 18 | // [REQUIRED] Specifies the name of the database. Valid names can be composed |
| 19 | // of the following characters: letters (either case); numbers; these |
| 20 | // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is |
| 21 | // permitted anywhere. Prohibited characters that are forbidden include: |
| 22 | // single quotes, double quotes, back quotes, semicolons, commas, backslashes, |
| 23 | // and forward slashes. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 24 | Name string |
| 25 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 26 | // [OPTIONAL] Set of symbols and encodings. The default character set is |
| 27 | // "utf8". See http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for |
| 28 | // supported character sets. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 29 | CharSet string |
| 30 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 31 | // [OPTIONAL] Set of rules for comparing characters in a character set. The |
| 32 | // default value for collate is "utf8_general_ci". See |
| 33 | // http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for supported |
| 34 | // collations. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 35 | Collate string |
| 36 | } |
| 37 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 38 | // ToMap is a helper function to convert individual DB create opt structures |
| 39 | // into sub-maps. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 40 | func (opts CreateOpts) ToMap() (map[string]string, error) { |
| 41 | if opts.Name == "" { |
| 42 | return nil, fmt.Errorf("Name is a required field") |
| 43 | } |
| 44 | if len(opts.Name) > 64 { |
| 45 | return nil, fmt.Errorf("Name must be less than 64 chars long") |
| 46 | } |
| 47 | |
| 48 | db := map[string]string{"name": opts.Name} |
| 49 | |
| 50 | if opts.CharSet != "" { |
| 51 | db["character_set"] = opts.CharSet |
| 52 | } |
| 53 | if opts.Collate != "" { |
| 54 | db["collate"] = opts.Collate |
| 55 | } |
| 56 | return db, nil |
| 57 | } |
| 58 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 59 | // BatchCreateOpts allows for multiple databases to created and modified. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 60 | type BatchCreateOpts []CreateOpts |
| 61 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 62 | // ToDBCreateMap renders a JSON map for creating DBs. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 63 | func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 64 | dbs := make([]map[string]string, len(opts)) |
| 65 | for i, db := range opts { |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 66 | dbMap, err := db.ToMap() |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 70 | dbs[i] = dbMap |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 71 | } |
| 72 | return map[string]interface{}{"databases": dbs}, nil |
| 73 | } |
| 74 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 75 | // Create will create a new database within the specified instance. If the |
| 76 | // specified instance does not exist, a 404 error will be returned. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 77 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) CreateResult { |
| 78 | var res CreateResult |
| 79 | |
| 80 | reqBody, err := opts.ToDBCreateMap() |
| 81 | if err != nil { |
| 82 | res.Err = err |
| 83 | return res |
| 84 | } |
| 85 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 86 | _, res.Err = client.Request("POST", baseURL(client, instanceID), gophercloud.RequestOpts{ |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 87 | JSONBody: &reqBody, |
| 88 | OkCodes: []int{202}, |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 89 | }) |
| 90 | |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 91 | return res |
| 92 | } |
| 93 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 94 | // List will list all of the databases for a specified instance. Note: this |
| 95 | // operation will only return user-defined databases; it will exclude system |
| 96 | // databases like "mysql", "information_schema", "lost+found" etc. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 97 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
| 98 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 99 | return DBPage{pagination.LinkedPageBase{PageResult: r}} |
| 100 | } |
| 101 | |
| 102 | return pagination.NewPager(client, baseURL(client, instanceID), createPageFn) |
| 103 | } |
| 104 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 105 | // Delete will permanently delete the database within a specified instance. |
| 106 | // All contained data inside the database will also be permanently deleted. |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 107 | func Delete(client *gophercloud.ServiceClient, instanceID, dbName string) DeleteResult { |
| 108 | var res DeleteResult |
| 109 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 110 | _, res.Err = client.Request("DELETE", dbURL(client, instanceID, dbName), gophercloud.RequestOpts{ |
Jamie Hannaford | d2b755f | 2015-10-07 14:01:57 +0200 | [diff] [blame] | 111 | OkCodes: []int{202}, |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 112 | }) |
| 113 | |
Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 114 | return res |
| 115 | } |