Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Created October 28, 2016 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leplatrem/a8dc9e5afd464eb55d5fa9bfc57c407b to your computer and use it in GitHub Desktop.
Save leplatrem/a8dc9e5afd464eb55d5fa9bfc57c407b to your computer and use it in GitHub Desktop.
import argparse
import logging
import ruamel.yaml
from kinto_http import cli_utils
from kinto_http import exceptions as kinto_exceptions
logger = logging.getLogger(__name__)
def introspect_server(client):
logger.info("Fetch buckets list.")
buckets = client.get_buckets()
return {
bucket['id']: introspect_bucket(client, bucket['id'])
for bucket in buckets
}
def introspect_bucket(client, bid):
logger.info("Fetch information of bucket {!r}".format(bid))
try:
bucket = client.get_bucket(bucket=bid)
except kinto_exceptions.BucketNotFound:
logger.error("Could not read bucket {!r}".format(bid))
return None
permissions = bucket.get('permissions', {})
if 'write' not in permissions:
error_msg = 'Could not read permissions of bucket {!r}'.format(bid)
raise kinto_exceptions.KintoException(error_msg)
collections = client.get_collections(bucket=bid)
groups = client.get_groups(bucket=bid)
return {
'permissions': permissions,
'collections': {
collection['id']: introspect_collection(client, bid, collection['id'])
for collection in collections
},
'groups': {
group['id']: introspect_group(client, bid, group['id'])
for group in groups
}
}
def introspect_collection(client, bid, cid):
logger.info("Fetch information of collection {!r}/{!r}".format(bid, cid))
collection = client.get_collection(bucket=bid, collection=cid)
return {
'permissions': collection['permissions'],
}
def introspect_group(client, bid, gid):
logger.info("Fetch information of group {!r}/{!r}".format(bid, gid))
group = client.get_group(bucket=bid, group=gid)
return {
'data': {'members': group['data']['members']},
'permissions': group['permissions']
}
def main():
parser = argparse.ArgumentParser(description="Kinto buckets to YAML")
cli_utils.add_parser_options(parser,
include_bucket=False,
include_collection=False)
args = parser.parse_args()
cli_utils.setup_logger(logger, args)
logger.debug("Instantiate Kinto client.")
client = cli_utils.create_client_from_args(args)
logger.debug("Start introspection...")
result = introspect_server(client)
print(ruamel.yaml.safe_dump(result))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment