Fix all flake8 errors
[pharos-tools.git] / dashboard / src / api / serializers / booking_serializer.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from rest_framework import serializers
12
13 from resource_inventory.models import (
14     HostConfiguration,
15     CpuProfile,
16     DiskProfile,
17     InterfaceProfile,
18     RamProfile,
19     Image,
20     Interface
21 )
22
23
24 class BookingField(serializers.Field):
25
26     def to_representation(self, booking):
27         """
28         Takes in a booking object.
29         Returns a dictionary of primitives representing that booking
30         """
31         ser = {}
32         ser['id'] = booking.id
33         # main loop to grab relevant info out of booking
34         host_configs = {}  # mapping hostname -> config
35         networks = {}  # mapping vlan id -> network_hosts
36         for host in booking.resource.hosts.all():
37             host_configs[host.name] = HostConfiguration.objects.get(host=host.template)
38             if "jumphost" not in ser and host_configs[host.name].opnfvRole.name.lower() == "jumphost":
39                 ser['jumphost'] = host.name
40             # host is a Host model
41             for i in range(len(host.interfaces.all())):
42                 interface = host.interfaces.all()[i]
43                 # interface is an Interface model
44                 for vlan in interface.config.all():
45                     # vlan is Vlan model
46                     if vlan.id not in networks:
47                         networks[vlan.id] = []
48                     net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface": i}
49                     networks[vlan.id].append(net_host)
50         # creates networking object of proper form
51         networking = []
52         for vlanid in networks:
53             network = {}
54             network['vlan_id'] = vlanid
55             network['hosts'] = networks[vlanid]
56
57         ser['networking'] = networking
58
59         # creates hosts object of correct form
60         hosts = []
61         for hostname in host_configs:
62             host = {"hostname": hostname}
63             host['deploy_image'] = True  # TODO?
64             image = host_configs[hostname].image
65             host['image'] = {
66                 "name": image.name,
67                 "lab_id": image.lab_id,
68                 "dashboard_id": image.id
69             }
70             hosts.append(host)
71
72         ser['hosts'] = hosts
73
74         return ser
75
76     def to_internal_value(self, data):
77         """
78         Takes in a dictionary of primitives
79         Returns a booking object
80
81         This is not going to be implemented or allowed.
82         If someone needs to create a booking through the api,
83         they will send a different booking object
84         """
85         return None
86
87
88 class BookingSerializer(serializers.Serializer):
89
90     booking = BookingField()
91
92
93 # Host Type stuff, for inventory
94 class CPUSerializer(serializers.ModelSerializer):
95     class Meta:
96         model = CpuProfile
97         fields = ('cores', 'architecture', 'cpus')
98
99
100 class DiskSerializer(serializers.ModelSerializer):
101     class Meta:
102         model = DiskProfile
103         fields = ('size', 'media_type', 'name')
104
105
106 class InterfaceProfileSerializer(serializers.ModelSerializer):
107     class Meta:
108         model = InterfaceProfile
109         fields = ('speed', 'name')
110
111
112 class RamSerializer(serializers.ModelSerializer):
113     class Meta:
114         model = RamProfile
115         fields = ('amount', 'channels')
116
117
118 class HostTypeSerializer(serializers.Serializer):
119     name = serializers.CharField(max_length=200)
120     ram = RamSerializer()
121     interface = InterfaceProfileSerializer()
122     description = serializers.CharField(max_length=1000)
123     disks = DiskSerializer()
124     cpu = CPUSerializer()
125
126
127 # the rest of the inventory stuff
128 class NetworkSerializer(serializers.Serializer):
129     cidr = serializers.CharField(max_length=200)
130     gateway = serializers.IPAddressField(max_length=200)
131     vlan = serializers.IntegerField()
132
133
134 class ImageSerializer(serializers.ModelSerializer):
135     lab_id = serializers.IntegerField()
136     id = serializers.IntegerField(source="dashboard_id")
137     name = serializers.CharField(max_length=50)
138     description = serializers.CharField(max_length=200)
139
140     class Meta:
141         model = Image
142
143
144 class InterfaceField(serializers.Field):
145     def to_representation(self, interface):
146         pass
147
148     def to_internal_value(self, data):
149         """
150         takes in a serialized interface and creates an Interface model
151         """
152         mac = data['mac']
153         bus_address = data['busaddr']
154         switch_name = data['switchport']['switch_name']
155         port_name = data['switchport']['port_name']
156         # TODO config??
157         return Interface.objects.create(
158             mac_address=mac,
159             bus_address=bus_address,
160             switch_name=switch_name,
161             port_name=port_name
162         )
163
164
165 class InventoryHostSerializer(serializers.Serializer):
166     hostname = serializers.CharField(max_length=100)
167     host_type = serializers.CharField(max_length=100)
168     interfaces = InterfaceField()
169
170
171 class InventorySerializer(serializers.Serializer):
172     hosts = InventoryHostSerializer()
173     networks = NetworkSerializer()
174     images = ImageSerializer()
175     host_types = HostTypeSerializer()