Commit b6a0fdb4 by Kálmán Viktor

occi: network list

parent 4b185b09
...@@ -523,6 +523,52 @@ class StorageLink(Link): ...@@ -523,6 +523,52 @@ class StorageLink(Link):
self.instance.detach_disk(user=user, disk=self.disk) self.instance.detach_disk(user=user, disk=self.disk)
class Network(Resource):
def __init__(self, vlan=None, data=None):
self.attrs = {}
if vlan:
self.location = "/network2/%d/" % (vlan.vid)
self.vlan = vlan
self.init_attrs()
@classmethod
def create_object(cls, data):
pass
def render_location(self):
return "%s" % self.location
def render_body(self):
kind = NETWORK_KIND
mixins = []
return render_to_string("occi/network.html", {
'kind': kind,
'attrs': self.attrs,
'mixins': mixins,
})
def init_attrs(self):
translate = {
'occi.core.id': "vid",
'occi.core.title': "name",
'occi.network.vlan': "vid",
'occi.network.label': "name",
}
for k, v in translate.items():
self.attrs[k] = getattr(self.vlan, v, None)
self.attrs['occi.compute.state'] = "active"
def trigger_action(self, data):
pass
def delete(self):
# TODO
user = User.objects.get(username="test")
self.instance.destroy(user=user)
"""predefined stuffs """predefined stuffs
...@@ -656,3 +702,35 @@ STORAGE_LINK_KIND = Kind( ...@@ -656,3 +702,35 @@ STORAGE_LINK_KIND = Kind(
location="/link/storagelink/", location="/link/storagelink/",
attributes=STORAGE_LINK_ATTRS attributes=STORAGE_LINK_ATTRS
) )
NETWORK_ATTRS = [
Attribute("occi.network.vlan"),
Attribute("occi.network.label"),
Attribute("occi.network.state", "immutable"),
]
NETWORK_KIND = Kind(
term="network",
scheme="http://schemas.ogf.org/occi/infrastructure#network",
class_="kind",
title="network resource",
rel="http://schemas.ogf.org/occi/core#resource",
location="/network2/",
attributes=NETWORK_ATTRS,
)
IPNETWORK_ATTRS = [
Attribute("occi.network.address"),
Attribute("occi.network.gateway"),
Attribute("occi.network.allocation"),
]
IPNETWORK_MIXIN = Kind(
term="ipnetwork",
scheme="http://schemas.ogf.org/occi/infrastructure/network#",
class_="mixin",
title="ipnetwork",
location="/mixin/ipnetwork/",
attributes=IPNETWORK_ATTRS,
)
Category: compute; scheme="{{ kind.scheme }}"; class="{{ kind.class }}";
{% for m in mixins %}
{{ m.render_body }}
{% endfor %}
{% for k, v in attrs.items %}
X-OCCI-Attribute: {{ k }}={% if v.isdigit == False or k == "occi.core.id" %}"{{ v }}"{% else %}{{ v }}{% endif %}
{% endfor %}
...@@ -20,7 +20,8 @@ from django.conf.urls import url, patterns ...@@ -20,7 +20,8 @@ from django.conf.urls import url, patterns
from occi.views import ( from occi.views import (
QueryInterface, ComputeInterface, VmInterface, OsTplInterface, QueryInterface, ComputeInterface, VmInterface, OsTplInterface,
StorageInterface, DiskInterface, StorageLinkInterface StorageInterface, DiskInterface, StorageLinkInterface,
NetworkInterface, VlanInterface,
) )
urlpatterns = patterns( urlpatterns = patterns(
...@@ -29,10 +30,14 @@ urlpatterns = patterns( ...@@ -29,10 +30,14 @@ urlpatterns = patterns(
url(r'^compute/$', ComputeInterface.as_view(), name="occi.compute"), url(r'^compute/$', ComputeInterface.as_view(), name="occi.compute"),
url(r'^os_tpl/$', OsTplInterface.as_view(), name="occi.os_tpl"), url(r'^os_tpl/$', OsTplInterface.as_view(), name="occi.os_tpl"),
url(r'^vm/(?P<pk>\d+)/?$', VmInterface.as_view(), name="occi.vm"), url(r'^vm/(?P<pk>\d+)/?$', VmInterface.as_view(), name="occi.vm"),
url(r'^storage/$', StorageInterface.as_view(), name="occi.storage"), url(r'^storage/$', StorageInterface.as_view(), name="occi.storage"),
url(r'^storage/(?P<pk>\d+)/?$', DiskInterface.as_view(), name="occi.disk"), url(r'^storage/(?P<pk>\d+)/?$', DiskInterface.as_view(), name="occi.disk"),
url(r'^link/storagelink/$', StorageLinkInterface.as_view()), url(r'^link/storagelink/$', StorageLinkInterface.as_view()),
url(r'^link/storagelink/vm(?P<vm_pk>\d+)_disk(?P<disk_pk>\d+)/?$', url(r'^link/storagelink/vm(?P<vm_pk>\d+)_disk(?P<disk_pk>\d+)/?$',
StorageLinkInterface.as_view(), name="occi.storagelink"), StorageLinkInterface.as_view(), name="occi.storagelink"),
url(r'^network2/?$', NetworkInterface.as_view(), ),
url(r'^network2/(?P<vid>\d+)/?$', VlanInterface.as_view(), ),
) )
...@@ -4,12 +4,14 @@ from django.utils.decorators import method_decorator ...@@ -4,12 +4,14 @@ from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View, DetailView from django.views.generic import View, DetailView
from firewall.models import Vlan
from vm.models import Instance, InstanceTemplate from vm.models import Instance, InstanceTemplate
from storage.models import Disk from storage.models import Disk
from .occi import ( from .occi import (
Compute, Compute,
Storage, Storage,
Network,
OsTemplate, OsTemplate,
StorageLink, StorageLink,
COMPUTE_KIND, COMPUTE_KIND,
...@@ -18,6 +20,8 @@ from .occi import ( ...@@ -18,6 +20,8 @@ from .occi import (
STORAGE_LINK_KIND, STORAGE_LINK_KIND,
COMPUTE_ACTIONS, COMPUTE_ACTIONS,
OS_TPL_MIXIN, OS_TPL_MIXIN,
NETWORK_KIND,
IPNETWORK_MIXIN,
) )
...@@ -49,6 +53,8 @@ class QueryInterface(View): ...@@ -49,6 +53,8 @@ class QueryInterface(View):
response += "Category: %s\n" % LINK_KIND.render_values() response += "Category: %s\n" % LINK_KIND.render_values()
response += "Category: %s\n" % STORAGE_LINK_KIND.render_values() response += "Category: %s\n" % STORAGE_LINK_KIND.render_values()
response += "Category: %s\n" % OS_TPL_MIXIN.render_values() response += "Category: %s\n" % OS_TPL_MIXIN.render_values()
response += "Category: %s\n" % NETWORK_KIND.render_values()
response += "Category: %s\n" % IPNETWORK_MIXIN.render_values()
for c in COMPUTE_ACTIONS: for c in COMPUTE_ACTIONS:
response += "Category: %s\n" % c.render_values() response += "Category: %s\n" % c.render_values()
...@@ -251,3 +257,48 @@ class StorageLinkInterface(View): ...@@ -251,3 +257,48 @@ class StorageLinkInterface(View):
@method_decorator(csrf_exempt) @method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
return super(StorageLinkInterface, self).dispatch(*args, **kwargs) return super(StorageLinkInterface, self).dispatch(*args, **kwargs)
class NetworkInterface(View):
def get(self, request, *args, **kwargs):
response = "\n".join([Network(vlan=v).render_location()
for v in Vlan.objects.all()])
return HttpResponse(
response,
content_type="text/plain",
)
def post(self, request, *args, **kwargs):
pass # we don't really want to create networks via OCCI yet
@method_decorator(csrf_exempt) # decorator on post method doesn't work
def dispatch(self, *args, **kwargs):
return super(NetworkInterface, self).dispatch(*args, **kwargs)
class VlanInterface(DetailView):
model = Vlan
slug_field = 'vid'
slug_url_kwarg = 'vid'
def get(self, request, *args, **kwargs):
vlan = self.get_object()
c = Network(vlan=vlan)
return HttpResponse(
c.render_body(),
content_type="text/plain",
)
def post(self, request, *args, **kwargs):
pass # no actions
def delete(self, request, *args, **kwargs):
vm = self.get_object()
Compute(instance=vm).delete()
return HttpResponse()
@method_decorator(csrf_exempt) # decorator on post method doesn't work
def dispatch(self, *args, **kwargs):
return super(VlanInterface, self).dispatch(*args, **kwargs)
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