← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/zone-model into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/zone-model into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/zone-model/+merge/112603

This branch is the first branch towards adding DNS support in MAAS.

It simply adds a new model Zone, linked (1<->1 relashionship) with NodeGroup.  It's will store DNS zones' information, currently only the zone serial.

= Pre-imp =

Discussed this Julian and Daviey.

= Notes =

- The usual format for a DNS zone's serial is "YYYYMMDDNN" where NN an integer starting at 0 and which gets incremented each time the file is modified.  Since we're going to modify the zone file in an automated fashion I thought this wasn't appropriate.  Instead, the serial is a simple counter which uses every single possibility an unsigned 32-bit integer offers.

- Maybe we will simply need one new field for this: the serial.  But I thought it was cleaner to have a separate model for this.

- I've changed make_node_group to be able to get a NodeGroup with random values.

- Drive-by fix: I've updated HACKING.txt to reflect the new organisation of the model files.
-- 
https://code.launchpad.net/~rvb/maas/zone-model/+merge/112603
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/zone-model into lp:maas.
=== modified file 'HACKING.txt'
--- HACKING.txt	2012-06-15 15:31:42 +0000
+++ HACKING.txt	2012-06-28 16:33:25 +0000
@@ -336,14 +336,17 @@
 Changing the schema
 ^^^^^^^^^^^^^^^^^^^
 
-Once you've made a change to ``src/<application>/models.py`` you have to run
-South's `schemamigration`_ command to create a migration file that will be
-stored in ``src/<application>/migrations/``.
+Once you've made a model change (i.e. a change to a file in
+``src/<application>/models/*.py``) you have to run South's `schemamigration`_
+command to create a migration file that will be stored in
+``src/<application>/migrations/``.
+
+Note that if you want to add a new model class you'll need to import it
+in ``src/<application>/models/__init__.py``
 
 .. _schemamigration: http://south.aeracode.org/docs/commands.html#schemamigration
 
-For instance if you're making changes to ``src/maasserver/models.py``, you
-will need to run::
+Once you've changed the code, will need to run::
 
     $ ./bin/maas schemamigration maasserver --auto description_of_the_change
 

=== added file 'src/maasserver/migrations/0011_add_zone.py'
--- src/maasserver/migrations/0011_add_zone.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/migrations/0011_add_zone.py	2012-06-28 16:33:25 +0000
@@ -0,0 +1,170 @@
+# flake8: noqa
+# SKIP this file when reformatting.
+# The rest of this file was generated by South.
+
+# encoding: utf-8
+import datetime
+
+from django.db import models
+from south.db import db
+from south.v2 import SchemaMigration
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding model 'Zone'
+        db.create_table(u'maasserver_zone', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('created', self.gf('django.db.models.fields.DateTimeField')()),
+            ('updated', self.gf('django.db.models.fields.DateTimeField')()),
+            ('node_group', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['maasserver.NodeGroup'], unique=True)),
+            ('_serial_number', self.gf('django.db.models.fields.IntegerField')(default=-2147483648)),
+        ))
+        db.send_create_signal(u'maasserver', ['Zone'])
+
+
+    def backwards(self, orm):
+        
+        # Deleting model 'Zone'
+        db.delete_table(u'maasserver_zone')
+
+
+    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', [], {'unique': 'True', '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'})
+        },
+        u'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'})
+        },
+        u'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': '200'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        u'maasserver.macaddress': {
+            'Meta': {'object_name': 'MACAddress'},
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mac_address': ('maasserver.fields.MACAddressField', [], {'unique': 'True'}),
+            'node': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['maasserver.Node']"}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        u'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.DateTimeField', [], {}),
+            'error': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '255', 'blank': 'True'}),
+            'hostname': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '255', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'netboot': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'owner': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+            'power_parameters': ('maasserver.fields.JSONObjectField', [], {'default': "u''", '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-2cb3544a-c137-11e1-9f67-00219bd0a2de'", 'unique': 'True', 'max_length': '41'}),
+            'token': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['piston.Token']", 'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        u'maasserver.nodegroup': {
+            'Meta': {'object_name': 'NodeGroup'},
+            'api_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '18'}),
+            'api_token': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['piston.Token']", 'unique': 'True'}),
+            'broadcast_ip': ('django.db.models.fields.IPAddressField', [], {'max_length': '15'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ip_range_high': ('django.db.models.fields.IPAddressField', [], {'unique': 'True', 'max_length': '15'}),
+            'ip_range_low': ('django.db.models.fields.IPAddressField', [], {'unique': 'True', 'max_length': '15'}),
+            'name': ('django.db.models.fields.CharField', [], {'default': "u''", 'unique': 'True', 'max_length': '80'}),
+            'router_ip': ('django.db.models.fields.IPAddressField', [], {'max_length': '15'}),
+            'subnet_mask': ('django.db.models.fields.IPAddressField', [], {'max_length': '15'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {}),
+            'worker_ip': ('django.db.models.fields.IPAddressField', [], {'unique': 'True', 'max_length': '15'})
+        },
+        u'maasserver.sshkey': {
+            'Meta': {'unique_together': "((u'user', u'key'),)", 'object_name': 'SSHKey'},
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'key': ('django.db.models.fields.TextField', [], {}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+        },
+        u'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'})
+        },
+        u'maasserver.zone': {
+            'Meta': {'object_name': 'Zone'},
+            '_serial_number': ('django.db.models.fields.IntegerField', [], {'default': '-2147483648'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'node_group': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['maasserver.NodeGroup']", 'unique': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        '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': '1340897851L'}),
+            '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/__init__.py'
--- src/maasserver/models/__init__.py	2012-06-15 05:07:40 +0000
+++ src/maasserver/models/__init__.py	2012-06-28 16:33:25 +0000
@@ -19,6 +19,7 @@
     'NodeGroup',
     'SSHKey',
     'UserProfile',
+    'Zone',
     ]
 
 from logging import getLogger
@@ -36,6 +37,7 @@
 from maasserver.models.sshkey import SSHKey
 from maasserver.models.user import create_user
 from maasserver.models.userprofile import UserProfile
+from maasserver.models.zone import Zone
 from maasserver.utils import ignore_unused
 from piston.models import Consumer
 
@@ -45,7 +47,8 @@
 
 # Suppress warning about symbols being imported, but only used for
 # export in __all__.
-ignore_unused(Config, FileStorage, MACAddress, NodeGroup, SSHKey, UserProfile)
+ignore_unused(
+    Config, FileStorage, MACAddress, NodeGroup, SSHKey, UserProfile, Zone)
 
 
 # Connect the 'create_user' method to the post save signal of User.

=== added file 'src/maasserver/models/zone.py'
--- src/maasserver/models/zone.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/models/zone.py	2012-06-28 16:33:25 +0000
@@ -0,0 +1,69 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""DNS zone."""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = [
+    'MAX_SERIAL',
+    'Zone',
+    ]
+
+
+from django.db.models import (
+    IntegerField,
+    OneToOneField,
+    )
+from maasserver import DefaultMeta
+from maasserver.models.cleansave import CleanSave
+from maasserver.models.nodegroup import NodeGroup
+from maasserver.models.timestampedmodel import TimestampedModel
+
+
+LIMIT_SERIAL = 2 ** 32
+
+MAX_SERIAL = LIMIT_SERIAL - 1
+
+LIMIT_SIGNED_INT = 2 ** 31
+
+
+class Zone(CleanSave, TimestampedModel):
+    """A DNS zone.
+
+    :ivar node_group: The associated NodeGroup.
+    :type node_group: class:`NodeGroup`
+    :ivar serial_number: The serial number (32-bit auto-incremented integer).
+    :type serial_number: int
+    """
+
+    class Meta(DefaultMeta):
+        """Needed for South to recognize this model."""
+
+    node_group = OneToOneField(NodeGroup, null=False, unique=True)
+    _serial_number = IntegerField(default=-LIMIT_SIGNED_INT)
+
+    def __unicode__(self):
+        return "nodegroup: %s / serial: %s" % (
+            self.node_group.id, self.serial)
+
+    def increment_serial_number(self):
+        self.serial_number = (self.serial_number + 1) % LIMIT_SERIAL
+        self.save()
+
+    def _get_serial_number(self):
+        return self._serial_number + LIMIT_SIGNED_INT
+
+    def _set_serial_number(self, new_serial_number):
+        self._serial_number = new_serial_number - LIMIT_SIGNED_INT
+
+    serial_number = property(_get_serial_number, _set_serial_number)
+
+    @property
+    def serial(self):
+        return '%0.10d' % self.serial_number

=== modified file 'src/maasserver/testing/factory.py'
--- src/maasserver/testing/factory.py	2012-06-15 05:07:40 +0000
+++ src/maasserver/testing/factory.py	2012-06-28 16:33:25 +0000
@@ -107,11 +107,29 @@
             Node.objects.filter(id=node.id).update(created=created)
         return reload_object(node)
 
-    def make_node_group(self, api_token=None, **kwargs):
+    def make_node_group(self, api_token=None, worker_ip=None,
+                        subnet_mask=None, broadcast_ip=None, router_ip=None,
+                        ip_range_low=None, ip_range_high=None, **kwargs):
         if api_token is None:
             user = self.make_user()
             api_token = create_auth_token(user)
-        ng = NodeGroup(api_token=api_token, api_key=api_token.key, **kwargs)
+        if worker_ip is None:
+            worker_ip = factory.getRandomIPAddress()
+        if subnet_mask is None:
+            subnet_mask = factory.getRandomIPAddress()
+        if broadcast_ip is None:
+            broadcast_ip = factory.getRandomIPAddress()
+        if router_ip is None:
+            router_ip = factory.getRandomIPAddress()
+        if ip_range_low is None:
+            ip_range_low = factory.getRandomIPAddress()
+        if ip_range_high is None:
+            ip_range_high = factory.getRandomIPAddress()
+        ng = NodeGroup(
+            api_token=api_token, api_key=api_token.key, worker_ip=worker_ip,
+            subnet_mask=subnet_mask, broadcast_ip=broadcast_ip,
+            router_ip=router_ip, ip_range_low=ip_range_low,
+            ip_range_high=ip_range_high, **kwargs)
         ng.save()
         return ng
 

=== added file 'src/maasserver/tests/test_zone.py'
--- src/maasserver/tests/test_zone.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/tests/test_zone.py	2012-06-28 16:33:25 +0000
@@ -0,0 +1,53 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Tests for the `Zone` class."""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = []
+
+
+import random
+
+from maasserver.models.zone import (
+    MAX_SERIAL,
+    Zone,
+    )
+from maasserver.testing.factory import factory
+from maasserver.testing.testcase import TestCase
+
+
+class ZoneTest(TestCase):
+    """Testing of the :class:`Zone` model."""
+
+    def test_serial_defaults_to_zero(self):
+        node_group = factory.make_node_group()
+        zone = Zone(node_group=node_group)
+        self.assertEqual(
+            (0, '0' * len(str(MAX_SERIAL))),
+            (zone.serial_number, zone.serial))
+
+    def test_increment_serial_number(self):
+        node_group = factory.make_node_group()
+        zone = Zone(node_group=node_group)
+        zone.increment_serial_number()
+        self.assertEqual(zone.serial_number, 1)
+
+    def test_serial_number_wraps_at_MAX_SERIAL(self):
+        node_group = factory.make_node_group()
+        zone = Zone(node_group=node_group)
+        zone.serial_number = MAX_SERIAL
+        zone.increment_serial_number()
+        self.assertEqual(zone.serial_number, 0)
+
+    def test_serial_display(self):
+        node_group = factory.make_node_group()
+        zone = Zone(node_group=node_group)
+        zone.serial_number = random.randint(0, MAX_SERIAL)
+        self.assertEqual(len(str(MAX_SERIAL)), len(zone.serial))

=== modified file 'src/maastesting/factory.py'
--- src/maastesting/factory.py	2012-06-25 09:03:14 +0000
+++ src/maastesting/factory.py	2012-06-28 16:33:25 +0000
@@ -62,6 +62,10 @@
         assert port_min >= 0 and port_max <= 65535
         return random.randint(port_min, port_max)
 
+    def getRandomIPAddress(self):
+        octets = islice(self.random_octets, 4)
+        return b'.'.join(map(str, octets))
+
     def getRandomMACAddress(self):
         octets = islice(self.random_octets, 6)
         return b":".join(format(octet, b"02x") for octet in octets)

=== modified file 'src/maastesting/tests/test_factory.py'
--- src/maastesting/tests/test_factory.py	2012-06-25 13:01:41 +0000
+++ src/maastesting/tests/test_factory.py	2012-06-28 16:33:25 +0000
@@ -41,6 +41,14 @@
     def test_getRandomPort_returns_int(self):
         self.assertIsInstance(factory.getRandomPort(), int)
 
+    def test_getRandomIPAddress(self):
+        ip_address = factory.getRandomIPAddress()
+        self.assertIsInstance(ip_address, str)
+        octets = ip_address.split('.')
+        self.assertEqual(4, len(octets))
+        for octet in octets:
+            self.assertTrue(0 <= int(octet) <= 255)
+
     def test_getRandomDate_returns_datetime(self):
         self.assertIsInstance(factory.getRandomDate(), datetime)