diff options
author | spisarski <s.pisarski@cablelabs.com> | 2017-08-04 14:57:08 -0600 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2017-08-07 09:52:43 -0600 |
commit | 27b8b1c246d63c14f112a99362873fe887c13c10 (patch) | |
tree | 7c9106818b2382be12e3095c2dbc387733e20530 /snaps/openstack/utils/neutron_utils.py | |
parent | 463dc2cd59bb26150f15e57a0e94fafbbf8634d4 (diff) |
Refactored neutron_utils#get_security_group()
Added a sec_grp_settings (SecurityGroupSettings) object parameter
in addition to sec_grp_name for more robust query options.
JIRA: SNAPS-164
Change-Id: I8bb21ff9e5e6b1f532773ca01d37964f8b47b530
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack/utils/neutron_utils.py')
-rw-r--r-- | snaps/openstack/utils/neutron_utils.py | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py index 01408fe..4bc1be2 100644 --- a/snaps/openstack/utils/neutron_utils.py +++ b/snaps/openstack/utils/neutron_utils.py @@ -386,23 +386,38 @@ def delete_security_group(neutron, sec_grp): neutron.delete_security_group(sec_grp.id) -def get_security_group(neutron, name, tenant_id=None): +def get_security_group(neutron, sec_grp_settings=None, sec_grp_name=None, + project_id=None): """ - Returns the first security group object of the given name else None + Returns the first security group for a given query. The query gets built + from the sec_grp_settings parameter if not None, else only the name of + the security group will be used, else if the query parameters are None then + None will be returned :param neutron: the client - :param name: the name of security group object to retrieve + :param sec_grp_settings: an instance of SecurityGroupSettings config object + :param sec_grp_name: the name of security group object to retrieve + :param project_id: the ID of the project/tentant object that owns the + secuity group to retrieve :return: a SNAPS-OO SecurityGroup domain object or None if not found """ - logger.info('Retrieving security group with name - ' + name) - filter = {'name': name} - if tenant_id: - filter['tenant_id'] = tenant_id - groups = neutron.list_security_groups(**filter) + sec_grp_filter = dict() + if project_id: + sec_grp_filter['tenant_id'] = project_id + + if sec_grp_settings: + sec_grp_filter['name'] = sec_grp_settings.name + + if sec_grp_settings.description: + sec_grp_filter['description'] = sec_grp_settings.description + elif sec_grp_name: + sec_grp_filter['name'] = sec_grp_name + else: + return None + + groups = neutron.list_security_groups(**sec_grp_filter) for group in groups['security_groups']: - if group['name'] == name: - return SecurityGroup(**group) - return None + return SecurityGroup(**group) def get_security_group_by_id(neutron, sec_grp_id): |