Commit 682fa2b9 by Czémán Arnold

minor modifications

parent 01bf8c3a
from django.contrib import admin
# Register your models here.
from django.db import models
# Create your models here.
import requests
from requests.exceptions import ConnectionError
import json
# import urllib3
......@@ -18,185 +17,181 @@ loginData = {"username": username, "password": password}
# automatikusan benne maradjon az osszes keresunkben
with requests.Session() as session:
headers = {"Content-Type": "application/json", "Referer": server}
try:
# Csrf-Token a bejelentkezeshez
req = session.get(server + "occi/login/", headers=headers,
verify=False)
print("csrf-token")
print("----------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# Csrf-Token a bejelentkezeshez
req = session.get(server + "occi/login/", headers=headers,
verify=False)
print("csrf-token")
print("----------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# Bejelentkezes
# POST, DELETE, PUT keresek elott be kell allitani az X-CSRFToken
# header erteket az aktualis csrftoken-re, amely mindig benne van
# a cookie-ban
headers["X-CSRFToken"] = req.cookies['csrftoken']
req = session.post(server + "occi/login/", verify=False,
data=json.dumps(loginData), headers=headers)
print("login")
print("-----")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# Bejelentkezes
# POST, DELETE, PUT keresek elott be kell allitani az X-CSRFToken
# header erteket az aktualis csrftoken-re, amely mindig benne van
# a cookie-ban
headers["X-CSRFToken"] = req.cookies['csrftoken']
req = session.post(server + "occi/login/", verify=False,
data=json.dumps(loginData), headers=headers)
print("login")
print("-----")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# query interface
req = session.get(server + "occi/-/", headers=headers, verify=False)
print("query-interface")
print("---------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# query interface
req = session.get(server + "occi/-/", headers=headers, verify=False)
print("query-interface")
print("---------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# osszes vm collectionkent
req = session.get(server + "occi/compute/", headers=headers,
verify=False)
print("compute-collection")
print("------------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# az elso vm a listabol
vmid = json.loads(req.text)["resources"][0]["id"]
req = session.get(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False)
print("compute-"+str(vmid))
print("------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# osszes vm collectionkent
req = session.get(server + "occi/compute/", headers=headers,
verify=False)
print("compute-collection")
print("------------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# ha nem active, akkor azza tesszuk
state = json.loads(req.text)["attributes"]["occi.compute.state"]
action = "http://schemas.ogf.org/occi/infrastructure/compute/action#"
if state != "active":
try:
headers["X-CSRFToken"] = req.cookies['csrftoken']
except:
pass
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps({"action": action + "start"}))
print("compute-" + str(vmid) + "-start")
print("---------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# az elso vm a listabol
vmid = json.loads(req.text)["resources"][0]["id"]
req = session.get(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False)
print("compute-"+str(vmid))
print("------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# restart
# ha nem active, akkor azza tesszuk
state = json.loads(req.text)["attributes"]["occi.compute.state"]
action = "http://schemas.ogf.org/occi/infrastructure/compute/action#"
if state != "active":
try:
headers["X-CSRFToken"] = req.cookies['csrftoken']
except:
pass
actionatrs = {"method": "cold"}
actioninv = {"action": action + "restart", "attributes": actionatrs}
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(actioninv))
print("compute-"+str(vmid) + "-restart")
print("-----------------")
data=json.dumps({"action": action + "start"}))
print("compute-" + str(vmid) + "-start")
print("---------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# suspend
try:
headers["X-CSRFToken"] = req.cookies['csrftoken']
except:
pass
actioninv["action"] = action + "suspend"
actioninv["attributes"]["method"] = "suspend"
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(actioninv))
print("compute-" + str(vmid) + "-suspend")
print("-----------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# restart
try:
headers["X-CSRFToken"] = req.cookies['csrftoken']
except:
pass
actionatrs = {"method": "cold"}
actioninv = {"action": action + "restart", "attributes": actionatrs}
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(actioninv))
print("compute-"+str(vmid) + "-restart")
print("-----------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# nem letezo action
try:
headers["X-CSRFToken"] = req.cookies["csrftoken"]
except:
pass
actioninv["action"] = action + "noaction"
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(actioninv))
print("compute-" + str(vmid) + "-noaction")
print("-------------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# suspend
try:
headers["X-CSRFToken"] = req.cookies['csrftoken']
except:
pass
actioninv["action"] = action + "suspend"
actioninv["attributes"]["method"] = "suspend"
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(actioninv))
print("compute-" + str(vmid) + "-suspend")
print("-----------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# vm krealas
try:
headers["X-CSRFToken"] = req.cookies["csrftoken"]
except:
pass
# a template mixinje benne kell legyen az adatokban
# az osszes template a query interfacen megjelenik mint mixin
# azok a mixinek templatek amik az os_tpl mixintol fuggnek
putdata = {"mixins": [
"http://circlecloud.org/occi/templates/os#os_template_1"],
"other_occi_compute_data": "may be provided"}
req = session.put(server + "occi/compute/1/",
headers=headers, verify=False,
data=json.dumps(putdata))
print("create_compute")
print("--------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# nem letezo action
try:
headers["X-CSRFToken"] = req.cookies["csrftoken"]
except:
pass
actioninv["action"] = action + "noaction"
req = session.post(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(actioninv))
print("compute-" + str(vmid) + "-noaction")
print("-------------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# vm krealas
try:
headers["X-CSRFToken"] = req.cookies["csrftoken"]
except:
pass
# a template mixinje benne kell legyen az adatokban
# az osszes template a query interfacen megjelenik mint mixin
# azok a mixinek templatek amik az os_tpl mixintol fuggnek
putdata = {"mixins": [
"http://circlecloud.org/occi/templates/os#os_template_1"],
"other_occi_compute_data": "may be provided"}
req = session.put(server + "occi/compute/1/",
headers=headers, verify=False,
data=json.dumps(putdata))
print("create_compute")
print("--------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# vm torles
try:
headers["X-CSRFToken"] = req.cookies["csrftoken"]
except:
pass
vmid = json.loads(req.text)["id"]
req = session.delete(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(putdata))
print("delete_compute")
print("--------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# vm torles
try:
headers["X-CSRFToken"] = req.cookies["csrftoken"]
except:
pass
vmid = json.loads(req.text)["id"]
req = session.delete(server + "occi/compute/" + vmid + "/",
headers=headers, verify=False,
data=json.dumps(putdata))
print("delete_compute")
print("--------------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
print
# Kijelentkezes
req = session.get(server + "occi/logout/", headers=headers,
verify=False)
print("logout")
print("------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
except ConnectionError as e:
print(e)
# Kijelentkezes
req = session.get(server + "occi/logout/", headers=headers,
verify=False)
print("logout")
print("------")
print("status_code: " + str(req.status_code))
print
print(json.dumps(json.loads(req.text), sort_keys=True,
indent=4, separators=(",", ": ")))
......@@ -215,8 +215,8 @@ CREDENTIALS_MIXIN = Mixin("http://circlecloud.org/occi/infrastructure#",
"credentials",
title="Credentials Mixin",
attributes=CREDENTIALS_ATTRIBUTES,
applies="http://schemas.ogf.org/occi/infrastructure" +
"#compute")
applies="http://schemas.ogf.org/occi/" +
"infrastructure#compute")
OS_TPL_MIXIN = Mixin("http://schemas.ogf.org/occi/infrastructure#",
"os_tpl",
......
......@@ -87,8 +87,8 @@ def validate_request(request, authentication_required=True,
if has_data:
# checking content type
if request.META.get("CONTENT_TYPE") != "application/json":
raise OcciRequestNotValid("Only application/json content type is " +
"allowed.")
raise OcciRequestNotValid("Only application/json content type" +
" is allowed.")
# checking if the data is a valid json
try:
data = json.loads(request.body.decode("utf-8"))
......
from django.test import TestCase
# Create your tests here.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment