def handle(self, request, data): redirect = reverse("horizon:project:security_groups:detail", args=[data['id']]) params = {} if 'description' in data: params['description'] = data['description'] try: rule = api.neutron.security_group_rule_create( request, filters.get_int_or_uuid(data['id']), data['direction'], data['ethertype'], data['ip_protocol'], data['from_port'], data['to_port'], data['cidr'], data['security_group'], **params) messages.success(request, _('Successfully added rule: %s') % six.text_type(rule)) return rule except exceptions.Conflict as error: exceptions.handle(request, error, redirect=redirect) except Exception: exceptions.handle(request, _('Unable to add rule to security group.'), redirect=redirect)
def _get_data(self): sg_id = filters.get_int_or_uuid(self.kwargs["security_group_id"]) try: return api.network.security_group_get(self.request, sg_id) except Exception: redirect = reverse("horizon:project:access_and_security:index") exceptions.handle(self.request, _("Unable to retrieve security group."), redirect=redirect)
def single(self, table, request, obj_id): try: fip = table.get_object_by_id(filters.get_int_or_uuid(obj_id)) api.network.floating_ip_disassociate(request, fip.id) LOG.info('Disassociating Floating IP "%s".' % obj_id) messages.success( request, _('Successfully disassociated Floating IP: %s') % fip.ip) # operation log config = _('Floating IP ID: %s') % obj_id api.logger.Logger(request).create( resource_type='floatip', action_name='Disassociate Floating IP', resource_name='Floating IP', config=config, status='Success') except Exception: exceptions.handle(request, _('Unable to disassociate floating IP.')) # operation log api.logger.Logger(request).create( resource_type='floatip', action_name='Disassociate Floating IP', resource_name='Floating IP', config=_('Unable to disassociate floating IP.'), status='Error') return shortcuts.redirect('horizon:network:floating_ips:index')
def _construct_parameters(self, data): params = { 'name': data['name'], 'admin_state_up': data['admin_state'], } # If a field value is None, it means the field is not supported, # If so, we skip sending such field. if data['binding__vnic_type'] is not None: params['binding__vnic_type'] = data['binding__vnic_type'] if data['mac_state'] is not None: params['mac_learning_enabled'] = data['mac_state'] if data['port_security_enabled'] is not None: params['port_security_enabled'] = data['port_security_enabled'] # If port_security_enabled is set to False, security groups on the port # must be cleared. We will clear the current security groups # in this case. if ('port_security_enabled' in params and not params['port_security_enabled']): params['security_groups'] = [] # In case of UpdatePortSecurityGroup registered, 'wanted_groups' # exists in data. elif 'wanted_groups' in data: # If data has that key, we need to set its value # even if its value is empty to clear sec group setting. groups = [filters.get_int_or_uuid(sg) for sg in data['wanted_groups']] params['security_groups'] = groups return params
def _construct_parameters(self, data): params = { 'name': data['name'], 'admin_state_up': data['admin_state'], } # If a field value is None, it means the field is not supported, # If so, we skip sending such field. if data['binding__vnic_type'] is not None: params['binding__vnic_type'] = data['binding__vnic_type'] if data['mac_state'] is not None: params['mac_learning_enabled'] = data['mac_state'] if data['port_security_enabled'] is not None: params['port_security_enabled'] = data['port_security_enabled'] # If port_security_enabled is set to False, security groups on the port # must be cleared. We will clear the current security groups # in this case. if ('port_security_enabled' in params and not params['port_security_enabled']): params['security_groups'] = [] # In case of UpdatePortSecurityGroup registered, 'wanted_groups' # exists in data. elif 'wanted_groups' in data: # If data has that key, we need to set its value # even if its value is empty to clear sec group setting. groups = [ filters.get_int_or_uuid(sg) for sg in data['wanted_groups'] ] params['security_groups'] = groups return params
def get_object(self): sg_id = filters.get_int_or_uuid(self.kwargs['security_group_id']) try: return api.network.security_group_get(self.request, sg_id) except Exception: msg = _('Unable to retrieve security group.') url = reverse('horizon:project:access_and_security:index') exceptions.handle(self.request, msg, redirect=url)
def _get_data(self): sg_id = filters.get_int_or_uuid(self.kwargs['security_group_id']) try: return api.neutron.security_group_get(self.request, sg_id) except Exception: redirect = reverse('horizon:project:security_groups:index') exceptions.handle(self.request, _('Unable to retrieve security group.'), redirect=redirect)
def _get_data(self): qos_id = filters.get_int_or_uuid(self.kwargs['qos_id']) try: return api.neutron.policy_get(self.request, qos_id) except Exception: redirect = reverse('horizon:project:access_and_security:index') exceptions.handle(self.request, _('Unable to retrieve qos.'), redirect=redirect)
def _get_data(self): sg_id = filters.get_int_or_uuid(self.kwargs['security_group_id']) try: return api.network.security_group_get(self.request, sg_id) except Exception: redirect = reverse('horizon:virtual:access_and_security:index') exceptions.handle(self.request, _('Unable to retrieve security group.'), redirect=redirect)
def _get_data(self): sg_id = filters.get_int_or_uuid(self.kwargs['security_group_id']) try: return api.network.security_group_get(self.request, sg_id) except Exception: redirect = reverse('horizon:floatingtest:access_and_security:index') exceptions.handle(self.request, _('Unable to retrieve security group.'), redirect=redirect)
def single(self, table, request, obj_id): try: fip = table.get_object_by_id(filters.get_int_or_uuid(obj_id)) api.network.floating_ip_disassociate(request, fip.id) LOG.info('Disassociating Floating IP "%s".' % obj_id) messages.success(request, _("Successfully disassociated Floating IP: %s") % fip.ip) except Exception: exceptions.handle(request, _("Unable to disassociate floating IP.")) return shortcuts.redirect("horizon:project:access_and_security:index")
def get_object(self): if not hasattr(self, "_object"): pol_id = filters.get_int_or_uuid(self.kwargs['policy_id']) try: self._object = policy_show(self.request, pol_id) except Exception: msg = _('Unable to retrieve network policy.') url = reverse('horizon:project:networking:index') exceptions.handle(self.request, msg, redirect=url) return self._object
def handle(self, request, data): instance_id = data['instance_id'] wanted_groups = [filters.get_int_or_uuid(sg) for sg in data['wanted_groups']] try: api.neutron.server_update_security_groups(request, instance_id, wanted_groups) except Exception as e: exceptions.handle(request, str(e)) return False return True
def _get_data(self): if not hasattr(self, '_pol'): pol_id = filters.get_int_or_uuid(self.kwargs['policy_id']) try: self._pol = policy_show(self.request, pol_id) except Exception: redirect = reverse('horizon:project:networking:index') exceptions.handle(self.request, _('Unable to retrieve network policy.'), redirect=redirect) return self._pol
def _get_data(self): if not hasattr(self, '_sg'): sg_id = filters.get_int_or_uuid(self.kwargs['security_group_id']) try: self._sg = api.network.security_group_get(self.request, sg_id) except Exception: redirect = reverse('horizon:project:access_and_security:index') exceptions.handle(self.request, _('Unable to retrieve security group.'), redirect=redirect) return self._sg
def single(self, table, request, obj_id): try: fip = table.get_object_by_id(filters.get_int_or_uuid(obj_id)) api.neutron.floating_ip_disassociate(request, fip.id) LOG.info('Disassociating Floating IP "%s".', obj_id) messages.success(request, _('Successfully disassociated Floating IP: %s') % fip.ip) except Exception: exceptions.handle(request, _('Unable to disassociate floating IP.')) return shortcuts.redirect('horizon:admin:floating_ips:index')
def single(self, table, request, obj_id): try: fip = table.get_object_by_id(get_int_or_uuid(obj_id)) api.network.floating_ip_disassociate(request, fip.id, fip.port_id) LOG.info('Disassociating Floating IP "%s".' % obj_id) messages.success( request, _('Successfully disassociated Floating IP: %s') % fip.ip) except: exceptions.handle(request, _('Unable to disassociate floating IP.')) return shortcuts.redirect('horizon:project:access_and_security:index')
def single(self, table, request, obj_id): try: fip = table.get_object_by_id(filters.get_int_or_uuid(obj_id)) api.neutron.floating_ip_disassociate(request, fip.id) LOG.info('Disassociating Floating IP "%s".', obj_id) messages.success( request, _('Successfully disassociated Floating IP: %s') % fip.ip) except Exception: exceptions.handle(request, _('Unable to disassociate floating IP.')) return shortcuts.redirect('horizon:project:floating_ips:index')
def handle(self, request, data): try: rule = api.network.security_group_rule_create( request, get_int_or_uuid(data['id']), data['direction'], data['ethertype'], data['ip_protocol'], data['from_port'], data['to_port'], data['cidr'], data['security_group']) messages.success(request, _('Successfully added rule: %s') % unicode(rule)) return rule except: redirect = reverse( "horizon:project:access_and_security:" "security_groups:detail", args=[data['id']]) exceptions.handle(request, _('Unable to add rule to security group.'), redirect=redirect)
def get_form_kwargs(self): kwargs = super(AddRuleView, self).get_form_kwargs() try: groups = api.network.security_group_list(self.request) except Exception: groups = [] exceptions.handle(self.request, _("Unable to retrieve security groups.")) security_groups = [] for group in groups: if group.id == filters.get_int_or_uuid(self.kwargs["security_group_id"]): security_groups.append((group.id, _("%s (current)") % group.name)) else: security_groups.append((group.id, group.name)) kwargs["sg_list"] = security_groups return kwargs
def handle(self, request, data): try: rule = api.network.security_group_rule_create( request, filters.get_int_or_uuid(data["id"]), data["direction"], data["ethertype"], data["ip_protocol"], data["from_port"], data["to_port"], data["cidr"], data["security_group"], ) messages.success(request, _("Successfully added rule: %s") % unicode(rule)) return rule except Exception: redirect = reverse("horizon:project:access_and_security:" "security_groups:detail", args=[data["id"]]) exceptions.handle(request, _("Unable to add rule to security group."), redirect=redirect)
def _construct_parameters(self, context): params = { 'network_id': context['network_id'], 'admin_state_up': context['admin_state'], 'name': context['name'], 'device_id': context['device_id'], 'device_owner': context['device_owner'] } if context.get('specify_ip') == 'subnet_id': if context.get('subnet_id'): params['fixed_ips'] = [{"subnet_id": context['subnet_id']}] elif context.get('specify_ip') == 'fixed_ip': if context.get('fixed_ip'): params['fixed_ips'] = [{"ip_address": context['fixed_ip']}] if context.get('binding__vnic_type'): params['binding__vnic_type'] = context['binding__vnic_type'] if context.get('mac_state'): params['mac_learning_enabled'] = context['mac_state'] if context['port_security_enabled'] is not None: params['port_security_enabled'] = context['port_security_enabled'] # Send mac_address only when it is specified. if context['mac_address']: params['mac_address'] = context['mac_address'] # If port_security_enabled is set to False, security groups on the port # must be cleared. We will clear the current security groups # in this case. if ('port_security_enabled' in params and not params['port_security_enabled']): params['security_groups'] = [] # In case of CreatePortSecurityGroup registered, 'wanted_groups' # exists in context. elif 'wanted_groups' in context: # If context has that key, we need to set its value # even if its value is empty to clear sec group setting. groups = [ filters.get_int_or_uuid(sg) for sg in context['wanted_groups'] ] params['security_groups'] = groups return params
def get_form_kwargs(self): kwargs = super(AddRuleView, self).get_form_kwargs() try: groups = api.network.security_group_list(self.request) except: groups = [] exceptions.handle(self.request, _("Unable to retrieve security groups.")) security_groups = [] for group in groups: if group.id == get_int_or_uuid(self.kwargs['security_group_id']): security_groups.append((group.id, _("%s (current)") % group.name)) else: security_groups.append((group.id, group.name)) kwargs['sg_list'] = security_groups return kwargs
def _construct_parameters(self, context): params = { 'network_id': context['network_id'], 'admin_state_up': context['admin_state'], 'name': context['name'], 'device_id': context['device_id'], 'device_owner': context['device_owner'] } if context.get('specify_ip') == 'subnet_id': if context.get('subnet_id'): params['fixed_ips'] = [{"subnet_id": context['subnet_id']}] elif context.get('specify_ip') == 'fixed_ip': if context.get('fixed_ip'): params['fixed_ips'] = [{"ip_address": context['fixed_ip']}] if context.get('binding__vnic_type'): params['binding__vnic_type'] = context['binding__vnic_type'] if context.get('mac_state'): params['mac_learning_enabled'] = context['mac_state'] if context['port_security_enabled'] is not None: params['port_security_enabled'] = context['port_security_enabled'] # Send mac_address only when it is specified. if context['mac_address']: params['mac_address'] = context['mac_address'] # If port_security_enabled is set to False, security groups on the port # must be cleared. We will clear the current security groups # in this case. if ('port_security_enabled' in params and not params['port_security_enabled']): params['security_groups'] = [] # In case of CreatePortSecurityGroup registered, 'wanted_groups' # exists in context. elif 'wanted_groups' in context: # If context has that key, we need to set its value # even if its value is empty to clear sec group setting. groups = [filters.get_int_or_uuid(sg) for sg in context['wanted_groups']] params['security_groups'] = groups return params
def handle(self, request, data): redirect = reverse( "horizon:admin:access_and_security:" "security_groups:detail", args=[data['id']]) try: rule = api.network.security_group_rule_create( request, filters.get_int_or_uuid(data['id']), data['direction'], data['ethertype'], data['ip_protocol'], data['from_port'], data['to_port'], data['cidr'], data['security_group']) messages.success( request, _('Successfully added rule: %s') % six.text_type(rule)) return rule except exceptions.Conflict as error: exceptions.handle(request, error, redirect=redirect) except Exception: exceptions.handle(request, _('Unable to add rule to security group.'), redirect=redirect)
def handle(self, request, data): try: rule = api.network.security_group_rule_create( request, filters.get_int_or_uuid(data['id']), data['direction'], data['ethertype'], data['ip_protocol'], data['from_port'], data['to_port'], data['cidr'], data['security_group']) messages.success(request, _('Successfully added rule: %s') % unicode(rule)) return rule except Exception: redirect = reverse("horizon:project:access_and_security:" "security_groups:detail", args=[data['id']]) exceptions.handle(request, _('Unable to add rule to security group.'), redirect=redirect)
def single(self, table, request, obj_id): try: fip = table.get_object_by_id(filters.get_int_or_uuid(obj_id)) api.network.floating_ip_disassociate(request, fip.id) LOG.info('Disassociating Floating IP "%s".' % obj_id) messages.success(request, _('Successfully disassociated Floating IP: %s') % fip.ip) # operation log config = _('Floating IP ID: %s') %obj_id api.logger.Logger(request).create(resource_type='floatip', action_name='Disassociate Floating IP', resource_name='Floating IP', config=config, status='Success') except Exception: exceptions.handle(request, _('Unable to disassociate floating IP.')) # operation log api.logger.Logger(request).create(resource_type='floatip', action_name='Disassociate Floating IP', resource_name='Floating IP', config= _('Unable to disassociate floating IP.'), status='Error') return shortcuts.redirect('horizon:network:floating_ips:index')
def test_accept_valid_integer_string(self): val = '100' ret = filters.get_int_or_uuid(val) self.assertEqual(int(val), ret)
def sanitize_id(self, obj_id): return filters.get_int_or_uuid(obj_id)
def _set_default_rule(self, username, password): for region in REGIONS: if region[2] != '': try: endpoint_url = "" kc = get_keystone_client(auth_url=region[0], username=username, password=password, tenant_name=username, endpoint_type="publicURL") token = kc.auth_ref.get("token", {}).get("id", "") services = kc.auth_ref.get("serviceCatalog", []) for service in services: service_type = service.get("type", "") if service_type == "network": for endpoint in service.get("endpoints", []): if endpoint.get("region", "") == region[1]: endpoint_url = endpoint.get("publicURL", "") neutron = get_neutron_client(token=token, auth_url=region[0], endpoint_url=endpoint_url) except Exception as exc: self.response['message'] = 'Set security default rule error: %s' % exc return False else: secgroups = neutron.list_security_groups(tenant_id=self.tenant_id) groups = [SecurityGroup(g) for g in secgroups.get('security_groups')] for group in groups: if group.name == "default": body = { 'security_group_rule': { 'security_group_id': filters.get_int_or_uuid(group.id), 'direction': 'ingress', 'ethertype': 'IPv4', 'protocol': 'tcp', 'port_range_min': '4200', 'port_range_max': '4200', 'remote_ip_prefix': '0.0.0.0/0', 'remote_group_id': None } } neutron.create_security_group_rule(body) body = { 'security_group_rule': { 'security_group_id': filters.get_int_or_uuid(group.id), 'direction': 'ingress', 'ethertype': 'IPv4', 'protocol': 'tcp', 'port_range_min': '22', 'port_range_max': '22', 'remote_ip_prefix': '0.0.0.0/0', 'remote_group_id': None } } neutron.create_security_group_rule(body) body = { 'security_group_rule': { 'security_group_id': filters.get_int_or_uuid(group.id), 'direction': 'ingress', 'ethertype': 'IPv4', 'protocol': 'icmp', } } neutron.create_security_group_rule(body) else: headers = {"Content-Type": "application/json"} security_url = region[4] requests.post(security_url, headers=headers, data=json.dumps({"username": username, "password": password, "tenant_id": self.tenant_id})) return True
def test_accept_valid_integer_string(self): val = '100' ret = get_int_or_uuid(val) self.assertEqual(int(val), ret)
def sanitize_id(self, obj_id): return get_int_or_uuid(obj_id)
def test_accept_valid_uuid(self): val = uuidutils.generate_uuid() ret = filters.get_int_or_uuid(val) self.assertEqual(val, ret)
def _set_default_rule(self, username, password): for region in REGIONS: if region[2] != '': try: endpoint_url = "" kc = get_keystone_client(auth_url=region[0], username=username, password=password, tenant_name=username, endpoint_type="publicURL") token = kc.auth_ref.get("token", {}).get("id", "") services = kc.auth_ref.get("serviceCatalog", []) for service in services: service_type = service.get("type", "") if service_type == "network": for endpoint in service.get("endpoints", []): if endpoint.get("region", "") == region[1]: endpoint_url = endpoint.get( "publicURL", "") neutron = get_neutron_client(token=token, auth_url=region[0], endpoint_url=endpoint_url) except Exception as exc: self.response[ 'message'] = 'Set security default rule error: %s' % exc return False else: secgroups = neutron.list_security_groups( tenant_id=self.tenant_id) groups = [ SecurityGroup(g) for g in secgroups.get('security_groups') ] for group in groups: if group.name == "default": body = { 'security_group_rule': { 'security_group_id': filters.get_int_or_uuid(group.id), 'direction': 'ingress', 'ethertype': 'IPv4', 'protocol': 'tcp', 'port_range_min': '4200', 'port_range_max': '4200', 'remote_ip_prefix': '0.0.0.0/0', 'remote_group_id': None } } neutron.create_security_group_rule(body) body = { 'security_group_rule': { 'security_group_id': filters.get_int_or_uuid(group.id), 'direction': 'ingress', 'ethertype': 'IPv4', 'protocol': 'tcp', 'port_range_min': '22', 'port_range_max': '22', 'remote_ip_prefix': '0.0.0.0/0', 'remote_group_id': None } } neutron.create_security_group_rule(body) body = { 'security_group_rule': { 'security_group_id': filters.get_int_or_uuid(group.id), 'direction': 'ingress', 'ethertype': 'IPv4', 'protocol': 'icmp', } } neutron.create_security_group_rule(body) else: headers = {"Content-Type": "application/json"} security_url = region[4] requests.post(security_url, headers=headers, data=json.dumps({ "username": username, "password": password, "tenant_id": self.tenant_id })) return True
def test_accept_valid_integer(self): val = 100 ret = filters.get_int_or_uuid(val) self.assertEqual(val, ret)
def test_accept_valid_integer_string(self): val = "100" ret = filters.get_int_or_uuid(val) self.assertEqual(int(val), ret)
def test_accept_valid_uuid(self): val = str(uuid.uuid4()) ret = filters.get_int_or_uuid(val) self.assertEqual(val, ret)