<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Jul 17, 2018 at 3:55 PM Jay Pipes <<a href="mailto:jaypipes@gmail.com">jaypipes@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 07/17/2018 03:36 AM, Neil Jerram wrote:<br>
> Can someone help me with how to look up a project name (aka tenant name) <br>
> for a known project/tenant ID, from code (specifically a mechanism <br>
> driver) running in the Neutron server?<br>
> <br>
> I believe that means I need to make a GET REST call as here: <br>
> <a href="https://developer.openstack.org/api-ref/identity/v3/index.html#projects" rel="noreferrer" target="_blank">https://developer.openstack.org/api-ref/identity/v3/index.html#projects</a>.  But <br>
> I don't yet understand how a piece of Neutron server code can ensure <br>
> that it has the right credentials to do that.  If someone happens to <br>
> have actual code for doing this, I'm sure that would be very helpful.<br>
> <br>
> (I'm aware that whenever the Neutron server processes an API request, <br>
> the project name for the project that generated that request is added <br>
> into the request context.  That is great when my code is running in an <br>
> API request context.  But there are other times when the code isn't in a <br>
> request context and still needs to map from a project ID to project <br>
> name; hence the question here.)<br>
<br>
Hi Neil,<br>
<br>
You basically answered your own question above :) The neutron request <br>
context gets built from oslo.context's Context.from_environ() [1] which <br>
has this note in the implementation [2]:<br>
<br>
# Load a new context object from the environment variables set by<br>
# auth_token middleware. See:<br>
# <br>
<a href="https://docs.openstack.org/keystonemiddleware/latest/api/keystonemiddleware.auth_token.html#what-auth-token-adds-to-the-request-for-use-by-the-openstack-service" rel="noreferrer" target="_blank">https://docs.openstack.org/keystonemiddleware/latest/api/keystonemiddleware.auth_token.html#what-auth-token-adds-to-the-request-for-use-by-the-openstack-service</a><br>
<br>
So, basically, simply look at the HTTP headers for HTTP_X_PROJECT_NAME. <br>
If you don't have access to a HTTP headers, then you'll need to pass <br>
some context object/struct to the code you're referring to. Might as <br>
well pass the neutron RequestContext (derived from oslo_context.Context) <br>
to the code you're referring to and you get all this for free.<br>
<br>
Best,<br>
-jay<br>
<br>
[1] <br>
<a href="https://github.com/openstack/oslo.context/blob/4abd5377e4d847102a4e87a528d689e31cc1713c/oslo_context/context.py#L424" rel="noreferrer" target="_blank">https://github.com/openstack/oslo.context/blob/4abd5377e4d847102a4e87a528d689e31cc1713c/oslo_context/context.py#L424</a><br>
<br>
[2] <br>
<a href="https://github.com/openstack/oslo.context/blob/4abd5377e4d847102a4e87a528d689e31cc1713c/oslo_context/context.py#L433-L435" rel="noreferrer" target="_blank">https://github.com/openstack/oslo.context/blob/4abd5377e4d847102a4e87a528d689e31cc1713c/oslo_context/context.py#L433-L435</a></blockquote><div><br></div><div>Many thanks for this reply, Jay.</div><div><br></div><div>If I'm understanding fully, I believe it all works beautifully so long as the Neutron server is processing a specific API request, e.g. a port CRUD operation.  Then, as you say, the RequestContext includes the name of the project/tenant that originated that request.</div><div><br></div><div>I have an additional requirement, though, to do a occasional audit of standing resources in the Neutron DB, and to check that my mechanism driver's programming for them is correct.  To do that, I have an independent eventlet thread that runs in admin context and occasionally queries Neutron resources, e.g. all the ports.  For each port, the Neutron DB data includes the project_id, but not project_name, and I'd like at that point to be able to map from the project_id for each port to project_name.</div><div><br></div><div>Do you have any thoughts on how I could do that?  (E.g. perhaps there is some way of generating and looping round a request with the project_id, such that the middleware populates the project_name... but that sounds a bit baroque; I would hope that there would be a way of doing a simpler Keystone DB lookup.)</div><div><br></div><div>Regards,</div><div>    Neil</div><div><br></div></div></div>