Skip to content

Commit

Permalink
Merge d03df10 into 9f8867c
Browse files Browse the repository at this point in the history
  • Loading branch information
enolfc committed May 4, 2015
2 parents 9f8867c + d03df10 commit f5e840f
Show file tree
Hide file tree
Showing 18 changed files with 1,309 additions and 89 deletions.
30 changes: 29 additions & 1 deletion ooi/api/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,34 @@
import json

import ooi.api.base
import ooi.api.network as network_api
from ooi import exception
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.occi import validator as occi_validator
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 __init__(self, *args, **kwargs):
super(Controller, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -180,7 +197,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 @@ -197,6 +213,18 @@ def show(self, req, id):
st = storage.StorageResource(title="storage", id=v["volumeId"])
comp.add_link(storage_link.StorageLink(comp, st,
deviceid=v["device"]))

# 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_set in addresses.values():
for addr in addr_set:
comp.add_link(_create_network_link(addr, comp,
floating_ips))

return [comp]

def delete(self, req, id):
Expand Down
74 changes: 74 additions & 0 deletions ooi/api/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- 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,
id=network_id,
state="active",
mixins=[network.ip_network])


class NetworkController(base.Controller):
def _floating_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 occi_network_resources

def general_index(self, req):
occi_network_resources = self._floating_index(req)
occi_network_resources.append(_build_network("fixed"))
return collection.Collection(resources=occi_network_resources)

def index(self, req):
occi_network_resources = self._floating_index(req)
return collection.Collection(resources=occi_network_resources)

def show_fixed(self, req):
return _build_network("fixed")

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()
155 changes: 155 additions & 0 deletions ooi/api/network_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# -*- 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 json

import webob.exc

from ooi.api import base
from ooi.api import network as network_api
from ooi import exception
from ooi.occi.core import collection
from ooi.occi.infrastructure import compute
from ooi.occi.infrastructure import network
from ooi.occi.infrastructure import network_link
from ooi.occi import validator as occi_validator
from ooi.openstack import network as os_network


class Controller(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-ips" % tenant_id)
response = req.get_response(self.app)
floating_ips = self.get_from_response(response, "floating_ips", [])
occi_link_resources = []
for ip in floating_ips:
if ip["instance_id"]:
net_id = "%s/%s" % (network_api.FLOATING_PREFIX, ip["pool"])
n = network.NetworkResource(title="network", id=net_id)
c = compute.ComputeResource(title="Compute",
id=ip["instance_id"])
# TODO(enolfc): get the MAC?
iface = os_network.OSNetworkInterface(c, n, "mac", ip["ip"])
occi_link_resources.append(iface)

return collection.Collection(resources=occi_link_resources)

def _get_os_network_ip(self, req, addr):
if addr["OS-EXT-IPS:type"] == "fixed":
return network.NetworkResource(title="network", id="fixed"), None
else:
tenant_id = req.environ["keystone.token_auth"].user.project_id
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 ip in floating_ips:
if addr["addr"] == ip["ip"]:
net = network.NetworkResource(
title="network",
id="%s/%s" % (network_api.FLOATING_PREFIX, ip["pool"]))
return net, ip["id"]
raise webob.exc.HTTPNotFound()

def _get_interface_from_id(self, req, id):
tenant_id = req.environ["keystone.token_auth"].user.project_id
try:
server_id, server_addr = id.split('_', 1)
except ValueError:
raise webob.exc.HTTPNotFound()
path = "/%s/servers/%s" % (tenant_id, server_id)
req = self._get_req(req, path=path, method="GET")
response = req.get_response(self.app)
s = self.get_from_response(response, "server", {})
addresses = s.get("addresses", {})
for addr_set in addresses.values():
for addr in addr_set:
if addr["addr"] == server_addr:
n, ip_id = self._get_os_network_ip(req, addr)
c = compute.ComputeResource(title="Compute",
id=server_id)
# TODO(enolfc): get the MAC?
return os_network.OSNetworkInterface(c, n, "mac",
addr["addr"], ip_id)
raise webob.exc.HTTPNotFound()

def show(self, req, id):
return [self._get_interface_from_id(req, id)]

def create(self, req, body):
tenant_id = req.environ["keystone.token_auth"].user.project_id
parser = req.get_parser()(req.headers, req.body)
scheme = {"category": network_link.NetworkInterface.kind}
obj = parser.parse()
validator = occi_validator.Validator(obj)
validator.validate(scheme)

attrs = obj.get("attributes", {})
server_id = attrs.get("occi.core.source")
net_id = attrs.get("occi.core.target")

# net_id is something like "fixed" or "floating/<pool_name>"
if net_id == "fixed":
raise exception.Invalid()
try:
_, pool_name = net_id.split("/", 1)
except ValueError:
raise webob.exc.HTTPNotFound()

# Allocate IP
path = "/%s/os-floating-ips" % tenant_id
req = self._get_req(req, path="/%s/os-floating-ips" % tenant_id,
body=json.dumps({"pool": pool_name}),
method="POST")
response = req.get_response(self.app)
ip = self.get_from_response(response, "floating_ip", {})

# Add it to server
req_body = {"addFloatingIp": {"address": ip["ip"]}}
path = "/%s/servers/%s/action" % (tenant_id, server_id)
req = self._get_req(req, path=path, body=json.dumps(req_body),
method="POST")
response = req.get_response(self.app)
if response.status_int != 202:
raise base.exception_from_response(response)
n = network.NetworkResource(title="network", id=net_id)
c = compute.ComputeResource(title="Compute", id=server_id)
l = os_network.OSNetworkInterface(c, n, "mac", ip["ip"])
return collection.Collection(resources=[l])

def delete(self, req, id):
iface = self._get_interface_from_id(req, id)
if iface.target.id == "fixed":
raise exception.Invalid()

# remove floating IP
tenant_id = req.environ["keystone.token_auth"].user.project_id
req_body = {"removeFloatingIp": {"address": iface.address}}
path = "/%s/servers/%s/action" % (tenant_id, iface.source.id)
req = self._get_req(req, path=path, body=json.dumps(req_body),
method="POST")
response = req.get_response(self.app)
if response.status_int != 202:
raise base.exception_from_response(response)

# release IP
path = "/%s/os-floating-ips/%s" % (tenant_id, iface.ip_id)
req = self._get_req(req, path=path, body=json.dumps(req_body),
method="DELETE")
response = req.get_response(self.app)
if response.status_int != 202:
raise base.exception_from_response(response)
return []
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
2 changes: 2 additions & 0 deletions ooi/api/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ def show(self, id, req):
st = storage.StorageResource(title=v["displayName"], id=v["id"],
size=v["size"], state=state)
return [st]

# TODO(enolfc): delete, create
1 change: 1 addition & 0 deletions ooi/api/storage_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ def delete(self, req, id):
response = req.get_response(self.app)
if response.status_int not in [202]:
raise base.exception_from_response(response)
return []

0 comments on commit f5e840f

Please sign in to comment.