def all(self, limit=0, offset=None): """Queries the 'ListView' of a given endpoint. Returns all objects from an endpoint. :arg int,optional limit: Overrides the max page size on paginated returns. :arg int,optional offset: Overrides the offset on paginated returns. :Returns: A :py:class:`.RecordSet` object. :Examples: >>> devices = nb.dcim.devices.all() >>> for device in devices: ... print(device.name) ... test1-leaf1 test1-leaf2 test1-leaf3 >>> """ if limit == 0 and offset is not None: raise ValueError("offset requires a positive limit value") req = Request( base="{}/".format(self.url), token=self.token, session_key=self.session_key, http_session=self.api.http_session, threading=self.api.threading, limit=limit, offset=offset, ) return RecordSet(self, req)
def all(self, limit=0): """Queries the 'ListView' of a given endpoint. Returns all objects from an endpoint. :arg int,optional limit: Overrides the max page size on paginated returns. :Returns: A :py:class:`.RecordSet` object. :Examples: >>> devices = nb.dcim.devices.all() >>> for device in devices: ... print(device.name) ... test1-leaf1 test1-leaf2 test1-leaf3 >>> """ req = Request( base="{}/".format(self.url), token=self.token, session_key=self.session_key, http_session=self.api.http_session, threading=self.api.threading, limit=limit, ) return RecordSet(self, req)
def test_delete_with_recordset(self): with patch("pynetbox.core.query.Request._make_call", return_value=Mock()) as mock: from pynetbox.core.response import RecordSet ids = [1, 3, 5] class FakeRequest: def get(self): return iter([{ "id": i, "name": "dummy" + str(i) } for i in ids]) mock.return_value = True api = Mock(base_url="http://localhost:8000/api") app = Mock(name="test") test_obj = Endpoint(api, app, "test") recordset = RecordSet(test_obj, FakeRequest()) test = test_obj.delete(recordset) mock.assert_called_with(verb="delete", data=[{ "id": i } for i in ids]) self.assertTrue(test)
def init_recordset(cls): data = [{ "id": i, "name": "dummy" + str(i), "status": "active" } for i in cls.ids] api = Mock(base_url="http://localhost:8000/api") app = Mock(name="test") class FakeRequest: def get(self): return iter(data) def patch(self): return iter(data) return RecordSet(Endpoint(api, app, "test"), FakeRequest())
def filter(self, *args, **kwargs): r"""Queries the 'ListView' of a given endpoint. Takes named arguments that match the usable filters on a given endpoint. If an argument is passed then it's used as a freeform search argument if the endpoint supports it. :arg str,optional \*args: Freeform search string that's accepted on given endpoint. :arg str,optional \**kwargs: Any search argument the endpoint accepts can be added as a keyword arg. :arg int,optional limit: Overrides the max page size on paginated returns. :arg int,optional offset: Overrides the offset on paginated returns. :Returns: A :py:class:`.RecordSet` object. :Examples: To return a list of objects matching a named argument filter. >>> devices = nb.dcim.devices.filter(role='leaf-switch') >>> for device in devices: ... print(device.name) ... test1-leaf1 test1-leaf2 test1-leaf3 >>> Using a freeform query along with a named argument. >>> devices = nb.dcim.devices.filter('a3', role='leaf-switch') >>> for device in devices: ... print(device.name) ... test1-a3-leaf1 test1-a3-leaf2 >>> Chaining multiple named arguments. >>> devices = nb.dcim.devices.filter(role='leaf-switch', status=True) >>> for device in devices: ... print(device.name) ... test1-leaf2 >>> Passing a list as a named argument adds multiple filters of the same value. >>> devices = nb.dcim.devices.filter(role=['leaf-switch', 'spine-switch']) >>> for device in devices: ... print(device.name) ... test1-a3-spine1 test1-a3-spine2 test1-a3-leaf1 >>> """ if args: kwargs.update({"q": args[0]}) if any(i in RESERVED_KWARGS for i in kwargs): raise ValueError( "A reserved kwarg was passed ({}). Please remove it " "and try again.".format(RESERVED_KWARGS)) limit = kwargs.pop("limit") if "limit" in kwargs else 0 offset = kwargs.pop("offset") if "offset" in kwargs else None if limit == 0 and offset is not None: raise ValueError("offset requires a positive limit value") req = Request( filters=kwargs, base=self.url, token=self.token, session_key=self.session_key, http_session=self.api.http_session, threading=self.api.threading, limit=limit, offset=offset, ) return RecordSet(self, req)
def get(self, *args, **kwargs): r"""Queries the DetailsView of a given endpoint. :arg int,optional key: id for the item to be retrieved. :arg str,optional \**kwargs: Accepts the same keyword args as filter(). Any search argument the endpoint accepts can be added as a keyword arg. :returns: A single :py:class:`.Record` object or None :raises ValueError: if kwarg search return more than one value. :Examples: Referencing with a kwarg that only returns one value. >>> nb.dcim.devices.get(name='test1-a3-tor1b') test1-a3-tor1b >>> Referencing with an id. >>> nb.dcim.devices.get(1) test1-edge1 >>> """ try: key = args[0] except IndexError: key = None if not key: resp = self.filter(**kwargs) ret = next(resp, None) if not ret: return ret try: next(resp) raise ValueError( "get() returned more than one result. " "Check that the kwarg(s) passed are valid for this " "endpoint or use filter() or all() instead.") except StopIteration: return ret req = Request( key=key, base=self.url, token=self.token, session_key=self.session_key, http_session=self.api.http_session, ) try: return next(RecordSet(self, req), None) except RequestError as e: if e.req.status_code == 404: return None else: raise e