|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Cisco Spark Licenses API wrapper. |
| 3 | +
|
| 4 | +Classes: |
| 5 | + License: Models a Spark License JSON object as a native Python object. |
| 6 | + LicensesAPI: Wraps the Cisco Spark Licenses API and exposes the |
| 7 | + API calls as Python method calls that return native Python objects. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +from builtins import object |
| 13 | +from six import string_types |
| 14 | + |
| 15 | +from ciscosparkapi.utils import generator_container |
| 16 | +from ciscosparkapi.restsession import RestSession |
| 17 | +from ciscosparkapi.sparkdata import SparkData |
| 18 | + |
| 19 | + |
| 20 | +__author__ = "Chris Lunsford" |
| 21 | +__author_email__ = "[email protected]" |
| 22 | +__copyright__ = "Copyright (c) 2016 Cisco Systems, Inc." |
| 23 | +__license__ = "MIT" |
| 24 | + |
| 25 | + |
| 26 | +class License(SparkData): |
| 27 | + """Model a Spark License JSON object as a native Python object.""" |
| 28 | + |
| 29 | + def __init__(self, json): |
| 30 | + """Init a new License data object from a dict or JSON string. |
| 31 | +
|
| 32 | + Args: |
| 33 | + json(dict, string_types): Input JSON object. |
| 34 | +
|
| 35 | + Raises: |
| 36 | + TypeError: If the input object is not a dictionary or string. |
| 37 | +
|
| 38 | + """ |
| 39 | + super(License, self).__init__(json) |
| 40 | + |
| 41 | + @property |
| 42 | + def id(self): |
| 43 | + """The unique id for the License.""" |
| 44 | + return self._json.get('id') |
| 45 | + |
| 46 | + @property |
| 47 | + def name(self): |
| 48 | + """The name of the License.""" |
| 49 | + return self._json.get('name') |
| 50 | + |
| 51 | + @property |
| 52 | + def totalUnits(self): |
| 53 | + """The total number of license units.""" |
| 54 | + return self._json.get('totalUnits') |
| 55 | + |
| 56 | + @property |
| 57 | + def consumedUnits(self): |
| 58 | + """The total number of license units consumed.""" |
| 59 | + return self._json.get('consumedUnits') |
| 60 | + |
| 61 | + |
| 62 | +class LicensesAPI(object): |
| 63 | + """Cisco Spark Licenses API wrapper. |
| 64 | +
|
| 65 | + Wraps the Cisco Spark Licenses API and exposes the API calls as Python |
| 66 | + method calls that return native Python objects. |
| 67 | +
|
| 68 | + """ |
| 69 | + |
| 70 | + def __init__(self, session): |
| 71 | + """Init a new LicensesAPI object with the provided RestSession. |
| 72 | +
|
| 73 | + Args: |
| 74 | + session(RestSession): The RESTful session object to be used for |
| 75 | + API calls to the Cisco Spark service. |
| 76 | +
|
| 77 | + Raises: |
| 78 | + AssertionError: If the parameter types are incorrect. |
| 79 | +
|
| 80 | + """ |
| 81 | + assert isinstance(session, RestSession) |
| 82 | + super(LicensesAPI, self).__init__() |
| 83 | + self._session = session |
| 84 | + |
| 85 | + @generator_container |
| 86 | + def list(self, orgId=None, max=None): |
| 87 | + """List Licenses. |
| 88 | +
|
| 89 | + Optionally filtered by Organization (orgId parameter). |
| 90 | +
|
| 91 | + This method supports Cisco Spark's implementation of RFC5988 Web |
| 92 | + Linking to provide pagination support. It returns a generator |
| 93 | + container that incrementally yields all objects returned by the |
| 94 | + query. The generator will automatically request additional 'pages' of |
| 95 | + responses from Spark as needed until all responses have been returned. |
| 96 | + The container makes the generator safe for reuse. A new API call will |
| 97 | + be made, using the same parameters that were specified when the |
| 98 | + generator was created, every time a new iterator is requested from the |
| 99 | + container. |
| 100 | +
|
| 101 | + Args: |
| 102 | + orgId(string_types): Filters the returned licenses to only include |
| 103 | + those liceses associated with the specified Organization |
| 104 | + (orgId). |
| 105 | + max(int): Limits the maximum number of entries returned from the |
| 106 | + Spark service per request (page size; requesting additional |
| 107 | + pages is handled automatically). |
| 108 | +
|
| 109 | + Returns: |
| 110 | + GeneratorContainer: When iterated, the GeneratorContainer, yields |
| 111 | + the objects returned from the Cisco Spark query. |
| 112 | +
|
| 113 | + Raises: |
| 114 | + AssertionError: If the parameter types are incorrect. |
| 115 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 116 | +
|
| 117 | + """ |
| 118 | + # Process args |
| 119 | + assert orgId is None or isinstance(orgId, string_types) |
| 120 | + assert max is None or isinstance(max, int) |
| 121 | + params = {} |
| 122 | + if orgId: |
| 123 | + params['orgId'] = orgId |
| 124 | + if max: |
| 125 | + params['max'] = max |
| 126 | + # API request - get items |
| 127 | + items = self._session.get_items('licenses', params=params) |
| 128 | + # Yield License objects created from the returned JSON objects |
| 129 | + for item in items: |
| 130 | + yield License(item) |
| 131 | + |
| 132 | + def get(self, licenseId): |
| 133 | + """Get the details of a License, by id. |
| 134 | +
|
| 135 | + Args: |
| 136 | + licenseId(string_types): The id of the License. |
| 137 | +
|
| 138 | + Returns: |
| 139 | + License: With the details of the requested License. |
| 140 | +
|
| 141 | + Raises: |
| 142 | + AssertionError: If the parameter types are incorrect. |
| 143 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 144 | +
|
| 145 | + """ |
| 146 | + # Process args |
| 147 | + assert isinstance(licenseId, string_types) |
| 148 | + # API request |
| 149 | + json_obj = self._session.get('licenses/' + licenseId) |
| 150 | + # Return a License object created from the returned JSON object |
| 151 | + return License(json_obj) |
0 commit comments