Adding node to group from other env is bad request

Validator was tweaked to check if node cluster is the same as node group
cluster.

Change-Id: I5021c21fce736d595c604981d8c53cf0dde495ba
Closes-bug: #1508395
This commit is contained in:
Maciej Kwiek 2015-10-27 14:19:23 +01:00
parent 820a096c3a
commit aae84550e2
2 changed files with 59 additions and 27 deletions

View File

@ -227,6 +227,7 @@ class NodeValidator(BasicValidator):
log_message=True
)
existent_node = None
q = db().query(Node)
if "mac" in d:
existent_node = q.filter_by(mac=d["mac"].lower()).first() \
@ -245,25 +246,14 @@ class NodeValidator(BasicValidator):
log_message=True
)
if not instance:
instance = existent_node
if d.get("hostname") is not None:
if instance:
node = instance
else:
node = objects.Node.get_by_mac_or_uid(
mac=d.get("mac"),
node_uid=d.get("id")
)
cls.validate_hostname(d["hostname"], node)
cls.validate_hostname(d["hostname"], instance)
if "roles" in d:
if instance:
node = instance
else:
node = objects.Node.get_by_mac_or_uid(
mac=d.get("mac"),
node_uid=d.get("id")
)
cls.validate_roles(d, node)
cls.validate_roles(d, instance)
if 'meta' in d:
d['meta'] = MetaValidator.validate_update(d['meta'])
@ -274,7 +264,14 @@ class NodeValidator(BasicValidator):
raise errors.InvalidData(
"Cannot assign node group (ID={0}) to node {1}. "
"The specified node group does not exist."
.format(d["group_id"], d.get("id"))
.format(d["group_id"], instance.id)
)
if instance.cluster_id != ng.cluster_id:
raise errors.InvalidData(
"Cannot assign node group (ID={0}) to node {1}. "
"Node belongs to other cluster than node group"
.format(d["group_id"], instance.id)
)
return d

View File

@ -16,6 +16,7 @@
import json
from nailgun import consts
from nailgun.db import db
from nailgun.db.sqlalchemy.models import NetworkGroup
from nailgun import objects
@ -29,8 +30,8 @@ class TestNodeGroups(BaseIntegrationTest):
super(TestNodeGroups, self).setUp()
self.cluster = self.env.create_cluster(
api=False,
net_provider='neutron',
net_segment_type='gre'
net_provider=consts.CLUSTER_NET_PROVIDERS.neutron,
net_segment_type=consts.NEUTRON_SEGMENT_TYPES.gre
)
def test_nodegroup_creation(self):
@ -51,11 +52,11 @@ class TestNodeGroups(BaseIntegrationTest):
)
def test_nodegroup_assignment(self):
self.env.create(
cluster = self.env.create(
cluster_kwargs={
'api': True,
'net_provider': 'neutron',
'net_segment_type': 'gre'
'net_provider': consts.CLUSTER_NET_PROVIDERS.neutron,
'net_segment_type': consts.NEUTRON_SEGMENT_TYPES.gre
},
nodes_kwargs=[{
'roles': [],
@ -65,7 +66,7 @@ class TestNodeGroups(BaseIntegrationTest):
)
node = self.env.nodes[0]
resp = self.env.create_node_group()
resp = self.env.create_node_group(cluster_id=cluster.get('id'))
ng_id = resp.json_body['id']
resp = self.app.put(
@ -119,8 +120,8 @@ class TestNodeGroups(BaseIntegrationTest):
def test_nodegroup_vlan_segmentation_type(self):
cluster = self.env.create_cluster(
api=False,
net_provider='neutron',
net_segment_type='vlan'
net_provider=consts.CLUSTER_NET_PROVIDERS.neutron,
net_segment_type=consts.NEUTRON_SEGMENT_TYPES.vlan
)
resp = self.app.post(
reverse('NodeGroupCollectionHandler'),
@ -140,8 +141,8 @@ class TestNodeGroups(BaseIntegrationTest):
def test_nodegroup_tun_segmentation_type(self):
cluster = self.env.create_cluster(
api=False,
net_provider='neutron',
net_segment_type='tun'
net_provider=consts.CLUSTER_NET_PROVIDERS.neutron,
net_segment_type=consts.NEUTRON_SEGMENT_TYPES.tun
)
resp = self.app.post(
reverse('NodeGroupCollectionHandler'),
@ -320,3 +321,37 @@ class TestNodeGroups(BaseIntegrationTest):
3,
objects.NodeGroupCollection.get_by_cluster_id(
self.cluster['id']).count())
def test_assign_nodegroup_to_node_in_another_cluster(self):
self.env.create(
cluster_kwargs={
'api': True,
'net_provider': consts.CLUSTER_NET_PROVIDERS.neutron,
'net_segment_type': consts.NEUTRON_SEGMENT_TYPES.gre
},
nodes_kwargs=[{
'roles': [],
'pending_roles': ['controller'],
'pending_addition': True,
'api': True}]
)
empty_cluster = self.env.create_cluster(
net_provider=consts.CLUSTER_NET_PROVIDERS.neutron,
net_segment_type=consts.NEUTRON_SEGMENT_TYPES.gre
)
node = self.env.nodes[0]
resp = self.env.create_node_group(cluster_id=empty_cluster.get('id'))
ng_id = resp.json_body['id']
resp = self.app.put(
reverse('NodeHandler', kwargs={'obj_id': node['id']}),
json.dumps({'group_id': ng_id}),
headers=self.default_headers,
expect_errors=True
)
message = resp.json_body['message']
self.assertEquals(resp.status_code, 400)
self.assertRegexpMatches(message, 'Cannot assign node group')