blob: 18e1af537ae594299b059f294141751978e82fd4 [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 {
Jamie Hannaford9793d942015-02-18 15:13:20 +010016 // [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 Hannaford1943b382015-02-12 11:50:02 +010022 Name string
23
Jamie Hannaford9793d942015-02-18 15:13:20 +010024 // [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 Hannaford1943b382015-02-12 11:50:02 +010027 CharSet string
28
Jamie Hannaford9793d942015-02-18 15:13:20 +010029 // [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 Hannaford1943b382015-02-12 11:50:02 +010033 Collate string
34}
35
Jamie Hannaford9793d942015-02-18 15:13:20 +010036// ToMap is a helper function to convert individual DB create opt structures
37// into sub-maps.
Jamie Hannaford1943b382015-02-12 11:50:02 +010038func (opts CreateOpts) ToMap() (map[string]string, error) {
39 if opts.Name == "" {
Jon Perritt763e5922016-03-07 03:21:18 -060040 err := gophercloud.ErrMissingInput{}
41 err.Function = "databases.ToMap"
42 err.Argument = "databases.CreateOpts.Name"
Jamie Hannaford1943b382015-02-12 11:50:02 +010043 }
44 if len(opts.Name) > 64 {
Jon Perritt763e5922016-03-07 03:21:18 -060045 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 Hannaford1943b382015-02-12 11:50:02 +010051 }
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 Hannaford9793d942015-02-18 15:13:20 +010064// BatchCreateOpts allows for multiple databases to created and modified.
Jamie Hannaford1943b382015-02-12 11:50:02 +010065type BatchCreateOpts []CreateOpts
66
Jamie Hannaford9793d942015-02-18 15:13:20 +010067// ToDBCreateMap renders a JSON map for creating DBs.
Jamie Hannaford1943b382015-02-12 11:50:02 +010068func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) {
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010069 dbs := make([]map[string]string, len(opts))
70 for i, db := range opts {
Jamie Hannaford1943b382015-02-12 11:50:02 +010071 dbMap, err := db.ToMap()
72 if err != nil {
73 return nil, err
74 }
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010075 dbs[i] = dbMap
Jamie Hannaford1943b382015-02-12 11:50:02 +010076 }
77 return map[string]interface{}{"databases": dbs}, nil
78}
79
Jamie Hannaford9793d942015-02-18 15:13:20 +010080// 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 Hannaford1943b382015-02-12 11:50:02 +010082func 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 Perritta33da232016-03-02 04:43:08 -060091 _, res.Err = client.Request("POST", baseURL(client, instanceID), &gophercloud.RequestOpts{
Jamie Hannaford52dbcee2015-10-06 16:09:56 +020092 JSONBody: &reqBody,
93 OkCodes: []int{202},
Jamie Hannaford1943b382015-02-12 11:50:02 +010094 })
95
Jamie Hannaford1943b382015-02-12 11:50:02 +010096 return res
97}
98
Jamie Hannaford9793d942015-02-18 15:13:20 +010099// 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 Hannaford1943b382015-02-12 11:50:02 +0100102func 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 Hannaford9793d942015-02-18 15:13:20 +0100110// Delete will permanently delete the database within a specified instance.
111// All contained data inside the database will also be permanently deleted.
Jamie Hannaford1943b382015-02-12 11:50:02 +0100112func Delete(client *gophercloud.ServiceClient, instanceID, dbName string) DeleteResult {
113 var res DeleteResult
114
Jon Perritta33da232016-03-02 04:43:08 -0600115 _, res.Err = client.Request("DELETE", dbURL(client, instanceID, dbName), &gophercloud.RequestOpts{
Jamie Hannafordd2b755f2015-10-07 14:01:57 +0200116 OkCodes: []int{202},
Jamie Hannaford1943b382015-02-12 11:50:02 +0100117 })
118
Jamie Hannaford1943b382015-02-12 11:50:02 +0100119 return res
120}