blob: f1eb5d9aeec03ab13afb2731e3d8714d89c8c894 [file] [log] [blame]
Jamie Hannaford1943b382015-02-12 11:50:02 +01001package databases
2
3import (
4 "fmt"
5
Jamie Hannaford1943b382015-02-12 11:50:02 +01006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannaford9793d942015-02-18 15:13:20 +010010// CreateOptsBuilder builds create options
Jamie Hannaford1943b382015-02-12 11:50:02 +010011type 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.
17type CreateOpts struct {
Jamie Hannaford9793d942015-02-18 15:13:20 +010018 // [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 Hannaford1943b382015-02-12 11:50:02 +010024 Name string
25
Jamie Hannaford9793d942015-02-18 15:13:20 +010026 // [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 Hannaford1943b382015-02-12 11:50:02 +010029 CharSet string
30
Jamie Hannaford9793d942015-02-18 15:13:20 +010031 // [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 Hannaford1943b382015-02-12 11:50:02 +010035 Collate string
36}
37
Jamie Hannaford9793d942015-02-18 15:13:20 +010038// ToMap is a helper function to convert individual DB create opt structures
39// into sub-maps.
Jamie Hannaford1943b382015-02-12 11:50:02 +010040func (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 Hannaford9793d942015-02-18 15:13:20 +010059// BatchCreateOpts allows for multiple databases to created and modified.
Jamie Hannaford1943b382015-02-12 11:50:02 +010060type BatchCreateOpts []CreateOpts
61
Jamie Hannaford9793d942015-02-18 15:13:20 +010062// ToDBCreateMap renders a JSON map for creating DBs.
Jamie Hannaford1943b382015-02-12 11:50:02 +010063func (opts BatchCreateOpts) ToDBCreateMap() (map[string]interface{}, error) {
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010064 dbs := make([]map[string]string, len(opts))
65 for i, db := range opts {
Jamie Hannaford1943b382015-02-12 11:50:02 +010066 dbMap, err := db.ToMap()
67 if err != nil {
68 return nil, err
69 }
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010070 dbs[i] = dbMap
Jamie Hannaford1943b382015-02-12 11:50:02 +010071 }
72 return map[string]interface{}{"databases": dbs}, nil
73}
74
Jamie Hannaford9793d942015-02-18 15:13:20 +010075// 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 Hannaford1943b382015-02-12 11:50:02 +010077func 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 Hannaforda50d1352015-02-18 11:38:38 +010086 _, res.Err = client.Request("POST", baseURL(client, instanceID), gophercloud.RequestOpts{
Jamie Hannaford52dbcee2015-10-06 16:09:56 +020087 JSONBody: &reqBody,
88 OkCodes: []int{202},
Jamie Hannaford1943b382015-02-12 11:50:02 +010089 })
90
Jamie Hannaford1943b382015-02-12 11:50:02 +010091 return res
92}
93
Jamie Hannaford9793d942015-02-18 15:13:20 +010094// 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 Hannaford1943b382015-02-12 11:50:02 +010097func 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 Hannaford9793d942015-02-18 15:13:20 +0100105// Delete will permanently delete the database within a specified instance.
106// All contained data inside the database will also be permanently deleted.
Jamie Hannaford1943b382015-02-12 11:50:02 +0100107func Delete(client *gophercloud.ServiceClient, instanceID, dbName string) DeleteResult {
108 var res DeleteResult
109
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100110 _, res.Err = client.Request("DELETE", dbURL(client, instanceID, dbName), gophercloud.RequestOpts{
Jamie Hannafordd2b755f2015-10-07 14:01:57 +0200111 OkCodes: []int{202},
Jamie Hannaford1943b382015-02-12 11:50:02 +0100112 })
113
Jamie Hannaford1943b382015-02-12 11:50:02 +0100114 return res
115}