From: Tony Mack Date: Sat, 16 Mar 2013 03:34:46 +0000 (-0400) Subject: added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t... X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=refs%2Fheads%2Fplcapi-nova;p=plcapi.git added create_network(), delete_network(), create_subnet(), delete_subnet(), process_tags() --- diff --git a/PLC/Slices.py b/PLC/Slices.py index e5f1823b..231d3670 100644 --- a/PLC/Slices.py +++ b/PLC/Slices.py @@ -121,6 +121,16 @@ class Slice(AlchemyObj): 'node_id': node['node_id']}) slice_node.sync() + def remove_node(self, node_filter, commit=True): + from PLC.Nodes import Nodes + assert 'slice_id' in self + nodes = Nodes(self.api, node_filter) + for node in nodes: + slice_node = SliceNode(self.api, {'slice_id': self['slice_id'], + 'node_id': node['node_id']}) + slice_node.delete() + + def spawn_instances(self, nodes): # use the caller's nova keypair keypairs = self.api.client_shell.nova.keypairs.list() @@ -213,16 +223,67 @@ class Slice(AlchemyObj): AlchemyObj.delete(SliceInstance, filter={'slice_id': self['slice_id'], 'instance_id': instance_id}) + + def create_network(self): + self.api.client_shell.quantum.create_network(name=self['name'], + admin_state_up=False) + def delete_network(self): + nets = self.api.client_shell.quantum.list_networks(name=self['name'], + tenant_id=self['tenant_id'])['networks'] + for net in nets: + # delete all subnets: + #subnets = self.api.client_shell.quantum.list_subnets(network_id=net['network_id'])['subnets'] + for subnet_id in net['subnets']: + self.delete_subnet(subnet_id) + self.api.client_shell.quantum.delete_network(net['id']) + - def remove_node(self, node_filter, commit=True): - from PLC.Nodes import Nodes - assert 'slice_id' in self - nodes = Nodes(self.api, node_filter) - for node in nodes: - slice_node = SliceNode(self.api, {'slice_id': self['slice_id'], - 'node_id': node['node_id']}) - slice_node.delete() + def create_subnet(self, cidr_ip, ip_version, start, end): + nets = self.api.client_shell.quantum.list_networks(name=self['name'], + tenant_id=self['tenant_id'])['networks'] + # cannot create a subnet if there is no network + if not nets: + return + net = nets[0] + allocation_pools = [{'start': start, 'end': end}] + self.api.client_shell.quantum.create_subnet(network_id=net['id'], + ip_version=ip_version, + cidr=cidr_ip, + allocation_pools=allocation_pools) + + def delete_subnet(self, id=None): + if id: + self.api.client_shell.quantum.delete_subnet(id=id) + else: + # delete all subnets + subnets = self.api.client_shell.quantum.list_subnets(name=self['name'], + tenant_id=self['tenant_id'])['subnets'] + for subnet in subnets: + self.api.client_shell.quantum.delete_subnet(id=id) + + def process_tags(self): + # create a subnet for each subnet tag if one doesn't alredy exist + tags = SliceTags(self.api, filter={'slice_id': self['slice_id']}) + subnet_cidr = None + subnet_start = None + subnet_end = None + + for tag in tags: + if tag['tagname'] == 'subnet_cidr': + subnet_cidr = tag['value'] + elif tag['tagname'] == 'subnet_start': + subnet_start = tag['value'] + elif tag['tagname'] == 'subnet_end': + subnet_end = tag['value'] + + if subnet_cidr and subnet_start and subnet_end: + allocation_pools = [{'start': subnet_start, 'end': subnet_end}] + subnets = self.api.client_shell.quantum.list_subnets(name=self['name'], + tenant_id=self['tenant_id'], + cidr=subnet_cidr, + allocation_pools=allocation_pools) + #add_to_node_whitelist = Row.add_object(Node, 'node_slice_whitelist') #delete_from_node_whitelist = Row.remove_object(Node, 'node_slice_whitelist') @@ -250,6 +311,9 @@ class Slice(AlchemyObj): AlchemyObj.insert(self, dict(self)) slice = AlchemyObj.select(self, filter={'tenant_id': self['tenant_id']})[0] self['slice_id'] = slice.slice_id + + # create quantum network + self.create_network() else: self.object = self.api.client_shell.keystone.tenants.update(self['tenant_id'], **nova_slice) AlchemyObj.updatedb(self, {'slice_id': self['slice_id']}, dict(self)) @@ -261,6 +325,9 @@ class Slice(AlchemyObj): assert 'slice_id' in self assert 'tenant_id' in self + # delete quantum networks + self.delete_network() + # delete the nova object tenant = self.api.client_shell.keystone.tenants.find(id=self['tenant_id']) self.api.client_shell.keystone.tenants.delete(tenant)