|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# This file is part of the Ingram Micro Cloud Blue Connect SDK. |
| 4 | +# Copyright (c) 2019 Ingram Micro. All Rights Reserved. |
| 5 | + |
| 6 | +from connect.config import Config |
| 7 | +from connect.models import Asset, Product, TierConfig |
| 8 | +from connect.resources.base import ApiClient |
| 9 | + |
| 10 | + |
| 11 | +class Directory(object): |
| 12 | + """ Allows listing and obtaining several types of objects. |
| 13 | +
|
| 14 | + :param Config config: Config object or ``None`` to use environment config (default). |
| 15 | + """ |
| 16 | + |
| 17 | + _config = None # type: Config |
| 18 | + |
| 19 | + def __init__(self, config=None): |
| 20 | + self._config = config or Config.get_instance() |
| 21 | + |
| 22 | + def list_assets(self, filters=None): |
| 23 | + """ List the assets. |
| 24 | +
|
| 25 | + :param (dict[str, Any] filters: Filters to pass to the request. |
| 26 | + :return: A list with the assets that match the given filters. |
| 27 | + :rtype: list[Asset] |
| 28 | + """ |
| 29 | + products = ','.join(self._config.products) if self._config.products else None |
| 30 | + url = self._config.api_url + 'assets?in(product.id,(' + products + '))' \ |
| 31 | + if products \ |
| 32 | + else 'assets' |
| 33 | + text, code = ApiClient(self._config, url).get(params=filters) |
| 34 | + return Asset.deserialize(text) |
| 35 | + |
| 36 | + def get_asset(self, asset_id): |
| 37 | + """ Returns the asset with the given id. |
| 38 | +
|
| 39 | + :param str asset_id: The id of the asset. |
| 40 | + :return: The asset with the given id, or ``None`` if such asset does not exist. |
| 41 | + :rtype: Asset|None |
| 42 | + """ |
| 43 | + text, code = ApiClient(self._config, 'assets/' + asset_id).get() |
| 44 | + return Asset.deserialize(text) |
| 45 | + |
| 46 | + def list_products(self): |
| 47 | + """ List the products. Filtering is not possible at the moment. |
| 48 | +
|
| 49 | + :return: A list with all products. |
| 50 | + :rtype: list[Product] |
| 51 | + """ |
| 52 | + text, code = ApiClient(self._config, 'products').get() |
| 53 | + return Product.deserialize(text) |
| 54 | + |
| 55 | + def get_product(self, product_id): |
| 56 | + """ Returns the product with the given id. |
| 57 | +
|
| 58 | + :param str product_id: The id of the product. |
| 59 | + :return: The product with the given id, or ``None`` if such product does not exist. |
| 60 | + :rtype: Product|None |
| 61 | + """ |
| 62 | + text, code = ApiClient(self._config, 'products/' + product_id).get() |
| 63 | + return Product.deserialize(text) |
| 64 | + |
| 65 | + def list_tier_configs(self, filters=None): |
| 66 | + """ List the tier configs. |
| 67 | +
|
| 68 | + :param (dict[str, Any] filters: Filters to pass to the request. |
| 69 | + :return: A list with the tier configs that match the given filters. |
| 70 | + :rtype: list[TierConfig] |
| 71 | + """ |
| 72 | + filters = filters or {} |
| 73 | + products_key = 'product.id' |
| 74 | + if products_key not in filters and self._config.products: |
| 75 | + filters[products_key] = ','.join(self._config.products) |
| 76 | + text, code = ApiClient(self._config, 'tier/configs').get(params=filters) |
| 77 | + return TierConfig.deserialize(text) |
| 78 | + |
| 79 | + def get_tier_config(self, tier_config_id): |
| 80 | + """ Returns the tier config with the given id. |
| 81 | +
|
| 82 | + :param str tier_config_id: The id of the tier config. |
| 83 | + :return: The Tier Config with the given id, or ``None`` if such Tier Config does not exist. |
| 84 | + :rtype: TierConfig|None |
| 85 | + """ |
| 86 | + text, code = ApiClient(self._config, 'tier/configs/' + tier_config_id).get() |
| 87 | + return TierConfig.deserialize(text) |
0 commit comments