Exemple #1
0
    def put(self, request, id):

        with self._handle_exception(request):
            nco = self._nco(request, id)
            method = request.data.get('method')
            ipaddr = gateway = dns_servers = search_domains = None
            if (method == 'manual'):
                ipaddr = request.data.get('ipaddr', None)
                gateway = request.data.get('gateway', None)
                dns_servers = request.data.get('dns_servers', None)
                search_domains = request.data.get('search_domains', None)

            if (nco.ctype == 'ethernet'):
                device = nco.networkdevice_set.first().name
                self._delete_connection(nco)
                network.new_ethernet_connection(nco.name, device, ipaddr,
                                                gateway, dns_servers, search_domains)
            elif (nco.ctype == 'team'):
                team_profile = nco.team_profile
                devices = []
                for child_nco in NetworkConnection.objects.filter(master=nco):
                    devices.append(child_nco.networkdevice_set.first().name)

                self._delete_connection(nco)
                network.new_team_connection(
                    nco.name, self.runners[team_profile], devices, ipaddr,
                    gateway, dns_servers, search_domains)

            return Response(NetworkConnectionSerializer(nco).data)
Exemple #2
0
    def post(self, request):
        with self._handle_exception(request):
            ipaddr = gateway = dns_servers = search_domains = None
            name = request.data.get('name')
            if (NetworkConnection.objects.filter(name=name).exists()):
                e_msg = ('Connection name(%s) is already in use. Choose a different name.' % name)
                handle_exception(Exception(e_msg), request)

            #auto of manual
            method = request.data.get('method')
            if (method not in self.config_methods):
                e_msg = ('Unsupported config method(%s). Supported ones include: %s' % (method, self.config_methods))
                handle_exception(Exception(e_msg), request)
            if (method == 'manual'):
                #ipaddr is of the format <IP>/<netmask>. eg: 192.168.1.2/24. If netmask is not given, it defaults to 32.
                ipaddr = request.data.get('ipaddr')
                gateway = request.data.get('gateway')
                dns_servers = request.data.get('dns_servers', None)
                search_domains = request.data.get('search_domains', None)

            #connection type can be one of ethernet, team or bond
            ctype = request.data.get('ctype')
            if (ctype not in self.ctypes):
                e_msg = ('Unsupported connection type(%s). Supported ones include: %s' % (ctype, self.ctypes))
                handle_exception(Exception(e_msg), request)
            devices = request.data.get('devices', None)
            if (ctype == 'team'):
                #gather required input for team
                team_profile = request.data.get('team_profile')
                if (team_profile not in self.team_profiles):
                    e_msg = ('Unsupported team profile(%s). Supported ones include: %s' % (team_profile, self.team_profiles))
                    handle_exception(Exception(e_msg), request)
                self._validate_devices(devices, request)
                network.new_team_connection(name, self.runners[team_profile],
                                            devices, ipaddr, gateway,
                                            dns_servers, search_domains)

            elif (ctype == 'ethernet'):
                device = request.data.get('device')
                self._validate_devices([device], request, size=1)
                network.new_ethernet_connection(name, device, ipaddr, gateway,
                                                dns_servers, search_domains)

            elif (ctype == 'bond'):
                bond_profile = request.data.get('bond_profile')
                if (bond_profile not in self.bond_profiles):
                    e_msg = ('Unsupported bond profile(%s). Supported ones include: %s' % (bond_profile, self.bond_profiles))
                    handle_exception(Exception(e_msg), request)
                self._validate_devices(devices, request)
                network.new_bond_connection(name, bond_profile, devices,
                                            ipaddr, gateway, dns_servers, search_domains)

            return Response()
Exemple #3
0
    def put(self, request, id):

        with self._handle_exception(request):
            nco = self._nco(request, id)
            method = request.data.get("method")
            mtu = DEFAULT_MTU
            try:
                e_msg = (
                    "The mtu must be an integer in {} - {} range.").format(
                        MIN_MTU, MAX_MTU)
                mtu = int(request.data.get("mtu", DEFAULT_MTU))
                if mtu < MIN_MTU or mtu > MAX_MTU:
                    handle_exception(Exception(e_msg), request)
            except ValueError:
                handle_exception(Exception(e_msg), request)
            ipaddr = gateway = dns_servers = search_domains = None
            if method == "manual":
                ipaddr = request.data.get("ipaddr", None)
                gateway = request.data.get("gateway", None)
                dns_servers = request.data.get("dns_servers", None)
                search_domains = request.data.get("search_domains", None)

            if nco.ctype == "ethernet":
                device = nco.networkdevice_set.first().name
                self._delete_connection(nco)
                network.new_ethernet_connection(nco.name, device, ipaddr,
                                                gateway, dns_servers,
                                                search_domains, mtu)
            elif nco.ctype == "team":
                team_profile = nco.team_profile
                devices = []
                for child_nco in NetworkConnection.objects.filter(master=nco):
                    devices.append(child_nco.networkdevice_set.first().name)

                self._delete_connection(nco)
                network.new_team_connection(
                    nco.name,
                    self.runners[team_profile],
                    devices,
                    ipaddr,
                    gateway,
                    dns_servers,
                    search_domains,
                    mtu,
                )

            return Response(NetworkConnectionSerializer(nco).data)
Exemple #4
0
    def put(self, request, id):

        with self._handle_exception(request):
            nco = self._nco(request, id)
            method = request.data.get('method')
            mtu = DEFAULT_MTU
            try:
                e_msg = ('The mtu must be an integer in {} - {} '
                         'range.').format(MIN_MTU, MAX_MTU)
                mtu = int(request.data.get('mtu', DEFAULT_MTU))
                if mtu < MIN_MTU or mtu > MAX_MTU:
                    handle_exception(Exception(e_msg), request)
            except ValueError:
                handle_exception(Exception(e_msg), request)
            ipaddr = gateway = dns_servers = search_domains = None
            if (method == 'manual'):
                ipaddr = request.data.get('ipaddr', None)
                gateway = request.data.get('gateway', None)
                dns_servers = request.data.get('dns_servers', None)
                search_domains = request.data.get('search_domains', None)

            if (nco.ctype == 'ethernet'):
                device = nco.networkdevice_set.first().name
                self._delete_connection(nco)
                network.new_ethernet_connection(nco.name, device, ipaddr,
                                                gateway, dns_servers,
                                                search_domains, mtu)
            elif (nco.ctype == 'team'):
                team_profile = nco.team_profile
                devices = []
                for child_nco in NetworkConnection.objects.filter(master=nco):
                    devices.append(child_nco.networkdevice_set.first().name)

                self._delete_connection(nco)
                network.new_team_connection(
                    nco.name, self.runners[team_profile], devices, ipaddr,
                    gateway, dns_servers, search_domains, mtu)

            return Response(NetworkConnectionSerializer(nco).data)
Exemple #5
0
    def put(self, request, id):
        with self._handle_exception(request):
            nco = self._nco(request, id)
            method = request.data.get("method")
            mtu = DEFAULT_MTU
            try:
                e_msg = ("The mtu must be an integer in {} - {} range.").format(
                    MIN_MTU, MAX_MTU
                )
                mtu = int(request.data.get("mtu", DEFAULT_MTU))
                if mtu < MIN_MTU or mtu > MAX_MTU:
                    handle_exception(Exception(e_msg), request)
            except ValueError:
                handle_exception(Exception(e_msg), request)
            ipaddr = (
                gateway
            ) = (
                dns_servers
            ) = (
                search_domains
            ) = (
                aux_address
            ) = (
                dgateway
            ) = host_binding = icc = internal = ip_masquerade = ip_range = subnet = None
            if method == "manual":
                ipaddr = request.data.get("ipaddr", None)
                gateway = request.data.get("gateway", None)
                dns_servers = request.data.get("dns_servers", None)
                search_domains = request.data.get("search_domains", None)
                aux_address = request.data.get("aux_address", None)
                dgateway = request.data.get("dgateway", None)
                host_binding = request.data.get("host_binding", None)
                icc = request.data.get("icc")
                internal = request.data.get("internal")
                ip_masquerade = request.data.get("ip_masquerade")
                ip_range = request.data.get("ip_range", None)
                mtu = request.data.get("mtu", 1500)
                subnet = request.data.get("subnet", None)

            ctype = request.data.get("ctype")
            if nco.ctype == "ethernet":
                device = nco.networkdevice_set.first().name
                self._delete_connection(nco)
                sysnet.new_ethernet_connection(
                    nco.name, device, ipaddr, gateway, dns_servers, search_domains, mtu
                )
            elif nco.ctype == "team":
                team_profile = nco.team_profile
                devices = []
                for child_nco in NetworkConnection.objects.filter(master=nco):
                    devices.append(child_nco.networkdevice_set.first().name)

                self._delete_connection(nco)
                sysnet.new_team_connection(
                    nco.name,
                    self.runners[team_profile],
                    devices,
                    ipaddr,
                    gateway,
                    dns_servers,
                    search_domains,
                    mtu,
                )
            elif ctype == "docker":
                docker_name = request.data.get("docker_name")
                dname = request.data.get("dname")
                # Get list of connected containers to re-connect them later
                clist = probe_containers(network=docker_name, all=True)[:-1]
                logger.debug("clist is {}".format(clist))
                if len(clist) > 0:
                    for c in clist:
                        dnet_disconnect(c, docker_name)
                # Remove docker network
                dnet_remove(network=docker_name)
                # Create the Docker network with new settings
                try:
                    dnet_create(
                        dname,
                        aux_address,
                        dgateway,
                        host_binding,
                        icc,
                        internal,
                        ip_masquerade,
                        ip_range,
                        mtu,
                        subnet,
                    )
                    # Disconnect and reconnect all containers (if any)
                    if len(clist) > 0:
                        for c in clist:
                            dnet_connect(c, dname, all=True)
                except Exception as e:
                    logger.debug(
                        "An error occurred while creating the docker network: {}".format(
                            e
                        )
                    )
                    # The creation of the new network has failed, so re-create the old one
                    dconf = BridgeConnection.objects.filter(
                        docker_name=docker_name
                    ).values()[0]
                    aux_address = dconf["aux_address"]
                    dgateway = dconf["dgateway"]
                    host_binding = dconf["host_binding"]
                    icc = dconf["icc"]
                    internal = dconf["internal"]
                    ip_masquerade = dconf["ip_masquerade"]
                    ip_range = dconf["ip_range"]
                    subnet = dconf["subnet"]
                    dnet_create(
                        docker_name,
                        aux_address,
                        dgateway,
                        host_binding,
                        icc,
                        internal,
                        ip_masquerade,
                        ip_range,
                        mtu,
                        subnet,
                    )
                    if len(clist) > 0:
                        for c in clist:
                            dnet_connect(c, docker_name, all=True)
                    raise e
            return Response(NetworkConnectionSerializer(nco).data)
Exemple #6
0
    def post(self, request):
        with self._handle_exception(request):
            ipaddr = (
                gateway
            ) = (
                dns_servers
            ) = (
                search_domains
            ) = (
                aux_address
            ) = (
                dgateway
            ) = host_binding = icc = internal = ip_masquerade = ip_range = subnet = None
            mtu = DEFAULT_MTU
            name = request.data.get("name")
            if NetworkConnection.objects.filter(name=name).exists():
                e_msg = (
                    "Connection name ({}) is already in use. Choose a "
                    "different name."
                ).format(name)
                handle_exception(Exception(e_msg), request)

            # auto of manual
            method = request.data.get("method")
            if method not in self.config_methods:
                e_msg = (
                    "Unsupported config method ({}). Supported ones include: ({})."
                ).format(method, self.config_methods)
                handle_exception(Exception(e_msg), request)
            if method == "manual":
                # ipaddr is of the format <IP>/<netmask>. eg:
                # 192.168.1.2/24. If netmask is not given, it defaults to 32.
                ipaddr = request.data.get("ipaddr")
                gateway = request.data.get("gateway", None)
                dns_servers = request.data.get("dns_servers", None)
                search_domains = request.data.get("search_domains", None)
                aux_address = request.data.get("aux_address", None)
                dgateway = request.data.get("dgateway", None)
                host_binding = request.data.get("host_binding", None)
                icc = request.data.get("icc")
                internal = request.data.get("internal")
                ip_masquerade = request.data.get("ip_masquerade")
                ip_range = request.data.get("ip_range", None)
                mtu = request.data.get("mtu", 1500)
                subnet = request.data.get("subnet", None)

            # connection type can be one of ethernet, team, bond, or docker
            ctype = request.data.get("ctype")
            if ctype not in self.ctypes:
                e_msg = (
                    "Unsupported connection type ({}). Supported ones include: ({})."
                ).format(ctype, self.ctypes)
                handle_exception(Exception(e_msg), request)
            devices = request.data.get("devices", None)
            if ctype == "team":
                # gather required input for team
                team_profile = request.data.get("team_profile")
                if team_profile not in self.team_profiles:
                    e_msg = (
                        "Unsupported team profile ({}). Supported ones "
                        "include: ({})."
                    ).format(team_profile, self.team_profiles)
                    handle_exception(Exception(e_msg), request)
                self._validate_devices(devices, request)
                sysnet.new_team_connection(
                    name,
                    self.runners[team_profile],
                    devices,
                    ipaddr,
                    gateway,
                    dns_servers,
                    search_domains,
                )

            elif ctype == "ethernet":
                device = request.data.get("device")
                self._validate_devices([device], request, size=1)
                sysnet.new_ethernet_connection(
                    name, device, ipaddr, gateway, dns_servers, search_domains
                )

            elif ctype == "bond":
                bond_profile = request.data.get("bond_profile")
                if bond_profile not in self.bond_profiles:
                    e_msg = (
                        "Unsupported bond profile ({}). Supported ones "
                        "include: ({})."
                    ).format(bond_profile, self.bond_profiles)
                    handle_exception(Exception(e_msg), request)
                self._validate_devices(devices, request)
                sysnet.new_bond_connection(
                    name,
                    bond_profile,
                    devices,
                    ipaddr,
                    gateway,
                    dns_servers,
                    search_domains,
                )

            elif ctype == "docker":
                dnet_create(
                    name,
                    aux_address,
                    dgateway,
                    host_binding,
                    icc,
                    internal,
                    ip_masquerade,
                    ip_range,
                    mtu,
                    subnet,
                )

            return Response()
Exemple #7
0
    def post(self, request):
        with self._handle_exception(request):
            ipaddr = gateway = dns_servers = search_domains = None
            name = request.data.get("name")
            if NetworkConnection.objects.filter(name=name).exists():
                e_msg = ("Connection name ({}) is already in use. Choose a "
                         "different name.").format(name)
                handle_exception(Exception(e_msg), request)

            # auto of manual
            method = request.data.get("method")
            if method not in self.config_methods:
                e_msg = (
                    "Unsupported config method ({}). Supported ones include: ({})."
                ).format(method, self.config_methods)
                handle_exception(Exception(e_msg), request)
            if method == "manual":
                # ipaddr is of the format <IP>/<netmask>. eg:
                # 192.168.1.2/24. If netmask is not given, it defaults to 32.
                ipaddr = request.data.get("ipaddr")
                gateway = request.data.get("gateway", None)
                dns_servers = request.data.get("dns_servers", None)
                search_domains = request.data.get("search_domains", None)

            # connection type can be one of ethernet, team or bond
            ctype = request.data.get("ctype")
            if ctype not in self.ctypes:
                e_msg = (
                    "Unsupported connection type ({}). Supported ones include: ({})."
                ).format(ctype, self.ctypes)
                handle_exception(Exception(e_msg), request)
            devices = request.data.get("devices", None)
            if ctype == "team":
                # gather required input for team
                team_profile = request.data.get("team_profile")
                if team_profile not in self.team_profiles:
                    e_msg = ("Unsupported team profile ({}). Supported ones "
                             "include: ({}).").format(team_profile,
                                                      self.team_profiles)
                    handle_exception(Exception(e_msg), request)
                self._validate_devices(devices, request)
                network.new_team_connection(
                    name,
                    self.runners[team_profile],
                    devices,
                    ipaddr,
                    gateway,
                    dns_servers,
                    search_domains,
                )

            elif ctype == "ethernet":
                device = request.data.get("device")
                self._validate_devices([device], request, size=1)
                network.new_ethernet_connection(name, device, ipaddr, gateway,
                                                dns_servers, search_domains)

            elif ctype == "bond":
                bond_profile = request.data.get("bond_profile")
                if bond_profile not in self.bond_profiles:
                    e_msg = ("Unsupported bond profile ({}). Supported ones "
                             "include: ({}).").format(bond_profile,
                                                      self.bond_profiles)
                    handle_exception(Exception(e_msg), request)
                self._validate_devices(devices, request)
                network.new_bond_connection(
                    name,
                    bond_profile,
                    devices,
                    ipaddr,
                    gateway,
                    dns_servers,
                    search_domains,
                )

            return Response()