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