launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #14844
[Merge] lp:~rvb/maas/notif-api-list into lp:maas
Raphaël Badin has proposed merging lp:~rvb/maas/notif-api-list into lp:maas.
Commit message:
Add API call to list the NodeCommissionResult objects.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~rvb/maas/notif-api-list/+merge/140737
Drive-by fix: moved commissioning_script_handler and commissioning_scripts_handler where they belong in src/maasserver/urls_api.py.
--
https://code.launchpad.net/~rvb/maas/notif-api-list/+merge/140737
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/notif-api-list into lp:maas.
=== modified file 'src/maasserver/api.py'
--- src/maasserver/api.py 2012-12-12 17:12:25 +0000
+++ src/maasserver/api.py 2012-12-19 17:31:21 +0000
@@ -60,6 +60,7 @@
"api_doc",
"api_doc_title",
"BootImagesHandler",
+ "CommissioningResultsHandler",
"FilesHandler",
"get_oauth_token",
"NodeGroupsHandler",
@@ -178,7 +179,10 @@
)
from maasserver.utils.orm import get_one
from metadataserver.fields import Bin
-from metadataserver.models import CommissioningScript
+from metadataserver.models import (
+ CommissioningScript,
+ NodeCommissionResult,
+ )
from piston.utils import rc
from provisioningserver.enum import POWER_TYPE
from provisioningserver.kernel_opts import KernelParameters
@@ -1850,6 +1854,36 @@
script.save()
+class CommissioningResultsHandler(OperationsHandler):
+ """Read the collection of NodeCommissionResult in the MAAS."""
+ create = read = update = delete = None
+
+ model = NodeCommissionResult
+ fields = ('name', 'script_result', 'updated', 'created', 'node', 'data')
+
+ @operation(idempotent=True)
+ def list(self, request):
+ """List NodeCommissionResult visible to the user, optionally filtered.
+
+ :param system_id: An optional list of system ids. Only the
+ commissioning results related to the nodes with these system ids
+ will be returned.
+ :type system_id: iterable
+ :param name: An optional list of names. Only the commissioning
+ results with the specified names will be returned.
+ :type name: iterable
+ """
+ # Get filters from request.
+ system_ids = get_optional_list(request.GET, 'system_id')
+ names = get_optional_list(request.GET, 'name')
+ nodes = Node.objects.get_nodes(
+ request.user, NODE_PERMISSION.VIEW, ids=system_ids)
+ results = NodeCommissionResult.objects.filter(node_id__in=nodes)
+ if names is not None:
+ results = results.filter(name__in=names)
+ return results
+
+
def describe(request):
"""Return a description of the whole MAAS API.
=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py 2012-12-12 17:18:53 +0000
+++ src/maasserver/tests/test_api.py 2012-12-19 17:31:21 +0000
@@ -4703,6 +4703,90 @@
self.assertEqual(httplib.FORBIDDEN, response.status_code)
+class NodeCommissionResultHandlerAPITest(APITestCase):
+
+ def test_list_returns_commissioning_results(self):
+ commissioning_results = []
+ for i in range(3):
+ node = factory.make_node()
+ result = factory.make_node_commission_result(node)
+ commissioning_results.append(result)
+ url = reverse('commissioning_results_handler')
+ response = self.client.get(url, {'op': 'list'})
+ self.assertEqual(httplib.OK, response.status_code, response.content)
+ parsed_results = json.loads(response.content)
+ self.assertItemsEqual(
+ [(
+ commissioning_result.name,
+ commissioning_result.script_result,
+ commissioning_result.data,
+ commissioning_result.node.system_id,
+ )
+ for commissioning_result in commissioning_results
+ ],
+ [(
+ result.get('name'),
+ result.get('script_result'),
+ result.get('data'),
+ result.get('node').get('system_id')
+ )
+ for result in parsed_results
+ ])
+
+ def test_list_can_be_filtered_by_node(self):
+ commissioning_results = []
+ for i in range(3):
+ node = factory.make_node()
+ result = factory.make_node_commission_result(node)
+ commissioning_results.append(result)
+ url = reverse('commissioning_results_handler')
+ response = self.client.get(
+ url,
+ {
+ 'op': 'list',
+ 'system_id':
+ [
+ commissioning_results[0].node.system_id,
+ commissioning_results[1].node.system_id,
+ ]
+ }
+ )
+ self.assertEqual(httplib.OK, response.status_code, response.content)
+ parsed_results = json.loads(response.content)
+ self.assertItemsEqual(
+ [commissioning_results[0].data, commissioning_results[1].data],
+ [result.get('data') for result in parsed_results])
+
+ def test_list_can_be_filtered_by_name(self):
+ commissioning_results = []
+ for i in range(3):
+ node = factory.make_node()
+ result = factory.make_node_commission_result(node)
+ commissioning_results.append(result)
+ url = reverse('commissioning_results_handler')
+ response = self.client.get(
+ url,
+ {
+ 'op': 'list',
+ 'name': commissioning_results[0].name
+ }
+ )
+ self.assertEqual(httplib.OK, response.status_code, response.content)
+ parsed_results = json.loads(response.content)
+ self.assertItemsEqual(
+ [commissioning_results[0].data],
+ [result.get('data') for result in parsed_results])
+
+ def test_list_displays_only_visible_nodes(self):
+ node = factory.make_node(owner=factory.make_user())
+ factory.make_node_commission_result(node)
+ url = reverse('commissioning_results_handler')
+ response = self.client.get(url, {'op': 'list'})
+ self.assertEqual(httplib.OK, response.status_code, response.content)
+ parsed_results = json.loads(response.content)
+ self.assertEqual([], parsed_results)
+
+
class TestDescribe(AnonAPITestCase):
"""Tests for the `describe` view."""
=== modified file 'src/maasserver/urls_api.py'
--- src/maasserver/urls_api.py 2012-11-23 14:27:34 +0000
+++ src/maasserver/urls_api.py 2012-12-19 17:31:21 +0000
@@ -20,6 +20,7 @@
AccountHandler,
api_doc,
BootImagesHandler,
+ CommissioningResultsHandler,
CommissioningScriptHandler,
CommissioningScriptsHandler,
describe,
@@ -59,10 +60,8 @@
BootImagesHandler, authentication=api_auth)
tag_handler = RestrictedResource(TagHandler, authentication=api_auth)
tags_handler = RestrictedResource(TagsHandler, authentication=api_auth)
-commissioning_script_handler = AdminRestrictedResource(
- CommissioningScriptHandler, authentication=api_auth)
-commissioning_scripts_handler = AdminRestrictedResource(
- CommissioningScriptsHandler, authentication=api_auth)
+commissioning_results_handler = RestrictedResource(
+ CommissioningResultsHandler, authentication=api_auth)
# Admin handlers.
@@ -71,6 +70,10 @@
NodeGroupInterfaceHandler, authentication=api_auth)
nodegroupinterfaces_handler = AdminRestrictedResource(
NodeGroupInterfacesHandler, authentication=api_auth)
+commissioning_script_handler = AdminRestrictedResource(
+ CommissioningScriptHandler, authentication=api_auth)
+commissioning_scripts_handler = AdminRestrictedResource(
+ CommissioningScriptsHandler, authentication=api_auth)
# API URLs accessible to anonymous users.
urlpatterns = patterns('',
@@ -106,6 +109,9 @@
url(r'boot-images/$', boot_images_handler, name='boot_images_handler'),
url(r'tags/(?P<name>[\w\-]+)/$', tag_handler, name='tag_handler'),
url(r'tags/$', tags_handler, name='tags_handler'),
+ url(
+ r'commissioning-results/$',
+ commissioning_results_handler, name='commissioning_results_handler'),
)
Follow ups