Skip to content

Commit

Permalink
Default to ctx user/project ID in sample POST API
Browse files Browse the repository at this point in the history
Fixes bug #1202143

Avoid RPC failure when project and user IDs not explicitly
specified in POST'd sample.

Instead default to identity in current context.

Change-Id: Id1368c7ccf730bc62bc2b32247266e87482844cb
(cherry picked from commit 1d0b639)
  • Loading branch information
Eoghan Glynn committed Jul 17, 2013
1 parent 3f044dd commit 64c4993
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ceilometer/api/controllers/v2.py
Expand Up @@ -514,6 +514,11 @@ def get_consistent_source():
if self._id != s.counter_name:
raise wsme.exc.InvalidInput('counter_name', s.counter_name,
'should be %s' % self._id)

s.user_id = (s.user_id or
pecan.request.headers.get('X-User-Id'))
s.project_id = (s.project_id or
pecan.request.headers.get('X-Project-Id'))
if auth_project and auth_project != s.project_id:
# non admin user trying to cross post to another project_id
auth_msg = 'can not post samples to other projects'
Expand Down
26 changes: 26 additions & 0 deletions tests/api/v2/post_samples.py
Expand Up @@ -203,3 +203,29 @@ def test_multiple_samples_some_null_sources(self):
self.assertEquals(data.json[x][k],
'%s:%s' % (s1[x]['project_id'],
'paperstack'))

def test_missing_project_user_id(self):
"""Ensure missing project & user IDs are defaulted appropriately.
"""
s1 = [{'counter_name': 'my_counter_name',
'counter_type': 'gauge',
'counter_unit': 'instance',
'counter_volume': 1,
'source': 'closedstack',
'resource_id': 'bd9431c1-8d69-4ad3-803a-8d4a6b89fd36',
'resource_metadata': {'name1': 'value1',
'name2': 'value2'}}]

project_id = 'bc23a9d531064583ace8f67dad60f6bb'
user_id = 'fd87807-12d2-4b38-9c70-5f5c2ac427ff'
data = self.post_json('/meters/my_counter_name/', s1,
expect_errors=True,
headers={
'X-Roles': 'chief-bottle-washer',
'X-Project-Id': project_id,
'X-User-Id': user_id,
})

self.assertEquals(data.status_int, 200)
self.assertEquals(data.json[0]['project_id'], project_id)
self.assertEquals(data.json[0]['user_id'], user_id)

0 comments on commit 64c4993

Please sign in to comment.