Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | db "github.com/gophercloud/gophercloud/openstack/db/v1/databases" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 9 | // CreateOptsBuilder is the top-level interface for creating JSON maps. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 10 | type CreateOptsBuilder interface { |
| 11 | ToUserCreateMap() (map[string]interface{}, error) |
| 12 | } |
| 13 | |
| 14 | // CreateOpts is the struct responsible for configuring a new user; often in the |
| 15 | // context of an instance. |
| 16 | type CreateOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 17 | // Specifies a name for the user. Valid names can be composed |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 18 | // of the following characters: letters (either case); numbers; these |
| 19 | // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is |
| 20 | // permitted anywhere. Prohibited characters that are forbidden include: |
| 21 | // single quotes, double quotes, back quotes, semicolons, commas, backslashes, |
| 22 | // and forward slashes. Spaces at the front or end of a user name are also |
| 23 | // not permitted. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 24 | Name string `json:"name" required:"true"` |
| 25 | // Specifies a password for the user. |
| 26 | Password string `json:"password" required:"true"` |
| 27 | // An array of databases that this user will connect to. The |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 28 | // "name" field is the only requirement for each option. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 29 | Databases db.BatchCreateOpts `json:"databases,omitempty"` |
| 30 | // Specifies the host from which a user is allowed to connect to |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 31 | // the database. Possible values are a string containing an IPv4 address or |
| 32 | // "%" to allow connecting from any host. Optional; the default is "%". |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 33 | Host string `json:"host,omitempty"` |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 36 | // ToMap is a convenience function for creating sub-maps for individual users. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 37 | func (opts CreateOpts) ToMap() (map[string]interface{}, error) { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 38 | if opts.Name == "root" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 39 | err := gophercloud.ErrInvalidInput{} |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 40 | err.Argument = "users.CreateOpts.Name" |
| 41 | err.Value = "root" |
| 42 | err.Info = "root is a reserved user name and cannot be used" |
| 43 | return nil, err |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 44 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 45 | return gophercloud.BuildRequestBody(opts, "") |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 48 | // BatchCreateOpts allows multiple users to be created at once. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 49 | type BatchCreateOpts []CreateOpts |
| 50 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 51 | // ToUserCreateMap will generate a JSON map. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 52 | func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 53 | users := make([]map[string]interface{}, len(opts)) |
| 54 | for i, opt := range opts { |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 55 | user, err := opt.ToMap() |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 59 | users[i] = user |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 60 | } |
| 61 | return map[string]interface{}{"users": users}, nil |
| 62 | } |
| 63 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 64 | // Create asynchronously provisions a new user for the specified database |
| 65 | // instance based on the configuration defined in CreateOpts. If databases are |
| 66 | // assigned for a particular user, the user will be granted all privileges |
| 67 | // for those specified databases. "root" is a reserved name and cannot be used. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 68 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 69 | b, err := opts.ToUserCreateMap() |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 70 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 71 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 72 | return |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 73 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 74 | _, r.Err = client.Post(baseURL(client, instanceID), &b, nil, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 75 | return |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 78 | // List will list all the users associated with a specified database instance, |
| 79 | // along with their associated databases. This operation will not return any |
| 80 | // system users or administrators for a database. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 81 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 82 | return pagination.NewPager(client, baseURL(client, instanceID), func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 83 | return UserPage{pagination.LinkedPageBase{PageResult: r}} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 84 | }) |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 87 | // Delete will permanently delete a user from a specified database instance. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 88 | func Delete(client *gophercloud.ServiceClient, instanceID, userName string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 89 | _, r.Err = client.Delete(userURL(client, instanceID, userName), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 90 | return |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 91 | } |