blob: 7c30079554e596bce3e8fc4fbdfed8d690067894 [file] [log] [blame]
Jamie Hannaford984e9172015-02-13 14:34:03 +01001package users
2
3import (
Jamie Hannaford984e9172015-02-13 14:34:03 +01004 "github.com/rackspace/gophercloud"
5 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
6 os "github.com/rackspace/gophercloud/openstack/db/v1/users"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010010/*
11ChangePassword changes the password for one or more users. For example, to
12change the respective passwords for two users:
13
14 opts := os.BatchCreateOpts{
15 os.CreateOpts{Name: "db_user_1", Password: "new_password_1"},
16 os.CreateOpts{Name: "db_user_2", Password: "new_password_2"},
17 }
18
19 ChangePassword(client, "instance_id", opts)
20*/
Jamie Hannaford984e9172015-02-13 14:34:03 +010021func ChangePassword(client *gophercloud.ServiceClient, instanceID string, opts os.BatchCreateOpts) UpdatePasswordsResult {
22 var res UpdatePasswordsResult
23
24 reqBody, err := opts.ToUserCreateMap()
25 if err != nil {
26 res.Err = err
27 return res
28 }
29
Jamie Hannaforda50d1352015-02-18 11:38:38 +010030 _, res.Err = client.Request("PUT", baseURL(client, instanceID), gophercloud.RequestOpts{
31 JSONBody: &reqBody,
32 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +010033 })
34
35 return res
36}
37
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010038// Update will modify the attributes of a specified user. Attributes that can
39// be updated are: user name, password, and host.
Jamie Hannaford984e9172015-02-13 14:34:03 +010040func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts os.CreateOpts) UpdateResult {
41 var res UpdateResult
42
43 reqBody, err := opts.ToMap()
44 if err != nil {
45 res.Err = err
46 return res
47 }
48 reqBody = map[string]interface{}{"user": reqBody}
49
Jamie Hannaforda50d1352015-02-18 11:38:38 +010050 _, res.Err = client.Request("PUT", userURL(client, instanceID, userName), gophercloud.RequestOpts{
51 JSONBody: &reqBody,
52 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +010053 })
54
55 return res
56}
57
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010058// Get will retrieve the details for a particular user.
Jamie Hannaford984e9172015-02-13 14:34:03 +010059func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult {
60 var res GetResult
61
Jamie Hannaforda50d1352015-02-18 11:38:38 +010062 _, res.Err = client.Request("GET", userURL(client, instanceID, userName), gophercloud.RequestOpts{
63 JSONResponse: &res.Body,
64 OkCodes: []int{200},
Jamie Hannaford984e9172015-02-13 14:34:03 +010065 })
66
67 return res
68}
69
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010070// ListAccess will list all of the databases a user has access to.
Jamie Hannaford984e9172015-02-13 14:34:03 +010071func ListAccess(client *gophercloud.ServiceClient, instanceID, userName string) pagination.Pager {
72 pageFn := func(r pagination.PageResult) pagination.Page {
73 return AccessPage{pagination.LinkedPageBase{PageResult: r}}
74 }
75
76 return pagination.NewPager(client, dbsURL(client, instanceID, userName), pageFn)
77}
78
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010079/*
80GrantAccess for the specified user to one or more databases on a specified
81instance. For example, to add a user to multiple databases:
82
83 opts := db.BatchCreateOpts{
84 db.CreateOpts{Name: "database_1"},
85 db.CreateOpts{Name: "database_3"},
86 db.CreateOpts{Name: "database_19"},
87 }
88
89 GrantAccess(client, "instance_id", "user_name", opts)
90*/
Jamie Hannaford984e9172015-02-13 14:34:03 +010091func GrantAccess(client *gophercloud.ServiceClient, instanceID, userName string, opts db.BatchCreateOpts) GrantAccessResult {
92 var res GrantAccessResult
93
94 reqBody, err := opts.ToDBCreateMap()
95 if err != nil {
96 res.Err = err
97 return res
98 }
99
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100100 _, res.Err = client.Request("PUT", dbsURL(client, instanceID, userName), gophercloud.RequestOpts{
101 JSONBody: &reqBody,
102 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100103 })
104
105 return res
106}
107
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100108/*
109RevokeAccess will revoke access for the specified user to one or more databases
110on a specified instance. For example, to remove a user's access to multiple
111databases:
112
113 opts := db.BatchCreateOpts{
114 db.CreateOpts{Name: "database_1"},
115 db.CreateOpts{Name: "database_3"},
116 db.CreateOpts{Name: "database_19"},
117 }
118
119 RevokeAccess(client, "instance_id", "user_name", opts)
120*/
Jamie Hannaford984e9172015-02-13 14:34:03 +0100121func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult {
122 var res RevokeAccessResult
123
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100124 _, res.Err = client.Request("DELETE", dbURL(client, instanceID, userName, dbName), gophercloud.RequestOpts{
125 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100126 })
127
128 return res
129}