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

Proposed by Raphaël Badin on 2012-10-26
Status: Merged
Approved by: Raphaël Badin on 2012-10-30
Approved revision: 1304
Merged at revision: 1317
Proposed branch: lp:~rvb/maas/add-fqdn
Merge into: lp: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) 2012-10-26 Approve on 2012-10-26
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.
lp:~rvb/maas/add-fqdn updated on 2012-10-26
1301. By Raphaël Badin on 2012-10-26

Merged bug-1070765-hostname into add-fqdn.

Jeroen T. Vermeulen (jtv) wrote :

No comment.

review: Approve
lp:~rvb/maas/add-fqdn updated on 2012-10-26
1302. By Raphaël Badin on 2012-10-26

Fix fqdn.

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.

lp:~rvb/maas/add-fqdn updated on 2012-10-29
1303. By Raphaël Badin on 2012-10-29

Merged bug-1070765-hostname into add-fqdn.

1304. By Raphaël Badin on 2012-10-29

Merged bug-1070765-hostname into add-fqdn.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/node.py'
2--- src/maasserver/models/node.py 2012-10-19 13:55:51 +0000
3+++ src/maasserver/models/node.py 2012-10-29 09:58:22 +0000
4@@ -63,7 +63,10 @@
5 from maasserver.models.dhcplease import DHCPLease
6 from maasserver.models.tag import Tag
7 from maasserver.models.timestampedmodel import TimestampedModel
8-from maasserver.utils import get_db_state
9+from maasserver.utils import (
10+ get_db_state,
11+ strip_domain,
12+ )
13 from maasserver.utils.orm import get_first
14 from piston.models import Token
15 from provisioningserver.enum import (
16@@ -491,6 +494,26 @@
17 else:
18 return self.system_id
19
20+ @property
21+ def fqdn(self):
22+ """Fully qualified domain name for this node.
23+
24+ If MAAS manages DNS for this node, the domain part of the
25+ hostname (if present), is replaced by the domain configured
26+ on the cluster controller.
27+ If not, simply return the node's hostname.
28+ """
29+ # Avoid circular imports.
30+ from maasserver.dns import is_dns_managed
31+ if is_dns_managed(self.nodegroup):
32+ # If the hostname field contains a domain, strip it.
33+ hostname = strip_domain(self.hostname)
34+ # Build the FQDN by using the hostname and nodegroup.name
35+ # as the domain name.
36+ return '%s.%s' % (hostname, self.nodegroup.name)
37+ else:
38+ return self.hostname
39+
40 def tag_names(self):
41 # We don't use self.tags.values_list here because this does not
42 # take advantage of the cache.
43
44=== modified file 'src/maasserver/tests/test_node.py'
45--- src/maasserver/tests/test_node.py 2012-10-19 13:55:51 +0000
46+++ src/maasserver/tests/test_node.py 2012-10-29 09:58:22 +0000
47@@ -26,6 +26,8 @@
48 NODE_STATUS,
49 NODE_STATUS_CHOICES,
50 NODE_STATUS_CHOICES_DICT,
51+ NODEGROUP_STATUS,
52+ NODEGROUPINTERFACE_MANAGEMENT,
53 )
54 from maasserver.exceptions import NodeStateViolation
55 from maasserver.models import (
56@@ -570,6 +572,30 @@
57 node = reload_object(node)
58 self.assertEqual([], list(node.tags.all()))
59
60+ def test_fqdn_returns_hostname_if_dns_not_managed(self):
61+ nodegroup = factory.make_node_group(
62+ name=factory.getRandomString(),
63+ management=NODEGROUPINTERFACE_MANAGEMENT.DHCP)
64+ hostname_with_domain = '%s.%s' % (
65+ factory.getRandomString(), factory.getRandomString())
66+ node = factory.make_node(
67+ nodegroup=nodegroup, hostname=hostname_with_domain)
68+ self.assertEqual(hostname_with_domain, node.fqdn)
69+
70+ def test_fqdn_replaces_hostname_if_dns_is_managed(self):
71+ hostname_without_domain = factory.make_name('hostname')
72+ hostname_with_domain = '%s.%s' % (
73+ hostname_without_domain, factory.getRandomString())
74+ domain = factory.make_name('domain')
75+ nodegroup = factory.make_node_group(
76+ status=NODEGROUP_STATUS.ACCEPTED,
77+ name=domain,
78+ management=NODEGROUPINTERFACE_MANAGEMENT.DHCP_AND_DNS)
79+ node = factory.make_node(
80+ hostname=hostname_with_domain, nodegroup=nodegroup)
81+ expected_hostname = '%s.%s' % (hostname_without_domain, domain)
82+ self.assertEqual(expected_hostname, node.fqdn)
83+
84
85 class NodeTransitionsTests(TestCase):
86 """Test the structure of NODE_TRANSITIONS."""