blob: e0616db00283558771d5914ebe0ae91ec3ca931b [file] [log] [blame]
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01001package users
2
3import (
Jamie Hannaford9793d942015-02-18 15:13:20 +01004 "errors"
5
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01006 "github.com/rackspace/gophercloud"
7 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannaford9793d942015-02-18 15:13:20 +010011// CreateOptsBuilder is the top-level interface for creating JSON maps.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010012type 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.
18type CreateOpts struct {
Jamie Hannaford9793d942015-02-18 15:13:20 +010019 // [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 Hannafordaf4570f2015-02-12 13:33:25 +010026 Name string
27
Jamie Hannaford9793d942015-02-18 15:13:20 +010028 // [REQUIRED] Specifies a password for the user.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010029 Password string
30
Jamie Hannaford9793d942015-02-18 15:13:20 +010031 // [OPTIONAL] An array of databases that this user will connect to. The
32 // "name" field is the only requirement for each option.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010033 Databases db.BatchCreateOpts
34
Jamie Hannaford9793d942015-02-18 15:13:20 +010035 // [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 Hannafordaf4570f2015-02-12 13:33:25 +010038 Host string
39}
40
Jamie Hannaford9793d942015-02-18 15:13:20 +010041// ToMap is a convenience function for creating sub-maps for individual users.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010042func (opts CreateOpts) ToMap() (map[string]interface{}, error) {
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010043
Jamie Hannaford9793d942015-02-18 15:13:20 +010044 if opts.Name == "root" {
45 return nil, errors.New("root is a reserved user name and cannot be used")
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010046 }
Jamie Hannaford9793d942015-02-18 15:13:20 +010047 if opts.Name == "" {
48 return nil, errors.New("Name is a required field")
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010049 }
Jamie Hannaford9793d942015-02-18 15:13:20 +010050 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 Hannafordaf4570f2015-02-12 13:33:25 +010059 if opts.Host != "" {
60 user["host"] = opts.Host
61 }
62
63 var dbs []map[string]string
64 for _, db := range opts.Databases {
65 dbs = append(dbs, map[string]string{"name": db.Name})
66 }
67 if len(dbs) > 0 {
68 user["databases"] = dbs
69 }
70
71 return user, nil
72}
73
Jamie Hannaford9793d942015-02-18 15:13:20 +010074// BatchCreateOpts allows multiple users to be created at once.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010075type BatchCreateOpts []CreateOpts
76
Jamie Hannaford9793d942015-02-18 15:13:20 +010077// ToUserCreateMap will generate a JSON map.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010078func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
79 var users []map[string]interface{}
80 for _, opt := range opts {
81 user, err := opt.ToMap()
82 if err != nil {
83 return nil, err
84 }
85 users = append(users, user)
86 }
87 return map[string]interface{}{"users": users}, nil
88}
89
Jamie Hannaford9793d942015-02-18 15:13:20 +010090// Create asynchronously provisions a new user for the specified database
91// instance based on the configuration defined in CreateOpts. If databases are
92// assigned for a particular user, the user will be granted all privileges
93// for those specified databases. "root" is a reserved name and cannot be used.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010094func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) CreateResult {
95 var res CreateResult
96
97 reqBody, err := opts.ToUserCreateMap()
98 if err != nil {
99 res.Err = err
100 return res
101 }
102
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100103 _, res.Err = client.Request("POST", baseURL(client, instanceID), gophercloud.RequestOpts{
104 JSONBody: &reqBody,
105 OkCodes: []int{202},
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100106 })
107
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100108 return res
109}
110
Jamie Hannaford9793d942015-02-18 15:13:20 +0100111// List will list all the users associated with a specified database instance,
112// along with their associated databases. This operation will not return any
113// system users or administrators for a database.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100114func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
115 createPageFn := func(r pagination.PageResult) pagination.Page {
116 return UserPage{pagination.LinkedPageBase{PageResult: r}}
117 }
118
119 return pagination.NewPager(client, baseURL(client, instanceID), createPageFn)
120}
121
Jamie Hannaford9793d942015-02-18 15:13:20 +0100122// Delete will permanently delete a user from a specified database instance.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100123func Delete(client *gophercloud.ServiceClient, instanceID, userName string) DeleteResult {
124 var res DeleteResult
125
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100126 _, res.Err = client.Request("DELETE", userURL(client, instanceID, userName), gophercloud.RequestOpts{
127 OkCodes: []int{202},
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100128 })
129
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100130 return res
131}