Skip to content

Commit 2db585d

Browse files
committed
Add node resource for cluster service
This patch adds the node resource for the cluster service. Change-Id: Ia4adff8670e37d749338689c986dd734fecd22ca
1 parent 7c69247 commit 2db585d

4 files changed

Lines changed: 328 additions & 0 deletions

File tree

‎openstack/cluster/v1/_proxy.py‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from openstack.cluster.v1 import action
1414
from openstack.cluster.v1 import cluster
15+
from openstack.cluster.v1 import node
1516
from openstack.cluster.v1 import policy
1617
from openstack import proxy
1718

@@ -83,6 +84,7 @@ def clusters(self, **query):
8384
whether a cluster should be included in the list result.
8485
* sort_keys: A list of key names for sorting the resulted list.
8586
* sort_dir: Direction for sorting, and its valid values are 'asc'
87+
and 'desc'.
8688
* limit: Requests a specified size of returned items from the
8789
query. Returns a number of items up to the specified limit
8890
value.
@@ -108,6 +110,92 @@ def update_cluster(self, value, **attrs):
108110
"""
109111
return self._update(cluster.Cluster, value, **attrs)
110112

113+
def create_node(self, **attrs):
114+
"""Create a new node from attributes.
115+
116+
:param dict attrs: Keyword arguments that will be used to create a
117+
:class:`~openstack.cluster.v1.node.Node`, it is comprised
118+
of the properties on the Node class.
119+
120+
:returns: The results of node creation.
121+
:rtype: :class:`~openstack.cluster.v1.node.Node`.
122+
"""
123+
return self._create(node.Node, **attrs)
124+
125+
def delete_node(self, value, ignore_missing=True):
126+
"""Delete a node.
127+
128+
:param value: The value can be either the name or ID of a node or a
129+
:class:`~openstack.cluster.v1.node.Node` instance.
130+
:param bool ignore_missing: When set to ``False``, an exception
131+
:class:`~openstack.exceptions.ResourceNotFound` will be raised when
132+
the node could not be found. When set to ``True``, no exception
133+
will be raised when attempting to delete a non-existent node.
134+
135+
:returns: ``None``
136+
"""
137+
self._delete(node.Node, value, ignore_missing=ignore_missing)
138+
139+
def find_node(self, value, ignore_missing=True):
140+
"""Find a single node.
141+
142+
:param value: The name or ID of a node.
143+
:returns: One :class:`~openstack.cluster.v1.node.Node` object or None.
144+
"""
145+
return node.Node.find(self.session, value,
146+
ignore_missing=ignore_missing)
147+
148+
def get_node(self, value):
149+
"""Get a single node.
150+
151+
:param value: The value can be the name or ID of a node or a
152+
:class:`~openstack.cluster.v1.node.Node` instance.
153+
154+
:returns: One :class:`~openstack.cluster.v1.node.Node`
155+
:raises: :class:`~openstack.exceptions.ResourceNotFound` when no
156+
node matching the name or ID could be found.
157+
"""
158+
return self._get(node.Node, value)
159+
160+
def nodes(self, **query):
161+
"""Retrieve a generator of nodes.
162+
163+
:param kwargs \*\*query: Optional query parameters to be sent to
164+
restrict the nodes to be returned. Available parameters include:
165+
166+
* cluster_id: A string including the name or ID of a cluster to
167+
which the resulted node(s) is a member.
168+
* show_deleted: A boolean value indicating whether soft-deleted
169+
nodes should be returned as well.
170+
* filters: A list of key-value pairs for server to determine
171+
whether a node should be included in the list result.
172+
* sort_keys: A list of key names for sorting the resulted list.
173+
* sort_dir: Direction for sorting, and its valid values are 'asc'
174+
and 'desc'.
175+
* limit: Requests at most the specified number of items be
176+
returned from the query.
177+
* marker: Specifies the ID of the last-seen node. Use the limit
178+
parameter to make an initial limited request and use the ID of
179+
the last-seen node from the response as the marker parameter
180+
value in a subsequent limited request.
181+
182+
:returns: A generator of node instances.
183+
"""
184+
return self._list(node.Node, paginated=True, **query)
185+
186+
def update_node(self, value, **attrs):
187+
"""Update a node.
188+
189+
:param value: Either the name or the ID of the node, or an instance
190+
of :class:`~openstack.cluster.v1.node.Node`.
191+
:param attrs: The attributes to update on the node represented by
192+
the ``value`` parameter.
193+
194+
:returns: The updated node.
195+
:rtype: :class:`~openstack.cluster.v1.node.Node`
196+
"""
197+
return self._update(node.Node, value, **attrs)
198+
111199
def create_policy(self, **attrs):
112200
"""Create a new policy from attributes.
113201

‎openstack/cluster/v1/node.py‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.cluster import cluster_service
14+
from openstack import resource
15+
from openstack import utils
16+
17+
18+
class Node(resource.Resource):
19+
resource_key = 'node'
20+
resources_key = 'nodes'
21+
base_path = '/nodes'
22+
service = cluster_service.ClusterService()
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+
#: The name of the node.
33+
name = resource.prop('name')
34+
#: The ID of the physical object that backs the node.
35+
physical_id = resource.prop('physical_id')
36+
#: The ID of the cluster in which this node is a member.
37+
#: A node is an orphan node if this field is empty.
38+
cluster_id = resource.prop('cluster_id')
39+
#: The ID of the profile used by this node.
40+
profile_id = resource.prop('profile_id')
41+
#: The ID of the project this node belongs to.
42+
project = resource.prop('project')
43+
#: The name of the profile used by this node.
44+
profile_name = resource.prop('profile_name')
45+
#: An integer that is unique inside the owning cluster.
46+
#: A value of -1 means this node is an orphan node.
47+
index = resource.prop('index', type=int)
48+
#: A string indicating the role the node plays in a cluster.
49+
role = resource.prop('role')
50+
#: The timestamp of the node object's initialization.
51+
init_time = resource.prop('init_time')
52+
#: The timestamp of the node's creation, i.e. the physical object
53+
#: represented by this node is also created.
54+
created_time = resource.prop('created_time')
55+
#: The timestamp the node was last updated.
56+
updated_time = resource.prop('updated_time')
57+
#: The timestamp the node was deleted. This is only used for node
58+
#: which has been soft deleted.
59+
deleted_time = resource.prop('deleted_time')
60+
#: A string indicating the node's status.
61+
status = resource.prop('status')
62+
#: A string describing why the node entered its current status.
63+
status_reason = resource.prop('status_reason')
64+
#: A map containing key-value pairs attached to the node.
65+
metadata = resource.prop('tags', type=dict)
66+
#: A map containing some runtime data for this node.
67+
data = resource.prop('data', type=dict)
68+
#: A map containing the details of the physical object this node
69+
#: represents
70+
details = resource.prop('details', type=dict)
71+
72+
def _action(self, session, body):
73+
"""Procedure the invoke an action API.
74+
75+
:param session: A session object used for sending request.
76+
:param body: The body of action to be sent.
77+
"""
78+
url = utils.urljoin(self.base_path, self.id, 'action')
79+
resp = session.put(url, service=self.service, json=body).body
80+
return resp
81+
82+
def join(self, session, cluster_id):
83+
"""An action procedure for the node to join a cluster.
84+
85+
:param session: A session object used for sending request.
86+
:param cluster_id: The ID, name or short ID of a cluster the
87+
node is about to join.
88+
:returns: A dictionary containing the action ID.
89+
"""
90+
body = {
91+
'join': {
92+
'cluster_id': cluster_id,
93+
}
94+
}
95+
return self._action(session, body)
96+
97+
def leave(self, session):
98+
"""An action procedure for the node to leave its current cluster.
99+
100+
:param session: A session object used for sending request.
101+
:returns: A dictionary containing the action ID.
102+
"""
103+
body = {
104+
'leave': {}
105+
}
106+
return self._action(session, body)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.cluster.v1 import node
17+
18+
19+
FAKE_ID = '123d0955-0099-aabb-b8fa-6a44655ceeff'
20+
FAKE_NAME = 'test_node'
21+
22+
FAKE = {
23+
'id': FAKE_ID,
24+
'cluster_id': 'clusterA',
25+
'metadata': {},
26+
'name': FAKE_NAME,
27+
'profile_id': 'myserver',
28+
'index': 1,
29+
'role': 'master',
30+
}
31+
32+
FAKE_CREATE_RESP = {
33+
'node': {
34+
'id': FAKE_ID,
35+
'name': FAKE_NAME,
36+
'cluster_id': '99001122-aabb-ccdd-ffff-efdcab124567',
37+
'action': '1122aabb-eeff-7755-2222-00991234dcba',
38+
'created_time': None,
39+
'deleted_time': None,
40+
'updated_time': None,
41+
'data': {},
42+
'role': 'master',
43+
'index': 1,
44+
'init_time': None,
45+
'metadata': {},
46+
'profile_id': '560a8f9d-7596-4a32-85e8-03645fa7be13',
47+
'profile_name': 'myserver',
48+
'project': '333acb15a43242f4a609a27cb097a8f2',
49+
'status': 'INIT',
50+
'status_reason': 'Initializing',
51+
}
52+
}
53+
54+
55+
class TestNode(testtools.TestCase):
56+
57+
def setUp(self):
58+
super(TestNode, self).setUp()
59+
60+
def test_basic(self):
61+
sot = node.Node()
62+
self.assertEqual('node', sot.resource_key)
63+
self.assertEqual('nodes', sot.resources_key)
64+
self.assertEqual('/nodes', sot.base_path)
65+
self.assertEqual('clustering', sot.service.service_type)
66+
self.assertTrue(sot.allow_create)
67+
self.assertTrue(sot.allow_retrieve)
68+
self.assertTrue(sot.allow_update)
69+
self.assertTrue(sot.allow_delete)
70+
self.assertTrue(sot.allow_list)
71+
72+
def test_instantiate(self):
73+
sot = node.Node(FAKE)
74+
self.assertEqual(FAKE['id'], sot.id)
75+
self.assertEqual(FAKE['name'], sot.name)
76+
77+
self.assertEqual(FAKE['profile_id'], sot.profile_id)
78+
self.assertEqual(FAKE['cluster_id'], sot.cluster_id)
79+
self.assertEqual(FAKE['name'], sot.name)
80+
self.assertEqual(FAKE['index'], sot.index)
81+
self.assertEqual(FAKE['role'], sot.role)
82+
self.assertEqual(FAKE['metadata'], sot.metadata)
83+
84+
def test_join(self):
85+
sot = node.Node(FAKE)
86+
sot['id'] = 'IDENTIFIER'
87+
88+
resp = mock.Mock()
89+
resp.body = {'action': '1234-5678-abcd'}
90+
sess = mock.Mock()
91+
sess.put = mock.MagicMock(return_value=resp)
92+
self.assertEqual(resp.body, sot.join(sess, 'cluster-b'))
93+
url = 'nodes/%s/action' % sot.id
94+
body = {'join': {'cluster_id': 'cluster-b'}}
95+
sess.put.assert_called_once_with(url, service=sot.service, json=body)
96+
97+
def test_leave(self):
98+
sot = node.Node(FAKE)
99+
sot['id'] = 'IDENTIFIER'
100+
101+
resp = mock.Mock()
102+
resp.body = {'action': '2345-6789-bbbb'}
103+
sess = mock.Mock()
104+
sess.put = mock.MagicMock(return_value=resp)
105+
self.assertEqual(resp.body, sot.leave(sess))
106+
url = 'nodes/%s/action' % sot.id
107+
body = {'leave': {}}
108+
sess.put.assert_called_once_with(url, service=sot.service, json=body)

‎openstack/tests/unit/cluster/v1/test_proxy.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from openstack.cluster.v1 import _proxy
1414
from openstack.cluster.v1 import action
1515
from openstack.cluster.v1 import cluster
16+
from openstack.cluster.v1 import node
1617
from openstack.cluster.v1 import policy
1718
from openstack.tests.unit import test_proxy_base
1819

@@ -47,6 +48,31 @@ def test_clusters(self):
4748
def test_cluster_update(self):
4849
self.verify_update(self.proxy.update_cluster, cluster.Cluster)
4950

51+
def test_node_create(self):
52+
self.verify_create(self.proxy.create_node, node.Node)
53+
54+
def test_node_delete(self):
55+
self.verify_delete(self.proxy.delete_node, node.Node, False)
56+
57+
def test_node_delete_ignore(self):
58+
self.verify_delete(self.proxy.delete_node, node.Node, True)
59+
60+
def test_node_find(self):
61+
self.verify_find('openstack.cluster.v1.node.Node.find',
62+
self.proxy.find_node)
63+
64+
def test_node_get(self):
65+
self.verify_get(self.proxy.get_node, node.Node)
66+
67+
def test_nodes(self):
68+
self.verify_list(self.proxy.nodes, node.Node,
69+
paginated=True,
70+
method_kwargs={'limit': 2},
71+
expected_kwargs={'limit': 2})
72+
73+
def test_node_update(self):
74+
self.verify_update(self.proxy.update_node, node.Node)
75+
5076
def test_policy_create(self):
5177
self.verify_create(self.proxy.create_policy, policy.Policy)
5278

0 commit comments

Comments
 (0)