예제 #1
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a network ID")

        network = get_network(args[0])

        # Validate subnet
        if options.get('subnet'):
            validate_network_info(options)

        # Validate state
        state = options.get('state')
        if state:
            allowed = [x[0] for x in Network.OPER_STATES]
            if state not in allowed:
                msg = "Invalid state, must be one of %s" % ', '.join(allowed)
                raise CommandError(msg)

        dhcp = options.get("dhcp")
        if dhcp:
            options["dhcp"] = parse_bool(dhcp)
        drained = options.get("drained")
        if drained:
            options["drained"] = parse_bool(drained)
        fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6',
                  'dhcp', 'state', 'link', 'mac_prefix', 'drained')
        for field in fields:
            value = options.get(field, None)
            if value is not None:
                network.__setattr__(field, value)

        add_reserved_ips = options.get('add_reserved_ips')
        remove_reserved_ips = options.get('remove_reserved_ips')
        if add_reserved_ips or remove_reserved_ips:
            if add_reserved_ips:
                add_reserved_ips = add_reserved_ips.split(",")
            if remove_reserved_ips:
                remove_reserved_ips = remove_reserved_ips.split(",")

        for bnetwork in network.backend_networks.all():
            with pooled_rapi_client(bnetwork.backend) as c:
                c.ModifyNetwork(network=network.backend_id,
                                add_reserved_ips=add_reserved_ips,
                                remove_reserved_ips=remove_reserved_ips)

        network.save()
예제 #2
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        dry_run = options["dry_run"]
        name = options['name']
        subnet = options['subnet']
        backend_id = options['backend_id']
        public = options['public']
        flavor = options['flavor']
        mode = options['mode']
        link = options['link']
        mac_prefix = options['mac_prefix']
        tags = options['tags']
        userid = options["owner"]

        if not name:
            raise CommandError("Name is required")
        if not subnet:
            raise CommandError("Subnet is required")
        if not flavor:
            raise CommandError("Flavor is required")
        if public and not backend_id:
            raise CommandError("backend-id is required")
        if not userid and not public:
            raise CommandError("'owner' is required for private networks")

        if mac_prefix and flavor == "MAC_FILTERED":
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
        if link and flavor == "PHYSICAL_VLAN":
            raise CommandError("Can not override PHYSICAL_VLAN link")

        if backend_id:
            backend = get_backend(backend_id)

        fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor)
        mode = mode or fmode
        link = link or flink
        mac_prefix = mac_prefix or fmac_prefix
        tags = tags or ftags

        try:
            validate_mac(mac_prefix + "0:00:00:00")
        except InvalidMacAddress:
            raise CommandError("Invalid MAC prefix '%s'" % mac_prefix)
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)

        if not link or not mode:
            raise CommandError("Can not create network."
                               " No connectivity link or mode")
        netinfo = {
           "name": name,
           "userid": options["owner"],
           "subnet": subnet,
           "gateway": gateway,
           "gateway6": gateway6,
           "subnet6": subnet6,
           "dhcp": options["dhcp"],
           "flavor": flavor,
           "public": public,
           "mode": mode,
           "link": link,
           "mac_prefix": mac_prefix,
           "tags": tags,
           "state": "ACTIVE"}

        if dry_run:
            self.stdout.write("Creating network:\n")
            pprint_table(self.stdout, tuple(netinfo.items()))
            return

        network = Network.objects.create(**netinfo)
        if userid:
            quotas.issue_and_accept_commission(network)

        if backend_id:
            # Create BackendNetwork only to the specified Backend
            network.create_backend_network(backend)
            create_network(network=network, backend=backend, connect=True)
예제 #3
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        dry_run = options["dry_run"]
        name = options['name']
        subnet = options['subnet']
        backend_id = options['backend_id']
        public = options['public']
        flavor = options['flavor']
        mode = options['mode']
        link = options['link']
        mac_prefix = options['mac_prefix']
        tags = options['tags']
        userid = options["owner"]

        if not name:
            raise CommandError("Name is required")
        if not subnet:
            raise CommandError("Subnet is required")
        if not flavor:
            raise CommandError("Flavor is required")
        if public and not backend_id:
            raise CommandError("backend-id is required")
        if not userid and not public:
            raise CommandError("'owner' is required for private networks")

        if mac_prefix and flavor == "MAC_FILTERED":
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
        if link and flavor == "PHYSICAL_VLAN":
            raise CommandError("Can not override PHYSICAL_VLAN link")

        if backend_id:
            backend = get_backend(backend_id)

        fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor)
        mode = mode or fmode
        link = link or flink
        mac_prefix = mac_prefix or fmac_prefix
        tags = tags or ftags

        try:
            validate_mac(mac_prefix + "0:00:00:00")
        except InvalidMacAddress:
            raise CommandError("Invalid MAC prefix '%s'" % mac_prefix)
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)

        if not link or not mode:
            raise CommandError("Can not create network."
                               " No connectivity link or mode")
        netinfo = {
            "name": name,
            "userid": options["owner"],
            "subnet": subnet,
            "gateway": gateway,
            "gateway6": gateway6,
            "subnet6": subnet6,
            "dhcp": options["dhcp"],
            "flavor": flavor,
            "public": public,
            "mode": mode,
            "link": link,
            "mac_prefix": mac_prefix,
            "tags": tags,
            "state": "ACTIVE"
        }

        if dry_run:
            self.stdout.write("Creating network:\n")
            pprint_table(self.stdout, tuple(netinfo.items()))
            return

        network = Network.objects.create(**netinfo)
        if userid:
            quotas.issue_and_accept_commission(network)

        if backend_id:
            # Create BackendNetwork only to the specified Backend
            network.create_backend_network(backend)
            create_network(network=network, backend=backend, connect=True)