Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
RECIRCLE
/
portal
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
11
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a94cb580
authored
Apr 08, 2020
by
Chif Gergő
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add instance list item serializer, fixes in create views
parent
1315d487
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
19 deletions
+42
-19
recircle/instance/serializers.py
+19
-11
recircle/instance/views.py
+23
-8
No files found.
recircle/instance/serializers.py
View file @
a94cb580
...
...
@@ -2,17 +2,6 @@ from rest_framework import serializers
from
.models
import
Flavor
,
Instance
,
Lease
class
InstanceSerializer
(
serializers
.
ModelSerializer
):
lease
=
serializers
.
PrimaryKeyRelatedField
(
read_only
=
True
)
flavor
=
serializers
.
PrimaryKeyRelatedField
(
read_only
=
True
)
class
Meta
:
model
=
Instance
fields
=
"__all__"
read_only_fields
=
(
"id"
,
"status"
"password"
,
"template"
,
"time_of_suspend"
,
"time_of_delete"
)
class
FlavorSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Flavor
...
...
@@ -23,3 +12,22 @@ class LeaseSerializer(serializers.ModelSerializer):
class
Meta
:
model
=
Lease
fields
=
"__all__"
class
InstanceListItemSerializer
(
serializers
.
ModelSerializer
):
lease
=
LeaseSerializer
(
read_only
=
True
)
class
Meta
:
model
=
Instance
fields
=
[
"id"
,
"name"
,
"status"
,
"system_type"
,
"distro"
,
"status"
,
"lease"
]
class
InstanceSerializer
(
serializers
.
ModelSerializer
):
lease
=
serializers
.
PrimaryKeyRelatedField
(
read_only
=
True
)
flavor
=
serializers
.
PrimaryKeyRelatedField
(
read_only
=
True
)
class
Meta
:
model
=
Instance
fields
=
"__all__"
read_only_fields
=
(
"id"
,
"status"
"password"
,
"template"
,
"time_of_suspend"
,
"time_of_delete"
)
recircle/instance/views.py
View file @
a94cb580
from
instance.serializers
import
InstanceSerializer
,
FlavorSerializer
,
LeaseSerializer
from
instance.serializers
import
(
InstanceSerializer
,
InstanceListItemSerializer
,
FlavorSerializer
,
LeaseSerializer
)
from
django.http
import
Http404
from
django.db.models
import
Q
from
rest_framework.viewsets
import
ViewSet
,
ModelViewSet
from
rest_framework.response
import
Response
from
rest_framework
import
status
from
rest_framework.decorators
import
action
from
rest_framework.permissions
import
IsAuthenticated
from
template.serializers
import
InstanceFromTemplateSerializer
from
instance.models
import
Instance
,
Flavor
,
Lease
from
template.models
import
ImageTemplate
from
template.serializers
import
ImageTemplateModelSerializer
from
authorization.mixins
import
AuthorizationMixin
from
guardian.shortcuts
import
get_perms_for_model
,
assign_perm
authorization
=
{
...
...
@@ -49,6 +53,7 @@ update_actions = [
class
InstanceViewSet
(
AuthorizationMixin
,
ViewSet
):
authorization
=
authorization
permission_classes
=
[
IsAuthenticated
]
def
get_object
(
self
,
pk
):
try
:
...
...
@@ -71,28 +76,35 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
def
list
(
self
,
request
):
instances
=
self
.
get_objects_with_perms
(
request
.
user
,
"list"
,
Instance
)
return
Response
(
InstanceSerializer
(
instances
,
many
=
True
)
.
data
)
return
Response
(
Instance
ListItem
Serializer
(
instances
,
many
=
True
)
.
data
)
def
create
(
self
,
request
):
if
not
self
.
has_perms_for_model
(
request
.
user
,
'create'
):
return
Response
({
"error"
:
"No permission to create Virtual Machine."
},
status
=
status
.
HTTP_401_UNAUTHORIZED
)
params
=
request
.
data
template
=
ImageTemplate
.
objects
.
get
(
pk
=
params
[
"template"
])
template
=
ImageTemplate
.
objects
.
get
(
Q
(
pk
=
params
[
"template"
])
&
(
Q
(
owned_by
=
request
.
user
)
|
Q
(
type
=
"SYSTEM"
)))
if
not
template
:
return
Response
({
"error"
:
"No permission to use this template."
},
status
=
status
.
HTTP_401_UNAUTHORIZED
)
flavor
=
Flavor
.
objects
.
get
(
pk
=
params
[
"flavor"
])
if
"flavor"
in
params
else
template
.
flavor
lease
=
Lease
.
objects
.
get
(
pk
=
params
[
"lease"
])
if
"lease"
in
params
else
template
.
lease
newInstance
=
Instance
.
create_instance_from_template
(
params
=
{
"name"
:
params
[
"name"
],
"description"
:
params
[
"description"
],
"access_method"
:
params
[
"access"
],
"system"
:
params
[
"system"
],
},
flavor
=
flavor
,
lease
=
lease
,
networks
=
[{
"uuid"
:
template
.
network_id
}],
template
=
template
,
flavor
=
flavor
,
networks
=
[{
"uuid"
:
template
.
network_id
}]
,
owner
=
request
.
user
,
)
for
perm
in
get_perms_for_model
(
Instance
):
print
(
perm
)
assign_perm
(
perm
,
request
.
user
,
newInstance
)
return
Response
(
InstanceSerializer
(
newInstance
)
.
data
)
def
retrieve
(
self
,
request
,
pk
):
...
...
@@ -202,12 +214,15 @@ class FlavorViewSet(ViewSet):
def
create
(
self
,
request
,
format
=
None
):
data
=
request
.
data
prior
=
1
if
"priority"
in
data
:
prior
=
data
[
"priority"
]
new_flavor
=
Flavor
.
create
(
name
=
data
[
"name"
],
description
=
data
[
"description"
],
ram
=
data
[
"ram"
],
vcpu
=
data
[
"vcpu"
],
initial_disk
=
data
[
"initial_disk"
],
priority
=
data
[
"priority"
]
)
priority
=
prior
)
return
Response
(
new_flavor
.
pk
)
def
update
(
self
,
request
,
pk
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment