Skip to content

Commit

Permalink
New network stuff.
Browse files Browse the repository at this point in the history
Added OCCI infrastructure network resource, mixins and links.
New network controller and add links in compute. Some tests added.
  • Loading branch information
enolfc committed Apr 16, 2015
1 parent b925d0c commit a2763ed
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 45 deletions.
32 changes: 30 additions & 2 deletions ooi/api/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,32 @@
import json

import ooi.api.base
import ooi.api.network as network_api
from ooi.occi.core import collection
from ooi.occi.infrastructure import compute
from ooi.occi.infrastructure import network
from ooi.occi.infrastructure import storage
from ooi.occi.infrastructure import storage_link
from ooi.openstack import contextualization
from ooi.openstack import helpers
from ooi.openstack import network as os_network
from ooi.openstack import templates


def _create_network_link(addr, comp, floating_ips):
if addr["OS-EXT-IPS:type"] == "floating":
for ip in floating_ips:
if addr["addr"] == ip["ip"]:
net = network.NetworkResource(
title="network",
id="%s/%s" % (network_api.FLOATING_PREFIX, ip["pool"]))
else:
net = network.NetworkResource(title="network", id="fixed")
return os_network.OSNetworkInterface(comp, net,
addr["OS-EXT-IPS-MAC:mac_addr"],
addr["addr"])


class Controller(ooi.api.base.Controller):
def _get_compute_resources(self, servers):
occi_compute_resources = []
Expand Down Expand Up @@ -133,7 +150,6 @@ def show(self, req, id):
image["name"])

# build the compute object
# TODO(enolfc): link to network + storage
comp = compute.ComputeResource(title=s["name"], id=s["id"],
cores=flavor["vcpus"],
hostname=s["name"],
Expand All @@ -144,7 +160,19 @@ def show(self, req, id):
vols_attached = s.get("os-extended-volumes:volumes_attached", [])
for v in vols_attached:
st = storage.StorageResource(title="storage", id=v["id"])
comp._links.append(storage_link.StorageLink(comp, st))
comp.add_link(storage_link.StorageLink(comp, st))

# network links
addresses = s.get("addresses", {})
if addresses:
req = self._get_req(req, path="/%s/os-floating-ips" % tenant_id)
response = req.get_response(self.app)
floating_ips = self.get_from_response(response, "floating_ips", [])
for addr_type in addresses.values():
for addr in addr_type:
comp.add_link(_create_network_link(addr, comp,
floating_ips))

return [comp]

def delete(self, req, id):
Expand Down
87 changes: 87 additions & 0 deletions ooi/api/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-

# Copyright 2015 Spanish National Research Council
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import webob.exc

from ooi.api import base
from ooi.occi.core import collection
from ooi.occi.infrastructure import network

FLOATING_PREFIX = "floating"


def _build_network(name, prefix=None):
if prefix:
network_id = '/'.join([prefix, name])
else:
network_id = name
return network.NetworkResource(title=name,
summary="A OpenStack IP Pool",
id=network_id,
state="active",
mixins=[network.ip_network])


class NetworkController(base.Controller):
def index(self, req):
occi_network_resources = []
occi_network_resources.append(_build_network("fixed"))

tenant_id = req.environ["keystone.token_auth"].user.project_id

req = self._get_req(req, path="/%s/os-floating-ip-pools" % tenant_id)
response = req.get_response(self.app)
pools = self.get_from_response(response, "floating_ip_pools", [])

for p in pools:
occi_network_resources.append(_build_network(p["name"],
FLOATING_PREFIX))

return collection.Collection(resources=occi_network_resources)

def show(self, id, req):
if id != "fixed":
raise webob.exc.HTTPNotFound()
return _build_network("fixed")


class PoolController(base.Controller):
def index(self, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id

req = self._get_req(req, path="/%s/os-floating-ip-pools" % tenant_id)
response = req.get_response(self.app)
pools = self.get_from_response(response, "floating_ip_pools", [])

occi_network_resources = []
for p in pools:
occi_network_resources.append(_build_network(p["name"],
FLOATING_PREFIX))

return collection.Collection(resources=occi_network_resources)

def show(self, req, id):
tenant_id = req.environ["keystone.token_auth"].user.project_id

# get info from server
req = self._get_req(req, path="/%s/os-floating-ip-pools" % tenant_id)
response = req.get_response(self.app)

pools = self.get_from_response(response, "floating_ip_pools", [])
for p in pools:
if p['name'] == id:
return [_build_network(p["name"], FLOATING_PREFIX)]
raise webob.exc.HTTPNotFound()
11 changes: 10 additions & 1 deletion ooi/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from ooi.occi.core import link
from ooi.occi.core import resource
from ooi.occi.infrastructure import compute
from ooi.occi.infrastructure import network
from ooi.occi.infrastructure import network_link
from ooi.occi.infrastructure import storage
from ooi.occi.infrastructure import storage_link
from ooi.occi.infrastructure import templates as infra_templates
Expand Down Expand Up @@ -70,7 +72,14 @@ def index(self, req):
l.append(storage_link.StorageLink.kind)
l.extend(storage.StorageResource.actions)

# OCCI infra mixins
# OCCI infra network
l.append(network.NetworkResource.kind)
l.extend(network.NetworkResource.actions)
l.append(network.ip_network)
l.append(network_link.NetworkInterface.kind)
l.append(network_link.ip_network_interface)

# OCCI infra compute mixins
l.append(infra_templates.os_tpl)
l.append(infra_templates.resource_tpl)

Expand Down
3 changes: 3 additions & 0 deletions ooi/occi/core/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def link(self, target, mixins=[]):
l = link.Link("", mixins, self, target)
self._links.append(l)

def add_link(self, link):
self._links.append(link)

@property
def summary(self):
return self.attributes["occi.core.summary"].value
Expand Down
78 changes: 78 additions & 0 deletions ooi/occi/infrastructure/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-

# Copyright 2015 Spanish National Research Council
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ooi.occi.core import action
from ooi.occi.core import attribute as attr
from ooi.occi.core import kind
from ooi.occi.core import mixin
from ooi.occi.core import resource
from ooi.occi import helpers

up = action.Action(helpers.build_scheme('infrastructure/network/action'),
"up", "up network instance")

down = action.Action(helpers.build_scheme('infrastructure/network/action'),
"down", "down network instance")


class NetworkResource(resource.Resource):
attributes = attr.AttributeCollection(["occi.network.vlan",
"occi.network.label",
"occi.network.state"])
actions = (up, down)
kind = kind.Kind(helpers.build_scheme('infrastructure'), 'network',
'network resource', attributes, '/network/',
actions=actions,
related=[resource.Resource.kind])

def __init__(self, title, summary=None, id=None, vlan=None, label=None,
state=None, mixins=[]):
super(NetworkResource, self).__init__(title, mixins, summary=summary,
id=id)
self.attributes["occi.network.vlan"] = attr.MutableAttribute(
"occi.network.vlan", vlan)
self.attributes["occi.network.label"] = attr.MutableAttribute(
"occi.network.label", label)
self.attributes["occi.network.state"] = attr.InmutableAttribute(
"occi.network.state", state)

@property
def vlan(self):
return self.attributes["occi.network.vlan"].value

@vlan.setter
def vlan(self, value):
self.attributes["occi.network.vlan"].value = value

@property
def label(self):
return self.attributes["occi.network.label"].value

@label.setter
def label(self, value):
self.attributes["occi.network.label"].value = value

@property
def state(self):
return self.attributes["occi.network.state"].value


ip_network = mixin.Mixin(helpers.build_scheme("infrastructure/network"),
"ipnetwork", "IP Networking Mixin",
attributes=attr.AttributeCollection([
"occi.network.address",
"occi.network.gateway",
"occi.network.allocation"]))
69 changes: 69 additions & 0 deletions ooi/occi/infrastructure/network_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-

# Copyright 2015 Spanish National Research Council
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ooi.occi.core import attribute as attr
from ooi.occi.core import kind
from ooi.occi.core import link
from ooi.occi.core import mixin
from ooi.occi import helpers


class NetworkInterface(link.Link):
attributes = attr.AttributeCollection(["occi.networkinterface.interface",
"occi.networkinterface.mac",
"occi.networkinterface.state"])
kind = kind.Kind(helpers.build_scheme('infrastructure'),
'networkinterface', 'network link resource',
attributes, '/networklink/',
related=[link.Link.kind])

def __init__(self, mixins, source, target, id=None, interface=None,
mac=None, state=None):

super(NetworkInterface, self).__init__(None, mixins, source,
target, id)

self.attributes["occi.networkinterface.interface"] = (
attr.InmutableAttribute("occi.networkinterface.interface",
interface))
self.attributes["occi.networkinterface.mac"] = attr.MutableAttribute(
"occi.networkinterface.mac", mac)
self.attributes["occi.networkinterface.state"] = (
attr.InmutableAttribute("occi.networkinterface.state", state))

@property
def interface(self):
return self.attributes["occi.networkinterface.interface"].value

@property
def mac(self):
return self.attributes["occi.networkinterface.mac"].value

@mac.setter
def mac(self, value):
self.attributes["occi.networkinterface.mac"].value = value

@property
def state(self):
return self.attributes["occi.networkinterface.state"].value

ip_network_interface = mixin.Mixin(
helpers.build_scheme("infrastructure/networkinterface"),
"ipnetworkinterface", "IP Network interface Mixin",
attributes=attr.AttributeCollection([
"occi.networkinterface.address",
"occi.networkinterface.gateway",
"occi.networkinterface.allocation"]))
38 changes: 38 additions & 0 deletions ooi/openstack/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-

# Copyright 2015 Spanish National Research Council
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ooi.occi.core import attribute as attr
from ooi.occi.infrastructure import network_link


class OSNetworkInterface(network_link.NetworkInterface):
attributes = attr.AttributeCollection(["occi.networkinterface.address",
"occi.networkinterface.gateway",
"occi.networkinterface.allocation"])

def __init__(self, source, target, mac, address):
link_id = '_'.join([source.id, address])
mixins = [network_link.ip_network_interface]
super(OSNetworkInterface, self).__init__(mixins, source, target,
link_id, "eth0", mac,
"active")
self.attributes["occi.networkinterface.address"] = (
attr.MutableAttribute("occi.networkinterface.address", address))
self.attributes["occi.networkinterface.gateway"] = (
attr.MutableAttribute("occi.networkinterface.gateway", None))
self.attributes["occi.networkinterface.allocation"] = (
attr.MutableAttribute("occi.networkinterface.allocation",
"dynamic"))
Loading

0 comments on commit a2763ed

Please sign in to comment.