66 API
This commit is contained in:
parent
0964f0acff
commit
a5fdada20a
6 changed files with 38 additions and 148 deletions
|
@ -39,3 +39,12 @@ class ControllerDataCenters:
|
||||||
'city': item.city,
|
'city': item.city,
|
||||||
})
|
})
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def exists(datacenter_id, status=1):
|
||||||
|
if models.DataCenters.select().where(
|
||||||
|
models.DataCenters.id == datacenter_id,
|
||||||
|
models.DataCenters.status == status
|
||||||
|
).count() == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
|
@ -14,6 +14,14 @@ class ControllerPlans(object):
|
||||||
return PlansVMs.select().where(PlansVMs.status == status)
|
return PlansVMs.select().where(PlansVMs.status == status)
|
||||||
return PlansVMs.select()
|
return PlansVMs.select()
|
||||||
|
|
||||||
|
def get_plans(self, status=None):
|
||||||
|
x = PlansVMs.select().where(PlansVMs.status == status) if status else PlansVMs.select()
|
||||||
|
|
||||||
|
results = list()
|
||||||
|
for i in x:
|
||||||
|
results.append(dict(id=str(i.id), name=i.name, price=i.price, cores=i.cores, storage=i.storage, swap=i.swap, memory=i.memory))
|
||||||
|
return results
|
||||||
|
|
||||||
def plan_get(self, plan_id):
|
def plan_get(self, plan_id):
|
||||||
return PlansVMs.select().where(PlansVMs.id == plan_id).get()
|
return PlansVMs.select().where(PlansVMs.id == plan_id).get()
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,30 @@
|
||||||
from SWSCloudCore.models import Vms
|
from SWSCloudCore.models import Vms
|
||||||
|
|
||||||
|
|
||||||
|
class Item:
|
||||||
|
def __init__(self, val):
|
||||||
|
self.val = val
|
||||||
|
|
||||||
|
|
||||||
class ControllerVMS(object):
|
class ControllerVMS(object):
|
||||||
def __init__(self, user_id):
|
def __init__(self, user_id):
|
||||||
self.user_id = user_id
|
self.user_id = user_id
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# TODO: DEPRECATED
|
||||||
def get(self, vm_id=None):
|
def get(self, vm_id=None):
|
||||||
if vm_id:
|
results = list()
|
||||||
return Vms.select().where(Vms.user == self.user_id, Vms.id == vm_id).get()
|
return Vms.select().where(Vms.user == self.user_id, Vms.id == vm_id).get() if vm_id else Vms.select().where(Vms.user == self.user_id)
|
||||||
return Vms.select().where(Vms.user == self.user_id)
|
|
||||||
|
def get_items(self):
|
||||||
|
items = list()
|
||||||
|
for x in Vms.select().where(Vms.user == self.user_id):
|
||||||
|
items.append(dict(id=str(x.id), datacenter=str(x.datacenter.id), plan=x.plan.id, hostname=x.hostname,
|
||||||
|
osname=x.os_name, ossuite=x.os_suite, ipv4=x.ipv4, ipv6=x.ipv6, status=x.status))
|
||||||
|
return items
|
||||||
|
|
||||||
|
def get_item(self, vm_id):
|
||||||
|
return Vms.select().where(Vms.user == self.user_id, Vms.id == vm_id).get()
|
||||||
|
|
||||||
def create(self, datacenter_id, server_id, vm_id,
|
def create(self, datacenter_id, server_id, vm_id,
|
||||||
hostname, ipv4, ipv6, plan, platform,
|
hostname, ipv4, ipv6, plan, platform,
|
||||||
|
|
|
@ -1,143 +0,0 @@
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
from flask import Blueprint, jsonify, request
|
|
||||||
from SWSCloudCore.controllers.containers import ControllerContainers
|
|
||||||
from SWSCloudCore.controllers.datacenters import ControllerDataCenters
|
|
||||||
from SWSCloudCore.controllers.users import ControllerAPI
|
|
||||||
from SWSCloudCore.controllers.users import ControllerUsers
|
|
||||||
|
|
||||||
viewAPI = Blueprint('api', __name__, url_prefix='/api')
|
|
||||||
|
|
||||||
|
|
||||||
# /auth
|
|
||||||
@viewAPI.route('/auth', methods=['POST'])
|
|
||||||
def auth():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/auth --data 'email=<email>&secret=<secret>'
|
|
||||||
TODO:
|
|
||||||
Процесс авторизации
|
|
||||||
- получаем емейл и секретный ключ
|
|
||||||
- создаём временный токен
|
|
||||||
- выдаём токен
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
email = request.form['email']
|
|
||||||
secret = request.form['secret']
|
|
||||||
|
|
||||||
if not ControllerAPI().auth(email, secret):
|
|
||||||
return jsonify(status=1)
|
|
||||||
|
|
||||||
user_id = ControllerUsers().get_id_by_email(email)
|
|
||||||
|
|
||||||
return jsonify(user_id=user_id)
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/datacenter/list', methods=['POST'])
|
|
||||||
def datacenter_list():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/datacenter/list --data 'email=<email>&secret=<secret>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
email = request.form['email']
|
|
||||||
secret = request.form['secret']
|
|
||||||
#
|
|
||||||
if not ControllerAPI().auth(email, secret):
|
|
||||||
return jsonify(status=403), 403
|
|
||||||
#
|
|
||||||
user_id = ControllerUsers().get_id_by_email(email)
|
|
||||||
# get containers list
|
|
||||||
datacenters = ControllerDataCenters().get()
|
|
||||||
#
|
|
||||||
return jsonify(
|
|
||||||
total=datacenters['total'],
|
|
||||||
items=datacenters['items']
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# /container/create
|
|
||||||
# /container/details/<uuid:container_id>
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/container/list', methods=['POST'])
|
|
||||||
def container_list():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/container/list --data 'email=<email>&secret=<secret>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
email = request.form['email']
|
|
||||||
secret = request.form['secret']
|
|
||||||
# expire = request.form['expire']
|
|
||||||
#
|
|
||||||
if not ControllerAPI().auth(email, secret):
|
|
||||||
return jsonify(status=1)
|
|
||||||
#
|
|
||||||
user_id = ControllerUsers().get_id_by_email(email)
|
|
||||||
# get containers list
|
|
||||||
containers = ControllerContainers(user_id).get_items()
|
|
||||||
#
|
|
||||||
return jsonify(
|
|
||||||
status=0,
|
|
||||||
total=containers['total'],
|
|
||||||
items=containers['items']
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/container/create', methods=['POST'])
|
|
||||||
def container_create():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/container/create --data 'email=<email>&secret=<secret>&datacenter_id=<datacenter_id>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
email = request.form['email']
|
|
||||||
secret = request.form['secret']
|
|
||||||
datacenter_id = request.form['datacenter_id']
|
|
||||||
# expire = request.form['expire']
|
|
||||||
#
|
|
||||||
if not ControllerAPI().auth(email, secret):
|
|
||||||
return jsonify(status=1)
|
|
||||||
#
|
|
||||||
user_id = ControllerUsers().get_id_by_email(email)
|
|
||||||
# get containers list
|
|
||||||
containers = ControllerContainers(user_id).get_items()
|
|
||||||
#
|
|
||||||
return jsonify(
|
|
||||||
status=0,
|
|
||||||
total=containers['total'],
|
|
||||||
items=containers['items']
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/container/delete', methods=['POST'])
|
|
||||||
def container_delete():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/container/delete --data 'email=<str:email>&secret=<str:secret>&container_id=<uuid:container_id>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
return jsonify(status=0)
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/container/stop', methods=['POST'])
|
|
||||||
def container_stop():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/container/stop --data 'email=<str:email>&secret=<str:secret>&container_id=<uuid:container_id>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
return jsonify(status=0)
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/container/start', methods=['POST'])
|
|
||||||
def container_start():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/container/start --data 'email=<str:email>&secret=<str:secret>&container_id=<uuid:container_id>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
return jsonify(status=0)
|
|
||||||
|
|
||||||
|
|
||||||
@viewAPI.route('/container/restart', methods=['POST'])
|
|
||||||
def container_restart():
|
|
||||||
"""
|
|
||||||
curl -X POST http://localhost:5000/api/container/restart --data 'email=<str:email>&secret=<str:secret>&container_id=<uuid:container_id>'
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
return jsonify(status=0)
|
|
|
@ -70,7 +70,7 @@ def create():
|
||||||
'ipv6_gateway': select_ip.ipv6_gateway,
|
'ipv6_gateway': select_ip.ipv6_gateway,
|
||||||
'username': 'ubuntu',
|
'username': 'ubuntu',
|
||||||
'password': password,
|
'password': password,
|
||||||
'ssh_key': None,
|
'ssh_key': '',
|
||||||
}
|
}
|
||||||
|
|
||||||
# sshkey
|
# sshkey
|
||||||
|
|
3
setup.py
3
setup.py
|
@ -8,6 +8,8 @@ setup(
|
||||||
author='Vyacheslav Anzhiganov',
|
author='Vyacheslav Anzhiganov',
|
||||||
author_email='hello@anzhiganov.com',
|
author_email='hello@anzhiganov.com',
|
||||||
packages=[
|
packages=[
|
||||||
|
'SWSCloudAPI',
|
||||||
|
'SWSCloudAPI.API',
|
||||||
'SWSCloudCore',
|
'SWSCloudCore',
|
||||||
'SWSCloudCore.controllers',
|
'SWSCloudCore.controllers',
|
||||||
'SWSCloudCore.controllers.administrators',
|
'SWSCloudCore.controllers.administrators',
|
||||||
|
@ -30,7 +32,6 @@ setup(
|
||||||
'SWSCloudCore.views',
|
'SWSCloudCore.views',
|
||||||
'SWSCloudCore.views.account',
|
'SWSCloudCore.views.account',
|
||||||
'SWSCloudCore.views.administrator',
|
'SWSCloudCore.views.administrator',
|
||||||
'SWSCloudCore.views.api',
|
|
||||||
'SWSCloudCore.views.containers',
|
'SWSCloudCore.views.containers',
|
||||||
'SWSCloudCore.views.documents',
|
'SWSCloudCore.views.documents',
|
||||||
'SWSCloudCore.views.kb',
|
'SWSCloudCore.views.kb',
|
||||||
|
|
Loading…
Add table
Reference in a new issue