Skip to content

Commit

Permalink
Added APIObject.update with jsonpatch
Browse files Browse the repository at this point in the history
  • Loading branch information
brosner committed Jan 31, 2016
1 parent 25de304 commit 1edf228
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
31 changes: 17 additions & 14 deletions pykube/objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import copy
import json
import time

import jsonpatch

from .exceptions import ObjectDoesNotExist
from .query import ObjectManager

Expand All @@ -15,6 +18,7 @@ class APIObject:

def __init__(self, api, obj):
self.api = api
self._original_obj = copy.deepcopy(obj)
self.obj = obj

@property
Expand All @@ -27,7 +31,7 @@ def api_kwargs(self, **kwargs):
if collection:
kw["url"] = self.endpoint
else:
kw["url"] = "{}/{}".format(self.endpoint, self.name)
kw["url"] = "{}/{}".format(self.endpoint, self._original_obj["metadata"]["name"])
if self.namespace is not None:
kw["namespace"] = self.namespace
kw.update(kwargs)
Expand All @@ -54,6 +58,16 @@ def reload(self):
r.raise_for_status()
self.obj = r.json()

def update(self):
patch = jsonpatch.make_patch(self._original_obj, self.obj)
r = self.api.patch(**self.api_kwargs(
headers={"Content-Type": "application/json-patch+json"},
data=str(patch),
))
r.raise_for_status()
self.obj = r.json()
self._original_obj = copy.deepcopy(self.obj)

def delete(self):
r = self.api.delete(**self.api_kwargs())
if r.status_code != 404:
Expand Down Expand Up @@ -113,19 +127,8 @@ def scale(self, replicas=None):
if replicas is None:
replicas = self.replicas
self.exists(ensure=True)
r = self.api.patch(
url="replicationcontrollers/{}".format(self.name),
namespace=self.namespace,
headers={
"Content-Type": "application/strategic-merge-patch+json",
},
data=json.dumps({
"spec": {
"replicas": replicas,
},
})
)
r.raise_for_status()
self.replicas = replicas
self.update()
while True:
self.reload()
if self.replicas == replicas:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"requests",
"PyYAML",
"six",
"jsonpatch",
],
)

0 comments on commit 1edf228

Please sign in to comment.