Merge "Set pod name from pod manifest"

This commit is contained in:
Jenkins 2015-01-13 21:00:31 +00:00 committed by Gerrit Code Review
commit 464a4422b7
2 changed files with 23 additions and 7 deletions

View File

@ -25,6 +25,7 @@ from magnum.api.controllers.v1 import collection
from magnum.api.controllers.v1 import types
from magnum.api.controllers.v1 import utils as api_utils
from magnum.common import exception
from magnum.common import k8s_manifest
from magnum import objects
@ -70,7 +71,7 @@ class Pod(base.APIBase):
uuid = types.uuid
"""Unique UUID for this pod"""
name = wtypes.text
name = wsme.wsattr(wtypes.text, readonly=True)
"""Name of this pod"""
desc = wtypes.text
@ -82,7 +83,7 @@ class Pod(base.APIBase):
images = [wtypes.text]
"""A list of images used by containers in this pod."""
labels = {wtypes.text: wtypes.text}
labels = wsme.wsattr({wtypes.text: wtypes.text}, readonly=True)
"""Labels of this pod"""
status = wtypes.text
@ -157,6 +158,15 @@ class Pod(base.APIBase):
sample._pod_uuid = '87504bd9-ca50-40fd-b14e-bcb23ed42b27'
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def parse_manifest(self):
# Set pod name from its manifest
# TODO(yuanying): retrive pod name from pod_definition_url
if hasattr(self, "pod_data") and self.pod_data is not None:
manifest = k8s_manifest.parse(self.pod_data)
self.name = manifest["id"]
if "labels" in manifest:
self.labels = manifest["labels"]
class PodCollection(collection.Collection):
"""API representation of a collection of pods."""
@ -276,6 +286,7 @@ class PodsController(rest.RestController):
if self.from_pods:
raise exception.OperationNotPermitted
pod.parse_manifest()
pod_obj = objects.Pod(pecan.request.context,
**pod.as_dict())
new_pod = pecan.request.rpcapi.pod_create(pod_obj)

View File

@ -27,10 +27,15 @@ class TestPodController(db_base.DbTestCase):
with patch.object(api.API, 'pod_create') as mock_method:
mock_method.side_effect = self.mock_pod_create
# Create a pod
params = '{"name": "pod_example_A", "desc": "My Pod",' \
'"bay_uuid": "7ae81bb3-dec3-4289-8d6c-da80bd8001ae",' \
'"images": ["ubuntu"], "labels": {"foo": "foo1"},' \
'"pod_definition_url": "http://172.17.1.2/pod.json"}'
params = '''
{
"desc": "My Pod",
"bay_uuid": "7ae81bb3-dec3-4289-8d6c-da80bd8001ae",
"images": ["ubuntu"],
"pod_data": "{\\"id\\": \\"name_of_pod\\", \
\\"labels\\": {\\"foo\\": \\"foo1\\"} }"
}
'''
response = self.app.post('/v1/pods',
params=params,
content_type='application/json')
@ -42,7 +47,7 @@ class TestPodController(db_base.DbTestCase):
self.assertEqual(1, len(response.json))
c = response.json['pods'][0]
self.assertIsNotNone(c.get('uuid'))
self.assertEqual('pod_example_A', c.get('name'))
self.assertEqual('name_of_pod', c.get('name'))
self.assertEqual('My Pod', c.get('desc'))
self.assertEqual('7ae81bb3-dec3-4289-8d6c-da80bd8001ae',
c.get('bay_uuid'))