diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 084021020..0b8f648ee 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -514,6 +514,11 @@ class MeterController(rest.RestController): 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' diff --git a/tests/api/v2/post_samples.py b/tests/api/v2/post_samples.py index 03dbfb2d2..3bb40cb0a 100644 --- a/tests/api/v2/post_samples.py +++ b/tests/api/v2/post_samples.py @@ -203,3 +203,29 @@ class TestPostSamples(FunctionalTest): 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)