Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 11 | // CreateOptsBuilder is the top-level interface for creating JSON maps. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 12 | type CreateOptsBuilder interface { |
| 13 | ToUserCreateMap() (map[string]interface{}, error) |
| 14 | } |
| 15 | |
| 16 | // CreateOpts is the struct responsible for configuring a new user; often in the |
| 17 | // context of an instance. |
| 18 | type CreateOpts struct { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 19 | // [REQUIRED] Specifies a name for the user. Valid names can be composed |
| 20 | // of the following characters: letters (either case); numbers; these |
| 21 | // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is |
| 22 | // permitted anywhere. Prohibited characters that are forbidden include: |
| 23 | // single quotes, double quotes, back quotes, semicolons, commas, backslashes, |
| 24 | // and forward slashes. Spaces at the front or end of a user name are also |
| 25 | // not permitted. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 26 | Name string |
| 27 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 28 | // [REQUIRED] Specifies a password for the user. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 29 | Password string |
| 30 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 31 | // [OPTIONAL] An array of databases that this user will connect to. The |
| 32 | // "name" field is the only requirement for each option. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 33 | Databases db.BatchCreateOpts |
| 34 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 35 | // [OPTIONAL] Specifies the host from which a user is allowed to connect to |
| 36 | // the database. Possible values are a string containing an IPv4 address or |
| 37 | // "%" to allow connecting from any host. Optional; the default is "%". |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 38 | Host string |
| 39 | } |
| 40 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 41 | // ToMap is a convenience function for creating sub-maps for individual users. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 42 | func (opts CreateOpts) ToMap() (map[string]interface{}, error) { |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 43 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 44 | if opts.Name == "root" { |
| 45 | return nil, errors.New("root is a reserved user name and cannot be used") |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 46 | } |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 47 | if opts.Name == "" { |
| 48 | return nil, errors.New("Name is a required field") |
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.Password == "" { |
| 51 | return nil, errors.New("Password is a required field") |
| 52 | } |
| 53 | |
| 54 | user := map[string]interface{}{ |
| 55 | "name": opts.Name, |
| 56 | "password": opts.Password, |
| 57 | } |
| 58 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 59 | if opts.Host != "" { |
| 60 | user["host"] = opts.Host |
| 61 | } |
| 62 | |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 63 | dbs := make([]map[string]string, len(opts.Databases)) |
| 64 | for i, db := range opts.Databases { |
| 65 | dbs[i] = map[string]string{"name": db.Name} |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 66 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 67 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 68 | if len(dbs) > 0 { |
| 69 | user["databases"] = dbs |
| 70 | } |
| 71 | |
| 72 | return user, nil |
| 73 | } |
| 74 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 75 | // BatchCreateOpts allows multiple users to be created at once. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 76 | type BatchCreateOpts []CreateOpts |
| 77 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 78 | // ToUserCreateMap will generate a JSON map. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 79 | func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 80 | users := make([]map[string]interface{}, len(opts)) |
| 81 | for i, opt := range opts { |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 82 | user, err := opt.ToMap() |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
Jamie Hannaford | 257c8dc | 2015-02-25 14:40:22 +0100 | [diff] [blame] | 86 | users[i] = user |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 87 | } |
| 88 | return map[string]interface{}{"users": users}, nil |
| 89 | } |
| 90 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 91 | // Create asynchronously provisions a new user for the specified database |
| 92 | // instance based on the configuration defined in CreateOpts. If databases are |
| 93 | // assigned for a particular user, the user will be granted all privileges |
| 94 | // 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] | 95 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) CreateResult { |
| 96 | var res CreateResult |
| 97 | |
| 98 | reqBody, err := opts.ToUserCreateMap() |
| 99 | if err != nil { |
| 100 | res.Err = err |
| 101 | return res |
| 102 | } |
| 103 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 104 | _, res.Err = client.Request("POST", baseURL(client, instanceID), gophercloud.RequestOpts{ |
| 105 | JSONBody: &reqBody, |
| 106 | OkCodes: []int{202}, |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 107 | }) |
| 108 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 109 | return res |
| 110 | } |
| 111 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 112 | // List will list all the users associated with a specified database instance, |
| 113 | // along with their associated databases. This operation will not return any |
| 114 | // system users or administrators for a database. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 115 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
| 116 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 117 | return UserPage{pagination.LinkedPageBase{PageResult: r}} |
| 118 | } |
| 119 | |
| 120 | return pagination.NewPager(client, baseURL(client, instanceID), createPageFn) |
| 121 | } |
| 122 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 123 | // Delete will permanently delete a user from a specified database instance. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 124 | func Delete(client *gophercloud.ServiceClient, instanceID, userName string) DeleteResult { |
| 125 | var res DeleteResult |
| 126 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 127 | _, res.Err = client.Request("DELETE", userURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 128 | OkCodes: []int{202}, |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 129 | }) |
| 130 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 131 | return res |
| 132 | } |