def quotas(self, sync, verify, user_ident, output_format, style): list_only = not sync and not verify if user_ident is not None: users = [self.get_user(user_ident)] else: users = AstakosUser.objects.verified() if list_only: qh_quotas, astakos_i = list_user_quotas(users) info = {} for user in users: info[user.uuid] = user.email print_data, labels = show_quotas(qh_quotas, astakos_i, info, style=style) utils.pprint_table(self.stdout, print_data, labels, output_format) elif verify or sync: qh_limits, diff_q = qh_sync_users_diffs(users, sync=sync) if verify: self.print_verify(qh_limits, diff_q) if sync: self.print_sync(diff_q)
def set_container_quota(args): try: utils = ManageAccounts() try: quota = int(args.quota) except: raise ValueError('Invalid quota') accounts = [args.account] if args.account \ else utils.existing_accounts() failed = [] def update_container_policy(account): trans = utils.backend.wrapper.conn.begin() try: utils.backend.update_container_policy( account, account, args.container, {'quota': quota} ) if args.dry: print "Skipping database commit." trans.rollback() else: trans.commit() except Exception, e: failed.append((account, e)) map(update_container_policy, accounts) if failed: sys.stdout.write( 'Failed for the following accounts:\n' ) pprint_table(sys.stdout, failed, headers=[])
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a server ID") server = get_vm(args[0]) flavor = '%s (%s)' % (server.flavor.id, server.flavor.name) userid = server.userid imageid = server.imageid try: image_name = get_image(imageid, userid).get('name') except: image_name = "None" image = '%s (%s)' % (imageid, image_name) usercache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN) kv = { 'id': server.id, 'name': server.name, 'owner_uuid': userid, 'owner_name': usercache.get_name(userid), 'created': utils.format_date(server.created), 'updated': utils.format_date(server.updated), 'image': image, 'host id': server.hostid, 'flavor': flavor, 'deleted': utils.format_bool(server.deleted), 'suspended': utils.format_bool(server.suspended), 'state': format_vm_state(server), } utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True)
def handle(self, *args, **options): chain_dict = Chain.objects.all_full_state() if not options['all']: f_states = [] if options['new']: f_states.append(Chain.PENDING) if options['modified']: f_states += Chain.MODIFICATION_STATES if options['pending']: f_states.append(Chain.PENDING) f_states += Chain.MODIFICATION_STATES if options['skip']: if not f_states: f_states = Chain.RELEVANT_STATES if f_states: chain_dict = filter_by(in_states(f_states), chain_dict) name = options['name'] if name: chain_dict = filter_by(is_name(name), chain_dict) owner = options['owner'] if owner: chain_dict = filter_by(is_owner(owner), chain_dict) labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID') info = chain_info(chain_dict) utils.pprint_table(self.stdout, info, labels, options["output_format"])
def set_container_quota(args): try: utils = ManageAccounts() try: quota = int(args.quota) except: raise ValueError('Invalid quota') accounts = [args.account] if args.account \ else utils.existing_accounts() failed = [] def update_container_policy(account): trans = utils.backend.wrapper.conn.begin() try: utils.backend.update_container_policy(account, account, args.container, {'quota': quota}) if args.dry: print "Skipping database commit." trans.rollback() else: trans.commit() except Exception, e: failed.append((account, e)) map(update_container_policy, accounts) if failed: sys.stdout.write('Failed for the following accounts:\n') pprint_table(sys.stdout, failed, headers=[])
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide an image ID") image_id = args[0] with image_backend(None) as backend: images = backend._list_images(None) try: image = filter(lambda x: x["id"] == image_id, images)[0] except IndexError: raise CommandError("Image not found. Use snf-manage image-list" " to get the list of all images.") utils.pprint_table(out=self.stdout, table=[image.values()], headers=image.keys(), vertical=True)
def list(args): try: utils = ManageAccounts() if args.only_duplicate: accounts = utils.duplicate_accounts() else: accounts = utils.existing_accounts() headers = ['uuid'] table = [(a,) for a in accounts] if args.output_format != "json" and not args.headers: headers = None pprint_table(sys.stdout, table, headers, args.output_format) except Exception, e: sys.stderr.write('%s\n' % e)
def handle(self, **options): user = options['userid'] with image_backend(user) as backend: images = backend._list_images(user) images.sort(key=lambda x: x['created_at'], reverse=True) headers = ("id", "name", "owner", "public") table = [] for img in images: fields = (img["id"], img["name"], img["owner"], str(img["is_public"])) table.append(fields) pprint_table(self.stdout, table, headers)
def list(args): try: utils = ManageAccounts() if args.only_duplicate: accounts = utils.duplicate_accounts() else: accounts = utils.existing_accounts() headers = ['uuid'] table = [(a, ) for a in accounts] if args.output_format != "json" and not args.headers: headers = None pprint_table(sys.stdout, table, headers, args.output_format) except Exception, e: sys.stderr.write('%s\n' % e)
def handle(self, *args, **options): if len(args) > 0: raise CommandError("Command takes no arguments") clustername = options['clustername'] port = options['port'] username = options['username'] password = options['password'] hypervisor = options["hypervisor"] if not (clustername and username and password): raise CommandError("Clustername, user and pass must be supplied") # Ensure correctness of credentials if options['check']: check_backend_credentials(clustername, port, username, password) kw = { "clustername": clustername, "port": port, "username": username, "password": password, "drained": True } if hypervisor: kw["hypervisor"] = hypervisor # Create the new backend in database try: backend = Backend.objects.create(**kw) except IntegrityError as e: raise CommandError("Cannot create backend: %s\n" % e) self.stdout.write('\nSuccessfully created backend with id %d\n' % backend.id) if not options['check']: return self.stdout.write('\rRetrieving backend resources:\n') resources = get_physical_resources(backend) attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] table = [[str(resources[x]) for x in attr]] pprint_table(self.stdout, table, attr) update_resources(backend, resources)
def handle(self, *args, **options): if len(args) > 0: raise CommandError("Command takes no arguments") clustername = options['clustername'] port = options['port'] username = options['username'] password = options['password'] hypervisor = options["hypervisor"] if not (clustername and username and password): raise CommandError("Clustername, user and pass must be supplied") # Ensure correctness of credentials if options['check']: check_backend_credentials(clustername, port, username, password) kw = {"clustername": clustername, "port": port, "username": username, "password": password, "drained": True} if hypervisor: kw["hypervisor"] = hypervisor # Create the new backend in database try: backend = Backend.objects.create(**kw) except IntegrityError as e: raise CommandError("Cannot create backend: %s\n" % e) self.stdout.write('\nSuccessfully created backend with id %d\n' % backend.id) if not options['check']: return self.stdout.write('\rRetrieving backend resources:\n') resources = get_physical_resources(backend) attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] table = [[str(resources[x]) for x in attr]] pprint_table(self.stdout, table, attr) update_resources(backend, resources)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please profile name") try: profile = Profile.objects.get(name=args[0]) except Profile.DoesNotExist: raise CommandError("Profile does not exist") kv = OrderedDict([('id', profile.id), ('is active', str(profile.active)), ('name', profile.name), ('is exclusive', profile.is_exclusive), ('policies', profile.policies), ('groups', profile.groups.all()), ('users', profile.users.all())]) utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please profile name") try: profile = Profile.objects.get(name=args[0]) except Profile.DoesNotExist: raise CommandError("Profile does not exist") kv = OrderedDict( [ ('id', profile.id), ('is active', str(profile.active)), ('name', profile.name), ('is exclusive', profile.is_exclusive), ('policies', profile.policies), ('groups', profile.groups.all()), ('users', profile.users.all()) ]) utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a service name or ID.") identifier = args[0] if identifier.isdigit(): try: service = Service.objects.get(id=int(identifier)) except Service.DoesNotExist: raise CommandError('No service found with ID %s.' % identifier) else: try: service = Service.objects.get(name=identifier) except Service.DoesNotExist: raise CommandError('No service found named %s.' % identifier) kv = OrderedDict( [ ('id', service.id), ('name', service.name), ('component', service.component), ('type', service.type), ]) utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True) self.stdout.write('\n') endpoint_data = EndpointData.objects.filter(endpoint__service=service) data = [] for ed in endpoint_data: data.append((ed.endpoint_id, ed.key, ed.value)) labels = ('endpoint', 'key', 'value') utils.pprint_table(self.stdout, data, labels, options["output_format"], title='Endpoints')
def quotas(self, sync, verify, user_ident, output_format, style): list_only = not sync and not verify if user_ident is not None: users = [self.get_user(user_ident)] else: users = AstakosUser.objects.accepted() if list_only: qh_quotas, astakos_i = list_user_quotas(users) info = {} for user in users: info[user.uuid] = user.email print_data, labels = show_quotas(qh_quotas, astakos_i, info, style=style) utils.pprint_table(self.stdout, print_data, labels, output_format) elif verify or sync: qh_limits, diff_q = qh_sync_users_diffs(users, sync=sync) if verify: self.print_verify(qh_limits, diff_q) if sync: self.print_sync(diff_q)
def display_fields(self): headers = ["Field", "Description"] table = [] for field, (_, help_msg) in self.FIELDS.items(): table.append((field, help_msg)) utils.pprint_table(self.stdout, table, headers)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a user ID or email") identifier = args[0] if identifier.isdigit(): users = AstakosUser.objects.filter(id=int(identifier)) else: try: uuid.UUID(identifier) except: users = AstakosUser.objects.filter(email__iexact=identifier) else: users = AstakosUser.objects.filter(uuid=identifier) if users.count() == 0: field = 'id' if identifier.isdigit() else 'email' msg = "Unknown user with %s '%s'" % (field, identifier) raise CommandError(msg) for user in users: kv = OrderedDict([ ('id', user.id), ('uuid', user.uuid), ('status', user.status_display), ('email', user.email), ('first name', user.first_name), ('last name', user.last_name), ('active', user.is_active), ('admin', user.is_superuser), ('last login', user.last_login), ('date joined', user.date_joined), ('last update', user.updated), #('token', user.auth_token), ('token expiration', user.auth_token_expires), ('providers', user.auth_providers_display), ('verified', user.is_verified), ('groups', [elem.name for elem in user.groups.all()]), ('permissions', [elem.codename for elem in user.user_permissions.all()]), ('group permissions', user.get_group_permissions()), ('email verified', user.email_verified), ('username', user.username), ('activation_sent_date', user.activation_sent), ]) if get_latest_terms(): has_signed_terms = user.signed_terms kv['has_signed_terms'] = has_signed_terms if has_signed_terms: kv['date_signed_terms'] = user.date_signed_terms utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True) if options["list_quotas"]: unit_style = options["unit_style"] check_style(unit_style) quotas, initial = list_user_quotas([user]) if quotas: self.stdout.write("\n") print_data, labels = show_quotas(quotas, initial, style=unit_style) utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="User Quota") if options["list_projects"]: print_data, labels = ownerships(user) if print_data: self.stdout.write("\n") utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="Owned Projects") print_data, labels = memberships(user) if print_data: self.stdout.write("\n") utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="Project Memberships")
def handle_noargs(self, **options): try: userid = options['userid'] # Get holding from Pithos DB db_usage = backend.node.node_account_usage(userid) users = set(db_usage.keys()) if userid and userid not in users: if backend._lookup_account(userid) is None: self.stdout.write("User '%s' does not exist in DB!\n" % userid) return # Get holding from Quotaholder try: qh_result = backend.astakosclient.service_get_quotas( backend.service_token, userid) except NotFound: self.stdout.write( "User '%s' does not exist in Quotaholder!\n" % userid) return users.update(qh_result.keys()) pending_exists = False unknown_user_exists = False unsynced = [] for uuid in users: db_value = db_usage.get(uuid, 0) try: qh_all = qh_result[uuid] except KeyError: self.stdout.write( "User '%s' does not exist in Quotaholder!\n" % uuid) unknown_user_exists = True continue else: qh = qh_all.get(DEFAULT_SOURCE, {}) for resource in [r['name'] for r in resources]: try: qh_resource = qh[resource] except KeyError: self.stdout.write( "Resource '%s' does not exist in Quotaholder " "for user '%s'!\n" % (resource, uuid)) continue if qh_resource['pending']: self.stdout.write( "Pending commission. " "User '%s', resource '%s'.\n" % (uuid, resource)) pending_exists = True continue qh_value = qh_resource['usage'] if db_value != qh_value: data = (uuid, resource, db_value, qh_value) unsynced.append(data) if unsynced: headers = ("User", "Resource", "Database", "Quotaholder") utils.pprint_table(self.stdout, unsynced, headers) if options['fix']: request = {} request['force'] = options['force'] request['auto_accept'] = True request['name'] = "RECONCILE" request['provisions'] = map(create_provision, unsynced) try: backend.astakosclient.issue_commission( backend.service_token, request) except QuotaLimit: self.stdout.write( "Reconciling failed because a limit has been " "reached. Use --force to ignore the check.\n") return if pending_exists: self.stdout.write( "Found pending commissions. Run 'snf-manage" " reconcile-commissions-pithos'\n") elif not (unsynced or unknown_user_exists): self.stdout.write("Everything in sync.\n") finally: backend.close()
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)
def handle(self, *args, **options): write = self.stdout.write force = options['force'] userid = options['userid'] try: astakos = Component.objects.get(name="astakos") except Component.DoesNotExist: raise CommandError("Component 'astakos' not found.") query = [userid] if userid is not None else None qh_holdings = service_get_quotas(astakos, query) if userid is None: users = AstakosUser.objects.verified() else: try: users = [AstakosUser.objects.get(uuid=userid)] except AstakosUser.DoesNotExist: raise CommandError("There is no user with uuid '%s'." % userid) db_holdings = count_pending_app(users) pending_exists = False unknown_user_exists = False unsynced = [] for user in users: uuid = user.uuid db_value = db_holdings.get(uuid, 0) try: qh_all = qh_holdings[uuid] except KeyError: write("User '%s' does not exist in Quotaholder!\n" % uuid) unknown_user_exists = True continue # Assuming only one source system_qh = qh_all.get(SYSTEM, {}) # Assuming only one resource resource = 'astakos.pending_app' try: qh_values = system_qh[resource] qh_value = qh_values['usage'] qh_pending = qh_values['pending'] except KeyError: write("Resource '%s' does not exist in Quotaholder" " for user '%s'!\n" % (resource, uuid)) continue if qh_pending: write("Pending commission. User '%s', resource '%s'.\n" % (uuid, resource)) pending_exists = True continue if db_value != qh_value: data = (uuid, resource, db_value, qh_value) unsynced.append(data) headers = ("User", "Resource", "Astakos", "Quotaholder") if unsynced: pprint_table(self.stderr, unsynced, headers) if options["fix"]: provisions = map(create_provision, unsynced) try: s = qh.issue_commission('astakos', provisions, name='RECONCILE', force=force) except qh_exception.NoCapacityError: write("Reconciling failed because a limit has been " "reached. Use --force to ignore the check.\n") return qh.resolve_pending_commission('astakos', s) write("Fixed unsynced resources\n") if pending_exists: write("Found pending commissions. " "This is probably a bug. Please report.\n") elif not (unsynced or unknown_user_exists): write("Everything in sync.\n")
def pprint_table(self, tbl, labels, title=None): utils.pprint_table(self.stdout, tbl, labels, self.output_format, title=title)
def handle(self, *args, **options): write = self.stdout.write userid = options['userid'] # Get holdings from Cyclades DB db_holdings = get_db_holdings(userid) # Get holdings from QuotaHolder qh_holdings = get_quotaholder_holdings(userid) users = set(db_holdings.keys()) users.update(qh_holdings.keys()) # Remove 'None' user users.discard(None) if userid and userid not in users: write("User '%s' does not exist in Quotaholder!", userid) return pending_exists = False unknown_user_exists = False unsynced = [] for user in users: db = db_holdings.get(user, {}) try: qh_all = qh_holdings[user] except KeyError: write("User '%s' does not exist in Quotaholder!\n" % user) unknown_user_exists = True continue # Assuming only one source qh = qh_all.get(quotas.DEFAULT_SOURCE, {}) qh = transform_quotas(qh) for resource in quotas.RESOURCES: db_value = db.pop(resource, 0) try: qh_value, _, qh_pending = qh[resource] except KeyError: write("Resource '%s' does not exist in Quotaholder" " for user '%s'!\n" % (resource, user)) continue if qh_pending: write("Pending commission. User '%s', resource '%s'.\n" % (user, resource)) pending_exists = True continue if db_value != qh_value: data = (user, resource, db_value, qh_value) unsynced.append(data) headers = ("User", "Resource", "Database", "Quotaholder") if unsynced: pprint_table(self.stderr, unsynced, headers) if options["fix"]: qh = quotas.Quotaholder.get() request = {} request["force"] = options["force"] request["auto_accept"] = True request["name"] = "RECONCILE" request["provisions"] = map(create_provision, unsynced) try: qh.issue_commission(ASTAKOS_TOKEN, request) except quotas.QuotaLimit: write("Reconciling failed because a limit has been " "reached. Use --force to ignore the check.\n") return write("Fixed unsynced resources\n") if pending_exists: write("Found pending commissions. Run 'snf-manage" " reconcile-commissions-cyclades'\n") elif not (unsynced or unknown_user_exists): write("Everything in sync.\n")
def pprint_dict(self, d, vertical=True): utils.pprint_table(self.stdout, [d.values()], d.keys(), self.output_format, vertical=vertical)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a user ID or email") identifier = args[0] if identifier.isdigit(): users = AstakosUser.objects.filter(id=int(identifier)) else: try: uuid.UUID(identifier) except: users = AstakosUser.objects.filter(email__iexact=identifier) else: users = AstakosUser.objects.filter(uuid=identifier) if users.count() == 0: field = 'id' if identifier.isdigit() else 'email' msg = "Unknown user with %s '%s'" % (field, identifier) raise CommandError(msg) for user in users: kv = OrderedDict( [ ('id', user.id), ('uuid', user.uuid), ('status', user.status_display), ('email', user.email), ('first name', user.first_name), ('last name', user.last_name), ('active', user.is_active), ('admin', user.is_superuser), ('last login', user.last_login), ('date joined', user.date_joined), ('last update', user.updated), #('token', user.auth_token), ('token expiration', user.auth_token_expires), ('providers', user.auth_providers_display), ('verified', user.is_verified), ('groups', [elem.name for elem in user.groups.all()]), ('permissions', [elem.codename for elem in user.user_permissions.all()]), ('group permissions', user.get_group_permissions()), ('email verified', user.email_verified), ('username', user.username), ('activation_sent_date', user.activation_sent), ]) if get_latest_terms(): has_signed_terms = user.signed_terms kv['has_signed_terms'] = has_signed_terms if has_signed_terms: kv['date_signed_terms'] = user.date_signed_terms utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True) if options["list_quotas"]: unit_style = options["unit_style"] check_style(unit_style) quotas, initial = list_user_quotas([user]) if quotas: self.stdout.write("\n") print_data, labels = show_quotas(quotas, initial, style=unit_style) utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="User Quota") if options["list_projects"]: print_data, labels = ownerships(user) if print_data: self.stdout.write("\n") utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="Owned Projects") print_data, labels = memberships(user) if print_data: self.stdout.write("\n") utils.pprint_table(self.stdout, print_data, labels, options["output_format"], title="Project Memberships")
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)
def display_filters(self): headers = ["Filter", "Description", "Help"] table = [] for field in self.object_class._meta.fields: table.append((field.name, field.verbose_name, field.help_text)) utils.pprint_table(self.stdout, table, headers)
def handle(self, *args, **options): if len(args) > 0: raise CommandError("List commands do not accept any argument") assert(self.object_class), "object_class variable must be declared" if options["list_fields"]: self.display_fields() return if options["list_filters"]: self.display_filters() return # --output option if options["fields"]: fields = options["fields"] fields = fields.split(",") self.validate_fields(fields) self.fields = options["fields"].split(",") # --filter-by option if options["filter_by"]: filters, excludes = utils.parse_filters(options["filter_by"]) else: filters, excludes = ({}, {}) self.filters.update(filters) self.excludes.update(excludes) # --user option user = options.get("user") if user: if "@" in user: ucache = UserCache(self.astakos_url, self.astakos_token) user = ucache.get_uuid(user) self.filters[self.user_uuid_field] = user # --deleted option if self.deleted_field: deleted = options.get("deleted") if deleted: self.filters[self.deleted_field] = True else: self.filters[self.deleted_field] = False # Special handling of arguments self.handle_args(self, *args, **options) objects = self.object_class.objects try: objects = objects.filter(**self.filters) objects = objects.exclude(**self.excludes) except FieldError as e: raise CommandError(e) except Exception as e: raise CommandError("Can not filter results: %s" % e) order_key = self.order_by if self.order_by is not None else 'pk' objects = objects.order_by(order_key) # --display-mails option display_mails = options.get("display_mails") if display_mails: if 'user_mail' in self.object_class._meta.get_all_field_names(): raise RuntimeError("%s has already a 'user_mail' attribute") self.fields.append("user.email") self.FIELDS["user.email"] =\ ("user_email", "The email of the owner.") uuids = [getattr(obj, self.user_uuid_field) for obj in objects] ucache = UserCache(self.astakos_url, self.astakos_token) ucache.fetch_names(list(set(uuids))) for obj in objects: uuid = getattr(obj, self.user_uuid_field) obj.user_email = ucache.get_name(uuid) # Special handling of DB results objects = list(objects) self.handle_db_objects(objects, **options) headers = self.fields columns = [self.FIELDS[key][0] for key in headers] table = [] for obj in objects: row = [] for attr in columns: if callable(attr): row.append(attr(obj)) else: item = obj attrs = attr.split(".") for attr in attrs: item = getattr(item, attr) row.append(item) table.append(row) # Special handle of output self.handle_output(table, headers) # Print output output_format = options["output_format"] if output_format != "json" and not options["headers"]: headers = None utils.pprint_table(self.stdout, table, headers, output_format)
def handle(self, *args, **options): write = self.stdout.write force = options['force'] userid = options['userid'] try: astakos = Component.objects.get(name="astakos") except Component.DoesNotExist: raise CommandError("Component 'astakos' not found.") query = [userid] if userid is not None else None qh_holdings = service_get_quotas(astakos, query) if userid is None: users = AstakosUser.objects.accepted() else: try: user = AstakosUser.objects.get(uuid=userid) except AstakosUser.DoesNotExist: raise CommandError("There is no user with uuid '%s'." % userid) if not user.is_accepted(): raise CommandError("%s is not an accepted user." % userid) users = [user] db_holdings = count_pending_app(users) pending_exists = False unknown_user_exists = False unsynced = [] for user in users: uuid = user.uuid db_value = db_holdings.get(uuid, 0) try: qh_all = qh_holdings[uuid] except KeyError: write("User '%s' does not exist in Quotaholder!\n" % uuid) unknown_user_exists = True continue # Assuming only one source system_qh = qh_all.get(SYSTEM, {}) # Assuming only one resource resource = 'astakos.pending_app' try: qh_values = system_qh[resource] qh_value = qh_values['usage'] qh_pending = qh_values['pending'] except KeyError: write("Resource '%s' does not exist in Quotaholder" " for user '%s'!\n" % (resource, uuid)) continue if qh_pending: write("Pending commission. User '%s', resource '%s'.\n" % (uuid, resource)) pending_exists = True continue if db_value != qh_value: data = (uuid, resource, db_value, qh_value) unsynced.append(data) headers = ("User", "Resource", "Astakos", "Quotaholder") if unsynced: pprint_table(self.stderr, unsynced, headers) if options["fix"]: provisions = map(create_provision, unsynced) try: s = qh.issue_commission('astakos', provisions, name='RECONCILE', force=force) except qh_exception.NoCapacityError: write("Reconciling failed because a limit has been " "reached. Use --force to ignore the check.\n") return qh.resolve_pending_commission('astakos', s) write("Fixed unsynced resources\n") if pending_exists: write("Found pending commissions. " "This is probably a bug. Please report.\n") elif not (unsynced or unknown_user_exists): write("Everything in sync.\n")
def handle(self, *args, **options): if len(args) > 0: raise CommandError("List commands do not accept any argument") assert (self.object_class), "object_class variable must be declared" if options["list_fields"]: self.display_fields() return if options["list_filters"]: self.display_filters() return # --output option if options["fields"]: fields = options["fields"] fields = fields.split(",") self.validate_fields(fields) self.fields = options["fields"].split(",") # --filter-by option if options["filter_by"]: filters, excludes = utils.parse_filters(options["filter_by"]) else: filters, excludes = ({}, {}) self.filters.update(filters) self.excludes.update(excludes) # --user option user = options.get("user") if user: if "@" in user: ucache = UserCache(self.astakos_url, self.astakos_token) user = ucache.get_uuid(user) self.filters[self.user_uuid_field] = user # --deleted option if self.deleted_field: deleted = options.get("deleted") if deleted: self.filters[self.deleted_field] = True else: self.filters[self.deleted_field] = False # Special handling of arguments self.handle_args(self, *args, **options) objects = self.object_class.objects try: objects = objects.filter(**self.filters) objects = objects.exclude(**self.excludes) except FieldError as e: raise CommandError(e) except Exception as e: raise CommandError("Can not filter results: %s" % e) order_key = self.order_by if self.order_by is not None else 'pk' objects = objects.order_by(order_key) # --display-mails option display_mails = options.get("display_mails") if display_mails: if 'user_mail' in self.object_class._meta.get_all_field_names(): raise RuntimeError("%s has already a 'user_mail' attribute") self.fields.append("user.email") self.FIELDS["user.email"] =\ ("user_email", "The email of the owner.") uuids = [getattr(obj, self.user_uuid_field) for obj in objects] ucache = UserCache(self.astakos_url, self.astakos_token) ucache.fetch_names(list(set(uuids))) for obj in objects: uuid = getattr(obj, self.user_uuid_field) obj.user_email = ucache.get_name(uuid) # Special handling of DB results objects = list(objects) self.handle_db_objects(objects, **options) headers = self.fields columns = [self.FIELDS[key][0] for key in headers] table = [] for obj in objects: row = [] for attr in columns: if callable(attr): row.append(attr(obj)) else: item = obj attrs = attr.split(".") for attr in attrs: item = getattr(item, attr) row.append(item) table.append(row) # Special handle of output self.handle_output(table, headers) # Print output output_format = options["output_format"] if output_format != "json" and not options["headers"]: headers = None utils.pprint_table(self.stdout, table, headers, output_format)