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 { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 17 | // [REQUIRED] Specifies a name for the user. Valid names can be composed |
| 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. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 24 | Name string |
| 25 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 26 | // [REQUIRED] Specifies a password for the user. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 27 | Password string |
| 28 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 29 | // [OPTIONAL] An array of databases that this user will connect to. The |
| 30 | // "name" field is the only requirement for each option. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 31 | Databases db.BatchCreateOpts |
| 32 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 33 | // [OPTIONAL] Specifies the host from which a user is allowed to connect to |
| 34 | // the database. Possible values are a string containing an IPv4 address or |
| 35 | // "%" to allow connecting from any host. Optional; the default is "%". |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 36 | Host string |
| 37 | } |
| 38 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 39 | // ToMap is a convenience function for creating sub-maps for individual users. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 40 | func (opts CreateOpts) ToMap() (map[string]interface{}, error) { |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 41 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 42 | if opts.Name == "root" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 43 | err := gophercloud.ErrInvalidInput{} |
| 44 | err.Function = "users.ToUserCreateMap" |
| 45 | err.Argument = "users.CreateOpts.Name" |
| 46 | err.Value = "root" |
| 47 | err.Info = "root is a reserved user name and cannot be used" |
| 48 | return nil, err |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 49 | } |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 50 | if opts.Name == "" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 51 | err := gophercloud.ErrMissingInput{} |
| 52 | err.Function = "users.ToUserCreateMap" |
| 53 | err.Argument = "users.CreateOpts.Name" |
| 54 | return nil, err |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 55 | } |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 56 | if opts.Password == "" { |
Jon Perritt | 763e592 | 2016-03-07 03:21:18 -0600 | [diff] [blame] | 57 | err := gophercloud.ErrMissingInput{} |
| 58 | err.Function = "users.ToUserCreateMap" |
| 59 | err.Argument = "users.CreateOpts.Password" |
| 60 | return nil, err |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | user := map[string]interface{}{ |
| 64 | "name": opts.Name, |
| 65 | "password": opts.Password, |
| 66 | } |
| 67 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 68 | if opts.Host != "" { |
| 69 | user["host"] = opts.Host |
| 70 | } |
| 71 | |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 72 | dbs := make([]map[string]string, len(opts.Databases)) |
| 73 | for i, db := range opts.Databases { |
| 74 | dbs[i] = map[string]string{"name": db.Name} |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 75 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 76 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 77 | if len(dbs) > 0 { |
| 78 | user["databases"] = dbs |
| 79 | } |
| 80 | |
| 81 | return user, nil |
| 82 | } |
| 83 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 84 | // BatchCreateOpts allows multiple users to be created at once. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 85 | type BatchCreateOpts []CreateOpts |
| 86 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 87 | // ToUserCreateMap will generate a JSON map. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 88 | func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 89 | users := make([]map[string]interface{}, len(opts)) |
| 90 | for i, opt := range opts { |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 91 | user, err := opt.ToMap() |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 95 | users[i] = user |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 96 | } |
| 97 | return map[string]interface{}{"users": users}, nil |
| 98 | } |
| 99 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 100 | // Create asynchronously provisions a new user for the specified database |
| 101 | // instance based on the configuration defined in CreateOpts. If databases are |
| 102 | // assigned for a particular user, the user will be granted all privileges |
| 103 | // for those specified databases. "root" is a reserved name and cannot be used. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 104 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) CreateResult { |
| 105 | var res CreateResult |
| 106 | |
| 107 | reqBody, err := opts.ToUserCreateMap() |
| 108 | if err != nil { |
| 109 | res.Err = err |
| 110 | return res |
| 111 | } |
| 112 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 113 | _, res.Err = client.Request("POST", baseURL(client, instanceID), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 114 | JSONBody: &reqBody, |
| 115 | OkCodes: []int{202}, |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 116 | }) |
| 117 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 118 | return res |
| 119 | } |
| 120 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 121 | // List will list all the users associated with a specified database instance, |
| 122 | // along with their associated databases. This operation will not return any |
| 123 | // system users or administrators for a database. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 124 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
| 125 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 126 | return UserPage{pagination.LinkedPageBase{PageResult: r}} |
| 127 | } |
| 128 | |
| 129 | return pagination.NewPager(client, baseURL(client, instanceID), createPageFn) |
| 130 | } |
| 131 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 132 | // Delete will permanently delete a user from a specified database instance. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 133 | func Delete(client *gophercloud.ServiceClient, instanceID, userName string) DeleteResult { |
| 134 | var res DeleteResult |
| 135 | |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 136 | _, res.Err = client.Request("DELETE", userURL(client, instanceID, userName), &gophercloud.RequestOpts{ |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 137 | OkCodes: []int{202}, |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 138 | }) |
| 139 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 140 | return res |
| 141 | } |