from __future__ import absolute_import

from api import api, serializers
from nodes.models import Node
from slices.models import Slice
from users.models import User, Group

from .models import SfaObject


class SfaObjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = SfaObject
        exclude = ('object_id', 'content_type', 'id')


# TODO /sfa writable -> UUID only writable on creation, once set cannot be changed

for model in [Node, User, Group, Slice]:
    api.aggregate(model, SfaObjectSerializer, name='sfa')
            # TODO deserialize addresses XXX
            #tinc_addr = TincAddressSerializer(data=data.get('addresses'), many=True)
            #for addr in data.get('addresses'):
            #    tinc_addr = TincAddressSerializer(data=addr)
            tinc_host.full_clean(exclude=['content_type', 'object_id', 'name'])
            return [tinc_host]
        return []


class HostCreateSerializer(serializers.UriHyperlinkedModelSerializer):
    id = serializers.Field()
    mgmt_net = MgmtNetConfRelatedField(source='related_mgmtnet')
    tinc = TincHostRelatedField(source='related_tinc', required=False)
    
    class Meta:
        model = Host
        exclude = ('owner',)
    
    def validate_tinc(self, attrs, source):
        return validate_tinc(self, attrs, source)


class HostSerializer(HostCreateSerializer):
    class Meta:
        model = Host


# Aggregate tinc to the Server and Node API
api.aggregate(Server, TincHostRelatedField, name='tinc', source='related_tinc', required=False)
api.aggregate(Node, TincHostRelatedField, name='tinc', source='related_tinc', required=False)
class HostCreateSerializer(serializers.UriHyperlinkedModelSerializer):
    id = serializers.Field()
    mgmt_net = MgmtNetConfRelatedField(source='related_mgmtnet')
    tinc = TincHostRelatedField(source='related_tinc', required=False)

    class Meta:
        model = Host
        exclude = ('owner', )

    def validate_tinc(self, attrs, source):
        return validate_tinc(self, attrs, source)


class HostSerializer(HostCreateSerializer):
    class Meta:
        model = Host


# Aggregate tinc to the Server and Node API
api.aggregate(Server,
              TincHostRelatedField,
              name='tinc',
              source='related_tinc',
              required=False)
api.aggregate(Node,
              TincHostRelatedField,
              name='tinc',
              source='related_tinc',
              required=False)
Beispiel #4
0
        try:
            # GenericRelation
            mgmt_net = value.first()
        except AttributeError:
            # MgmtNetConf object
            mgmt_net = value
        if mgmt_net is None:
            return {}
        return MgmtNetConfSerializer(mgmt_net).to_native(mgmt_net)
    
    def from_native(self, data):
        """ Return a list of management network objects """
        data = self.validate(data)
        mgmt_net = MgmtNetConf(backend=data.get('backend'))
        mgmt_net.full_clean(exclude=['content_type', 'object_id'])
        return [mgmt_net]

    def validate(self, attrs):
        if 'backend' not in attrs:
            raise serializers.ValidationError('backend field must be provided.')
        ## backwards compatibility #157.note-36 ##
        ## keep tinc_{client|server} until all nodes are compatible ##
        if attrs['backend'] in ['tinc_client', 'tinc_server']:
            attrs['backend'] = MgmtNetConf.TINC
        return attrs


# Aggregate mgmt_network to the API
for model in [Server, Node]:
    api.aggregate(model, MgmtNetConfRelatedField, name='mgmt_net', source='related_mgmtnet')

class ResourceReqSerializer(serializers.ModelSerializer):
    unit = serializers.CharField(read_only=True)
    
    class Meta:
        model = ResourceReq
        fields = ['name', 'req', 'unit']


for producer_model in ResourcePlugin.get_producers_models():
    if producer_model == Testbed:
        TestbedSerializer.base_fields.update({'testbed_resources':
            ResourceSerializer(source='resources', many=True, required=False)})
        continue
    api.aggregate(producer_model, ResourceSerializer, name='resources', many=True, required=False)


for consumer_model in ResourcePlugin.get_consumers_models():
    api.aggregate(consumer_model, ResourceReqSerializer, name='resources', many=True, required=False)


# Hack to show explicit handled resource (Vlan) - #46-note87
class VlanResourceReqSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField('get_vlan_name')
    unit = serializers.SerializerMethodField('get_vlan_unit')
    req = serializers.SerializerMethodField('get_vlan_req')
    alloc = serializers.SerializerMethodField('get_vlan_alloc')
    
    class Meta:
        model = Slice
    class Meta:
        model = ResourceReq
        fields = ['name', 'req', 'unit']


for producer_model in ResourcePlugin.get_producers_models():
    if producer_model == Testbed:
        TestbedSerializer.base_fields.update({
            'testbed_resources':
            ResourceSerializer(source='resources', many=True, required=False)
        })
        continue
    api.aggregate(producer_model,
                  ResourceSerializer,
                  name='resources',
                  many=True,
                  required=False)

for consumer_model in ResourcePlugin.get_consumers_models():
    api.aggregate(consumer_model,
                  ResourceReqSerializer,
                  name='resources',
                  many=True,
                  required=False)


# Hack to show explicit handled resource (Vlan) - #46-note87
class VlanResourceReqSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField('get_vlan_name')
    unit = serializers.SerializerMethodField('get_vlan_unit')
from __future__ import absolute_import

from rest_framework import serializers

from api import api
from nodes.models import Server, Node

from .models import CnHost


class CnHostSerializer(serializers.ModelSerializer):
    class Meta:
        model = CnHost
        exclude = ['id', 'content_type', 'object_id', 'cndb_cached_on']


# TODO: POST/PUT this resource fails. Related info:
# https://groups.google.com/forum/#!topic/django-rest-framework/2iEat5mCbvY/discussion
api.aggregate(Node, CnHostSerializer, name='cn', required=False)
api.aggregate(Server, CnHostSerializer, name='cn', required=False)
from __future__ import absolute_import

from api import api, serializers
from nodes.models import Node
from slices.models import Slice
from users.models import User, Group

from .models import SfaObject


class SfaObjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = SfaObject
        exclude = ('object_id', 'content_type', 'id')

# TODO /sfa writable -> UUID only writable on creation, once set cannot be changed

for model in [Node, User, Group, Slice]:
    api.aggregate(model, SfaObjectSerializer, name='sfa')