Rename `resource`_data/url attributes to manifest/manifest_url

Pod, Service and ReplicationController have data and definition_url attribute,
but it's name is needlessly long.
This fixes it to more appropriate attribute name.

Change-Id: I64e8922ef8f6fc0dc4a7fda02aa0913ca15177e4
changes/59/147759/1
OTSUKA, Yuanying 2015-01-16 15:14:38 +09:00
parent aa45150fd4
commit a61f1ff719
12 changed files with 58 additions and 131 deletions

View File

@ -286,17 +286,17 @@ To start a kubernetes pod, use Kolla as an example repo::
git clone http://github.com/stackforge/kolla
cd kolla/k8s/pod
magnum pod-create --pod-file ./mariadb-pod.yaml --bay-id $BAY_UUID
magnum pod-create --manifest ./mariadb-pod.yaml --bay-id $BAY_UUID
To start a kubernetes service, use Kolla as an example repo::
cd ../service
magnum service-create --service-file ./mariadb-service.yaml --bay-id $BAY_UUID
magnum service-create --manifest ./mariadb-service.yaml --bay-id $BAY_UUID
To start a kubernetes replication controller, use Kolla as an example repo::
cd ../replication
magnum rc-create --rc-file ./nova-compute-replicationyaml --bay-id $BAY_UUID
magnum rc-create --manifest ./nova-compute-replicationyaml --bay-id $BAY_UUID
Full lifecycle and introspection operations for each object are supported. For
exmaple, magnum bay-create magnum baymodel-delete, magnum rc-show, magnum service-list.

View File

@ -89,10 +89,10 @@ class Pod(base.APIBase):
status = wtypes.text
"""Staus of this pod """
pod_definition_url = wtypes.text
manifest_url = wtypes.text
"""URL for pod file to create the pod"""
pod_data = wtypes.text
manifest = wtypes.text
"""Data for pod to create the pod"""
links = wsme.wsattr([link.Link], readonly=True)
@ -160,9 +160,9 @@ class Pod(base.APIBase):
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)
# TODO(yuanying): retrive pod name from manifest_url
if hasattr(self, "manifest") and self.manifest is not None:
manifest = k8s_manifest.parse(self.manifest)
self.name = manifest["id"]
if "labels" in manifest:
self.labels = manifest["labels"]
@ -319,10 +319,10 @@ class PodsController(rest.RestController):
# Update only the fields that have changed
for field in objects.Pod.fields:
# ignore pod_definition_url as it was used for create pod
if field == 'pod_definition_url':
# ignore manifest_url as it was used for create pod
if field == 'manifest_url':
continue
if field == 'pod_data':
if field == 'manifest':
continue
try:
patch_val = getattr(pod, field)

View File

@ -86,7 +86,7 @@ class ReplicationController(base.APIBase):
replicas = wsme.wsattr(wtypes.IntegerType(), readonly=True)
"""Replicas of this ReplicationController"""
rc_definition_url = wtypes.text
manifest_url = wtypes.text
"""URL for ReplicationController file to create the RC"""
replicationcontroller_data = wtypes.text
@ -158,7 +158,7 @@ class ReplicationController(base.APIBase):
def parse_manifest(self):
# Set replication controller name and labels from its manifest
# TODO(jay-lau-513): retrieve replication controller name from
# rc_definition_url
# manifest_url
if (hasattr(self, "replicationcontroller_data")
and self.replicationcontroller_data is not None):
manifest = k8s_manifest.parse(self.replicationcontroller_data)
@ -327,8 +327,8 @@ class ReplicationControllersController(rest.RestController):
# Update only the fields that have changed
for field in objects.ReplicationController.fields:
# ignore rc_definition_url as it was used for create rc
if field == 'rc_definition_url':
# ignore manifest_url as it was used for create rc
if field == 'manifest_url':
continue
# ignore replicationcontroller_data as it was used for create rc
if field == 'replicationcontroller_data':

View File

@ -80,10 +80,10 @@ class Service(base.APIBase):
port = wtypes.IntegerType()
"""Port of this service"""
service_definition_url = wtypes.text
manifest_url = wtypes.text
"""URL for service file to create the service"""
service_data = wtypes.text
manifest = wtypes.text
"""Data for service to create the service"""
links = wsme.wsattr([link.Link], readonly=True)
@ -143,8 +143,8 @@ class Service(base.APIBase):
def parse_manifest(self):
# Set service name and port from its manifest
# TODO(yuanying): retrive service name from definition_url
if hasattr(self, "service_data") and self.service_data is not None:
manifest = k8s_manifest.parse(self.service_data)
if hasattr(self, "manifest") and self.manifest is not None:
manifest = k8s_manifest.parse(self.manifest)
self.name = manifest["id"]
if "port" in manifest:
self.port = manifest["port"]
@ -311,10 +311,10 @@ class ServicesController(rest.RestController):
# Update only the fields that have changed
for field in objects.Service.fields:
# ignore service_definition_url as it was used for create service
if field == 'service_definition_url':
# ignore manifest_url as it was used for create service
if field == 'manifest_url':
continue
if field == 'service_data':
if field == 'manifest':
continue
try:
patch_val = getattr(service, field)

View File

@ -20,25 +20,9 @@ from magnum.openstack.common import utils
LOG = logging.getLogger(__name__)
def _extract_resource_type(resource):
return resource.__class__.__name__.lower()
def _extract_resource_data(resource):
resource_type = _extract_resource_type(resource)
data_attribute = "%s_data" % resource_type
return getattr(resource, data_attribute, None)
def _extract_resource_definition_url(resource):
resource_type = _extract_resource_type(resource)
definition_url_attribute = "%s_definition_url" % resource_type
return getattr(resource, definition_url_attribute, None)
def _k8s_create(master_address, resource):
data = _extract_resource_data(resource)
definition_url = _extract_resource_definition_url(resource)
data = resource.manifest
definition_url = resource.manifest_url
if data is not None:
return _k8s_create_with_data(master_address, data)
else:
@ -59,8 +43,8 @@ def _k8s_create_with_data(master_address, resource_data):
def _k8s_update(master_address, resource):
data = _extract_resource_data(resource)
definition_url = _extract_resource_definition_url(resource)
data = resource.manifest
definition_url = resource.manifest_url
if data is not None:
return _k8s_update_with_data(master_address, data)
else:
@ -123,8 +107,8 @@ class KubeClient(object):
try:
out = utils.execute('kubectl', 'get', 'services',
'-s', master_address,)
pod_data = [s.split() for s in out.split('\n')]
return pod_data
manifest = [s.split() for s in out.split('\n')]
return manifest
except Exception as e:
LOG.error("Couldn't get list of services due to error %s" % e)
return None
@ -196,8 +180,8 @@ class KubeClient(object):
LOG.debug("pod_list")
try:
out = utils.execute('kubectl', 'get', 'pods', '-s', master_address)
pod_data = [s.split() for s in out.split('\n')]
return pod_data
manifest = [s.split() for s in out.split('\n')]
return manifest
except Exception as e:
LOG.error("Couldn't get list of pods due to error %s" % e)
return None

View File

@ -35,18 +35,18 @@ class Pod(base.MagnumObject):
'images': obj_utils.list_or_none,
'labels': obj_utils.dict_or_none,
'status': obj_utils.str_or_none,
'pod_definition_url': obj_utils.str_or_none,
'pod_data': obj_utils.str_or_none,
'manifest_url': obj_utils.str_or_none,
'manifest': obj_utils.str_or_none,
}
@staticmethod
def _from_db_object(pod, db_pod):
"""Converts a database entity to a formal object."""
for field in pod.fields:
# ignore pod_definition_url as it was used for create pod
if field == 'pod_definition_url':
# ignore manifest_url as it was used for create pod
if field == 'manifest_url':
continue
if field == 'pod_data':
if field == 'manifest':
continue
pod[field] = db_pod[field]

View File

@ -33,19 +33,19 @@ class ReplicationController(base.MagnumObject):
'bay_uuid': obj_utils.str_or_none,
'labels': obj_utils.dict_or_none,
'replicas': obj_utils.int_or_none,
'rc_definition_url': obj_utils.str_or_none,
'repplicationcontroller_data': obj_utils.str_or_none,
'manifest_url': obj_utils.str_or_none,
'manifest': obj_utils.str_or_none,
}
@staticmethod
def _from_db_object(rc, db_rc):
"""Converts a database entity to a formal object."""
for field in rc.fields:
# ignore rc_definition_url as it was used for create rc
if field == 'rc_definition_url':
# ignore manifest_url as it was used for create rc
if field == 'manifest_url':
continue
# ignore repplicationcontroller_data as it was used for create rc
if field == 'repplicationcontroller_data':
# ignore manifest as it was used for create rc
if field == 'manifest':
continue
rc[field] = db_rc[field]

View File

@ -35,18 +35,18 @@ class Service(base.MagnumObject):
'selector': obj_utils.dict_or_none,
'ip': obj_utils.str_or_none,
'port': obj_utils.int_or_none,
'service_definition_url': obj_utils.str_or_none,
'service_data': obj_utils.str_or_none,
'manifest_url': obj_utils.str_or_none,
'manifest': obj_utils.str_or_none,
}
@staticmethod
def _from_db_object(service, db_service):
"""Converts a database entity to a formal object."""
for field in service.fields:
# ignore service_definition_url as it was used for create service
if field == 'service_definition_url':
# ignore manifest_url as it was used for create service
if field == 'manifest_url':
continue
if field == 'service_data':
if field == 'manifest':
continue
service[field] = db_service[field]

View File

@ -34,7 +34,7 @@ class TestPodController(db_base.DbTestCase):
"desc": "My Pod",
"bay_uuid": "7ae81bb3-dec3-4289-8d6c-da80bd8001ae",
"images": ["ubuntu"],
"pod_data": "{\\"id\\": \\"name_of_pod\\", \
"manifest": "{\\"id\\": \\"name_of_pod\\", \
\\"labels\\": {\\"foo\\": \\"foo1\\"} }"
}
'''

View File

@ -33,7 +33,7 @@ class TestServiceController(db_base.DbTestCase):
params = '''
{
"bay_uuid": "7ae81bb3-dec3-4289-8d6c-da80bd8001ae",
"service_data": "\
"manifest": "\
{\
\\"id\\": \\"service_foo\\",\
\\"kind\\": \\"Service\\",\

View File

@ -13,7 +13,6 @@
# under the License.
from magnum.conductor.handlers.common import kube_utils
from magnum import objects
from magnum.tests import base
import mock
@ -24,71 +23,27 @@ class TestKubeUtils(base.BaseTestCase):
def setUp(self):
super(TestKubeUtils, self).setUp()
def test_extract_resource_type(self):
expected_resource_type = 'bay'
bay = objects.Bay({})
actual_type = kube_utils._extract_resource_type(bay)
self.assertEqual(expected_resource_type, actual_type)
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_type')
def test_extract_resource_data_with_data(self,
mock_extract_resource_type):
expected_data = 'expected_data'
mock_extract_resource_type.return_value = 'mock_type'
mock_resource = mock.MagicMock()
mock_resource.mock_type_data = expected_data
actual_data = kube_utils._extract_resource_data(mock_resource)
self.assertEqual(expected_data, actual_data)
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_type')
def test_extract_resource_definition_url(self,
mock_extract_resource_type):
expected_data = 'expected_url'
mock_extract_resource_type.return_value = 'mock_type'
mock_resource = mock.MagicMock()
mock_resource.mock_type_definition_url = expected_data
actual_data = kube_utils._extract_resource_definition_url(
mock_resource)
self.assertEqual(expected_data, actual_data)
@patch('magnum.conductor.handlers.common.kube_utils._k8s_create_with_data')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_data')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_definition_url')
def test_k8s_create_data(self,
mock_definition_url,
mock_data,
mock_create_with_data):
expected_data = 'data'
master_address = 'master_address'
mock_data.return_value = expected_data
mock_definition_url.return_value = None
mock_resource = mock.MagicMock()
mock_resource.manifest = expected_data
mock_resource.manifest_url = None
kube_utils._k8s_create(master_address, mock_resource)
mock_create_with_data.assert_called_once_with(master_address,
expected_data)
@patch('magnum.conductor.handlers.common.kube_utils._k8s_create_with_path')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_data')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_definition_url')
def test_k8s_create_url(self,
mock_definition_url,
mock_data,
mock_create_with_path):
expected_url = 'url'
master_address = 'master_address'
mock_data.return_value = None
mock_definition_url.return_value = expected_url
mock_resource = mock.MagicMock()
mock_resource.manifest = None
mock_resource.manifest_url = expected_url
kube_utils._k8s_create(master_address, mock_resource)
mock_create_with_path.assert_called_once_with(master_address,
@ -129,38 +84,26 @@ class TestKubeUtils(base.BaseTestCase):
expected_filename)
@patch('magnum.conductor.handlers.common.kube_utils._k8s_update_with_data')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_data')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_definition_url')
def test_k8s_update_data(self,
mock_definition_url,
mock_data,
mock_update_with_data):
expected_data = 'data'
master_address = 'master_address'
mock_data.return_value = expected_data
mock_definition_url.return_value = None
mock_resource = mock.MagicMock()
mock_resource.manifest = expected_data
mock_resource.manifest_url = None
kube_utils._k8s_update(master_address, mock_resource)
mock_update_with_data.assert_called_once_with(master_address,
expected_data)
@patch('magnum.conductor.handlers.common.kube_utils._k8s_update_with_path')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_data')
@patch('magnum.conductor.handlers.common.kube_utils.'
'_extract_resource_definition_url')
def test_k8s_update_url(self,
mock_definition_url,
mock_data,
mock_update_with_path):
mock_update_with_path):
expected_url = 'url'
master_address = 'master_address'
mock_data.return_value = None
mock_definition_url.return_value = expected_url
mock_resource = mock.MagicMock()
mock_resource.manifest = None
mock_resource.manifest_url = expected_url
kube_utils._k8s_update(master_address, mock_resource)
mock_update_with_path.assert_called_once_with(master_address,

View File

@ -183,7 +183,7 @@ def get_test_rc(**kw):
'bay_uuid': kw.get('bay_uuid', '10a47dd1-4874-4298-91cf-eff046dbdb8e'),
'labels': kw.get('labels', {'name': 'foo'}),
'replicas': kw.get('replicas', 3),
'rc_definition_url': kw.get('file:///tmp/rc.yaml'),
'manifest_url': kw.get('file:///tmp/rc.yaml'),
'created_at': kw.get('created_at'),
'updated_at': kw.get('updated_at'),
}