Merge lp:~blake-rouse/maas/fix-1470591 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: 4147
Proposed branch: lp:~blake-rouse/maas/fix-1470591
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 178 lines (+126/-8)
3 files modified
src/maasserver/api/tests/test_maas.py (+75/-0)
src/maasserver/forms.py (+24/-2)
src/maasserver/forms_settings.py (+27/-6)
To merge this branch: bzr merge lp:~blake-rouse/maas/fix-1470591
Reviewer Review Type Date Requested Status
Ricardo Bánffy (community) Approve
Review via email: mp+266606@code.launchpad.net

Commit message

Fix setting the default_distro_series over the API.

To post a comment you must log in.
Revision history for this message
Ricardo Bánffy (rbanffy) wrote :

Looks OK. One typo and one naming suggestion.

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

Thanks for the review. Fixing the typo.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'src/maasserver/api/tests/test_maas.py'
--- src/maasserver/api/tests/test_maas.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/api/tests/test_maas.py 2015-07-31 23:12:48 +0000
@@ -0,0 +1,75 @@
1# Copyright 2013-2015 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).
3
4"""Tests for maas endpoint in the API."""
5
6from __future__ import (
7 absolute_import,
8 print_function,
9 unicode_literals,
10 )
11
12str = None
13
14__metaclass__ = type
15__all__ = []
16
17import httplib
18
19from django.core.urlresolvers import reverse
20from maasserver.models.config import Config
21from maasserver.testing.api import APITestCase
22from maasserver.testing.factory import factory
23from maasserver.testing.osystems import (
24 make_osystem_with_releases,
25 make_usable_osystem,
26 patch_usable_osystems,
27)
28
29
30class MAASHandlerAPITest(APITestCase):
31
32 def test_get_config_default_distro_series(self):
33 self.become_admin()
34 default_distro_series = factory.make_name("distro_series")
35 Config.objects.set_config(
36 "default_distro_series", default_distro_series)
37 response = self.client.get(
38 reverse('maas_handler'), {
39 "op": "get_config",
40 "name": "default_distro_series",
41 })
42 self.assertEquals(httplib.OK, response.status_code, response.content)
43 self.assertEquals('"%s"' % default_distro_series, response.content)
44
45 def test_set_config_default_distro_series(self):
46 self.become_admin()
47 osystem = make_usable_osystem(self)
48 Config.objects.set_config("default_osystem", osystem['name'])
49 selected_release = osystem['releases'][0]['name']
50 response = self.client.post(
51 reverse('maas_handler'), {
52 "op": "set_config",
53 "name": "default_distro_series",
54 "value": selected_release,
55 })
56 self.assertEquals(httplib.OK, response.status_code, response.content)
57 self.assertEquals(
58 selected_release,
59 Config.objects.get_config("default_distro_series"))
60
61 def test_set_config_only_default_osystem_are_valid_for_distro_series(self):
62 self.become_admin()
63 default_osystem = make_osystem_with_releases(self)
64 other_osystem = make_osystem_with_releases(self)
65 patch_usable_osystems(self, [default_osystem, other_osystem])
66 Config.objects.set_config("default_osystem", default_osystem['name'])
67 invalid_release = other_osystem['releases'][0]['name']
68 response = self.client.post(
69 reverse('maas_handler'), {
70 "op": "set_config",
71 "name": "default_distro_series",
72 "value": invalid_release,
73 })
74 self.assertEquals(
75 httplib.BAD_REQUEST, response.status_code, response.content)
076
=== modified file 'src/maasserver/forms.py'
--- src/maasserver/forms.py 2015-07-28 04:54:11 +0000
+++ src/maasserver/forms.py 2015-07-31 23:12:48 +0000
@@ -119,6 +119,7 @@
119 CONFIG_ITEMS_KEYS,119 CONFIG_ITEMS_KEYS,
120 get_config_field,120 get_config_field,
121 INVALID_SETTING_MSG_TEMPLATE,121 INVALID_SETTING_MSG_TEMPLATE,
122 validate_missing_boot_images,
122)123)
123from maasserver.models import (124from maasserver.models import (
124 Bcache,125 Bcache,
@@ -1389,10 +1390,31 @@
1389 # don't want _load_initial called until the field has been added.1390 # don't want _load_initial called until the field has been added.
1390 Form.__init__(self, *args, **kwargs)1391 Form.__init__(self, *args, **kwargs)
1391 self.fields['default_osystem'] = get_config_field('default_osystem')1392 self.fields['default_osystem'] = get_config_field('default_osystem')
1392 self.fields['default_distro_series'] = get_config_field(1393 self.fields['default_distro_series'] = (
1393 'default_distro_series')1394 self._get_default_distro_series_field_for_ui())
1394 self._load_initials()1395 self._load_initials()
13951396
1397 def _get_default_distro_series_field_for_ui(self):
1398 """This create the field with os/release. This is needed by the UI
1399 to filter the releases based on the OS selection. The API uses the
1400 field defined in forms_settings.py"""
1401 usable_oses = list_all_usable_osystems()
1402 release_choices = list_release_choices(
1403 list_all_usable_releases(usable_oses), include_default=False)
1404 if len(release_choices) == 0:
1405 release_choices = [('---', '--- No Usable Release ---')]
1406 field = forms.ChoiceField(
1407 initial=Config.objects.get_config('default_distro_series'),
1408 choices=release_choices,
1409 validators=[validate_missing_boot_images],
1410 error_messages={
1411 'invalid_choice': compose_invalid_choice_text(
1412 'release', release_choices)
1413 },
1414 label="Default OS release used for deployment",
1415 required=False)
1416 return field
1417
1396 def _load_initials(self):1418 def _load_initials(self):
1397 super(DeployForm, self)._load_initials()1419 super(DeployForm, self)._load_initials()
1398 initial_os = self.fields['default_osystem'].initial1420 initial_os = self.fields['default_osystem'].initial
13991421
=== modified file 'src/maasserver/forms_settings.py'
--- src/maasserver/forms_settings.py 2015-07-26 13:15:53 +0000
+++ src/maasserver/forms_settings.py 2015-07-31 23:12:48 +0000
@@ -39,7 +39,6 @@
39 list_all_usable_releases,39 list_all_usable_releases,
40 list_commissioning_choices,40 list_commissioning_choices,
41 list_osystem_choices,41 list_osystem_choices,
42 list_release_choices,
43)42)
4443
4544
@@ -74,13 +73,35 @@
74 return field73 return field
7574
7675
76def get_default_usable_osystem(default_osystem):
77 """Return the osystem from the clusters that matches the default_osystem.
78 """
79 usable_oses = list_all_usable_osystems()
80 for usable_os in usable_oses:
81 if usable_os["name"] == default_osystem:
82 return usable_os
83 return None
84
85
86def list_choices_for_releases(releases):
87 """List all the release choices."""
88 return [
89 (release['name'], release['title'])
90 for release in releases
91 ]
92
93
77def make_default_distro_series_field(*args, **kwargs):94def make_default_distro_series_field(*args, **kwargs):
78 """Build and return the default_distro_series field."""95 """Build and return the default_distro_series field."""
79 usable_oses = list_all_usable_osystems()96 default_osystem = Config.objects.get_config('default_osystem')
80 release_choices = list_release_choices(97 default_usable_os = get_default_usable_osystem(default_osystem)
81 list_all_usable_releases(usable_oses), include_default=False)98 release_choices = [('---', '--- No Usable Release ---')]
82 if len(release_choices) == 0:99 if default_usable_os is not None:
83 release_choices = [('---', '--- No Usable Release ---')]100 releases = list_all_usable_releases(
101 [default_usable_os])[default_osystem]
102 valid_release_choices = list_choices_for_releases(releases)
103 if len(valid_release_choices) > 0:
104 release_choices = valid_release_choices
84 field = forms.ChoiceField(105 field = forms.ChoiceField(
85 initial=Config.objects.get_config('default_distro_series'),106 initial=Config.objects.get_config('default_distro_series'),
86 choices=release_choices,107 choices=release_choices,