blob: bc4dd010d804bfe800658d7ef038295740463b35 [file] [log] [blame]
Tony Mack7130ac32013-03-22 21:58:00 -04001from django.forms import widgets
2from rest_framework import serializers
Siobhan Tully30fd4292013-05-10 08:59:56 -04003from core.models import *
Tony Mack7130ac32013-03-22 21:58:00 -04004
5
Tony Macka28f1cd2013-04-07 20:02:31 -04006class RoleSerializer(serializers.HyperlinkedModelSerializer):
Tony Macka2dfb372013-04-09 11:02:32 -04007 # HyperlinkedModelSerializer doesn't include the id by default
8 id = serializers.Field()
Tony Macka28f1cd2013-04-07 20:02:31 -04009 class Meta:
10 model = Role
Tony Mack879d14a2013-04-09 09:34:19 -040011 fields = ('id',
12 'role_id',
Siobhan Tully47ae1b52013-05-10 15:53:14 -040013 'role',
Tony Macka28f1cd2013-04-07 20:02:31 -040014 'role_type')
15
Tony Mack5cc16ba2013-04-09 10:30:45 -040016
17class UserSerializer(serializers.HyperlinkedModelSerializer):
Tony Macka2dfb372013-04-09 11:02:32 -040018 # HyperlinkedModelSerializer doesn't include the id by default
19 id = serializers.Field()
Tony Mack5cc16ba2013-04-09 10:30:45 -040020 site = serializers.HyperlinkedRelatedField(view_name='site-detail')
21 slice_memberships = serializers.HyperlinkedRelatedField(view_name='slice-membership-detail')
22 site_privileges = serializers.HyperlinkedRelatedField(view_name='site-privilege-detail')
23 class Meta:
Siobhan Tully30fd4292013-05-10 08:59:56 -040024 model = User
Tony Mack5cc16ba2013-04-09 10:30:45 -040025 fields = ('id',
26 'user_id',
Siobhan Tully30fd4292013-05-10 08:59:56 -040027 'kuser_id',
Tony Mack5cc16ba2013-04-09 10:30:45 -040028 'firstname',
29 'lastname',
30 'email',
Tony Mack8484bdb2013-04-14 20:26:03 -040031 'password',
Tony Mack5cc16ba2013-04-09 10:30:45 -040032 'phone',
33 'user_url',
34 'is_admin',
35 'site',
36 'slice_memberships',
37 'site_privileges')
38
Tony Macke4cee7a2013-04-09 12:45:32 -040039class KeySerializer(serializers.HyperlinkedModelSerializer):
40 id = serializers.Field()
41 user = serializers.HyperlinkedRelatedField(view_name='user-detail')
42 class Meta:
43 model = Key
44 fields = ('id',
45 'name',
46 'key',
47 'type',
48 'blacklisted',
49 'user')
50
Tony Mack5cc16ba2013-04-09 10:30:45 -040051
Tony Mack7130ac32013-03-22 21:58:00 -040052class SliceSerializer(serializers.HyperlinkedModelSerializer):
Tony Macka2dfb372013-04-09 11:02:32 -040053 # HyperlinkedModelSerializer doesn't include the id by default
54 id = serializers.Field()
Tony Mack7130ac32013-03-22 21:58:00 -040055 site = serializers.HyperlinkedRelatedField(view_name='site-detail')
Tony Mack3a1af412013-04-09 22:36:27 -040056 slivers = serializers.HyperlinkedRelatedField(view_name='sliver-detail')
Tony Mack7130ac32013-03-22 21:58:00 -040057 class Meta:
58 model = Slice
Tony Mack879d14a2013-04-09 09:34:19 -040059 fields = ('id',
Tony Mack3a1af412013-04-09 22:36:27 -040060 'tenant_id',
61 'enabled',
Tony Mack7130ac32013-03-22 21:58:00 -040062 'name',
Tony Mack3a1af412013-04-09 22:36:27 -040063 'url',
Tony Mack7130ac32013-03-22 21:58:00 -040064 'omf_friendly',
65 'description',
66 'slice_url',
Tony Mack3a1af412013-04-09 22:36:27 -040067 'network_id',
68 'router_id',
Tony Mack1e7169e2013-05-07 11:09:57 -040069 'subnet_id',
Tony Mack7130ac32013-03-22 21:58:00 -040070 'site',
Tony Mack3a1af412013-04-09 22:36:27 -040071 'slivers',
Tony Mack7130ac32013-03-22 21:58:00 -040072 'updated',
73 'created')
74
Tony Mack29c287f2013-04-11 21:07:16 -040075class SliceMembershipSerializer(serializers.HyperlinkedModelSerializer):
76 id = serializers.Field()
77 slice = serializers.HyperlinkedRelatedField(view_name='slice-detail')
78 user = serializers.HyperlinkedRelatedField(view_name='user-detail')
79 role = serializers.HyperlinkedRelatedField(view_name='role-detail')
80 class Meta:
81 model = SitePrivilege
82 fields = ('id',
83 'user',
84 'slice',
85 'role')
86
Tony Mack7130ac32013-03-22 21:58:00 -040087class SiteSerializer(serializers.HyperlinkedModelSerializer):
88
89 #Experimenting with whether to use ids, hyperlinks, or nested includes
90 #slices = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
91 #slices = serializers.RelatedField(many=True, read_only=True)
92 #slices = SliceSerializer(many=True)
Tony Macka2dfb372013-04-09 11:02:32 -040093 # HyperlinkedModelSerializer doesn't include the id by default
94 id = serializers.Field()
Tony Mack7130ac32013-03-22 21:58:00 -040095 slices = serializers.HyperlinkedRelatedField(many=True, read_only=True,view_name='slice-detail')
Tony Mack7130ac32013-03-22 21:58:00 -040096
97 class Meta:
98 model = Site
Tony Mack879d14a2013-04-09 09:34:19 -040099 fields = ('id',
100 'url',
Tony Mack7130ac32013-03-22 21:58:00 -0400101 'name',
Tony Mack7130ac32013-03-22 21:58:00 -0400102 'slices',
103 'site_url',
104 'enabled',
105 'longitude',
106 'latitude',
107 'login_base',
Tony Macke87bffa2013-04-08 23:18:04 -0400108 'tenant_id',
Tony Mack7130ac32013-03-22 21:58:00 -0400109 'is_public',
110 'abbreviated_name',
111 'updated',
112 'created')
113
Tony Mack29c287f2013-04-11 21:07:16 -0400114class SitePrivilegeSerializer(serializers.HyperlinkedModelSerializer):
115 id = serializers.Field()
116 site = serializers.HyperlinkedRelatedField(view_name='site-detail')
117 user = serializers.HyperlinkedRelatedField(view_name='user-detail')
118 role = serializers.HyperlinkedRelatedField(view_name='role-detail')
119 class Meta:
120 model = SitePrivilege
121 fields = ('id',
122 'user',
123 'site',
124 'role')
125
Tony Mack7130ac32013-03-22 21:58:00 -0400126class DeploymentNetworkSerializer(serializers.HyperlinkedModelSerializer):
127
Tony Macka2dfb372013-04-09 11:02:32 -0400128 # HyperlinkedModelSerializer doesn't include the id by default
129 id = serializers.Field()
Tony Mack29c287f2013-04-11 21:07:16 -0400130 sites = serializers.HyperlinkedRelatedField(view_name='deploymentnetwork-detail')
Tony Mack7130ac32013-03-22 21:58:00 -0400131 class Meta:
132 model = DeploymentNetwork
Tony Mack879d14a2013-04-09 09:34:19 -0400133 fields = ('id',
134 'name',
Tony Mack7130ac32013-03-22 21:58:00 -0400135 'sites'
136 )
137
Tony Macka2dfb372013-04-09 11:02:32 -0400138class SliverSerializer(serializers.HyperlinkedModelSerializer):
139 # HyperlinkedModelSerializer doesn't include the id by default
140 id = serializers.Field()
Tony Mack29c287f2013-04-11 21:07:16 -0400141 image = serializers.HyperlinkedRelatedField(view_name='image-detail')
142 key = serializers.HyperlinkedRelatedField(view_name='key-detail')
143 slice = serializers.HyperlinkedRelatedField(view_name='slice-detail')
144 deployment_network = serializers.HyperlinkedRelatedField(view_name='deployment_network-detail')
145 node = serializers.HyperlinkedRelatedField(view_name='node-detail')
146
147
Tony Mack7130ac32013-03-22 21:58:00 -0400148 #slice = serializers.PrimaryKeyRelatedField(read_only=True)
149
150 class Meta:
151 model = Sliver
152 fields = ('id',
Tony Mack29c287f2013-04-11 21:07:16 -0400153 'instance_id',
Tony Mack5d6598c2013-04-12 12:45:02 -0400154 'name',
Tony Mack79748612013-05-01 14:52:03 -0400155 'instance_name',
Tony Mackd90cdbf2013-04-16 22:48:40 -0400156 'ip',
Tony Mack29c287f2013-04-11 21:07:16 -0400157 'image',
Tony Mack5d6598c2013-04-12 12:45:02 -0400158 'key',
Tony Mack7130ac32013-03-22 21:58:00 -0400159 'slice',
Tony Mack5d6598c2013-04-12 12:45:02 -0400160 'deploymentNetwork',
161 'node')
Tony Mack7130ac32013-03-22 21:58:00 -0400162
Tony Macka2dfb372013-04-09 11:02:32 -0400163class NodeSerializer(serializers.HyperlinkedModelSerializer):
164 # HyperlinkedModelSerializer doesn't include the id by default
165 id = serializers.Field()
Tony Mack7130ac32013-03-22 21:58:00 -0400166 class Meta:
167 model = Node
168 fields = ('id',
169 'name')
170
Tony Macka2dfb372013-04-09 11:02:32 -0400171class ImageSerializer(serializers.HyperlinkedModelSerializer):
172 # HyperlinkedModelSerializer doesn't include the id by default
173 id = serializers.Field()
Tony Mack53310082013-04-09 09:26:12 -0400174 class Meta:
175 model = Image
Tony Mack879d14a2013-04-09 09:34:19 -0400176 fields = ('id',
177 'image_id',
Tony Mack53310082013-04-09 09:26:12 -0400178 'name',
179 'disk_format',
180 'container_format')
181
Tony Macka28f1cd2013-04-07 20:02:31 -0400182serializerLookUp = {
183 Role: RoleSerializer,
Siobhan Tully30fd4292013-05-10 08:59:56 -0400184 User: UserSerializer,
Tony Macke4cee7a2013-04-09 12:45:32 -0400185 Key: KeySerializer,
Tony Macka28f1cd2013-04-07 20:02:31 -0400186 Site: SiteSerializer,
Tony Mack29c287f2013-04-11 21:07:16 -0400187 SitePrivilege: SitePrivilegeSerializer,
Tony Mack7130ac32013-03-22 21:58:00 -0400188 Slice: SliceSerializer,
Tony Mack29c287f2013-04-11 21:07:16 -0400189 SliceMembership: SliceMembershipSerializer,
Tony Mack7130ac32013-03-22 21:58:00 -0400190 Node: NodeSerializer,
191 Sliver: SliverSerializer,
192 DeploymentNetwork: DeploymentNetworkSerializer,
Tony Mack53310082013-04-09 09:26:12 -0400193 Image: ImageSerializer,
Tony Mack7130ac32013-03-22 21:58:00 -0400194 None: None,
195 }
Tony Mack53310082013-04-09 09:26:12 -0400196