Skip to content

Commit

Permalink
TPT 1880: Fixed issue with updating non-populated NodeBalancerNode (#277
Browse files Browse the repository at this point in the history
)

## 📝 Description

Previously, attempting to update a non-populated NodeBalancerNode would
result in the update actually updating the resource silently. This
change fixes the issue.

## ✔️ How to Test

`pytest test`

Note: Since this change deals with making updates to real resources as
opposed to fixtures, it cannot be tested using mocks and must therefore
be tested manually. To do this, first create a NodeBalancerNode in your
Linode account if one does not already exist, and then run this python
script and verify that the weight of the node was actually updated.

```
#!/usr/bin/env python3

from linode_api4 import LinodeClient
from linode_api4.objects import  NodeBalancerNode

client = LinodeClient(<personal_access_token>)

node = NodeBalancerNode(client, <node_id>, <config_id>, <nodebalancer_id>)

node.weight = 60

node.save()
```

Resolves #97
  • Loading branch information
ezilber-akamai-zz committed May 9, 2023
1 parent 233a899 commit d511f44
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions linode_api4/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,26 @@ def save(self, force=True) -> bool:
if not force and not self._changed:
return False

resp = self._client.put(
type(self).api_endpoint, model=self, data=self._serialize()
)
data = None
if not self._populated:
data = {
a: object.__getattribute__(self, a)
for a in type(self).properties
if type(self).properties[a].mutable
and object.__getattribute__(self, a) is not None
}

for key, value in data.items():
if (
isinstance(value, ExplicitNullValue)
or value == ExplicitNullValue
):
data[key] = None

else:
data = self._serialize()

resp = self._client.put(type(self).api_endpoint, model=self, data=data)

if "error" in resp:
return False
Expand Down

0 comments on commit d511f44

Please sign in to comment.