From 589f587b9139b84437d1b7e8cb017018fc9a4e06 Mon Sep 17 00:00:00 2001 From: spisarski Date: Tue, 11 Jul 2017 12:08:53 -0600 Subject: Created domain classes for security groups. Created SecurityGroup and SecurityGroupRule classes so the neutron_utils for security groups will be returning objects of these types instead of the OpenStack objects returned by the API calls. JIRA: SNAPS-116 Change-Id: I76ed1f85f7d54b984fc6f6ac28cee7680a1109e5 Signed-off-by: spisarski --- snaps/domain/network.py | 77 +++++++++++++++++++++++++++++++++ snaps/domain/test/network_tests.py | 87 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 snaps/domain/network.py create mode 100644 snaps/domain/test/network_tests.py (limited to 'snaps/domain') diff --git a/snaps/domain/network.py b/snaps/domain/network.py new file mode 100644 index 0000000..9e02ba6 --- /dev/null +++ b/snaps/domain/network.py @@ -0,0 +1,77 @@ +# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs") +# and others. 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. + + +class SecurityGroup: + """ + SNAPS domain object for SecurityGroups. Should contain attributes that + are shared amongst cloud providers + """ + def __init__(self, **kwargs): + """ + Constructor + :param name: the security group's name + :param id: the security group's id + """ + self.name = kwargs.get('name') + self.id = kwargs.get('id') + self.project_id = kwargs.get('project_id', kwargs.get('tenant_id')) + + def __eq__(self, other): + return (self.name == other.name and self.id == other.id and + self.project_id == other.project_id) + + +class SecurityGroupRule: + """ + SNAPS domain object for Security Group Rules. Should contain attributes + that are shared amongst cloud providers + """ + def __init__(self, **kwargs): + """ + Constructor + :param id: the security group rule's id + :param sec_grp_id: the ID of the associated security group + :param description: the security group rule's description + :param direction: the security group rule's direction + :param ethertype: the security group rule's ethertype + :param port_range_min: the security group rule's port_range_min + :param port_range_max: the security group rule's port_range_max + :param protocol: the security group rule's protocol + :param remote_group_id: the security group rule's remote_group_id + :param remote_ip_prefix: the security group rule's remote_ip_prefix + """ + self.id = kwargs.get('id') + self.security_group_id = kwargs.get('security_group_id') + self.description = kwargs.get('description') + self.direction = kwargs.get('direction') + self.ethertype = kwargs.get('ethertype') + self.port_range_min = kwargs.get('port_range_min') + self.port_range_max = kwargs.get('port_range_max') + self.protocol = kwargs.get('protocol') + self.remote_group_id = kwargs.get('remote_group_id') + self.remote_ip_prefix = kwargs.get('remote_ip_prefix') + + def __eq__(self, other): + return (self.id == other.id and + self.security_group_id == other.security_group_id and + self.description == other.description and + self.direction == other.direction and + self.ethertype == other.ethertype and + self.port_range_min == other.port_range_min and + self.port_range_max == other.port_range_max and + self.protocol == other.protocol and + self.remote_group_id == other.remote_group_id and + self.remote_ip_prefix == other.remote_ip_prefix) diff --git a/snaps/domain/test/network_tests.py b/snaps/domain/test/network_tests.py new file mode 100644 index 0000000..a2f1374 --- /dev/null +++ b/snaps/domain/test/network_tests.py @@ -0,0 +1,87 @@ +# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs") +# and others. 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. + +import unittest +from snaps.domain.network import SecurityGroup, SecurityGroupRule + + +class SecurityGroupDomainObjectTests(unittest.TestCase): + """ + Tests the construction of the snaps.domain.test.SecurityGroup class + """ + + def test_construction_proj_id_kwargs(self): + sec_grp = SecurityGroup( + **{'name': 'name', 'id': 'id', + 'project_id': 'foo'}) + self.assertEqual('name', sec_grp.name) + self.assertEqual('id', sec_grp.id) + self.assertEqual('foo', sec_grp.project_id) + + def test_construction_tenant_id_kwargs(self): + sec_grp = SecurityGroup( + **{'name': 'name', 'id': 'id', + 'tenant_id': 'foo'}) + self.assertEqual('name', sec_grp.name) + self.assertEqual('id', sec_grp.id) + self.assertEqual('foo', sec_grp.project_id) + + def test_construction_named(self): + sec_grp = SecurityGroup(tenant_id='foo', id='id', name='name') + self.assertEqual('name', sec_grp.name) + self.assertEqual('id', sec_grp.id) + self.assertEqual('foo', sec_grp.project_id) + + +class SecurityGroupRuleDomainObjectTests(unittest.TestCase): + """ + Tests the construction of the snaps.domain.test.SecurityGroupRule class + """ + + def test_construction_kwargs(self): + sec_grp_rule = SecurityGroupRule( + **{'id': 'id', 'security_group_id': 'grp_id', 'description': 'desc', + 'direction': 'dir', 'ethertype': 'eType', + 'port_range_min': '10.0.0.100', 'port_range_max': '10.0.0.200', + 'protocol': 'proto', 'remote_group_id': 'group_id', + 'remote_ip_prefix': 'ip_prefix'}) + self.assertEqual('id', sec_grp_rule.id) + self.assertEqual('grp_id', sec_grp_rule.security_group_id) + self.assertEqual('desc', sec_grp_rule.description) + self.assertEqual('dir', sec_grp_rule.direction) + self.assertEqual('eType', sec_grp_rule.ethertype) + self.assertEqual('10.0.0.100', sec_grp_rule.port_range_min) + self.assertEqual('10.0.0.200', sec_grp_rule.port_range_max) + self.assertEqual('proto', sec_grp_rule.protocol) + self.assertEqual('group_id', sec_grp_rule.remote_group_id) + self.assertEqual('ip_prefix', sec_grp_rule.remote_ip_prefix) + + def test_construction_named(self): + sec_grp_rule = SecurityGroupRule( + remote_ip_prefix='ip_prefix', remote_group_id='group_id', + protocol='proto', port_range_min='10.0.0.100', + port_range_max='10.0.0.200', ethertype='eType', + direction='dir', description='desc', security_group_id='grp_id', + id='id') + self.assertEqual('id', sec_grp_rule.id) + self.assertEqual('grp_id', sec_grp_rule.security_group_id) + self.assertEqual('desc', sec_grp_rule.description) + self.assertEqual('dir', sec_grp_rule.direction) + self.assertEqual('eType', sec_grp_rule.ethertype) + self.assertEqual('10.0.0.100', sec_grp_rule.port_range_min) + self.assertEqual('10.0.0.200', sec_grp_rule.port_range_max) + self.assertEqual('proto', sec_grp_rule.protocol) + self.assertEqual('group_id', sec_grp_rule.remote_group_id) + self.assertEqual('ip_prefix', sec_grp_rule.remote_ip_prefix) -- cgit 1.2.3-korg