Exemplo n.º 1
0
    def update_subnet(
        self,
        subnet_id,
        name=None,
        cidr=None,
        network_id=None,
        gateway=None,
        dns=None,
    ):
        if network_id:
            if not self.idl.ls_get(network_id).execute():
                raise SubnetConfigError(
                    'Unable to move subnet to network {network_id}. '
                    'The network does not exit.'.format(network_id=network_id))
            subnet_by_network = self._get_dhcp_by_network_id(network_id)
            if subnet_by_network and str(subnet_by_network.uuid) != subnet_id:
                raise SubnetConfigError(
                    'Unable to move subnet to network {network_id}. The'
                    ' network already has a subnet assigned'.format(
                        network_id=network_id))

        db_set_command = DbSetCommand(self.idl, ovnconst.TABLE_DHCP_Options,
                                      subnet_id)

        if cidr:
            dhcp_server_ip = cidr.split('/', 1)[0]
            db_set_command.add(
                ovnconst.ROW_DHCP_OPTIONS,
                {SubnetMapper.OVN_DHCP_SERVER_ID: dhcp_server_ip})
            db_set_command.add(ovnconst.ROW_DHCP_CIDR, cidr)
            self.idl.db_set(
                ovnconst.TABLE_LS,
                network_id,
                (ovnconst.ROW_LS_OTHER_CONFIG, {
                    NetworkMapper.OVN_SUBNET: cidr
                }),
            ).execute()

        db_set_command.add(ovnconst.ROW_DHCP_EXTERNAL_IDS,
                           {SubnetMapper.OVN_NAME: name}, name)
        db_set_command.add(ovnconst.ROW_DHCP_EXTERNAL_IDS,
                           {SubnetMapper.OVN_NETWORK_ID: network_id},
                           network_id)
        db_set_command.add(ovnconst.ROW_DHCP_OPTIONS,
                           {SubnetMapper.OVN_GATEWAY: gateway}, gateway)
        db_set_command.add(ovnconst.ROW_DHCP_OPTIONS,
                           {SubnetMapper.OVN_DNS_SERVER: dns}, dns)
        db_set_command.add(
            ovnconst.ROW_DHCP_OPTIONS,
            {SubnetMapper.OVN_DHCP_LEASE_TIME: dhcp_lease_time()})
        db_set_command.add(
            ovnconst.ROW_DHCP_OPTIONS,
            {SubnetMapper.OVN_DHCP_SERVER_MAC: dhcp_server_mac()})
        db_set_command.execute()

        return self.get_subnet(subnet_id)
Exemplo n.º 2
0
    def add_subnet(
        self,
        name,
        cidr,
        network_id,
        gateway,
        dns=None,
    ):
        try:
            network = self._get_network(network_id)
        except ElementNotFoundError:
            raise SubnetConfigError('Subnet can not be created, network {}'
                                    ' does not exist'.format(network_id))

        if self._get_dhcp_by_network_id(network_id):
            raise SubnetConfigError('Unable to create more than one subnet'
                                    ' for network {}'.format(network_id))

        external_ids = {
            SubnetMapper.OVN_NAME: name,
            SubnetMapper.OVN_NETWORK_ID: network_id
        }
        dhcp_server_ip = cidr.split('/', 1)[0]

        options = {
            SubnetMapper.OVN_DHCP_SERVER_ID: dhcp_server_ip,
            SubnetMapper.OVN_DHCP_SERVER_MAC: dhcp_server_mac(),
            SubnetMapper.OVN_DHCP_LEASE_TIME: dhcp_lease_time(),
        }
        if gateway:
            options[SubnetMapper.OVN_GATEWAY] = gateway
        if dhcp_enable_mtu():
            options[SubnetMapper.OVN_DHCP_MTU] = dhcp_mtu()

        if dns:
            options[SubnetMapper.OVN_DNS_SERVER] = dns

        self.idl.db_set(
            ovnconst.TABLE_LS,
            network_id,
            (ovnconst.ROW_LS_OTHER_CONFIG, {
                NetworkMapper.OVN_SUBNET: cidr
            }),
        ).execute()

        subnet = self.idl.dhcp_options_add(cidr, **external_ids).execute()
        self.idl.dhcp_options_set_options(subnet.uuid, **options).execute()

        for port in network.ports:
            if port.type == ovnconst.LSP_TYPE_ROUTER:
                continue
            self._update_port_address(port, network_id=network_id)

        return self.get_subnet(subnet.uuid)
Exemplo n.º 3
0
    def test_add_subnet(self, mock_setoptions_command, mock_add_command,
                        mock_dbset_command, mock_connection):
        add_execute = mock_add_command.return_value.execute
        add_execute.return_value = TestOvnNorth.SUBNET_102
        subnet_cidr = '1.1.1.0/24'
        ovn_north = OvnNorth()
        rest_data = {
            SubnetMapper.REST_SUBNET_NAME: 'subnet_name',
            SubnetMapper.REST_SUBNET_CIDR: subnet_cidr,
            SubnetMapper.REST_SUBNET_NETWORK_ID:
            str(TestOvnNorth.NETWORK_ID10),
            SubnetMapper.REST_SUBNET_DNS_NAMESERVERS: ['1.1.1.1'],
            SubnetMapper.REST_SUBNET_GATEWAY_IP: '1.1.1.0',
        }
        result = ovn_north.add_subnet(rest_data)
        assert result['id'] == str(TestOvnNorth.SUBNET_ID102)
        assert mock_dbset_command.call_count == 1
        assert mock_add_command.call_count == 1
        assert mock_setoptions_command.call_count == 1

        expected_dbset_call = mock.call(
            ovn_north.idl,
            ovnconst.TABLE_LS,
            str(TestOvnNorth.NETWORK_ID10),
            (ovnconst.ROW_LS_OTHER_CONFIG, {
                NetworkMapper.OVN_SUBNET: subnet_cidr
            }),
        )
        assert mock_dbset_command.mock_calls[0] == expected_dbset_call

        expected_add_call = mock.call(ovn_north.idl,
                                      subnet_cidr,
                                      ovirt_name='subnet_name',
                                      ovirt_network_id=str(
                                          TestOvnNorth.NETWORK_ID10))
        assert mock_add_command.mock_calls[0] == expected_add_call

        expected_options_call = mock.call(ovn_north.idl,
                                          TestOvnNorth.SUBNET_ID102,
                                          dns_server='1.1.1.1',
                                          lease_time=dhcp_lease_time(),
                                          router='1.1.1.0',
                                          server_id='1.1.1.0',
                                          server_mac=dhcp_server_mac(),
                                          mtu=dhcp_mtu())
        assert mock_setoptions_command.mock_calls[0] == expected_options_call