def post(self): """ Creates a subnet with the name, specified within the request under ['subnet']['name']. :return: * 400, if the 'CIDR' format is wrong or it does not exist. * 404, if the network was not found. * 409, if the corresponding network already has one subnet. * 500, if any exception occurred while creation and * 201, if everything worked out. :rtype: :class:`flask.response` """ LOG.debug("API CALL: %s POST" % str(self.__class__.__name__)) try: subnet_dict = json.loads(request.data) net = self.api.compute.find_network_by_name_or_id( subnet_dict['subnet']['network_id']) if net is None: return Response('Could not find network.\n', status=404, mimetype='application/json') net.subnet_name = subnet_dict["subnet"].get( 'name', str(net.name) + '-sub') if net.subnet_id is not None: LOG.error( "Only one subnet per network is supported: {}".format( net.subnet_id)) return Response('Only one subnet per network is supported\n', status=409, mimetype='application/json') if "id" in subnet_dict["subnet"]: net.subnet_id = subnet_dict["subnet"]["id"] else: net.subnet_id = str(uuid.uuid4()) import emuvim.api.openstack.ip_handler as IP net.set_cidr(IP.get_new_cidr(net.subnet_id)) if "tenant_id" in subnet_dict["subnet"]: pass if "allocation_pools" in subnet_dict["subnet"]: pass if "gateway_ip" in subnet_dict["subnet"]: net.gateway_ip = subnet_dict["subnet"]["gateway_ip"] if "ip_version" in subnet_dict["subnet"]: pass if "enable_dhcp" in subnet_dict["subnet"]: pass return Response(json.dumps({'subnet': net.create_subnet_dict()}), status=201, mimetype='application/json') except Exception as ex: LOG.exception("Neutron: Create network excepiton.") return Response(ex.message, status=500, mimetype='application/json')
def update_subnet_cidr(self, old_stack, new_stack): """ Updates the subnet IP addresses. If the new stack contains subnets from the old stack it will take those IP addresses. Otherwise it will create new IP addresses for the subnet. :param old_stack: The currently running stack :type old_stack: :class:`heat.resources.stack` :param new_stack: The new created stack :type new_stack: :class:`heat.resources.stack` """ for old_subnet in old_stack.nets.values(): IP.free_cidr(old_subnet.get_cidr(), old_subnet.subnet_id) for subnet in new_stack.nets.values(): subnet.clear_cidr() for old_subnet in old_stack.nets.values(): if subnet.subnet_name == old_subnet.subnet_name: if IP.assign_cidr(old_subnet.get_cidr(), subnet.subnet_id): subnet.set_cidr(old_subnet.get_cidr()) for subnet in new_stack.nets.values(): if IP.is_cidr_issued(subnet.get_cidr()): continue cird = IP.get_new_cidr(subnet.subnet_id) subnet.set_cidr(cird) return
def post(self): """ Creates a subnet with the name, specified within the request under ['subnet']['name']. :return: * 400, if the 'CIDR' format is wrong or it does not exist. * 404, if the network was not found. * 409, if the corresponding network already has one subnet. * 500, if any exception occurred while creation and * 201, if everything worked out. :rtype: :class:`flask.response` """ LOG.debug("API CALL: %s POST" % str(self.__class__.__name__)) try: subnet_dict = json.loads(request.data) net = self.api.compute.find_network_by_name_or_id(subnet_dict['subnet']['network_id']) if net is None: return Response('Could not find network.\n', status=404, mimetype='application/json') net.subnet_name = subnet_dict["subnet"].get('name', str(net.name) + '-sub') if net.subnet_id is not None: return Response('Only one subnet per network is supported\n', status=409, mimetype='application/json') if "id" in subnet_dict["subnet"]: net.subnet_id = subnet_dict["subnet"]["id"] else: net.subnet_id = str(uuid.uuid4()) import emuvim.api.openstack.ip_handler as IP net.set_cidr(IP.get_new_cidr(net.subnet_id)) if "tenant_id" in subnet_dict["subnet"]: pass if "allocation_pools" in subnet_dict["subnet"]: pass if "gateway_ip" in subnet_dict["subnet"]: net.gateway_ip = subnet_dict["subnet"]["gateway_ip"] if "ip_version" in subnet_dict["subnet"]: pass if "enable_dhcp" in subnet_dict["subnet"]: pass return Response(json.dumps({'subnet': net.create_subnet_dict()}), status=201, mimetype='application/json') except Exception as ex: LOG.exception("Neutron: Create network excepiton.") return Response(ex.message, status=500, mimetype='application/json')
def handle_resource(self, resource, stack, dc_label, stack_update=False): """ This function will take a resource (from a heat template) and determines which type it is and creates the corresponding class, with its required parameters, for further calculations (like deploying the stack). If it is not possible to create the class, because of unresolved dependencies, it will buffer the resource within the 'self.bufferResource' list. :param resource: Dict which contains all important informations about the type and parameters. :type resource: ``dict`` :param stack: Reference of the stack that should finally contain the created class. :type stack: :class:`heat.resources.stack` :param dc_label: String that contains the label of the used data center :type dc_label: ``str`` :param stack_update: Specifies if a new stack will be created or a older one will be updated :type stack_update: ``bool`` :return: void :rtype: ``None`` """ if "OS::Neutron::Net" in resource['type']: try: net_name = resource['properties']['name'] if net_name not in stack.nets: stack.nets[net_name] = self.compute.create_network( net_name, True) except Exception as e: LOG.warning('Could not create Net: ' + str(e)) return if 'OS::Neutron::Subnet' in resource['type'] and "Net" not in resource[ 'type']: try: net_name = resource['properties']['network']['get_resource'] if net_name not in stack.nets: net = self.compute.create_network(net_name, stack_update) stack.nets[net_name] = net else: net = stack.nets[net_name] net.subnet_name = resource['properties']['name'] if 'gateway_ip' in resource['properties']: net.gateway_ip = resource['properties']['gateway_ip'] net.subnet_id = resource['properties'].get( 'id', str(uuid.uuid4())) net.subnet_creation_time = str(datetime.now()) if not stack_update: net.set_cidr(IP.get_new_cidr(net.subnet_id)) except Exception as e: LOG.warning('Could not create Subnet: ' + str(e)) return if 'OS::Neutron::Port' in resource['type']: try: port_name = resource['properties']['name'] if port_name not in stack.ports: port = self.compute.create_port(port_name, stack_update) stack.ports[port_name] = port else: port = stack.ports[port_name] if str(resource['properties']['network'] ['get_resource']) in stack.nets: net = stack.nets[resource['properties']['network'] ['get_resource']] if net.subnet_id is not None: port.net_name = net.name port.ip_address = net.get_new_ip_address(port.name) return except Exception as e: LOG.warning('Could not create Port: ' + str(e)) self.bufferResource.append(resource) return if 'OS::Nova::Server' in resource['type']: try: compute_name = str(dc_label) + '_' + str(stack.stack_name) + \ '_' + str(resource['properties']['name']) shortened_name = str(dc_label) + '_' + str(stack.stack_name) + '_' + \ self.shorten_server_name( str(resource['properties']['name']), stack) nw_list = resource['properties']['networks'] if shortened_name not in stack.servers: server = self.compute.create_server( shortened_name, stack_update) stack.servers[shortened_name] = server else: server = stack.servers[shortened_name] server.full_name = compute_name server.template_name = str(resource['properties']['name']) server.command = resource['properties'].get( 'command', '/bin/sh') server.image = resource['properties']['image'] server.flavor = resource['properties']['flavor'] for port in nw_list: port_name = port['port']['get_resource'] # just create a port # we don't know which network it belongs to yet, but the resource will appear later in a valid # template if port_name not in stack.ports: stack.ports[port_name] = self.compute.create_port( port_name, stack_update) server.port_names.append(port_name) return except Exception as e: LOG.warning('Could not create Server: ' + str(e)) return if 'OS::Neutron::RouterInterface' in resource['type']: try: router_name = None subnet_name = resource['properties']['subnet']['get_resource'] if 'get_resource' in resource['properties']['router']: router_name = resource['properties']['router'][ 'get_resource'] else: router_name = resource['properties']['router'] if router_name not in stack.routers: stack.routers[router_name] = Router(router_name) for tmp_net in stack.nets.values(): if tmp_net.subnet_name == subnet_name: stack.routers[router_name].add_subnet(subnet_name) return except Exception as e: LOG.warning('Could not create RouterInterface: ' + e.__repr__()) self.bufferResource.append(resource) return if 'OS::Neutron::FloatingIP' in resource['type']: try: port_name = resource['properties']['port_id']['get_resource'] floating_network_id = resource['properties'][ 'floating_network_id'] if port_name not in stack.ports: stack.ports[port_name] = self.compute.create_port( port_name, stack_update) stack.ports[port_name].floating_ip = floating_network_id except Exception as e: LOG.warning('Could not create FloatingIP: ' + str(e)) return if 'OS::Neutron::Router' in resource['type']: try: name = resource['properties']['name'] if name not in stack.routers: stack.routers[name] = Router(name) except Exception as e: print('Could not create Router: ' + str(e)) return if 'OS::Heat::ResourceGroup' in resource['type']: try: embedded_resource = resource['properties']['resource_def'] LOG.debug("Found resource in resource group: {}".format( embedded_resource)) # recursively parse embedded resource self.handle_resource(embedded_resource, stack, dc_label, stack_update) except Exception as e: print('Could not create Router: ' + str(e)) return LOG.warning('Could not determine resource type: {}'.format( resource['type'])) return