File: users.py

package info (click to toggle)
python-troveclient 1%3A2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,760 kB
  • sloc: python: 16,424; sh: 95; makefile: 14
file content (120 lines) | stat: -rw-r--r-- 5,010 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2011 OpenStack Foundation
# Copyright 2013 Rackspace Hosting
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from troveclient import base
from troveclient import common
from troveclient import exceptions
from troveclient.v1 import databases


class User(base.Resource):
    """A database user."""

    def __repr__(self):
        return "<User: %s>" % self.name


class Users(base.ManagerWithFind):
    """Manage :class:`Users` resources."""
    resource_class = User

    def create(self, instance, users):
        """Create users with permissions to the specified databases."""
        body = {"users": users}
        url = "/instances/%s/users" % base.getid(instance)
        resp, body = self.api.client.post(url, body=body)
        common.check_for_exceptions(resp, body, url)

    def delete(self, instance, username, hostname=None):
        """Delete an existing user in the specified instance."""
        user = common.quote_user_host(username, hostname)
        url = "/instances/%s/users/%s" % (base.getid(instance), user)
        resp, body = self.api.client.delete(url)
        common.check_for_exceptions(resp, body, url)

    def list(self, instance, limit=None, marker=None):
        """Get a list of all Users from the instance's Database.

        :rtype: list of :class:`User`.
        """
        url = "/instances/%s/users" % base.getid(instance)
        return self._paginated(url, "users", limit, marker)

    def get(self, instance, username, hostname=None):
        """Get a single User from the instance's Database.

        :rtype: :class:`User`.
        """
        user = common.quote_user_host(username, hostname)
        url = "/instances/%s/users/%s" % (base.getid(instance), user)
        return self._get(url, "user")

    def update_attributes(self, instance, username, newuserattr=None,
                          hostname=None):
        """Update attributes of a single User in an instance.

        :rtype: :class:`User`.
        """
        if not newuserattr:
            raise exceptions.ValidationError("No updates specified for user %s"
                                             % username)
        instance_id = base.getid(instance)
        user = common.quote_user_host(username, hostname)
        user_dict = {}
        user_dict['user'] = newuserattr
        url = "/instances/%s/users/%s" % (instance_id, user)
        resp, body = self.api.client.put(url, body=user_dict)
        common.check_for_exceptions(resp, body, url)

    def list_access(self, instance, username, hostname=None):
        """Show all databases the given user has access to."""
        instance_id = base.getid(instance)
        user = common.quote_user_host(username, hostname)
        url = "/instances/%(instance_id)s/users/%(user)s/databases"
        local_vars = locals()
        resp, body = self.api.client.get(url % local_vars)
        common.check_for_exceptions(resp, body, url)
        if not body:
            raise Exception("Call to %s did not return to a body" % url)
        return [databases.Database(self, db) for db in body['databases']]

    def grant(self, instance, username, databases, hostname=None):
        """Allow an existing user permissions to access a database."""
        instance_id = base.getid(instance)
        user = common.quote_user_host(username, hostname)
        url = "/instances/%(instance_id)s/users/%(user)s/databases"
        dbs = {'databases': [{'name': db} for db in databases]}
        local_vars = locals()
        resp, body = self.api.client.put(url % local_vars, body=dbs)
        common.check_for_exceptions(resp, body, url)

    def revoke(self, instance, username, database, hostname=None):
        """Revoke from an existing user access permissions to a database."""
        instance_id = base.getid(instance)
        user = common.quote_user_host(username, hostname)
        url = ("/instances/%(instance_id)s/users/%(user)s/"
               "databases/%(database)s")
        local_vars = locals()
        resp, body = self.api.client.delete(url % local_vars)
        common.check_for_exceptions(resp, body, url)

    def change_passwords(self, instance, users):
        """Change the password for one or more users."""
        instance_id = base.getid(instance)
        user_dict = {"users": users}
        url = "/instances/%s/users" % instance_id
        resp, body = self.api.client.put(url, body=user_dict)
        common.check_for_exceptions(resp, body, url)