def __init__(self, request, *args, **kwargs):
        super(CreateEnvironmentForm, self).__init__(request, *args, **kwargs)
        env_fixed_network = getattr(settings, 'USE_FIXED_NETWORK', False)
        if env_fixed_network:
            net_choices = net.get_project_assigned_network(request)
            help_text = None
            if not net_choices:
                msg = _("Default network is either not specified for "
                        "this project, or specified incorrectly, "
                        "please contact administrator.")
                messages.error(request, msg)
                raise exceptions.ConfigurationError(msg)
            else:
                self.fields['net_config'].required = False
                self.fields['net_config'].widget.attrs['readonly'] = True
        else:
            net_choices = net.get_available_networks(
                request, murano_networks='translate')

            if net_choices is None:  # NovaNetwork case
                net_choices = [((None, None), _('Unavailable'))]
                help_text = net.NN_HELP
            else:
                net_choices.insert(0, ((None, None), _('Create New')))
                help_text = net.NEUTRON_NET_HELP
        self.fields['net_config'].choices = net_choices
        self.fields['net_config'].help_text = help_text
Example #2
0
    def test_get_available_networks_with_filter_none(self, mock_neutron):
        foo_mock_network = mock.Mock(router__external=False,
                                     id='foo-network-id',
                                     subnets=[mock.Mock(id='foo-subnet-id')])
        foo_mock_network.configure_mock(name='foo-network')
        bar_mock_subnet = mock.Mock(id='bar-subnet-id',
                                    name_or_id='bar-subnet',
                                    cidr='255.0.0.0')
        bar_mock_network = mock.Mock(router__external=False,
                                     id='bar-network-id',
                                     name_or_id='bar-network',
                                     subnets=[bar_mock_subnet])
        bar_mock_network.configure_mock(name='bar-network')
        mock_neutron.network_list_for_tenant.return_value = [
            foo_mock_network, bar_mock_network
        ]

        test_filter = '^[\w]+\-[\w]+'
        result = net.get_available_networks(self.mock_request,
                                            filter=test_filter,
                                            murano_networks='include')

        expected_result = [
            (('foo-network-id', 'foo-subnet-id'), "Network of 'foo'"),
            (('foo-network-id', None), "Network of 'foo': random subnet"),
            (('bar-network-id', 'bar-subnet-id'),
             'bar-network: 255.0.0.0 bar-subnet'),
            (('bar-network-id', None), "bar-network: random subnet"),
        ]

        self.assertEqual(expected_result, result)
        mock_neutron.network_list_for_tenant.assert_called_once_with(
            self.mock_request, tenant_id='foo_tenant_id')
        self.mock_env_api.environments_list.assert_called_once_with(
            self.mock_request)
Example #3
0
    def __init__(self, request, *args, **kwargs):
        super(CreateEnvironmentForm, self).__init__(request, *args, **kwargs)
        env_fixed_network = getattr(settings, 'USE_FIXED_NETWORK', False)
        if env_fixed_network:
            net_choices = net.get_project_assigned_network(request)
            help_text = None
            if not net_choices:
                msg = _("Default network is either not specified for "
                        "this project, or specified incorrectly, "
                        "please contact administrator.")
                messages.error(request, msg)
                raise exceptions.ConfigurationError(msg)
            else:
                self.fields['net_config'].required = False
                self.fields['net_config'].widget.attrs['readonly'] = True
        else:
            net_choices = net.get_available_networks(
                request,
                murano_networks='translate')

            if net_choices is None:  # NovaNetwork case
                net_choices = [((None, None), _('Unavailable'))]
                help_text = net.NN_HELP
            else:
                net_choices.insert(0, ((None, None), _('Create New')))
                help_text = net.NEUTRON_NET_HELP
        self.fields['net_config'].choices = net_choices
        self.fields['net_config'].help_text = help_text
Example #4
0
    def test_get_available_networks_with_filter_one(self, mock_neutron):
        foo_mock_network = mock.Mock(router__external=False,
                                     id='foo-network-id',
                                     subnets=[mock.Mock(id='foo-subnet-id')])
        foo_mock_network.configure_mock(name='foo-network')
        bar_mock_network = mock.Mock()  # Will be excluded by test_filter.
        bar_mock_network.configure_mock(name='bar-network')
        mock_neutron.network_list_for_tenant.return_value = [
            foo_mock_network, bar_mock_network
        ]

        test_filter = '^foo\-[\w]+'
        result = net.get_available_networks(self.mock_request,
                                            include_subnets=True,
                                            filter=test_filter,
                                            murano_networks='include')

        expected_result = [(('foo-network-id', 'foo-subnet-id'),
                            "Network of 'foo'")]

        self.assertEqual(expected_result, result)
        mock_neutron.network_list_for_tenant.assert_called_once_with(
            self.mock_request, tenant_id='foo_tenant_id')
        self.mock_env_api.environments_list.assert_called_once_with(
            self.mock_request)
Example #5
0
    def update(self, request, **kwargs):
        """Populates available networks in the control

        This method is called automatically when the form which contains it is
        rendered
        """
        network_choices = net.get_available_networks(request, self.include_subnets, self.filter, self.murano_networks)
        if self.allow_auto:
            network_choices.insert(0, ((None, None), _("Auto")))
        self.choices = network_choices or []
    def update(self, request, **kwargs):
        """Populates available networks in the control

        This method is called automatically when the form which contains it is
        rendered
        """
        network_choices = net.get_available_networks(request, self.filter,
                                                     self.murano_networks)
        if self.allow_auto:
            network_choices.insert(0, ((None, None), _('Auto')))
        self.choices = network_choices or []
Example #7
0
    def test_get_available_networks_except_service_catalog_exception(
            self, mock_neutron, mock_log):
        mock_neutron.network_list_for_tenant.side_effect = \
            exceptions.ServiceCatalogException('test_exception')
        result = net.get_available_networks(self.mock_request)

        self.assertEqual([], result)
        mock_log.warning.assert_called_once_with(
            'Neutron not found. Assuming Nova Network usage')
        mock_neutron.network_list_for_tenant.assert_called_once_with(
            self.mock_request, tenant_id='foo_tenant_id')
Example #8
0
 def __init__(self, request, *args, **kwargs):
     super(CreateEnvironmentForm, self).__init__(request, *args, **kwargs)
     net_choices = net.get_available_networks(request,
                                              murano_networks='translate')
     if net_choices is None:  # NovaNetwork case
         net_choices = [((None, None), _('Unavailable'))]
         help_text = net.NN_HELP
     else:
         net_choices.insert(0, ((None, None), _('Create New')))
         help_text = net.NEUTRON_NET_HELP
     self.fields['net_config'].choices = net_choices
     self.fields['net_config'].help_text = help_text
Example #9
0
    def test_get_available_networks_without_include_subnet(self, mock_neutron):
        mock_netwok = mock.Mock(router__external=False,
                                id='foo-network-id',
                                subnets=[mock.Mock(id='foo-subnet-id')])
        mock_netwok.configure_mock(name='foo-network')
        mock_neutron.network_list_for_tenant.return_value = [mock_netwok]

        result = net.get_available_networks(self.mock_request,
                                            include_subnets=False,
                                            filter=None,
                                            murano_networks='include')

        # Subnet specified in mock_network should be None with include_subnets
        # set to False.
        expected_result = [(('foo-network-id', None), "Network of 'foo'")]

        self.assertEqual(expected_result, result)
        mock_neutron.network_list_for_tenant.assert_called_once_with(
            self.mock_request, tenant_id='foo_tenant_id')
        self.mock_env_api.environments_list.assert_called_once_with(
            self.mock_request)
Example #10
0
    def test_get_available_networks(self, mock_neutron):
        foo_subnets = [
            type(
                '%s-subnet' % k, (object, ), {
                    'id': '%s-subnet-id' % k,
                    'cidr': '255.0.0.0',
                    'name_or_id': '%s-name-or-id' % k
                }) for k in ('fake1', 'fake2')
        ]
        bar_subnets = [
            type(
                'fake3-subnet', (object, ), {
                    'id': 'fake3-subnet-id',
                    'cidr': '255.255.0.0',
                    'name_or_id': 'fake3-name-or-id'
                })
        ]
        foo_network = type(
            'FooNetwork', (object, ), {
                'router__external': False,
                'id': 'foo-network-id',
                'subnets': foo_subnets,
                'name': 'foo-network-name',
                'name_or_id': 'foo-network-name-or-id',
            })
        bar_network = type(
            'BarNetwork', (object, ), {
                'router__external': False,
                'id': 'bar-network-id',
                'subnets': bar_subnets,
                'name': 'bar-network-name',
                'name_or_id': 'bar-network-name-or-id',
            })
        mock_neutron.network_list_for_tenant.return_value = [
            foo_network,
            bar_network,
        ]

        result = net.get_available_networks(self.mock_request,
                                            filter=None,
                                            murano_networks='exclude')

        expected_result = [
            ((foo_network.id, foo_subnets[0].id),
             '%s: %s %s' % (foo_network.name_or_id, foo_subnets[0].cidr,
                            foo_subnets[0].name_or_id)),
            ((foo_network.id, foo_subnets[1].id),
             '%s: %s %s' % (foo_network.name_or_id, foo_subnets[1].cidr,
                            foo_subnets[1].name_or_id)),
            ((foo_network.id, None),
             '%s: random subnet' % foo_network.name_or_id),
            ((bar_network.id, bar_subnets[0].id),
             '%s: %s %s' % (bar_network.name_or_id, bar_subnets[0].cidr,
                            bar_subnets[0].name_or_id)),
            ((bar_network.id, None),
             '%s: random subnet' % bar_network.name_or_id),
        ]

        self.assertIsInstance(result, list)
        self.assertEqual(len(expected_result), len(result))
        for choice in expected_result:
            self.assertIn(choice, result)
        mock_neutron.network_list_for_tenant.assert_called_once_with(
            self.mock_request, tenant_id='foo_tenant_id')
        self.mock_env_api.environments_list.assert_called_once_with(
            self.mock_request)