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