Skip to content

Commit 00a0a7a

Browse files
committed
database/v1.0 instance resource
Change-Id: Iaf830eb2d6879bfb1209d6a5cab1453b518945dc
1 parent 4c01c6b commit 00a0a7a

File tree

8 files changed

+227
-0
lines changed

8 files changed

+227
-0
lines changed

openstack/database/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
from openstack.auth import service_filter
14+
15+
16+
class DatabaseService(service_filter.ServiceFilter):
17+
"""The database service."""
18+
19+
def __init__(self):
20+
"""Create an database service."""
21+
super(DatabaseService, self).__init__(service_type='database')

openstack/database/v1/__init__.py

Whitespace-only changes.

openstack/database/v1/instance.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
from openstack.database import database_service
14+
from openstack import resource
15+
from openstack import utils
16+
17+
18+
class Instance(resource.Resource):
19+
resource_key = 'instance'
20+
resources_key = 'instances'
21+
base_path = '/instances'
22+
service = database_service.DatabaseService()
23+
24+
# capabilities
25+
allow_create = True
26+
allow_retrieve = True
27+
allow_update = True
28+
allow_delete = True
29+
allow_list = True
30+
31+
# Properties
32+
flavor = resource.prop('flavor')
33+
links = resource.prop('links')
34+
name = resource.prop('name')
35+
status = resource.prop('status')
36+
volume = resource.prop('volume')
37+
38+
def enable_root_user(self, session):
39+
url = utils.urljoin(self.base_path, self.id, 'root')
40+
resp = session.post(url, service=self.service).body
41+
return resp['user']
42+
43+
def is_root_enabled(self, session):
44+
url = utils.urljoin(self.base_path, self.id, 'root')
45+
resp = session.get(url, service=self.service).body
46+
return resp['rootEnabled']
47+
48+
def restart(self, session):
49+
body = {'restart': {}}
50+
url = utils.urljoin(self.base_path, self.id, 'action')
51+
session.post(url, service=self.service, json=body)
52+
53+
def resize(self, session, flavor_reference):
54+
body = {'resize': {'flavorRef': flavor_reference}}
55+
url = utils.urljoin(self.base_path, self.id, 'action')
56+
session.post(url, service=self.service, json=body)
57+
58+
def resize_volume(self, session, volume_size):
59+
body = {'resize': {'volume': volume_size}}
60+
url = utils.urljoin(self.base_path, self.id, 'action')
61+
session.post(url, service=self.service, json=body)

openstack/tests/database/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import testtools
14+
15+
from openstack.database import database_service
16+
17+
18+
class TestDatabaseService(testtools.TestCase):
19+
20+
def test_service(self):
21+
sot = database_service.DatabaseService()
22+
self.assertEqual('database', sot.service_type)
23+
self.assertEqual('public', sot.visibility)
24+
self.assertIsNone(sot.region)
25+
self.assertIsNone(sot.service_name)

openstack/tests/database/v1/__init__.py

Whitespace-only changes.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import mock
14+
import testtools
15+
16+
from openstack.database.v1 import instance
17+
18+
IDENTIFIER = 'IDENTIFIER'
19+
EXAMPLE = {
20+
'flavor': '1',
21+
'id': IDENTIFIER,
22+
'links': '3',
23+
'name': '4',
24+
'status': '5',
25+
'volume': '6',
26+
}
27+
28+
29+
class TestInstance(testtools.TestCase):
30+
31+
def test_basic(self):
32+
sot = instance.Instance()
33+
self.assertEqual('instance', sot.resource_key)
34+
self.assertEqual('instances', sot.resources_key)
35+
self.assertEqual('/instances', sot.base_path)
36+
self.assertEqual('database', sot.service.service_type)
37+
self.assertTrue(sot.allow_create)
38+
self.assertTrue(sot.allow_retrieve)
39+
self.assertTrue(sot.allow_update)
40+
self.assertTrue(sot.allow_delete)
41+
self.assertTrue(sot.allow_list)
42+
43+
def test_make_it(self):
44+
sot = instance.Instance(EXAMPLE)
45+
self.assertEqual(EXAMPLE['flavor'], sot.flavor)
46+
self.assertEqual(EXAMPLE['id'], sot.id)
47+
self.assertEqual(EXAMPLE['links'], sot.links)
48+
self.assertEqual(EXAMPLE['name'], sot.name)
49+
self.assertEqual(EXAMPLE['status'], sot.status)
50+
self.assertEqual(EXAMPLE['volume'], sot.volume)
51+
52+
def test_enable_root_user(self):
53+
sot = instance.Instance(EXAMPLE)
54+
response = mock.Mock()
55+
response.body = {'user': {'name': 'root', 'password': 'foo'}}
56+
sess = mock.Mock()
57+
sess.post = mock.MagicMock()
58+
sess.post.return_value = response
59+
60+
self.assertEqual(response.body['user'], sot.enable_root_user(sess))
61+
62+
url = ("instances/%s/root" % IDENTIFIER)
63+
sess.post.assert_called_with(url, service=sot.service)
64+
65+
def test_is_root_enabled(self):
66+
sot = instance.Instance(EXAMPLE)
67+
response = mock.Mock()
68+
response.body = {'rootEnabled': True}
69+
sess = mock.Mock()
70+
sess.get = mock.MagicMock()
71+
sess.get.return_value = response
72+
73+
self.assertEqual(True, sot.is_root_enabled(sess))
74+
75+
url = ("instances/%s/root" % IDENTIFIER)
76+
sess.get.assert_called_with(url, service=sot.service)
77+
78+
def test_action_restart(self):
79+
sot = instance.Instance(EXAMPLE)
80+
response = mock.Mock()
81+
response.body = ''
82+
sess = mock.Mock()
83+
sess.post = mock.MagicMock()
84+
sess.post.return_value = response
85+
86+
self.assertEqual(None, sot.restart(sess))
87+
88+
url = ("instances/%s/action" % IDENTIFIER)
89+
body = {'restart': {}}
90+
sess.post.assert_called_with(url, service=sot.service, json=body)
91+
92+
def test_action_resize(self):
93+
sot = instance.Instance(EXAMPLE)
94+
response = mock.Mock()
95+
response.body = ''
96+
sess = mock.Mock()
97+
sess.post = mock.MagicMock()
98+
sess.post.return_value = response
99+
flavor = 'http://flavor/flav'
100+
101+
self.assertEqual(None, sot.resize(sess, flavor))
102+
103+
url = ("instances/%s/action" % IDENTIFIER)
104+
body = {'resize': {'flavorRef': flavor}}
105+
sess.post.assert_called_with(url, service=sot.service, json=body)
106+
107+
def test_action_resize_volume(self):
108+
sot = instance.Instance(EXAMPLE)
109+
response = mock.Mock()
110+
response.body = ''
111+
sess = mock.Mock()
112+
sess.post = mock.MagicMock()
113+
sess.post.return_value = response
114+
size = 4
115+
116+
self.assertEqual(None, sot.resize_volume(sess, size))
117+
118+
url = ("instances/%s/action" % IDENTIFIER)
119+
body = {'resize': {'volume': size}}
120+
sess.post.assert_called_with(url, service=sot.service, json=body)

0 commit comments

Comments
 (0)