From 2582e31868480f4610f2efe8ae171eb3b85aeab6 Mon Sep 17 00:00:00 2001 From: modulos Date: Fri, 26 May 2017 23:23:39 +0200 Subject: [PATCH] Remove public- from vm name --- opennebula_api/serializers.py | 4 +++- opennebula_api/tests.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 6c86776a..4af6c3ff 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -79,7 +79,7 @@ class VirtualMachineTemplateSerializer(serializers.Serializer): class VirtualMachineSerializer(serializers.Serializer): """Serializer to map the virtual machine instance into JSON format.""" - name = serializers.CharField(read_only=True) + name = serializers.SerializerMethodField() cores = serializers.IntegerField(source='template.vcpu') disk = serializers.IntegerField(write_only=True) set_memory = serializers.IntegerField(write_only=True, label='Memory') @@ -164,6 +164,8 @@ class VirtualMachineSerializer(serializers.Serializer): nic = obj.template.nics[0] return nic.ip6_global + def get_name(self, obj): + return obj.name.strip('public-') def hexstr2int(string): return int(string.replace(':', ''), 16) diff --git a/opennebula_api/tests.py b/opennebula_api/tests.py index 14694e6f..2dc9884f 100644 --- a/opennebula_api/tests.py +++ b/opennebula_api/tests.py @@ -113,6 +113,15 @@ class VirtualMachineTestCase(TestCase): self.virtualmachine = VirtualMachine(opennebula_id=self.opennebula_id, template=self.template) + + def test_serializer_strips_of_public(self): + """ Test the serialized object contains no 'public-'.""" + + template = self.manager.get_templates().first() + serialized = VirtualMachineTemplateSerializer(template) + self.assertEqual(serialized.data.name, template.name.strip('public-')) + + def test_model_can_create_a_virtualmachine(self): """Test the virtualmachine model can create a virtualmachine."""