Beispiel #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)
Beispiel #2
0
 def get(self, *args, **kwargs):
     try:
         data = NetworkConnection.objects.get(id=self.kwargs['id'])
         sdata = NetworkConnectionSerializer(data)
         return Response(sdata.data)
     except NetworkConnection.DoesNotExist:
         raise NotFound(detail=None)
Beispiel #3
0
 def post(self, request):
     with self._handle_exception(request):
         self._refresh_connections()
         self._refresh_devices()
         ns = NetworkConnectionSerializer(NetworkConnection.objects.all(),
                                          many=True)
         return Response(ns.data)
Beispiel #4
0
 def post(self, request, id, switch):
     #switch the connection up, down or reload.
     with self._handle_exception(request):
         nco = self._nco(request, id)
         if (switch == 'up' and nco.ctype in ('team', 'bond')):
             #order_by('name') because in some cases member interfaces must
             #be brought up in order. eg: active-backup.
             for mnco in nco.networkconnection_set.all().order_by('name'):
                 logger.debug('upping %s %s' % (mnco.name, mnco.uuid))
                 network.toggle_connection(mnco.uuid, switch)
         else:
             network.toggle_connection(nco.uuid, switch)
         return Response(NetworkConnectionSerializer(nco).data)
Beispiel #5
0
 def post(self, request, id, switch):
     # switch the connection up, down or reload.
     with self._handle_exception(request):
         nco = self._nco(request, id)
         if switch == "up" and nco.ctype in ("team", "bond"):
             # order_by('name') because in some cases member interfaces must
             # be brought up in order. eg: active-backup.
             for mnco in nco.networkconnection_set.all().order_by("name"):
                 logger.debug("upping {} {}".format(mnco.name, mnco.uuid))
                 sysnet.toggle_connection(mnco.uuid, switch)
         else:
             sysnet.toggle_connection(nco.uuid, switch)
         return Response(NetworkConnectionSerializer(nco).data)
Beispiel #6
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)
Beispiel #7
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)