summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCerberus <matt.dietz@rackspace.com>2010-09-28 00:44:32 -0500
committerCerberus <matt.dietz@rackspace.com>2010-09-28 00:44:32 -0500
commit7e25838ea1965231df09f29675fc3ab40e194483 (patch)
tree7f111c9bc38e6a2c87e343b4e0386795a12c5b7f
parent2f72b2a9fc9fee508b16c0b96285124279ef89ca (diff)
downloadnova-7e25838ea1965231df09f29675fc3ab40e194483.tar.gz
nova-7e25838ea1965231df09f29675fc3ab40e194483.tar.xz
nova-7e25838ea1965231df09f29675fc3ab40e194483.zip
db api call to get instances by user and user checking in each of the server actions
-rw-r--r--nova/api/rackspace/servers.py29
-rw-r--r--nova/db/api.py3
-rw-r--r--nova/db/sqlalchemy/api.py7
-rw-r--r--nova/tests/api/rackspace/servers.py12
4 files changed, 37 insertions, 14 deletions
diff --git a/nova/api/rackspace/servers.py b/nova/api/rackspace/servers.py
index d825e734e..becac9140 100644
--- a/nova/api/rackspace/servers.py
+++ b/nova/api/rackspace/servers.py
@@ -57,28 +57,32 @@ class Controller(wsgi.Controller):
self.db = utils.import_object(db_driver)
def index(self, req):
- instance_list = self.db.instance_get_all(None)
+ user_id = req.environ['nova.context']['user']['id']
+ instance_list = self.db.instance_get_all_by_user(None, user_id)
res = [self._entity_inst(inst)['server'] for inst in instance_list]
return self._entity_list(res)
def detail(self, req):
+ user_id = req.environ['nova.context']['user']['id']
res = [self._entity_detail(inst)['server'] for inst in
- self.db.instance_get_all(None)]
+ self.db.instance_get_all_by_user(None, user_id)]
return self._entity_list(res)
def show(self, req, id):
- user = req.environ['nova.context']['user']
+ user_id = req.environ['nova.context']['user']['id']
inst = self.db.instance_get(None, id)
if inst:
- return self._entity_detail(inst)
+ if inst.user_id == user_id:
+ return self._entity_detail(inst)
raise exc.HTTPNotFound()
def delete(self, req, id):
+ user_id = req.environ['nova.context']['user']['id']
instance = self.db.instance_get(None, id)
- if not instance:
- return exc.HTTPNotFound()
- self.db.instance_destroy(None, id)
- return exc.HTTPAccepted()
+ if instance and instance['user_id'] == user_id:
+ self.db.instance_destroy(None, id)
+ return exc.HTTPAccepted()
+ return exc.HTTPNotFound()
def create(self, req):
inst = self._build_server_instance(req)
@@ -95,7 +99,7 @@ class Controller(wsgi.Controller):
attrs = req.environ['nova.context'].get('model_attributes', None)
if attrs:
- self.db.instance_update(None, id, attrs)
+ self.db.instance_update(None, id, self._filter_params(attrs))
return exc.HTTPNoContent()
def action(self, req, id):
@@ -142,7 +146,12 @@ class Controller(wsgi.Controller):
return inst
def _filter_params(self, inst_dict):
- pass
+ keys = ['name', 'adminPass']
+ new_attrs = {}
+ for k in keys:
+ if inst_dict.has_key(k):
+ new_attrs[k] = inst_dict[k]
+ return new_attrs
def _entity_list(self, entities):
return dict(servers=entities)
diff --git a/nova/db/api.py b/nova/db/api.py
index c1cb1953a..b0b8f234d 100644
--- a/nova/db/api.py
+++ b/nova/db/api.py
@@ -251,6 +251,9 @@ def instance_get_all(context):
"""Get all instances."""
return IMPL.instance_get_all(context)
+def instance_get_all_by_user(context, user_id):
+ """Get all instances."""
+ return IMPL.instance_get_all(context, user_id)
def instance_get_by_project(context, project_id):
"""Get all instance belonging to a project."""
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index 2b0dd6ea6..5c1ceee92 100644
--- a/nova/db/sqlalchemy/api.py
+++ b/nova/db/sqlalchemy/api.py
@@ -389,6 +389,13 @@ def instance_get_all(context):
).filter_by(deleted=_deleted(context)
).all()
+def instance_get_all_by_user(context, user_id):
+ session = get_session()
+ return session.query(models.Instance
+ ).options(joinedload_all('fixed_ip.floating_ips')
+ ).filter_by(deleted=_deleted(context)
+ ).filter_by(user_id=user_id
+ ).all()
def instance_get_by_project(context, project_id):
session = get_session()
diff --git a/nova/tests/api/rackspace/servers.py b/nova/tests/api/rackspace/servers.py
index 0ef0f4256..674dab0b6 100644
--- a/nova/tests/api/rackspace/servers.py
+++ b/nova/tests/api/rackspace/servers.py
@@ -32,12 +32,14 @@ FLAGS = flags.FLAGS
def return_server(context, id):
return stub_instance(id)
-def return_servers(context):
- return [stub_instance(i) for i in xrange(5)]
+def return_servers(context, user_id=1):
+ return [stub_instance(i, user_id) for i in xrange(5)]
-def stub_instance(id):
+
+def stub_instance(id, user_id=1):
return Instance(
- id=id, state=0, image_id=10, server_name='server%s'%id
+ id=id, state=0, image_id=10, server_name='server%s'%id,
+ user_id=user_id
)
class ServersTest(unittest.TestCase):
@@ -50,6 +52,8 @@ class ServersTest(unittest.TestCase):
test_helper.stub_out_auth(self.stubs)
self.stubs.Set(nova.db.api, 'instance_get_all', return_servers)
self.stubs.Set(nova.db.api, 'instance_get', return_server)
+ self.stubs.Set(nova.db.api, 'instance_get_all_by_user',
+ return_servers)
def tearDown(self):
self.stubs.UnsetAll()