From 0853ebebedd8663c6348ae59e22a37745a2bd8fb Mon Sep 17 00:00:00 2001
From: Lvov Maxim <mlvov@mirantis.com>
Date: Thu, 9 Jun 2011 10:39:13 +0400
Subject: [PATCH] support for project id header

---
 novaclient/__init__.py |  4 ++--
 novaclient/client.py   |  7 +++++--
 novaclient/shell.py    | 10 ++++++++--
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/novaclient/__init__.py b/novaclient/__init__.py
index 9c949d514..afe5de81f 100644
--- a/novaclient/__init__.py
+++ b/novaclient/__init__.py
@@ -61,10 +61,10 @@ class OpenStack(object):
     &c.
     """
 
-    def __init__(self, username, apikey,
+    def __init__(self, username, apikey, projectid
                  auth_url='https://auth.api.rackspacecloud.com/v1.0'):
         self.backup_schedules = BackupScheduleManager(self)
-        self.client = OpenStackClient(username, apikey, auth_url)
+        self.client = OpenStackClient(username, apikey, projectid, auth_url)
         self.flavors = FlavorManager(self)
         self.images = ImageManager(self)
         self.ipgroups = IPGroupManager(self)
diff --git a/novaclient/client.py b/novaclient/client.py
index fef21d585..7215a0f96 100644
--- a/novaclient/client.py
+++ b/novaclient/client.py
@@ -28,10 +28,11 @@ class OpenStackClient(httplib2.Http):
 
     USER_AGENT = 'python-novaclient/%s' % novaclient.__version__
 
-    def __init__(self, user, apikey, auth_url):
+    def __init__(self, user, apikey, projectid auth_url):
         super(OpenStackClient, self).__init__()
         self.user = user
         self.apikey = apikey
+        self.projectid = projectid
         self.auth_url = auth_url
 
         self.management_url = None
@@ -90,6 +91,8 @@ class OpenStackClient(httplib2.Http):
         # re-authenticate and try again. If it still fails, bail.
         try:
             kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token
+            kwargs['headers']['X-Auth-Project-Id'] = self.projectid
+
             resp, body = self.request(self.management_url + url, method,
                                       **kwargs)
             return resp, body
@@ -116,7 +119,7 @@ class OpenStackClient(httplib2.Http):
         return self._cs_request(url, 'DELETE', **kwargs)
 
     def authenticate(self):
-        headers = {'X-Auth-User': self.user, 'X-Auth-Key': self.apikey}
+        headers = {'X-Auth-User': self.user, 'X-Auth-Key': self.apikey, 'X-Auth-Project-Id': self.    projectid}
         resp, body = self.request(self.auth_url, 'GET', headers=headers)
         self.management_url = resp['x-server-management-url']
         self.auth_token = resp['x-auth-token']
diff --git a/novaclient/shell.py b/novaclient/shell.py
index 1306174d7..6e120d4a7 100644
--- a/novaclient/shell.py
+++ b/novaclient/shell.py
@@ -96,6 +96,10 @@ class OpenStackShell(object):
             default=env('NOVA_API_KEY'),
             help='Defaults to env[NOVA_API_KEY].')
 
+     self.parser.add_argument('--projectid',
+            default=env('NOVA_PROJECT_ID'),                                                       
+            help='Defaults to env[NOVA_PROJECT_ID].')
+
         auth_url = env('NOVA_URL')
         if auth_url == '':
             auth_url = 'https://auth.api.rackspacecloud.com/v1.0'
@@ -144,7 +148,9 @@ class OpenStackShell(object):
         if args.debug:
             httplib2.debuglevel = 1
 
-        user, apikey, url = args.username, args.apikey, args.url
+        user, apikey, projectid, url = args.username, args.apikey, args.projectid, args.url
+        #FIXME(usrleon): Here should be restrict for project id same as for username or apikey
+        # but for compatibility it is not.
         if not user:
             raise CommandError("You must provide a username, either via "
                                "--username or via env[NOVA_USERNAME]")
@@ -152,7 +158,7 @@ class OpenStackShell(object):
             raise CommandError("You must provide an API key, either via "
                                "--apikey or via env[NOVA_API_KEY]")
 
-        self.cs = self._api_class(user, apikey, url)
+        self.cs = self._api_class(user, apikey, projectid, url)
         try:
             self.cs.authenticate()
         except novaclient.Unauthorized: