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
8c418722
authored
Apr 26, 2019
by
Belákovics Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basic API design
parent
70b0f82c
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
114 additions
and
44 deletions
+114
-44
recircle/instance/models.py
+34
-5
recircle/instance/serializers.py
+1
-1
recircle/instance/urls.py
+8
-5
recircle/instance/views.py
+51
-27
recircle/recircle/settings.py
+12
-2
recircle/recircle/urls.py
+8
-4
No files found.
recircle/instance/models.py
View file @
8c418722
from
django.db
import
models
from
django.conf
import
settings
class
Instance
(
models
.
Model
):
name
=
models
.
CharField
(
blank
=
True
,
max_length
=
100
,
verbose_name
=
"name"
,
help_text
=
"Human readable name of instance."
)
description
=
models
.
TextField
(
blank
=
True
,
verbose_name
=
"description"
,
help_text
=
"The description of the instance."
)
ACCESS_METHODS
=
tuple
([(
key
,
val
[
0
])
for
key
,
val
in
settings
.
VM_ACCESS_PROTOCOLS
.
items
()])
# Temporary solution
LEASE_TYPES
=
(
(
"project"
,
"Project"
),
(
"server"
,
"Server"
),
(
"infinite"
,
"Infinite lease"
)
)
# Temporary solution
OS_TYPES
=
(
(
"win10"
,
"Windows 10"
),
(
"ubuntu1804"
,
"Ubuntu 18.04"
),
)
class
Instance
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
,
help_text
=
"Human readable name of instance"
)
description
=
models
.
TextField
(
blank
=
True
,
help_text
=
"The description of the instance"
)
access_method
=
models
.
CharField
(
max_length
=
10
,
choices
=
ACCESS_METHODS
,
help_text
=
"Primary remote access method."
)
lease
=
models
.
CharField
(
max_length
=
50
,
choices
=
LEASE_TYPES
,
help_text
=
"Expiration method"
)
system
=
models
.
CharField
(
max_length
=
50
,
choices
=
OS_TYPES
,
help_text
=
"Operating system type"
)
# template
# password
# time_of_suspend
# time_of_delete
# disks
# owner
recircle/instance/serializers.py
View file @
8c418722
...
...
@@ -9,4 +9,4 @@ class InstanceSerializer(serializers.ModelSerializer):
model
=
Instance
fields
=
(
"name"
,
"description"
,
)
)
recircle/instance/urls.py
View file @
8c418722
from
django.urls
import
path
from
.
import
views
from
rest_framework.urlpatterns
import
format_suffix_patterns
from
instance
import
views
urlpatterns
=
[
path
(
''
,
views
.
instance_list
),
]
\ No newline at end of file
path
(
'instances/'
,
views
.
InstanceList
.
as_view
()),
path
(
'instances/<int:pk>/'
,
views
.
InstanceDetail
.
as_view
()),
path
(
'instances/<int:pk>/action/'
,
views
.
InstanceAction
.
as_view
())
]
urlpatterns
=
format_suffix_patterns
(
urlpatterns
)
recircle/instance/views.py
View file @
8c418722
from
django.http
import
HttpResponse
,
JsonResponse
from
django.views.decorators.csrf
import
csrf_exempt
from
rest_framework.renderers
import
JSONRenderer
from
rest_framework.parsers
import
JSONParser
from
instance.models
import
Instance
from
instance.serializers
import
InstanceSerializer
def
index
(
request
):
return
HttpResponse
(
"Hello, world. You're at the instance index."
)
@csrf_exempt
def
instance_list
(
request
):
"""
List all instances, or create a new one.
"""
if
request
.
method
==
'GET'
:
instances
=
Instance
.
objects
.
all
()
serializer
=
InstanceSerializer
(
instances
,
many
=
True
)
return
JsonResponse
(
serializer
.
data
,
safe
=
False
)
elif
request
.
method
==
'POST'
:
data
=
JSONParser
()
.
parse
(
request
)
serializer
=
InstanceSerializer
(
data
=
data
)
if
serializer
.
is_valid
():
serializer
.
save
()
return
JsonResponse
(
serializer
.
data
,
status
=
201
)
return
JsonResponse
(
serializer
.
errors
,
status
=
400
)
from
django.http
import
Http404
from
rest_framework.views
import
APIView
from
rest_framework.response
import
Response
from
rest_framework
import
status
# class InstanceList(APIView):
# def get(self, request, format=None):
# instances = Instance.objects.all()
# # get OpenStack attribs
# # create response
# # serializer = SnippetSerializer(snippets, many=True)
# return Response(serializer.data)
# def post(self, request, format=None):
# serializer = SnippetSerializer(data=request.data)
# if serializer.is_valid():
# serializer.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# class SnippetDetail(APIView):
# """
# Retrieve, update or delete a snippet instance.
# """
# def get_object(self, pk):
# try:
# return Snippet.objects.get(pk=pk)
# except Snippet.DoesNotExist:
# raise Http404
# def get(self, request, pk, format=None):
# snippet = self.get_object(pk)
# serializer = SnippetSerializer(snippet)
# return Response(serializer.data)
# def put(self, request, pk, format=None):
# snippet = self.get_object(pk)
# serializer = SnippetSerializer(snippet, data=request.data)
# if serializer.is_valid():
# serializer.save()
# return Response(serializer.data)
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# def delete(self, request, pk, format=None):
# snippet = self.get_object(pk)
# snippet.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
recircle/recircle/settings.py
View file @
8c418722
...
...
@@ -43,6 +43,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles'
,
'rest_framework'
,
'djoser'
,
'rest_framework_swagger'
,
]
MIDDLEWARE
=
[
...
...
@@ -123,4 +124,14 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL
=
'/static/'
\ No newline at end of file
STATIC_URL
=
'/static/'
################################################################################################
# RECIRCLE CONFIG
################################################################################################
# VM ACCESS PROTOCOLS
VM_ACCESS_PROTOCOLS
=
{
"rdp"
:
[
"Remote Desktop Protocol"
,
3389
,
"tcp"
],
"ssh"
:
[
"Secure Shell"
,
22
,
"tcp"
]
}
recircle/recircle/urls.py
View file @
8c418722
...
...
@@ -15,10 +15,15 @@ Including another URLconf
"""
from
django.contrib
import
admin
from
django.urls
import
path
,
re_path
,
include
from
rest_framework_swagger.views
import
get_swagger_view
schema_view
=
get_swagger_view
(
title
=
'RECIRCLE API'
)
urlpatterns
=
[
path
(
'
instance/'
,
include
(
'instance.urls'
)),
path
(
'
api/v1/'
,
include
(
endpoints_v1
)),
path
(
'admin/'
,
admin
.
site
.
urls
),
re_path
(
r'^auth/'
,
include
(
'djoser.urls'
)),
re_path
(
r'^auth/'
,
include
(
'djoser.urls.authtoken'
))
]
\ No newline at end of file
re_path
(
r'^auth/'
,
include
(
'djoser.urls.authtoken'
)),
path
(
r'swagger'
,
schema_view
)
]
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