Commit 53fedb73 by Kálmán Viktor

occi: networkinterface describe, create, delete

parent 0ab76a05
...@@ -533,7 +533,7 @@ class Network(Resource): ...@@ -533,7 +533,7 @@ class Network(Resource):
def __init__(self, vlan=None, data=None): def __init__(self, vlan=None, data=None):
self.attrs = {} self.attrs = {}
if vlan: if vlan:
self.location = "/network2/%d/" % (vlan.vid) self.location = "/network/%d/" % (vlan.vid)
self.vlan = vlan self.vlan = vlan
self.init_attrs() self.init_attrs()
...@@ -608,19 +608,20 @@ class NetworkInterface(Link): ...@@ -608,19 +608,20 @@ class NetworkInterface(Link):
self.vlan = vlan self.vlan = vlan
self.attrs = {} self.attrs = {}
self.attrs['occi.core.id'] = "vm_%d_vlan_%d" % (instance.pk, vlan.vid) self.attrs['occi.core.id'] = "vm_%d_network_%d" % (instance.pk,
vlan.vid)
self.attrs['occi.core.target'] = Network(vlan).render_location() self.attrs['occi.core.target'] = Network(vlan).render_location()
self.attrs['occi.core.source'] = Compute(instance).render_location() self.attrs['occi.core.source'] = Compute(instance).render_location()
interface = Interface.objects.get(vlan=vlan, instance=instance) interface = Interface.objects.get(vlan=vlan, instance=instance)
# via networkinterface # via networkinterface
self.attrs['occi.networkinterface.mac'] = unicode(interface.host.mac) self.attrs['occi.networkinterface.mac'] = unicode(interface.mac)
self.attrs['occi.networkinterface.interface'] = self._get_interface() self.attrs['occi.networkinterface.interface'] = self._get_interface()
self.attrs['occi.core.state'] = "active" self.attrs['occi.core.state'] = "active"
# via ipnetworkinterface mixin # via ipnetworkinterface mixin
self.attrs['occi.networkinterface.address'] = unicode( self.attrs['occi.networkinterface.address'] = unicode(
interface.host.ipv4) interface.host.ipv4) if interface.host else "-"
self.attrs['occi.networkinterface.gateway'] = unicode( self.attrs['occi.networkinterface.gateway'] = unicode(
interface.vlan.network4.ip) interface.vlan.network4.ip)
self.attrs['occi.networkinterface.allocation'] = "dynamic" self.attrs['occi.networkinterface.allocation'] = "dynamic"
...@@ -647,14 +648,14 @@ class NetworkInterface(Link): ...@@ -647,14 +648,14 @@ class NetworkInterface(Link):
# TODO user # TODO user
user = User.objects.get(username="test") user = User.objects.get(username="test")
g = re.match(occi_attribute_link_regex % "vlan", target) g = re.match(occi_attribute_link_regex % "network", target)
vlan_vid = g.group("id") vlan_vid = g.group("id")
g = re.match(occi_attribute_link_regex % "vm", source) g = re.match(occi_attribute_link_regex % "vm", source)
vm_pk = g.group("id") vm_pk = g.group("id")
try: try:
vm = Instance.objects.filter(destroyed_at=None).get(pk=vm_pk) vm = Instance.objects.filter(destroyed_at=None).get(pk=vm_pk)
vlan = Vlan.objects.filter(destroyed=None).get(vid=vlan_vid) vlan = Vlan.objects.get(vid=vlan_vid)
except (Instance.DoesNotExist, Vlan.DoesNotExist): except (Instance.DoesNotExist, Vlan.DoesNotExist):
return None return None
...@@ -663,13 +664,13 @@ class NetworkInterface(Link): ...@@ -663,13 +664,13 @@ class NetworkInterface(Link):
except: except:
pass pass
cls.location = "%slink/networkinterface/vm_%s_vlan_%s" % ( cls.location = "%slink/networkinterface/vm_%s_network_%s" % (
OCCI_ADDR, vm_pk, vlan_vid) OCCI_ADDR, vm_pk, vlan_vid)
return cls return cls
def render_location(self): def render_location(self):
return "/link/networkinterface/vm_%d_vlan_%d" % (self.instance.pk, return "/link/networkinterface/vm_%d_network_%d" % (self.instance.pk,
self.vlan.vid) self.vlan.vid)
def render_as_link(self): def render_as_link(self):
kind = NETWORK_INTERFACE_KIND kind = NETWORK_INTERFACE_KIND
...@@ -683,9 +684,11 @@ class NetworkInterface(Link): ...@@ -683,9 +684,11 @@ class NetworkInterface(Link):
def render_as_category(self): def render_as_category(self):
kind = NETWORK_INTERFACE_KIND kind = NETWORK_INTERFACE_KIND
mixins = [IPNetworkInterface()]
return render_to_string("occi/networkinterface.html", { return render_to_string("occi/networkinterface.html", {
'kind': kind, 'kind': kind,
'mixins': mixins,
'attrs': self.attrs, 'attrs': self.attrs,
}) })
...@@ -698,6 +701,28 @@ class NetworkInterface(Link): ...@@ -698,6 +701,28 @@ class NetworkInterface(Link):
self.instance.remove_interface(user=user, interface=interface) self.instance.remove_interface(user=user, interface=interface)
class IPNetworkInterface(Mixin):
def __init__(self):
self.term = "ipnetworkinterface"
self.title = "ipnnetwork interface mixin"
self.scheme = ("http://schemas.ogf.org/occi/infrastructure/"
"networkinterface#")
self.location = "/mixin/ipnetworkinterface/"
def render_location(self):
return self.location
def render_body(self):
return render_to_string("occi/ipnetworkinterface.html", {
'term': self.term,
'scheme': self.scheme,
'location': self.location,
'class': "mixin",
'title': self.title,
})
"""predefined stuffs """predefined stuffs
...@@ -893,6 +918,6 @@ IPNETWORK_INTERFACE_MIXIN = Kind( ...@@ -893,6 +918,6 @@ IPNETWORK_INTERFACE_MIXIN = Kind(
scheme="http://schemas.ogf.org/occi/infrastructure/networkinterface#", scheme="http://schemas.ogf.org/occi/infrastructure/networkinterface#",
class_="mixin", class_="mixin",
title="ipnetwork", title="ipnetwork",
location="/mixin/ipnetwork/", location="/mixin/ipnetworkinterface/",
attributes=IPNETWORK_ATTRS, attributes=IPNETWORK_ATTRS,
) )
{% spaceless %}
Category: {{ term }}; scheme="{{ scheme }}"; class="{{ class }}"; title="{{ title }}"; location="{{ location }}";
{% endspaceless %}
Category: compute; scheme="{{ kind.scheme }}"; class="{{ kind.class }}"; Category: network; scheme="{{ kind.scheme }}"; class="{{ kind.class }}";
{% for m in mixins %} {% for m in mixins %}
{{ m.render_body }} {{ m.render_body }}
{% endfor %} {% endfor %}
......
Category: networkinterface; scheme="{{ kind.scheme }}"; class="{{ kind.class }}"; Category: compute; scheme="{{ kind.scheme }}"; class="{{ kind.class }}";
{% for m in mixins %}
{{ m.render_body }}
{% endfor %}
{% for k, v in attrs.items %} {% for k, v in attrs.items %}
X-OCCI-Attribute: {{ k }}={% if v.isdigit == False or k == "occi.core.id" %}"{{ v }}"{% else %}{{ v }}{% endif %} X-OCCI-Attribute: {{ k }}={% if v.isdigit == False or k == "occi.core.id" %}"{{ v }}"{% else %}{{ v }}{% endif %}
{% endfor %} {% endfor %}
...@@ -40,9 +40,10 @@ urlpatterns = patterns( ...@@ -40,9 +40,10 @@ urlpatterns = patterns(
StorageLinkInterface.as_view(), name="occi.storagelink"), StorageLinkInterface.as_view(), name="occi.storagelink"),
url(r'^network2/?$', NetworkInterfaceView.as_view(), ), url(r'^network2/?$', NetworkInterfaceView.as_view(), ),
url(r'^network2/(?P<vid>\d+)/?$', VlanInterface.as_view(), ), url(r'^network/(?P<vid>\d+)/?$', VlanInterface.as_view(), ),
url(r'^link/networkinterface/$', CIRCLEInterface.as_view()), url(r'^link/networkinterface/$', CIRCLEInterface.as_view()),
url(r'^link/networkinterface/vm(?P<vm_pk>\d+)_vlan(?P<vlan_vid>\d+)/?$', url(r'^link/networkinterface/'
'vm_(?P<vm_pk>\d+)_network_(?P<vlan_vid>\d+)/?$',
CIRCLEInterface.as_view(), name="occi.networkinterface"), CIRCLEInterface.as_view(), name="occi.networkinterface"),
) )
...@@ -12,7 +12,7 @@ from .occi import ( ...@@ -12,7 +12,7 @@ from .occi import (
Compute, Compute,
Storage, Storage,
Network, Network,
# NetworkInterface, NetworkInterface,
OsTemplate, OsTemplate,
StorageLink, StorageLink,
COMPUTE_KIND, COMPUTE_KIND,
...@@ -284,5 +284,44 @@ class VlanInterface(CSRFExemptMixin, DetailView): ...@@ -284,5 +284,44 @@ class VlanInterface(CSRFExemptMixin, DetailView):
pass pass
class CIRCLEInterface(CSRFExemptMixin, View): class CIRCLEInterface(CSRFExemptMixin, OCCIPostDataAsListMixin, View):
pass def get_vm_and_vlan(self):
vlan_vid = self.kwargs['vlan_vid']
vm = get_object_or_404(Instance.objects.filter(destroyed_at=None),
pk=self.kwargs['vm_pk'])
vlan = get_object_or_404(Vlan, vid=vlan_vid)
return vm, vlan
def get(self, request, *args, **kwargs):
vm, vlan = self.get_vm_and_vlan()
ni = NetworkInterface(instance=vm, vlan=vlan)
return HttpResponse(
ni.render_as_category(),
content_type="text/plain",
)
def post(self, request, *args, **kwargs):
# we don't support actions for networkinterfaces
# (they don't even exist in the model)
if request.GET.get("action"):
return HttpResponse("", status=500)
data = self.get_post_data(request)
sl = NetworkInterface.create_object(data=data)
if sl:
response = HttpResponse(
"X-OCCI-Location: %s" % sl.location,
status=201,
content_type="text/plain",
)
return response
else:
return HttpResponse("VM or Network does not exist.", status=500)
def delete(self, request, *args, **kwargs):
vm, vlan = self.get_vm_and_vlan()
ni = NetworkInterface(instance=vm, vlan=vlan)
ni.delete()
return HttpResponse("")
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