Skip to content

Commit ab8308a

Browse files
author
Brian Curtin
committed
Add support for HEAD requests of resources
Allow Resource.head* to make HEAD requests. When header data comes back, X-Trans-Id headers are converted into 'id' data to match the rest of the response types, and to match Resource.id attribute access. Change-Id: I6d898f0734bbdbcb02b79d3387ee8248e401a36e
1 parent 1923d1e commit ab8308a

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

openstack/resource.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ def get(self, session):
252252
self._attrs.update(body)
253253
self._loaded = True
254254

255+
@classmethod
256+
def head_data_by_id(cls, session, r_id):
257+
if not cls.allow_retrieve:
258+
raise exceptions.MethodNotSupported('retrieve')
259+
260+
url = utils.urljoin(cls.base_path, r_id)
261+
262+
data = session.head(url, service=cls.service, accept=None).headers
263+
resp_id = data.pop("X-Trans-Id", None)
264+
if resp_id:
265+
data["id"] = resp_id
266+
267+
return data
268+
269+
@classmethod
270+
def head_by_id(cls, session, r_id):
271+
data = cls.head_data_by_id(session, r_id)
272+
return cls.existing(**data)
273+
274+
def head(self, session):
275+
data = self.head_data_by_id(session, self.id)
276+
self._attrs.update(data)
277+
self._loaded = True
278+
255279
@classmethod
256280
def update_by_id(cls, session, r_id, attrs):
257281
if not cls.allow_update:

openstack/tests/test_resource.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ def test_get(self):
9999
self.assertEqual(fake_attr1, obj.first)
100100
self.assertEqual(fake_attr2, obj.second)
101101

102+
@httpretty.activate
103+
def test_head(self):
104+
self.stub_url(httpretty.HEAD, path=[fake_path, fake_id],
105+
name=fake_name,
106+
attr1=fake_attr1,
107+
attr2=fake_attr2,
108+
x_trans_id=fake_id)
109+
obj = FakeResource.head_by_id(self.session, fake_id)
110+
111+
self.assertEqual(fake_id, int(obj.id))
112+
self.assertEqual(fake_name, obj['name'])
113+
self.assertEqual(fake_attr1, obj['attr1'])
114+
self.assertEqual(fake_attr2, obj['attr2'])
115+
116+
self.assertEqual(fake_name, obj.name)
117+
self.assertEqual(fake_attr1, obj.first)
118+
self.assertEqual(fake_attr2, obj.second)
119+
102120
@httpretty.activate
103121
def test_update(self):
104122
new_attr1 = 'attr5'

0 commit comments

Comments
 (0)