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 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: ' + e.message) 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: ' + e.message) 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: ' + e.message) 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: ' + e.message) 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: ' + e.message) 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: ' + e.message) 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: ' + e.message) return LOG.warning('Could not determine resource type: {}'.format( resource['type'])) return
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: logging.warning('Could not create Net: ' + e.message) 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: logging.warning('Could not create Subnet: ' + e.message) 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 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: logging.warning('Could not create Port: ' + e.message) 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: logging.warning('Could not create Server: ' + e.message) 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: logging.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: logging.warning('Could not create FloatingIP: ' + e.message) 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: ' + e.message) return logging.warning('Could not determine resource type!') return