Merge lp:~rvb/maas/add-fqdn into lp:~maas-committers/maas/trunk

Proposed by Raphaël Badin
Status: Merged
Approved by: Raphaël Badin
Approved revision: no longer in the source branch.
Merged at revision: 1317
Proposed branch: lp:~rvb/maas/add-fqdn
Merge into: lp:~maas-committers/maas/trunk
Prerequisite: lp:~rvb/maas/bug-1070765-hostname
Diff against target: 86 lines (+50/-1)
2 files modified
src/maasserver/models/node.py (+24/-1)
src/maasserver/tests/test_node.py (+26/-0)
To merge this branch: bzr merge lp:~rvb/maas/add-fqdn
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) Approve
Review via email: mp+131540@code.launchpad.net

Commit message

Add a fqdn property on nodes.

Description of the change

Add a fqdn property on nodes.

To post a comment you must log in.
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

No comment.

review: Approve
Revision history for this message
Raphaël Badin (rvb) wrote :

According to the new strategy the two of us just talked about, I just changed the 'fqdn' property:
- if the nodegroup manages DNS: strip the domain part out of the hostname (if any) and then use nodegroup.name as the domain
- if the nodegroup does not mange DNS: just return whatever is in the 'hostname' field.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/maasserver/models/node.py'
--- src/maasserver/models/node.py 2012-10-19 13:55:51 +0000
+++ src/maasserver/models/node.py 2012-10-29 09:58:22 +0000
@@ -63,7 +63,10 @@
63from maasserver.models.dhcplease import DHCPLease63from maasserver.models.dhcplease import DHCPLease
64from maasserver.models.tag import Tag64from maasserver.models.tag import Tag
65from maasserver.models.timestampedmodel import TimestampedModel65from maasserver.models.timestampedmodel import TimestampedModel
66from maasserver.utils import get_db_state66from maasserver.utils import (
67 get_db_state,
68 strip_domain,
69 )
67from maasserver.utils.orm import get_first70from maasserver.utils.orm import get_first
68from piston.models import Token71from piston.models import Token
69from provisioningserver.enum import (72from provisioningserver.enum import (
@@ -491,6 +494,26 @@
491 else:494 else:
492 return self.system_id495 return self.system_id
493496
497 @property
498 def fqdn(self):
499 """Fully qualified domain name for this node.
500
501 If MAAS manages DNS for this node, the domain part of the
502 hostname (if present), is replaced by the domain configured
503 on the cluster controller.
504 If not, simply return the node's hostname.
505 """
506 # Avoid circular imports.
507 from maasserver.dns import is_dns_managed
508 if is_dns_managed(self.nodegroup):
509 # If the hostname field contains a domain, strip it.
510 hostname = strip_domain(self.hostname)
511 # Build the FQDN by using the hostname and nodegroup.name
512 # as the domain name.
513 return '%s.%s' % (hostname, self.nodegroup.name)
514 else:
515 return self.hostname
516
494 def tag_names(self):517 def tag_names(self):
495 # We don't use self.tags.values_list here because this does not518 # We don't use self.tags.values_list here because this does not
496 # take advantage of the cache.519 # take advantage of the cache.
497520
=== modified file 'src/maasserver/tests/test_node.py'
--- src/maasserver/tests/test_node.py 2012-10-19 13:55:51 +0000
+++ src/maasserver/tests/test_node.py 2012-10-29 09:58:22 +0000
@@ -26,6 +26,8 @@
26 NODE_STATUS,26 NODE_STATUS,
27 NODE_STATUS_CHOICES,27 NODE_STATUS_CHOICES,
28 NODE_STATUS_CHOICES_DICT,28 NODE_STATUS_CHOICES_DICT,
29 NODEGROUP_STATUS,
30 NODEGROUPINTERFACE_MANAGEMENT,
29 )31 )
30from maasserver.exceptions import NodeStateViolation32from maasserver.exceptions import NodeStateViolation
31from maasserver.models import (33from maasserver.models import (
@@ -570,6 +572,30 @@
570 node = reload_object(node)572 node = reload_object(node)
571 self.assertEqual([], list(node.tags.all()))573 self.assertEqual([], list(node.tags.all()))
572574
575 def test_fqdn_returns_hostname_if_dns_not_managed(self):
576 nodegroup = factory.make_node_group(
577 name=factory.getRandomString(),
578 management=NODEGROUPINTERFACE_MANAGEMENT.DHCP)
579 hostname_with_domain = '%s.%s' % (
580 factory.getRandomString(), factory.getRandomString())
581 node = factory.make_node(
582 nodegroup=nodegroup, hostname=hostname_with_domain)
583 self.assertEqual(hostname_with_domain, node.fqdn)
584
585 def test_fqdn_replaces_hostname_if_dns_is_managed(self):
586 hostname_without_domain = factory.make_name('hostname')
587 hostname_with_domain = '%s.%s' % (
588 hostname_without_domain, factory.getRandomString())
589 domain = factory.make_name('domain')
590 nodegroup = factory.make_node_group(
591 status=NODEGROUP_STATUS.ACCEPTED,
592 name=domain,
593 management=NODEGROUPINTERFACE_MANAGEMENT.DHCP_AND_DNS)
594 node = factory.make_node(
595 hostname=hostname_with_domain, nodegroup=nodegroup)
596 expected_hostname = '%s.%s' % (hostname_without_domain, domain)
597 self.assertEqual(expected_hostname, node.fqdn)
598
573599
574class NodeTransitionsTests(TestCase):600class NodeTransitionsTests(TestCase):
575 """Test the structure of NODE_TRANSITIONS."""601 """Test the structure of NODE_TRANSITIONS."""