Created
October 28, 2016 11:50
-
-
Save leplatrem/a8dc9e5afd464eb55d5fa9bfc57c407b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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