Beispiel #1
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()
Beispiel #2
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()
Beispiel #3
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()