Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | type CreateOptsBuilder interface { |
| 10 | ToUserCreateMap() (map[string]interface{}, error) |
| 11 | } |
| 12 | |
| 13 | // CreateOpts is the struct responsible for configuring a new user; often in the |
| 14 | // context of an instance. |
| 15 | type CreateOpts struct { |
| 16 | // Specifies a name for the user. |
| 17 | Name string |
| 18 | |
| 19 | // Specifies a password for the user. |
| 20 | Password string |
| 21 | |
| 22 | // An array of databases that this user will connect to. The `name` field is |
| 23 | // the only requirement for each option. |
| 24 | Databases db.BatchCreateOpts |
| 25 | |
| 26 | // Specifies the host from which a user is allowed to connect to the database. |
| 27 | // Possible values are a string containing an IPv4 address or "%" to allow |
| 28 | // connecting from any host. Optional; the default is "%". |
| 29 | Host string |
| 30 | } |
| 31 | |
| 32 | func (opts CreateOpts) ToMap() (map[string]interface{}, error) { |
| 33 | user := map[string]interface{}{} |
| 34 | |
| 35 | if opts.Name != "" { |
| 36 | user["name"] = opts.Name |
| 37 | } |
| 38 | if opts.Password != "" { |
| 39 | user["password"] = opts.Password |
| 40 | } |
| 41 | if opts.Host != "" { |
| 42 | user["host"] = opts.Host |
| 43 | } |
| 44 | |
| 45 | var dbs []map[string]string |
| 46 | for _, db := range opts.Databases { |
| 47 | dbs = append(dbs, map[string]string{"name": db.Name}) |
| 48 | } |
| 49 | if len(dbs) > 0 { |
| 50 | user["databases"] = dbs |
| 51 | } |
| 52 | |
| 53 | return user, nil |
| 54 | } |
| 55 | |
| 56 | type BatchCreateOpts []CreateOpts |
| 57 | |
| 58 | func (opts BatchCreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
| 59 | var users []map[string]interface{} |
| 60 | for _, opt := range opts { |
| 61 | user, err := opt.ToMap() |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | users = append(users, user) |
| 66 | } |
| 67 | return map[string]interface{}{"users": users}, nil |
| 68 | } |
| 69 | |
| 70 | func Create(client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) CreateResult { |
| 71 | var res CreateResult |
| 72 | |
| 73 | reqBody, err := opts.ToUserCreateMap() |
| 74 | if err != nil { |
| 75 | res.Err = err |
| 76 | return res |
| 77 | } |
| 78 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame^] | 79 | _, res.Err = client.Request("POST", baseURL(client, instanceID), gophercloud.RequestOpts{ |
| 80 | JSONBody: &reqBody, |
| 81 | OkCodes: []int{202}, |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 82 | }) |
| 83 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 84 | return res |
| 85 | } |
| 86 | |
| 87 | func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { |
| 88 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 89 | return UserPage{pagination.LinkedPageBase{PageResult: r}} |
| 90 | } |
| 91 | |
| 92 | return pagination.NewPager(client, baseURL(client, instanceID), createPageFn) |
| 93 | } |
| 94 | |
| 95 | func Delete(client *gophercloud.ServiceClient, instanceID, userName string) DeleteResult { |
| 96 | var res DeleteResult |
| 97 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame^] | 98 | _, res.Err = client.Request("DELETE", userURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 99 | OkCodes: []int{202}, |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 100 | }) |
| 101 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 102 | return res |
| 103 | } |