Skip to content

Commit 78f97d0

Browse files
committed
Handles foreign keys in POST of mappers
1 parent 582437b commit 78f97d0

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ It is possible to get a subresourses of an object, and/or specify a query:
125125

126126
Any `kwargs` (here `q=`) is used as a GET parameter for the request.
127127

128+
#### Foreign keys
129+
130+
Foreign keys are handle automatically by the mapper.
131+
132+
```python
133+
>>> site = next(netbox_mapper.get())
134+
>>> print(site.region.name)
135+
"Some region"
136+
```
137+
138+
When accessing to `site.region`, a query will be done to fetch the foreign
139+
object. It will then be saved in cache to avoid unnecessary queries for next
140+
accesses.
141+
142+
To refresh an object and its foreign keys, just do:
143+
144+
```python
145+
>>> site = next(site.get())
146+
```
147+
128148
### POST
129149

130150
Use the `kwargs` of a mapper to send a post request and create a new object:
@@ -134,6 +154,9 @@ Use the `kwargs` of a mapper to send a post request and create a new object:
134154
<NetboxMapper> # corresponding to the new created object
135155
```
136156

157+
If a mapper is sent as parameter, `post()` will automatically take its id.
158+
However, it will not update the foreign object.
159+
137160
### PUT
138161

139162
Use `put()` in a child mapper to update the resource upstream by reflecting

netboxapi/mapper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def post(self, **json):
8484
8585
:returns: child_mapper: Mapper containing the created object
8686
"""
87+
for k, v in json.items():
88+
if isinstance(v, NetboxMapper):
89+
try:
90+
json[k] = v.id
91+
except KeyError:
92+
raise ValueError("Mapper {} has no id".format(k))
8793
new_mapper_dict = self.netbox_api.post(self._route, json=json)
8894
try:
8995
return next(self.get(new_mapper_dict["id"]))

tests/test_mapper.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,34 @@ def update_or_create_resource_json_callback(self, request, context):
214214
json["id"] = 1
215215
return json
216216

217+
def test_post_foreign_key(self, mapper):
218+
url = self.get_mapper_url(mapper)
219+
220+
fk_mapper = NetboxMapper(mapper.netbox_api, "foo", "bar")
221+
fk_mapper.id = 2
222+
223+
with requests_mock.Mocker() as m:
224+
received_req = m.register_uri(
225+
"post", url, json={
226+
"id": 1, "name": "testname", "fk": {"id": 2}
227+
}
228+
)
229+
m.register_uri(
230+
"get", url + "1/",
231+
json={
232+
"count": 1, "next": None, "previous": None,
233+
"results": [{
234+
"id": 1, "name": "testname", "fk": {
235+
"id": 2,
236+
"url": self.get_mapper_url(fk_mapper) + "2/"
237+
}
238+
}]
239+
}
240+
)
241+
mapper.post(name="testname", fk=fk_mapper)
242+
243+
assert received_req.last_request.json()["fk"] == 2
244+
217245
def test_put(self, mapper):
218246
child_mapper = self.get_child_mapper(mapper)
219247
url = self.get_mapper_url(child_mapper) + "{}/".format(child_mapper.id)

0 commit comments

Comments
 (0)