Skip to content

Commit 7fb7c10

Browse files
committed
Port database v1 to resource2
This change ports the database service over to resource2. It's generally a renaming, though there are some changes from the old path_args way to the new one in the proxy. Change-Id: Ib7e0ac2cf449ea1e60fcbf45ff1e46d690c1cf77
1 parent a703db3 commit 7fb7c10

10 files changed

Lines changed: 177 additions & 99 deletions

File tree

openstack/database/v1/_proxy.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,36 @@
1414
from openstack.database.v1 import flavor as _flavor
1515
from openstack.database.v1 import instance as _instance
1616
from openstack.database.v1 import user as _user
17-
from openstack import proxy
17+
from openstack import proxy2
1818

1919

20-
class Proxy(proxy.BaseProxy):
20+
class Proxy(proxy2.BaseProxy):
2121

22-
def create_database(self, **attrs):
22+
def create_database(self, instance, **attrs):
2323
"""Create a new database from attributes
2424
25+
:param instance: This can be either the ID of an instance
26+
or a :class:`~openstack.database.v1.instance.Instance`
2527
:param dict attrs: Keyword arguments which will be used to create
2628
a :class:`~openstack.database.v1.database.Database`,
2729
comprised of the properties on the Database class.
2830
2931
:returns: The results of server creation
3032
:rtype: :class:`~openstack.database.v1.database.Database`
3133
"""
32-
return self._create(_database.Database, **attrs)
34+
instance = self._get_resource(_instance.Instance, instance)
35+
return self._create(_database.Database, instance_id=instance.id,
36+
**attrs)
3337

34-
def delete_database(self, database, ignore_missing=True):
38+
def delete_database(self, database, instance=None, ignore_missing=True):
3539
"""Delete a database
3640
3741
:param database: The value can be either the ID of a database or a
3842
:class:`~openstack.database.v1.database.Database` instance.
43+
:param instance: This parameter needs to be specified when
44+
an ID is given as `database`.
45+
It can be either the ID of an instance
46+
or a :class:`~openstack.database.v1.instance.Instance`
3947
:param bool ignore_missing: When set to ``False``
4048
:class:`~openstack.exceptions.ResourceNotFound` will be
4149
raised when the database does not exist.
@@ -44,37 +52,52 @@ def delete_database(self, database, ignore_missing=True):
4452
4553
:returns: ``None``
4654
"""
47-
self._delete(_database.Database, database,
55+
instance_id = self._get_uri_attribute(database, instance,
56+
"instance_id")
57+
self._delete(_database.Database, database, instance_id=instance_id,
4858
ignore_missing=ignore_missing)
4959

50-
def find_database(self, name_or_id, ignore_missing=True):
60+
def find_database(self, name_or_id, instance, ignore_missing=True):
5161
"""Find a single database
5262
5363
:param name_or_id: The name or ID of a database.
64+
:param instance: This can be either the ID of an instance
65+
or a :class:`~openstack.database.v1.instance.Instance`
5466
:param bool ignore_missing: When set to ``False``
5567
:class:`~openstack.exceptions.ResourceNotFound` will be
5668
raised when the resource does not exist.
5769
When set to ``True``, None will be returned when
5870
attempting to find a nonexistent resource.
5971
:returns: One :class:`~openstack.database.v1.database.Database` or None
6072
"""
73+
instance = self._get_resource(_instance.Instance, instance)
6174
return self._find(_database.Database, name_or_id,
75+
instance_id=instance.id,
6276
ignore_missing=ignore_missing)
6377

64-
def databases(self, **query):
78+
def databases(self, instance, **query):
6579
"""Return a generator of databases
6680
81+
:param instance: This can be either the ID of an instance
82+
or a :class:`~openstack.database.v1.instance.Instance`
83+
instance that the interface belongs to.
6784
:param kwargs \*\*query: Optional query parameters to be sent to limit
6885
the resources being returned.
6986
7087
:returns: A generator of database objects
7188
:rtype: :class:`~openstack.database.v1.database.Database`
7289
"""
73-
return self._list(_database.Database, paginated=False, **query)
90+
instance = self._get_resource(_instance.Instance, instance)
91+
return self._list(_database.Database, paginated=False,
92+
instance_id=instance.id, **query)
7493

75-
def get_database(self, database):
94+
def get_database(self, database, instance=None):
7695
"""Get a single database
7796
97+
:param instance: This parameter needs to be specified when
98+
an ID is given as `database`.
99+
It can be either the ID of an instance
100+
or a :class:`~openstack.database.v1.instance.Instance`
78101
:param database: The value can be the ID of a database or a
79102
:class:`~openstack.database.v1.database.Database`
80103
instance.
@@ -202,23 +225,30 @@ def update_instance(self, instance, **attrs):
202225
"""
203226
return self._update(_instance.Instance, instance, **attrs)
204227

205-
def create_user(self, **attrs):
228+
def create_user(self, instance, **attrs):
206229
"""Create a new user from attributes
207230
231+
:param instance: This can be either the ID of an instance
232+
or a :class:`~openstack.database.v1.instance.Instance`
208233
:param dict attrs: Keyword arguments which will be used to create
209234
a :class:`~openstack.database.v1.user.User`,
210235
comprised of the properties on the User class.
211236
212237
:returns: The results of server creation
213238
:rtype: :class:`~openstack.database.v1.user.User`
214239
"""
215-
return self._create(_user.User, **attrs)
240+
instance = self._get_resource(_instance.Instance, instance)
241+
return self._create(_user.User, instance_id=instance.id, **attrs)
216242

217-
def delete_user(self, user, ignore_missing=True):
243+
def delete_user(self, user, instance=None, ignore_missing=True):
218244
"""Delete a user
219245
220246
:param user: The value can be either the ID of a user or a
221247
:class:`~openstack.database.v1.user.User` instance.
248+
:param instance: This parameter needs to be specified when
249+
an ID is given as `user`.
250+
It can be either the ID of an instance
251+
or a :class:`~openstack.database.v1.instance.Instance`
222252
:param bool ignore_missing: When set to ``False``
223253
:class:`~openstack.exceptions.ResourceNotFound` will be
224254
raised when the user does not exist.
@@ -227,41 +257,55 @@ def delete_user(self, user, ignore_missing=True):
227257
228258
:returns: ``None``
229259
"""
230-
self._delete(_user.User, user, ignore_missing=ignore_missing)
260+
instance = self._get_resource(_instance.Instance, instance)
261+
self._delete(_user.User, user, ignore_missing=ignore_missing,
262+
instance_id=instance.id)
231263

232-
def find_user(self, name_or_id, ignore_missing=True):
264+
def find_user(self, name_or_id, instance, ignore_missing=True):
233265
"""Find a single user
234266
235267
:param name_or_id: The name or ID of a user.
268+
:param instance: This can be either the ID of an instance
269+
or a :class:`~openstack.database.v1.instance.Instance`
236270
:param bool ignore_missing: When set to ``False``
237271
:class:`~openstack.exceptions.ResourceNotFound` will be
238272
raised when the resource does not exist.
239273
When set to ``True``, None will be returned when
240274
attempting to find a nonexistent resource.
241275
:returns: One :class:`~openstack.database.v1.user.User` or None
242276
"""
243-
return self._find(_user.User, name_or_id,
277+
instance = self._get_resource(_instance.Instance, instance)
278+
return self._find(_user.User, name_or_id, instance_id=instance.id,
244279
ignore_missing=ignore_missing)
245280

246-
def users(self, **query):
281+
def users(self, instance, **query):
247282
"""Return a generator of users
248283
284+
:param instance: This can be either the ID of an instance
285+
or a :class:`~openstack.database.v1.instance.Instance`
249286
:param kwargs \*\*query: Optional query parameters to be sent to limit
250287
the resources being returned.
251288
252289
:returns: A generator of user objects
253290
:rtype: :class:`~openstack.database.v1.user.User`
254291
"""
255-
return self._list(_user.User, paginated=False, **query)
292+
instance = self._get_resource(_instance.Instance, instance)
293+
return self._list(_user.User, instance_id=instance.id,
294+
paginated=False, **query)
256295

257-
def get_user(self, user):
296+
def get_user(self, user, instance=None):
258297
"""Get a single user
259298
260299
:param user: The value can be the ID of a user or a
261300
:class:`~openstack.database.v1.user.User` instance.
301+
:param instance: This parameter needs to be specified when
302+
an ID is given as `database`.
303+
It can be either the ID of an instance
304+
or a :class:`~openstack.database.v1.instance.Instance`
262305
263306
:returns: One :class:`~openstack.database.v1.user.User`
264307
:raises: :class:`~openstack.exceptions.ResourceNotFound`
265308
when no resource can be found.
266309
"""
310+
instance = self._get_resource(_instance.Instance, instance)
267311
return self._get(_user.User, user)

openstack/database/v1/database.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
# under the License.
1212

1313
from openstack.database import database_service
14-
from openstack import resource
14+
from openstack import resource2 as resource
1515

1616

1717
class Database(resource.Resource):
18-
id_attribute = 'name'
1918
resource_key = 'database'
2019
resources_key = 'databases'
2120
base_path = '/instances/%(instance_id)s/databases'
@@ -28,11 +27,11 @@ class Database(resource.Resource):
2827

2928
# Properties
3029
#: Set of symbols and encodings. The default character set is ``utf8``.
31-
character_set = resource.prop('character_set')
30+
character_set = resource.Body('character_set')
3231
#: Set of rules for comparing characters in a character set.
3332
#: The default value for collate is ``utf8_general_ci``.
34-
collate = resource.prop('collate')
33+
collate = resource.Body('collate')
3534
#: The ID of the instance
36-
instance_id = resource.prop('instance_id')
35+
instance_id = resource.URI('instance_id')
3736
#: The name of the database
38-
name = resource.prop('name')
37+
name = resource.Body('name', alternate_id=True)

openstack/database/v1/flavor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# under the License.
1212

1313
from openstack.database import database_service
14-
from openstack import resource
14+
from openstack import resource2 as resource
1515

1616

1717
class Flavor(resource.Resource):
@@ -22,12 +22,12 @@ class Flavor(resource.Resource):
2222

2323
# capabilities
2424
allow_list = True
25-
allow_retrieve = True
25+
allow_get = True
2626

2727
# Properties
2828
#: Links associated with the flavor
29-
links = resource.prop('links')
29+
links = resource.Body('links')
3030
#: The name of the flavor
31-
name = resource.prop('name')
31+
name = resource.Body('name')
3232
#: The size in MB of RAM the flavor has
33-
ram = resource.prop('ram')
33+
ram = resource.Body('ram')

openstack/database/v1/instance.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# under the License.
1212

1313
from openstack.database import database_service
14-
from openstack import resource
14+
from openstack import resource2 as resource
1515
from openstack import utils
1616

1717

@@ -23,22 +23,35 @@ class Instance(resource.Resource):
2323

2424
# capabilities
2525
allow_create = True
26-
allow_retrieve = True
26+
allow_get = True
2727
allow_update = True
2828
allow_delete = True
2929
allow_list = True
3030

3131
# Properties
3232
#: The flavor of the instance
33-
flavor = resource.prop('flavor')
33+
flavor = resource.Body('flavor')
3434
#: Links associated with the instance
35-
links = resource.prop('links')
35+
links = resource.Body('links')
3636
#: The name of the instance
37-
name = resource.prop('name')
37+
name = resource.Body('name')
3838
#: The status of the instance
39-
status = resource.prop('status')
39+
status = resource.Body('status')
4040
#: The size of the volume
41-
volume = resource.prop('volume')
41+
volume = resource.Body('volume')
42+
#: A dictionary of datastore details, often including 'type' and 'version'
43+
#: keys
44+
datastore = resource.Body('datastore', type=dict)
45+
#: The ID of this instance
46+
id = resource.Body('id')
47+
#: The region this instance resides in
48+
region = resource.Body('region')
49+
#: The name of the host
50+
hostname = resource.Body('hostname')
51+
#: The timestamp when this instance was created
52+
created_at = resource.Body('created')
53+
#: The timestamp when this instance was updated
54+
updated_at = resource.Body('updated')
4255

4356
def enable_root_user(self, session):
4457
"""Enable login for the root user.

openstack/database/v1/user.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
# under the License.
1212

1313
from openstack.database import database_service
14-
from openstack import resource
14+
from openstack import resource2 as resource
15+
from openstack import utils
1516

1617

1718
class User(resource.Resource):
18-
id_attribute = 'name'
1919
resource_key = 'user'
2020
resources_key = 'users'
2121
base_path = '/instances/%(instance_id)s/users'
@@ -26,21 +26,25 @@ class User(resource.Resource):
2626
allow_delete = True
2727
allow_list = True
2828

29-
# path args
30-
instance_id = resource.prop('instance_id')
29+
instance_id = resource.URI('instance_id')
3130

3231
# Properties
3332
#: Databases the user has access to
34-
databases = resource.prop('databases')
33+
databases = resource.Body('databases')
3534
#: The name of the user
36-
name = resource.prop('name')
35+
name = resource.Body('name', alternate_id=True)
3736
#: The password of the user
38-
password = resource.prop('password')
39-
40-
@classmethod
41-
def create_by_id(cls, session, attrs, r_id=None, path_args=None):
42-
url = cls._get_url(path_args)
43-
# Create expects an array of users
44-
body = {'users': [attrs]}
45-
resp = session.post(url, endpoint_filter=cls.service, json=body)
46-
return resp.json()
37+
password = resource.Body('password')
38+
39+
def _prepare_request(self, requires_id=True, prepend_key=True):
40+
"""Prepare a request for the database service's create call
41+
42+
User.create calls require the resources_key.
43+
The base_prepare_request would insert the resource_key (singular)
44+
"""
45+
body = {self.resources_key: self._body.dirty}
46+
47+
uri = self.base_path % self._uri.attributes
48+
uri = utils.urljoin(uri, self.id)
49+
50+
return resource._Request(uri, body, None)

openstack/tests/unit/database/v1/test_database.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ def test_basic(self):
3636
self.assertEqual('database', sot.service.service_type)
3737
self.assertTrue(sot.allow_list)
3838
self.assertTrue(sot.allow_create)
39-
self.assertFalse(sot.allow_retrieve)
39+
self.assertFalse(sot.allow_get)
4040
self.assertFalse(sot.allow_update)
4141
self.assertTrue(sot.allow_delete)
4242

4343
def test_make_it(self):
44-
sot = database.Database(EXAMPLE)
44+
sot = database.Database(**EXAMPLE)
4545
self.assertEqual(IDENTIFIER, sot.id)
4646
self.assertEqual(EXAMPLE['character_set'], sot.character_set)
4747
self.assertEqual(EXAMPLE['collate'], sot.collate)
4848
self.assertEqual(EXAMPLE['instance_id'], sot.instance_id)
4949
self.assertEqual(IDENTIFIER, sot.name)
50+
self.assertEqual(IDENTIFIER, sot.id)

openstack/tests/unit/database/v1/test_flavor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def test_basic(self):
3333
self.assertEqual('database', sot.service.service_type)
3434
self.assertTrue(sot.allow_list)
3535
self.assertFalse(sot.allow_create)
36-
self.assertTrue(sot.allow_retrieve)
36+
self.assertTrue(sot.allow_get)
3737
self.assertFalse(sot.allow_update)
3838
self.assertFalse(sot.allow_delete)
3939

4040
def test_make_it(self):
41-
sot = flavor.Flavor(EXAMPLE)
41+
sot = flavor.Flavor(**EXAMPLE)
4242
self.assertEqual(IDENTIFIER, sot.id)
4343
self.assertEqual(EXAMPLE['links'], sot.links)
4444
self.assertEqual(EXAMPLE['name'], sot.name)

0 commit comments

Comments
 (0)