I was wondering if someone could take a look at the following attached patch. It's a new component to the Libcloud library to support virtual network drivers.
My efforts are towards supporting an OpenNebula network driver for interfacing with an OpenNebula cloud computing infrastructure through the OCCI interface. Before I begin work on implementing that driver, I would like to get feedback on this patch. -- Hutson Betts Computer Science and Engineering Texas A&M University
=== added directory 'libcloud/network' === added file 'libcloud/network/__init__.py' --- libcloud/network/__init__.py 1970-01-01 00:00:00 +0000 +++ libcloud/network/__init__.py 2011-11-07 08:11:49 +0000 @@ -0,0 +1,3 @@ +""" +Module for working with virtual networks. +""" === added file 'libcloud/network/base.py' --- libcloud/network/base.py 1970-01-01 00:00:00 +0000 +++ libcloud/network/base.py 2011-11-07 18:37:13 +0000 @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" +Provides base classes for working with virtual networks. +""" + + +from libcloud.common.base import ConnectionUserAndKey, BaseDriver + + +__all__ = ['Network', + 'NetworkDriver'] + + +class Network(object): + """ + Provide a common interface for handling networks of all types. + + Network objects are analogous to physical switches connecting two or + more physical nodes together. The Network object provides the interface in + libcloud through which we can manipulate networks in different cloud + providers in the same way. Network objects don't actually do much directly + themselves, instead the network driver handles the connection to the + network. + + You don't normally create a network object yourself; instead you use + a driver and then have that create the network for you. + + >>> from libcloud.compute.drivers.dummy import DummyNodeDriver + >>> driver = DummyNetworkDriver() + >>> network = driver.create_network() + >>> network = driver.list_networks()[0] + >>> network.name + 'dummy-1' + """ + + def __init__(self, id, name, address, size, driver, extra=None): + self.id = str(id) + self.name = name + self.address = address + self.size = size + self.driver = driver + self.extra = extra or {} + + def get_uuid(self): + """ + Unique hash for this network. + + @return: C{string} + + The hash is a function of an SHA1 hash of the network's ID and + its driver which means that it should be unique between all + networks. In some subclasses (e.g. GoGrid) there is no ID + available so the public IP address is used. This means that, + unlike a properly done system UUID, the same UUID may mean a + different system install at a different time + + >>> from libcloud.network.drivers.dummy import DummyNetworkDriver + >>> driver = DummyNetworkDriver() + >>> network = driver.create_network() + >>> network.get_uuid() + 'd3748461511d8b9b0e0bfa0d4d3383a619a2bb9f' + + Note, for example, that this example will always produce the + same UUID! + """ + return hashlib.sha1("%s:%d" % (self.id, self.driver.type)).hexdigest() + + def destroy(self): + """ + Destroy this network. + + @return: C{bool} + + This calls the networks's driver and destroys the network. + + >>> from libcloud.network.drivers.dummy import DummyNetworkDriver + >>> driver = DummyNetworkDriver() + >>> network = driver.create_network() + >>> network.destroy() + True + """ + return self.driver.destroy_network(self) + + def __repr__(self): + return (('<NodeNetwork: uuid=%s, name=%s, address=%s, size=%s,' + 'provider=%s ...>') + % (self.uuid, self.name, self.address, self.size, + self.driver.name)) + + +class NetworkDriver(BaseDriver): + """ + A base NetworkDriver to derive from. + """ + + connectionCls = ConnectionUserAndKey + name = None + type = None + port = None + features = {"create_network": []} + + def __init__(self, key, secret=None, secure=True, host=None, port=None, + api_version=None): + super(NetworkDriver, self).__init__(key=key, secret=secret, + secure=secure, host=host, port=port, + api_version=api_version) + + def create_network(self, **kwargs): + """ + Create a new network instance. + + @keyword name: String with a name for this new network (required) + @type name: str + + @keyword address: IP address based on the Classful Address scheme. + @type address: str + + @keyword size: Size of the network in number of IPs within the + network. Size of 255 corresponds to + [address]/24 using the Classless Inter- + Domain Routing scheme. + @type size: int + + @return: The newly created L{Network}. + """ + raise NotImplementedError( + 'create_network not implemented for this driver.') + + def destroy_network(self, network): + """ + Destroy a network. + + Depending upon the provider, this may destroy all data associated with + the network. + + @return: C{bool} True if the destroy was successful, otherwise False. + """ + raise NotImplementedError( + 'destroy_network not implemented for this driver.') + + def list_networks(self, location=None): + """ + List virtual networks on a provider. + + @return: C{list} of L{Network} objects. + """ + raise NotImplementedError( + 'list_networks not implemented for this driver.') + +if __name__ == "__main__": + import doctest + doctest.testmod() === added directory 'libcloud/network/drivers' === added file 'libcloud/network/drivers/__init__.py' --- libcloud/network/drivers/__init__.py 1970-01-01 00:00:00 +0000 +++ libcloud/network/drivers/__init__.py 2011-11-07 08:11:49 +0000 @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" +Drivers for working with different providers. +""" + + +__all__ = ['dummy', + 'opennebula'] === added file 'libcloud/network/drivers/dummy.py' --- libcloud/network/drivers/dummy.py 1970-01-01 00:00:00 +0000 +++ libcloud/network/drivers/dummy.py 2011-11-07 18:39:56 +0000 @@ -0,0 +1,120 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" +Dummy Driver +""" + +import uuid + +from libcloud.network.base import NetworkDriver, Network +from libcloud.network.types import Provider + + +class DummyNetworkDriver(NetworkDriver): + """ + Dummy network driver. + + This is a fake driver which appears to always create or destroy + networks successfully. + + >>> from libcloud.compute.drivers.dummy import DummyNodeDriver + >>> driver = DummyNetworkDriver() + >>> network = driver.create_network() + >>> network = driver.list_networks()[0] + >>> network.name + 'dummy-1' + """ + + name = "Dummy Network Provider" + type = Provider.DUMMY + + def __init__(self): + self.nl = [] + + def get_uuid(self): + return str(uuid.uuid4()) + + def list_networks(self): + """ + List the networks known to a particular driver. + + >>> from libcloud.network.drivers.dummy import DummyNetworkDriver + >>> driver = DummyNetworkDriver() + >>> network_list = driver.list_networks() + >>> print network_list + [] + + Each item in the list returned is a network object from which you + can carry out any network actions you wish. + + As more networks are added, list_networks will return them. + + >>> network = driver.create_network() + >>> sorted([network.name for network in driver.list_networks()]) + ['dummy-1'] + """ + return self.nl + + def destroy_network(self, network): + """ + Remove the network from the network list. + + >>> from libcloud.network.drivers.dummy import DummyNetworkDriver + >>> driver = DummyNetworkDriver() + >>> network = driver.create_network() + >>> network = [network for network in driver.list_networks() if network.name == 'dummy-1'][0] + >>> driver.destroy_network(network) + True + >>> [network for network in driver.list_networks() if network.name == 'dummy-1'] + [] + """ + + self.nl.remove(network) + return True + + def create_network(self, **kwargs): + """ + Creates a dummy network; the network id is equal to the number of + networks in the network list. + + >>> from libcloud.network.drivers.dummy import DummyNetworkDriver + >>> driver = DummyNetworkDriver() + >>> sorted([network.name for network in driver.list_networks()]) + [] + >>> network = driver.create_network() + >>> sorted([network.name for network in driver.list_networks()]) + ['dummy-1'] + >>> driver.create_network().name + 'dummy-2' + >>> driver.destroy_network(network) + True + >>> sorted([network.name for network in driver.list_networks()]) + ['dummy-2'] + """ + l = len(self.nl) + 1 + n = Network(id=l, + name='dummy-%d' % l, + address=['127.0.%d.0' % l], + size=255, + driver=self, + extra={'foo': 'bar'}) + self.nl.append(n) + return n + + +if __name__ == "__main__": + import doctest + doctest.testmod() === added file 'libcloud/network/providers.py' --- libcloud/network/providers.py 1970-01-01 00:00:00 +0000 +++ libcloud/network/providers.py 2011-11-07 08:11:49 +0000 @@ -0,0 +1,36 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +""" +Provider related utilities +""" + +from libcloud.utils import get_driver as _get_provider_driver +from libcloud.compute.types import Provider + +__all__ = [ + "Provider", + "DRIVERS", + "get_driver"] + +DRIVERS = { + Provider.DUMMY: + ('libcloud.network.drivers.dummy', 'DummyNodeDriver'), + Provider.OPENNEBULA: + ('libcloud.network.drivers.opennebula', 'OpenNebulaNetworkDriver'), +} + + +def get_driver(provider): + return _get_provider_driver(DRIVERS, provider) === added file 'libcloud/network/types.py' --- libcloud/network/types.py 1970-01-01 00:00:00 +0000 +++ libcloud/network/types.py 2011-11-07 08:11:49 +0000 @@ -0,0 +1,32 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" +Base types used by other parts of libcloud. +""" + + +__all__ = ['Provider'] + + +class Provider(object): + """ + Defines for each of the supported providers + + @cvar DUMMY: Example provider + @cvar OPENNEBULA: OpenNebula.org + """ + DUMMY = 0 + OPENNEBULA = 16
signature.asc
Description: This is a digitally signed message part
