← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~julian-edwards/maas/api_get_allocated into lp:maas

 

Julian Edwards has proposed merging lp:~julian-edwards/maas/api_get_allocated into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~julian-edwards/maas/api_get_allocated/+merge/98617

Add a get_allocated API call that will return nodes that are allocated to the requesting user *using the same Oauth key*.  This is to provide functionality that Juju needs to work correctly.

It does this by:
 * adding a token column to Node (FK to Token) with a DB migration
 * the existing acquire API call adds the user's current token data to the node that it returns
 * the existing release API call removes the token from the node

I am too tired to concentrate further at this point so I'm not sure the test coverage is great, please see if I could improve it.


-- 
https://code.launchpad.net/~julian-edwards/maas/api_get_allocated/+merge/98617
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~julian-edwards/maas/api_get_allocated into lp:maas.
=== modified file 'src/maasserver/api.py'
--- src/maasserver/api.py	2012-03-21 05:56:36 +0000
+++ src/maasserver/api.py	2012-03-21 12:14:21 +0000
@@ -64,6 +64,7 @@
     BaseHandler,
     HandlerMetaClass,
     )
+from piston.models import Token
 from piston.resource import Resource
 from piston.utils import rc
 
@@ -399,13 +400,29 @@
         nodes = Node.objects.get_visible_nodes(request.user, ids=match_ids)
         return nodes.order_by('id')
 
+    @api_exported('list_allocated', 'GET')
+    def list_allocated(self, request):
+        """Fetch Nodes that were allocated to the User/oauth token."""
+        auth_header = request.META.get("HTTP_AUTHORIZATION")
+        assert auth_header is not None, (
+            "HTTP_AUTHORIZATION not set on request")
+        key = extract_oauth_key(auth_header)
+        token = Token.objects.get(key=key)
+        nodes = Node.objects.get_allocated_visible_nodes(request.user, token)
+        return nodes.order_by('id')
+
     @api_exported('acquire', 'POST')
     def acquire(self, request):
         """Acquire an available node for deployment."""
         node = Node.objects.get_available_node_for_acquisition(request.user)
         if node is None:
             raise NodesNotAvailable("No node is available.")
-        node.acquire(request.user)
+        auth_header = request.META.get("HTTP_AUTHORIZATION")
+        assert auth_header is not None, (
+            "HTTP_AUTHORIZATION not set on request")
+        key = extract_oauth_key(auth_header)
+        token = Token.objects.get(key=key)
+        node.acquire(request.user, token)
         node.save()
         return node
 

=== added file 'src/maasserver/migrations/0002_add_token_to_node.py'
--- src/maasserver/migrations/0002_add_token_to_node.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/migrations/0002_add_token_to_node.py	2012-03-21 12:14:21 +0000
@@ -0,0 +1,133 @@
+# flake8: noqa
+# SKIP this file when reformatting.
+# The rest of this file was generated by South.
+
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding field 'Node.token'
+        db.add_column('maasserver_node', 'token', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['piston.Token'], null=True), keep_default=False)
+
+
+    def backwards(self, orm):
+        
+        # Deleting field 'Node.token'
+        db.delete_column('maasserver_node', 'token_id')
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'maasserver.config': {
+            'Meta': {'object_name': 'Config'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'value': ('maasserver.fields.JSONObjectField', [], {'null': 'True'})
+        },
+        'maasserver.filestorage': {
+            'Meta': {'object_name': 'FileStorage'},
+            'data': ('django.db.models.fields.files.FileField', [], {'max_length': '255'}),
+            'filename': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'maasserver.macaddress': {
+            'Meta': {'object_name': 'MACAddress'},
+            'created': ('django.db.models.fields.DateField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mac_address': ('maasserver.fields.MACAddressField', [], {}),
+            'node': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['maasserver.Node']"}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        'maasserver.node': {
+            'Meta': {'object_name': 'Node'},
+            'after_commissioning_action': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'architecture': ('django.db.models.fields.CharField', [], {'default': "u'i386'", 'max_length': '10'}),
+            'created': ('django.db.models.fields.DateField', [], {}),
+            'hostname': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '255', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'owner': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+            'power_type': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '10', 'blank': 'True'}),
+            'status': ('django.db.models.fields.IntegerField', [], {'default': '0', 'max_length': '10'}),
+            'system_id': ('django.db.models.fields.CharField', [], {'default': "u'node-b2aa2496-7323-11e1-b1d8-0026c71eea0e'", 'unique': 'True', 'max_length': '41'}),
+            'token': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['piston.Token']", 'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        'maasserver.sshkeys': {
+            'Meta': {'object_name': 'SSHKeys'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'key': ('django.db.models.fields.TextField', [], {}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['maasserver.UserProfile']"})
+        },
+        'maasserver.userprofile': {
+            'Meta': {'object_name': 'UserProfile'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'})
+        },
+        'piston.consumer': {
+            'Meta': {'object_name': 'Consumer'},
+            'description': ('django.db.models.fields.TextField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'key': ('django.db.models.fields.CharField', [], {'max_length': '18'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'secret': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'status': ('django.db.models.fields.CharField', [], {'default': "'pending'", 'max_length': '16'}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'consumers'", 'null': 'True', 'to': "orm['auth.User']"})
+        },
+        'piston.token': {
+            'Meta': {'object_name': 'Token'},
+            'callback': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'callback_confirmed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'consumer': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['piston.Consumer']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_approved': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'key': ('django.db.models.fields.CharField', [], {'max_length': '18'}),
+            'secret': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'timestamp': ('django.db.models.fields.IntegerField', [], {'default': '1332313295L'}),
+            'token_type': ('django.db.models.fields.IntegerField', [], {}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'tokens'", 'null': 'True', 'to': "orm['auth.User']"}),
+            'verifier': ('django.db.models.fields.CharField', [], {'max_length': '10'})
+        }
+    }
+
+    complete_apps = ['maasserver']

=== modified file 'src/maasserver/models.py'
--- src/maasserver/models.py	2012-03-21 07:30:59 +0000
+++ src/maasserver/models.py	2012-03-21 12:14:21 +0000
@@ -224,6 +224,21 @@
                 models.Q(owner__isnull=True) | models.Q(owner=user))
         return self.filter_by_ids(visible_nodes, ids)
 
+    def get_allocated_visible_nodes(self, user, token):
+        """Fetch Nodes that were allocated to the User_/oauth token.
+
+        :param user: The user whose nodes to fetch
+        :type user: User_
+        :param token: The OAuth token associated with the Nodes.
+        :type token: piston.models.Token.
+
+        .. _User: https://
+           docs.djangoproject.com/en/dev/topics/auth/
+           #django.contrib.auth.models.User
+        """
+        nodes = self.filter(owner=user, token=token)
+        return nodes
+
     def get_editable_nodes(self, user, ids=None):
         """Fetch Nodes a User_ has ownership privileges on.
 
@@ -368,6 +383,9 @@
         max_length=10, choices=POWER_TYPE_CHOICES, null=False, blank=True,
         default=POWER_TYPE.DEFAULT)
 
+    token = models.ForeignKey(
+        Token, db_index=True, null=True, editable=False, unique=False)
+
     objects = NodeManager()
 
     def __unicode__(self):
@@ -429,12 +447,14 @@
             power_type = self.power_type
         return power_type
 
-    def acquire(self, by_user):
-        """Mark commissioned node as acquired by the given user."""
+    def acquire(self, by_user, token=None):
+        """Mark commissioned node as acquired by the given user and token."""
         assert self.status == NODE_STATUS.READY
         assert self.owner is None
         self.status = NODE_STATUS.ALLOCATED
         self.owner = by_user
+        if token is not None:
+            self.token = token
 
     def release(self):
         """Mark allocated or reserved node as available again."""
@@ -445,6 +465,7 @@
             ]
         self.status = NODE_STATUS.READY
         self.owner = None
+        self.token = None
 
 
 mac_re = re.compile(r'^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$')

=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py	2012-03-21 05:56:36 +0000
+++ src/maasserver/tests/test_api.py	2012-03-21 12:14:21 +0000
@@ -22,6 +22,7 @@
 from maasserver.models import (
     ARCHITECTURE_CHOICES,
     Config,
+    create_auth_token,
     MACAddress,
     Node,
     NODE_STATUS,
@@ -393,6 +394,18 @@
             [NODE_STATUS.READY] * len(owned_nodes),
             [node.status for node in reload_objects(Node, owned_nodes)])
 
+    def test_POST_release_removes_token_and_user(self):
+        node = factory.make_node(status=NODE_STATUS.READY)
+        self.client.post(self.get_uri('nodes/'), {'op': 'acquire'})
+        node = Node.objects.get(system_id=node.system_id)
+        self.assertEqual(NODE_STATUS.ALLOCATED, node.status)
+        self.assertEqual(self.logged_in_user, node.owner)
+        self.assertEqual(self.client.token.key, node.token.key)
+        self.client.post(self.get_node_uri(node), {'op': 'release'})
+        node = Node.objects.get(system_id=node.system_id)
+        self.assertIs(None, node.owner)
+        self.assertIs(None, node.token)
+
     def test_POST_release_does_nothing_for_unowned_node(self):
         node = factory.make_node(status=NODE_STATUS.READY)
         response = self.client.post(
@@ -463,7 +476,7 @@
         self.assertEqual(NODE_STATUS.READY, reload_object(node).status)
 
     def test_PUT_updates_node(self):
-        # The api allows to update a Node.
+        # The api allows the updating of a Node.
         node = factory.make_node(hostname='diane')
         response = self.client.put(
             self.get_node_uri(node), {'hostname': 'francis'})
@@ -640,7 +653,27 @@
         self.assertItemsEqual(
             [existing_id], extract_system_ids(parsed_result))
 
-    def test_POST_returns_available_node(self):
+    def test_GET_list_allocated_returns_only_allocated_with_user_token(self):
+        # If the user's allocated nodes have different session tokens,
+        # list_allocated should only return the nodes that have the
+        # current request's token on them.
+        available_status = NODE_STATUS.READY
+        node_1 = factory.make_node(status=available_status, owner=None)
+        self.client.post(self.get_uri('nodes/'), {'op': 'acquire'})
+        user_2 = factory.make_user()
+        token = create_auth_token(user_2)
+        factory.make_node(
+            owner=self.logged_in_user, status=NODE_STATUS.ALLOCATED,
+            token=token)
+
+        response = self.client.get(self.get_uri('nodes/'), {
+            'op': 'list_allocated'})
+        self.assertEqual(httplib.OK, response.status_code)
+        parsed_result = json.loads(response.content)
+        self.assertItemsEqual(
+            [node_1.system_id], extract_system_ids(parsed_result))
+
+    def test_POST_acquire_returns_available_node(self):
         # The "acquire" operation returns an available node.
         available_status = NODE_STATUS.READY
         node = factory.make_node(status=available_status, owner=None)
@@ -664,6 +697,16 @@
         # Fails with Conflict error: resource can't satisfy request.
         self.assertEqual(httplib.CONFLICT, response.status_code)
 
+    def test_POST_acquire_sets_a_token(self):
+        # "acquire" should set the Token being used in the request on
+        # the Node that is allocated.
+        available_status = NODE_STATUS.READY
+        node = factory.make_node(status=available_status, owner=None)
+        self.client.post(self.get_uri('nodes/'), {'op': 'acquire'})
+        node = Node.objects.get(system_id=node.system_id)
+        oauth_key = self.client.token.key
+        self.assertEqual(oauth_key, node.token.key)
+
 
 class MACAddressAPITest(APITestCase):
 

=== modified file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py	2012-03-19 15:41:55 +0000
+++ src/maasserver/tests/test_models.py	2012-03-21 12:14:21 +0000
@@ -74,6 +74,12 @@
             NODE_STATUS_CHOICES_DICT[NODE_STATUS.DECLARED],
             node.display_status())
 
+    def test_add_node_with_token(self):
+        user = factory.make_user()
+        token = create_auth_token(user)
+        node = factory.make_node(token=token)
+        self.assertEqual(token, node.token)
+
     def test_add_mac_address(self):
         node = factory.make_node()
         node.add_mac_address('AA:BB:CC:DD:EE:FF')