From 6770597bc32590114cab9cf5e3d6f0c3e9f846f1 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Fri, 16 Sep 2016 06:44:01 -0500 Subject: [PATCH] Extended security groups API to include FAWS fields Added new fields: "external_service" and "external_service_id" Change-Id: I0ec8427893f7838847883761de9c2349fa4a00ba JIRA: NCP-2078 Closes-Bug: #1624354 --- quark/api/extensions/security_group_rules.py | 62 ++++++++++++++++++++ quark/api/extensions/security_groups.py | 62 ++++++++++++++++++++ quark/plugin.py | 4 +- quark/plugin_views.py | 8 ++- quark/tests/functional/test_faws.py | 58 ++++++++++++++++++ 5 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 quark/api/extensions/security_group_rules.py create mode 100644 quark/api/extensions/security_groups.py create mode 100644 quark/tests/functional/test_faws.py diff --git a/quark/api/extensions/security_group_rules.py b/quark/api/extensions/security_group_rules.py new file mode 100644 index 0000000..92b43b2 --- /dev/null +++ b/quark/api/extensions/security_group_rules.py @@ -0,0 +1,62 @@ +# Copyright (c) 2016 Rackspace Hosting Inc. +# 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. + +from neutron.api import extensions + +EXTENDED_ATTRIBUTES_2_0 = { + "security_group_rules": { + "external_service": {"allow_post": True, + "allow_put": True, + "default": None, + "is_visible": True}, + "external_service_id": {"allow_post": True, + "allow_put": True, + "default": None, + "is_visible": True} + } +} + + +class Security_group_rules(extensions.ExtensionDescriptor): + """Extends Security Group Rules for FAWS purposes.""" + + @classmethod + def get_name(cls): + return 'Quark security-group-rules extension' + + @classmethod + def get_alias(cls): + # NOTE(alexm): This string must be listed in + # supported_extension_aliases in quark/plugin.py + return 'faws-security-group-rule-ext' + + @classmethod + def get_description(cls): + return 'Quark Security Group Rules API Extension' + + @classmethod + def get_namespace(cls): + return ("http://docs.openstack.org/api/openstack-network/2.0/content/" + "SecurityGroupRules.html") + + @classmethod + def get_updated(cls): + return "2016-09-15T10:00:00-00:00" + + def get_extended_resources(self, version): + if version == "2.0": + return EXTENDED_ATTRIBUTES_2_0 + else: + return {} diff --git a/quark/api/extensions/security_groups.py b/quark/api/extensions/security_groups.py new file mode 100644 index 0000000..b2f01a3 --- /dev/null +++ b/quark/api/extensions/security_groups.py @@ -0,0 +1,62 @@ +# Copyright (c) 2016 Rackspace Hosting Inc. +# 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. + +from neutron.api import extensions + +EXTENDED_ATTRIBUTES_2_0 = { + "security_groups": { + "external_service": {"allow_post": True, + "allow_put": True, + "default": None, + "is_visible": True}, + "external_service_id": {"allow_post": True, + "allow_put": True, + "default": None, + "is_visible": True} + } +} + + +class Security_groups(extensions.ExtensionDescriptor): + """Extends Security Groups for FAWS purposes.""" + + @classmethod + def get_name(cls): + return 'Quark security-group extension' + + @classmethod + def get_alias(cls): + # NOTE(alexm): This string must be listed in + # supported_extension_aliases in quark/plugin.py + return 'faws-security-group-ext' + + @classmethod + def get_description(cls): + return 'Quark Security Groups API Extension' + + @classmethod + def get_namespace(cls): + return ("http://docs.openstack.org/api/openstack-network/2.0/content/" + "SecurityGroups.html") + + @classmethod + def get_updated(cls): + return "2016-09-15T10:00:00-00:00" + + def get_extended_resources(self, version): + if version == "2.0": + return EXTENDED_ATTRIBUTES_2_0 + else: + return {} diff --git a/quark/plugin.py b/quark/plugin.py index 824cfeb..c6eca41 100644 --- a/quark/plugin.py +++ b/quark/plugin.py @@ -133,7 +133,9 @@ class Plugin(neutron_plugin_base_v2.NeutronPluginBaseV2, "networks_quark", "router", "ip_availabilities", "ports_quark", "floatingip", "segment_allocation_ranges", - "scalingip", "jobs"] + "scalingip", "jobs", + "faws-security-group-ext", + "faws-security-group-rule-ext"] def __init__(self): LOG.info("Starting quark plugin") diff --git a/quark/plugin_views.py b/quark/plugin_views.py index 6e696a8..223a9aa 100644 --- a/quark/plugin_views.py +++ b/quark/plugin_views.py @@ -138,7 +138,9 @@ def _make_security_group_dict(security_group, fields=None): res = {"id": security_group.get("id"), "description": security_group.get("description"), "name": security_group.get("name"), - "tenant_id": security_group.get("tenant_id")} + "tenant_id": security_group.get("tenant_id"), + "external_service": security_group.get("external_service"), + "external_service_id": security_group.get("external_service_id")} res["security_group_rules"] = [ _make_security_group_rule_dict(r) for r in security_group["rules"]] return res @@ -159,7 +161,9 @@ def _make_security_group_rule_dict(security_rule, fields=None): "protocol": protocol, "remote_ip_prefix": security_rule.get("remote_ip_prefix"), "security_group_id": security_rule.get("group_id"), - "remote_group_id": security_rule.get("remote_group_id")} + "remote_group_id": security_rule.get("remote_group_id"), + "external_service": security_rule.get("external_service"), + "external_service_id": security_rule.get("external_service_id")} return res diff --git a/quark/tests/functional/test_faws.py b/quark/tests/functional/test_faws.py new file mode 100644 index 0000000..0b22189 --- /dev/null +++ b/quark/tests/functional/test_faws.py @@ -0,0 +1,58 @@ +# Copyright (c) 2016 Rackspace Hosting Inc. +# +# 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. + +from quark import plugin + +from quark.api.extensions import security_group_rules +from quark.api.extensions import security_groups + +from quark.tests.functional.base import BaseFunctionalTest + + +class QuarkFAWSTest(BaseFunctionalTest): + def setUp(self): + super(QuarkFAWSTest, self).setUp() + self.plugin = plugin.Plugin() + self.version = '2.0' + + +class QuarkPluginSecurityGroupsTest(QuarkFAWSTest): + + def test_supported_extension_aliases(self): + self.assertTrue('faws-security-group-ext' in + self.plugin.supported_extension_aliases) + self.assertTrue('faws-security-group-rule-ext' in + self.plugin.supported_extension_aliases) + + def test_security_group_extension(self): + self.assertTrue('faws-security-group-ext' == + security_groups.Security_groups.get_alias()) + sg = security_groups.Security_groups() + self.assertTrue('security_groups' in + sg.get_extended_resources(self.version)) + attrs = sg.get_extended_resources(self.version)['security_groups'] + self.assertTrue('external_service' in attrs) + self.assertTrue('external_service_id' in attrs) + + def test_security_group_rule_extension(self): + self.assertTrue('faws-security-group-rule-ext' == + security_group_rules.Security_group_rules.get_alias()) + sgr = security_group_rules.Security_group_rules() + self.assertTrue('security_group_rules' in + sgr.get_extended_resources(self.version)) + attrs = \ + sgr.get_extended_resources(self.version)['security_group_rules'] + self.assertTrue('external_service' in attrs) + self.assertTrue('external_service_id' in attrs)