blob: a341508c587e488ea71cb12760971e48f6758d5b [file] [log] [blame]
Tony Mack7130ac32013-03-22 21:58:00 -04001from django.forms import widgets
2from rest_framework import serializers
Tony Mackf1c12242013-04-09 16:08:43 -04003from plstackapi.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',
Tony Macka28f1cd2013-04-07 20:02:31 -040013 'role_type')
14
Tony Mack5cc16ba2013-04-09 10:30:45 -040015
16class UserSerializer(serializers.HyperlinkedModelSerializer):
Tony Macka2dfb372013-04-09 11:02:32 -040017 # HyperlinkedModelSerializer doesn't include the id by default
18 id = serializers.Field()
Tony Mack5cc16ba2013-04-09 10:30:45 -040019 site = serializers.HyperlinkedRelatedField(view_name='site-detail')
20 slice_memberships = serializers.HyperlinkedRelatedField(view_name='slice-membership-detail')
21 site_privileges = serializers.HyperlinkedRelatedField(view_name='site-privilege-detail')
22 class Meta:
Tony Mack0e723b92013-04-27 11:08:19 -040023 model = PLUser
Tony Mack5cc16ba2013-04-09 10:30:45 -040024 fields = ('id',
25 'user_id',
26 'firstname',
27 'lastname',
28 'email',
Tony Mack8484bdb2013-04-14 20:26:03 -040029 'password',
Tony Mack5cc16ba2013-04-09 10:30:45 -040030 'phone',
31 'user_url',
32 'is_admin',
33 'site',
34 'slice_memberships',
35 'site_privileges')
36
Tony Macke4cee7a2013-04-09 12:45:32 -040037class KeySerializer(serializers.HyperlinkedModelSerializer):
38 id = serializers.Field()
39 user = serializers.HyperlinkedRelatedField(view_name='user-detail')
40 class Meta:
41 model = Key
42 fields = ('id',
43 'name',
44 'key',
45 'type',
46 'blacklisted',
47 'user')
48
Tony Mack5cc16ba2013-04-09 10:30:45 -040049
Tony Mack7130ac32013-03-22 21:58:00 -040050class SliceSerializer(serializers.HyperlinkedModelSerializer):
Tony Macka2dfb372013-04-09 11:02:32 -040051 # HyperlinkedModelSerializer doesn't include the id by default
52 id = serializers.Field()
Tony Mack7130ac32013-03-22 21:58:00 -040053 site = serializers.HyperlinkedRelatedField(view_name='site-detail')
Tony Mack3a1af412013-04-09 22:36:27 -040054 slivers = serializers.HyperlinkedRelatedField(view_name='sliver-detail')
Tony Mack771f4b92013-04-12 01:49:57 -040055 subnet= serializers.HyperlinkedRelatedField(view_name='subnet-detail')
Tony Mack7130ac32013-03-22 21:58:00 -040056 class Meta:
57 model = Slice
Tony Mack879d14a2013-04-09 09:34:19 -040058 fields = ('id',
Tony Mack3a1af412013-04-09 22:36:27 -040059 'tenant_id',
60 'enabled',
Tony Mack7130ac32013-03-22 21:58:00 -040061 'name',
Tony Mack3a1af412013-04-09 22:36:27 -040062 'url',
Tony Mack7130ac32013-03-22 21:58:00 -040063 'instantiation',
64 'omf_friendly',
65 'description',
66 'slice_url',
Tony Mack3a1af412013-04-09 22:36:27 -040067 'network_id',
68 'router_id',
Tony Mack7130ac32013-03-22 21:58:00 -040069 'site',
Tony Mack3a1af412013-04-09 22:36:27 -040070 'slivers',
Tony Mack7130ac32013-03-22 21:58:00 -040071 'updated',
72 'created')
73
Tony Mack29c287f2013-04-11 21:07:16 -040074class SliceMembershipSerializer(serializers.HyperlinkedModelSerializer):
75 id = serializers.Field()
76 slice = serializers.HyperlinkedRelatedField(view_name='slice-detail')
77 user = serializers.HyperlinkedRelatedField(view_name='user-detail')
78 role = serializers.HyperlinkedRelatedField(view_name='role-detail')
79 class Meta:
80 model = SitePrivilege
81 fields = ('id',
82 'user',
83 'slice',
84 'role')
85
86class SubnetSerializer(serializers.HyperlinkedModelSerializer):
87 id = serializers.Field()
88 slice = serializers.HyperlinkedRelatedField(view_name='slice-detail')
89 class Meta:
90 model = Subnet
91 fields = ('id',
92 'subnet_id',
93 'cidr',
94 'ip_version',
95 'start',
96 'end',
97 'slice')
98
Tony Mack7130ac32013-03-22 21:58:00 -040099class SiteSerializer(serializers.HyperlinkedModelSerializer):
100
101 #Experimenting with whether to use ids, hyperlinks, or nested includes
102 #slices = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
103 #slices = serializers.RelatedField(many=True, read_only=True)
104 #slices = SliceSerializer(many=True)
Tony Macka2dfb372013-04-09 11:02:32 -0400105 # HyperlinkedModelSerializer doesn't include the id by default
106 id = serializers.Field()
Tony Mack7130ac32013-03-22 21:58:00 -0400107 slices = serializers.HyperlinkedRelatedField(many=True, read_only=True,view_name='slice-detail')
Tony Mack7130ac32013-03-22 21:58:00 -0400108
109 class Meta:
110 model = Site
Tony Mack879d14a2013-04-09 09:34:19 -0400111 fields = ('id',
112 'url',
Tony Mack7130ac32013-03-22 21:58:00 -0400113 'name',
Tony Mack7130ac32013-03-22 21:58:00 -0400114 'slices',
115 'site_url',
116 'enabled',
117 'longitude',
118 'latitude',
119 'login_base',
Tony Macke87bffa2013-04-08 23:18:04 -0400120 'tenant_id',
Tony Mack7130ac32013-03-22 21:58:00 -0400121 'is_public',
122 'abbreviated_name',
123 'updated',
124 'created')
125
Tony Mack29c287f2013-04-11 21:07:16 -0400126class SitePrivilegeSerializer(serializers.HyperlinkedModelSerializer):
127 id = serializers.Field()
128 site = serializers.HyperlinkedRelatedField(view_name='site-detail')
129 user = serializers.HyperlinkedRelatedField(view_name='user-detail')
130 role = serializers.HyperlinkedRelatedField(view_name='role-detail')
131 class Meta:
132 model = SitePrivilege
133 fields = ('id',
134 'user',
135 'site',
136 'role')
137
Tony Mack7130ac32013-03-22 21:58:00 -0400138class DeploymentNetworkSerializer(serializers.HyperlinkedModelSerializer):
139
Tony Macka2dfb372013-04-09 11:02:32 -0400140 # HyperlinkedModelSerializer doesn't include the id by default
141 id = serializers.Field()
Tony Mack29c287f2013-04-11 21:07:16 -0400142 sites = serializers.HyperlinkedRelatedField(view_name='deploymentnetwork-detail')
Tony Mack7130ac32013-03-22 21:58:00 -0400143 class Meta:
144 model = DeploymentNetwork
Tony Mack879d14a2013-04-09 09:34:19 -0400145 fields = ('id',
146 'name',
Tony Mack7130ac32013-03-22 21:58:00 -0400147 'sites'
148 )
149
Tony Macka2dfb372013-04-09 11:02:32 -0400150class SliverSerializer(serializers.HyperlinkedModelSerializer):
151 # HyperlinkedModelSerializer doesn't include the id by default
152 id = serializers.Field()
Tony Mack29c287f2013-04-11 21:07:16 -0400153 image = serializers.HyperlinkedRelatedField(view_name='image-detail')
154 key = serializers.HyperlinkedRelatedField(view_name='key-detail')
155 slice = serializers.HyperlinkedRelatedField(view_name='slice-detail')
156 deployment_network = serializers.HyperlinkedRelatedField(view_name='deployment_network-detail')
157 node = serializers.HyperlinkedRelatedField(view_name='node-detail')
158
159
Tony Mack7130ac32013-03-22 21:58:00 -0400160 #slice = serializers.PrimaryKeyRelatedField(read_only=True)
161
162 class Meta:
163 model = Sliver
164 fields = ('id',
Tony Mack29c287f2013-04-11 21:07:16 -0400165 'instance_id',
Tony Mack5d6598c2013-04-12 12:45:02 -0400166 'name',
Tony Mack79748612013-05-01 14:52:03 -0400167 'instance_name',
Tony Mackd90cdbf2013-04-16 22:48:40 -0400168 'ip',
Tony Mack29c287f2013-04-11 21:07:16 -0400169 'image',
Tony Mack5d6598c2013-04-12 12:45:02 -0400170 'key',
Tony Mack7130ac32013-03-22 21:58:00 -0400171 'slice',
Tony Mack5d6598c2013-04-12 12:45:02 -0400172 'deploymentNetwork',
173 'node')
Tony Mack7130ac32013-03-22 21:58:00 -0400174
Tony Macka2dfb372013-04-09 11:02:32 -0400175class NodeSerializer(serializers.HyperlinkedModelSerializer):
176 # HyperlinkedModelSerializer doesn't include the id by default
177 id = serializers.Field()
Tony Mack7130ac32013-03-22 21:58:00 -0400178 class Meta:
179 model = Node
180 fields = ('id',
181 'name')
182
Tony Macka2dfb372013-04-09 11:02:32 -0400183class ImageSerializer(serializers.HyperlinkedModelSerializer):
184 # HyperlinkedModelSerializer doesn't include the id by default
185 id = serializers.Field()
Tony Mack53310082013-04-09 09:26:12 -0400186 class Meta:
187 model = Image
Tony Mack879d14a2013-04-09 09:34:19 -0400188 fields = ('id',
189 'image_id',
Tony Mack53310082013-04-09 09:26:12 -0400190 'name',
191 'disk_format',
192 'container_format')
193
Tony Macka28f1cd2013-04-07 20:02:31 -0400194serializerLookUp = {
195 Role: RoleSerializer,
Tony Mack0e723b92013-04-27 11:08:19 -0400196 PLUser: UserSerializer,
Tony Macke4cee7a2013-04-09 12:45:32 -0400197 Key: KeySerializer,
Tony Macka28f1cd2013-04-07 20:02:31 -0400198 Site: SiteSerializer,
Tony Mack29c287f2013-04-11 21:07:16 -0400199 SitePrivilege: SitePrivilegeSerializer,
Tony Mack7130ac32013-03-22 21:58:00 -0400200 Slice: SliceSerializer,
Tony Mack29c287f2013-04-11 21:07:16 -0400201 SliceMembership: SliceMembershipSerializer,
202 Subnet: SubnetSerializer,
Tony Mack7130ac32013-03-22 21:58:00 -0400203 Node: NodeSerializer,
204 Sliver: SliverSerializer,
205 DeploymentNetwork: DeploymentNetworkSerializer,
Tony Mack53310082013-04-09 09:26:12 -0400206 Image: ImageSerializer,
Tony Mack7130ac32013-03-22 21:58:00 -0400207 None: None,
208 }
Tony Mack53310082013-04-09 09:26:12 -0400209