Merge lp:~blake-rouse/maas/node-effective-license-key into lp:~maas-committers/maas/trunk

Proposed by Blake Rouse
Status: Merged
Approved by: Blake Rouse
Approved revision: no longer in the source branch.
Merged at revision: 2486
Proposed branch: lp:~blake-rouse/maas/node-effective-license-key
Merge into: lp:~maas-committers/maas/trunk
Prerequisite: lp:~blake-rouse/maas/global-licensekey-model
Diff against target: 105 lines (+48/-1)
4 files modified
src/maasserver/models/node.py (+27/-0)
src/maasserver/models/tests/test_node.py (+19/-0)
src/maasserver/preseed.py (+1/-0)
src/maasserver/tests/test_preseed.py (+1/-1)
To merge this branch: bzr merge lp:~blake-rouse/maas/node-effective-license-key
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) Approve
Review via email: mp+224354@code.launchpad.net

Commit message

Adds the effective license key into the node context for the preseed. Allows preseed templates to access the effective license key .

Description of the change

This is the second part of a series of changes to allow a global registry of license key's in MAAS. The end result will allow a user to set a license key for each operating system and series globally. When a booting operating system and series requires a license key, first the node will be check to see if a specific key has been set in the license_key field. If not then this LicenseKey model will be used to see if one is set globally for the selected operating system and series.

This branch includes the logic to return the correct license key for the preseed. get_effective_license_key contains the logic to retrieve the global license key if a license key is not set on the node.

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

Short and sweet. The explanation of what it means to get an "effective" licence key would go nicely in the docstring for get_effectivel_license_key.

Out of interest, why the calls to both LicenseKey.objects.has_license_key and LicenseKey.objects.get_license_key? Depending on the semantics, they sound a bit as if the combination is effectively one operation on LicenseKey.objects.

review: Approve
Revision history for this message
Blake Rouse (blake-rouse) wrote :

At the time it was to make the code read easier, but looking back it doesn't. So I have changed it to just one call with a catch on DoesNotExist.

Updated the docstring as well.

Thanks.

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 2014-06-26 05:01:45 +0000
3+++ src/maasserver/models/node.py 2014-06-27 11:36:23 +0000
4@@ -68,6 +68,7 @@
5 from maasserver.models.cleansave import CleanSave
6 from maasserver.models.config import Config
7 from maasserver.models.dhcplease import DHCPLease
8+from maasserver.models.licensekey import LicenseKey
9 from maasserver.models.staticipaddress import (
10 StaticIPAddress,
11 StaticIPAddressExhaustion,
12@@ -1046,6 +1047,32 @@
13 else:
14 return self.distro_series
15
16+ def get_effective_license_key(self):
17+ """Return effective license key.
18+
19+ This returns the license key that should be used during the
20+ installation of the operating system for this node. This method first
21+ checks to see if the node has a specific license key set, if not then
22+ the license key registry is checked, if neither exists for this node or
23+ the booting operating system and release combination then an empty
24+ string is returned. An empty string can mean two things, one the
25+ operating system does not require a license key, or the installation
26+ media already has the license key builtin.
27+ """
28+ use_global_license_key = (
29+ self.license_key is None or
30+ self.license_key == '')
31+ if use_global_license_key:
32+ osystem = self.get_osystem()
33+ distro_series = self.get_distro_series()
34+ try:
35+ return LicenseKey.objects.get_license_key(
36+ osystem, distro_series)
37+ except LicenseKey.DoesNotExist:
38+ return ''
39+ else:
40+ return self.license_key
41+
42 def get_effective_power_parameters(self):
43 """Return effective power parameters, including any defaults."""
44 if self.power_parameters:
45
46=== modified file 'src/maasserver/models/tests/test_node.py'
47--- src/maasserver/models/tests/test_node.py 2014-06-27 02:05:14 +0000
48+++ src/maasserver/models/tests/test_node.py 2014-06-27 11:36:23 +0000
49@@ -37,6 +37,7 @@
50 from maasserver.fields import MAC
51 from maasserver.models import (
52 Config,
53+ LicenseKey,
54 MACAddress,
55 Node,
56 node as node_module,
57@@ -296,6 +297,24 @@
58 series = Config.objects.get_config('default_distro_series')
59 self.assertEqual(series, node.get_distro_series())
60
61+ def test_get_effective_license_key_returns_node_value(self):
62+ license_key = factory.make_name('license_key')
63+ node = factory.make_node(license_key=license_key)
64+ self.assertEqual(license_key, node.get_effective_license_key())
65+
66+ def test_get_effective_license_key_returns_blank(self):
67+ node = factory.make_node()
68+ self.assertEqual('', node.get_effective_license_key())
69+
70+ def test_get_effective_license_key_returns_global(self):
71+ license_key = factory.make_name('license_key')
72+ osystem = factory.make_name('os')
73+ series = factory.make_name('series')
74+ LicenseKey.objects.create(
75+ osystem=osystem, distro_series=series, license_key=license_key)
76+ node = factory.make_node(osystem=osystem, distro_series=series)
77+ self.assertEqual(license_key, node.get_effective_license_key())
78+
79 def test_delete_node_deletes_related_mac(self):
80 node = factory.make_node()
81 mac = node.add_mac_address('AA:BB:CC:DD:EE:FF')
82
83=== modified file 'src/maasserver/preseed.py'
84--- src/maasserver/preseed.py 2014-06-25 16:29:11 +0000
85+++ src/maasserver/preseed.py 2014-06-27 11:36:23 +0000
86@@ -453,6 +453,7 @@
87 'preseed_data': compose_preseed(get_preseed_type_for(node), node),
88 'node_disable_pxe_url': node_disable_pxe_url,
89 'node_disable_pxe_data': node_disable_pxe_data,
90+ 'license_key': node.get_effective_license_key(),
91 }
92
93
94
95=== modified file 'src/maasserver/tests/test_preseed.py'
96--- src/maasserver/tests/test_preseed.py 2014-06-25 16:49:53 +0000
97+++ src/maasserver/tests/test_preseed.py 2014-06-27 11:36:23 +0000
98@@ -499,7 +499,7 @@
99 self.assertItemsEqual(
100 ['driver', 'driver_package', 'node',
101 'node_disable_pxe_data', 'node_disable_pxe_url',
102- 'preseed_data', 'third_party_drivers',
103+ 'preseed_data', 'third_party_drivers', 'license_key',
104 ],
105 context)
106