blob: e5967397d0d39ed084d139ce8287bbbf9b7ec80e [file] [log] [blame]
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01001package users
2
3import (
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01004 "github.com/rackspace/gophercloud"
5 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9type 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.
15type 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
32func (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
56type BatchCreateOpts []CreateOpts
57
58func (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
70func 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 Hannaforda50d1352015-02-18 11:38:38 +010079 _, res.Err = client.Request("POST", baseURL(client, instanceID), gophercloud.RequestOpts{
80 JSONBody: &reqBody,
81 OkCodes: []int{202},
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010082 })
83
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010084 return res
85}
86
87func 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
95func Delete(client *gophercloud.ServiceClient, instanceID, userName string) DeleteResult {
96 var res DeleteResult
97
Jamie Hannaforda50d1352015-02-18 11:38:38 +010098 _, res.Err = client.Request("DELETE", userURL(client, instanceID, userName), gophercloud.RequestOpts{
99 OkCodes: []int{202},
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100100 })
101
Jamie Hannafordaf4570f2015-02-12 13:33:25 +0100102 return res
103}