blob: d342de34470fdf3149711b05709dc27b57d05c56 [file] [log] [blame]
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01001package users
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 db "github.com/gophercloud/gophercloud/openstack/db/v1/databases"
6 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01007)
8
Jamie Hannaford9793d942015-02-18 15:13:20 +01009// CreateOptsBuilder is the top-level interface for creating JSON maps.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010010type 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.
16type CreateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060017 // Specifies a name for the user. Valid names can be composed
Jamie Hannaford9793d942015-02-18 15:13:20 +010018 // 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 Perrittdb0ae142016-03-13 00:33:41 -060024 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 Hannaford9793d942015-02-18 15:13:20 +010028 // "name" field is the only requirement for each option.
Jon Perrittdb0ae142016-03-13 00:33:41 -060029 Databases db.BatchCreateOpts `json:"databases,omitempty"`
30 // Specifies the host from which a user is allowed to connect to
Jamie Hannaford9793d942015-02-18 15:13:20 +010031 // the database. Possible values are a string containing an IPv4 address or
32 // "%" to allow connecting from any host. Optional; the default is "%".
Jon Perrittdb0ae142016-03-13 00:33:41 -060033 Host string `json:"host,omitempty"`
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010034}
35
Jamie Hannaford9793d942015-02-18 15:13:20 +010036// ToMap is a convenience function for creating sub-maps for individual users.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010037func (opts CreateOpts) ToMap() (map[string]interface{}, error) {
Jamie Hannaford9793d942015-02-18 15:13:20 +010038 if opts.Name == "root" {
Jon Perritt763e5922016-03-07 03:21:18 -060039 err := gophercloud.ErrInvalidInput{}
Jon Perritt763e5922016-03-07 03:21:18 -060040 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 Hannafordaf4570f2015-02-12 13:33:25 +010044 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 return gophercloud.BuildRequestBody(opts, "")
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010046}
47
Jamie Hannaford9793d942015-02-18 15:13:20 +010048// BatchCreateOpts allows multiple users to be created at once.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010049type BatchCreateOpts []CreateOpts
50
Jamie Hannaford9793d942015-02-18 15:13:20 +010051// ToUserCreateMap will generate a JSON map.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010052func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010053 users := make([]map[string]interface{}, len(opts))
54 for i, opt := range opts {
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010055 user, err := opt.ToMap()
56 if err != nil {
57 return nil, err
58 }
Jamie Hannaford257c8dc2015-02-25 14:40:22 +010059 users[i] = user
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010060 }
61 return map[string]interface{}{"users": users}, nil
62}
63
Jamie Hannaford9793d942015-02-18 15:13:20 +010064// 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 Perritt3860b512016-03-29 12:01:48 -050068func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060069 b, err := opts.ToUserCreateMap()
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010070 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050072 return
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010073 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060074 _, r.Err = client.Post(baseURL(client, instanceID), &b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050075 return
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010076}
77
Jamie Hannaford9793d942015-02-18 15:13:20 +010078// 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 Hannafordaf4570f2015-02-12 13:33:25 +010081func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -060082 return pagination.NewPager(client, baseURL(client, instanceID), func(r pagination.PageResult) pagination.Page {
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010083 return UserPage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -060084 })
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010085}
86
Jamie Hannaford9793d942015-02-18 15:13:20 +010087// Delete will permanently delete a user from a specified database instance.
Jon Perritt3860b512016-03-29 12:01:48 -050088func Delete(client *gophercloud.ServiceClient, instanceID, userName string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060089 _, r.Err = client.Delete(userURL(client, instanceID, userName), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050090 return
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010091}