From 3424a3b99749db46e3c1d3e944f6db5e8a8227ef Mon Sep 17 00:00:00 2001 From: Anthony Martinet Date: Wed, 9 Mar 2022 14:53:34 +0100 Subject: [PATCH] feat(object-storage): be able to query bucket stats --- README.rst | 14 ++++++++++ scaleway/apis/__init__.py | 1 + scaleway/apis/api_object_storage.py | 43 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 scaleway/apis/api_object_storage.py diff --git a/README.rst b/README.rst index e659cc9..3e44959 100644 --- a/README.rst +++ b/README.rst @@ -123,6 +123,20 @@ Examples {u'servers': [...]} +- Get bucket informations: + +.. code-block:: python + + >>> from scaleway.apis import ObjectStorageAPI + >>> api = ObjectStorageAPI(auth_token='') # Set your token here! + >>> params = {'project_id': '12345678-9abc-def0-1234-567899abcdef', 'buckets_name': ['your_bucket_name']} + # Tiny hack due to slumber as we cant call directly .buckets-infos.post() + >>> print getattr(api.query(), 'buckets-info').post(params) + {u'buckets': {...} + # Or choose your region, as in apis/api_compute.py + >>> api = ComputeAPI(region='nl-ams', auth_token='') # Set your token here! + + - Get details of a server: .. code-block:: python diff --git a/scaleway/apis/__init__.py b/scaleway/apis/__init__.py index 0325c94..060ddaf 100644 --- a/scaleway/apis/__init__.py +++ b/scaleway/apis/__init__.py @@ -181,3 +181,4 @@ def query(self, serialize=True, **kwargs): from .api_compute import ComputeAPI # noqa # isort:skip from .api_metadata import MetadataAPI # noqa # isort:skip from .api_billing import BillingAPI # noqa # isort:skip +from .api_object_storage import ObjectStorageAPI # noqa # isort:skip diff --git a/scaleway/apis/api_object_storage.py b/scaleway/apis/api_object_storage.py new file mode 100644 index 0000000..9e41d1a --- /dev/null +++ b/scaleway/apis/api_object_storage.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2013-2016 Online SAS and Contributors. All Rights Reserved. +# Julien Castets +# Kevin Deldycke +# +# Licensed under the BSD 2-Clause License (the "License"); you may not use this +# file except in compliance with the License. You may obtain a copy of the +# License at https://opensource.org/licenses/BSD-2-Clause + +from . import API + +REGIONS = { + 'fr-par': { + 'url': 'https://api-fr-par.scaleway.com/object-private/v1/regions/fr-par/', + }, + 'nl-ams': { + 'url': 'https://api-nl-ams.scaleway.com/object-private/v1/regions/nl-ams/', + } +} + + +class ObjectStorageAPI(API): + """ The default region is par1 as it was the first availability zone + provided by Scaleway, but it could change in the future. + """ + + def __init__(self, **kwargs): + region = kwargs.pop('region', None) + base_url = kwargs.pop('base_url', None) + + assert region is None or base_url is None, \ + "Specify either region or base_url, not both." + + if base_url is None: + region = region or 'fr-par' + + assert region in REGIONS, \ + "'%s' is not a valid Scaleway region." % region + + base_url = REGIONS.get(region)['url'] + + super(ObjectStorageAPI, self).__init__(base_url=base_url, **kwargs)