def add(self, ctx, current_entities): """Handles adding a relationship :return: the add_node.modification node """ # Update source relationships and plugins # Extract the new relationship from the deployment update plan new_relationship = \ ctx.raw_node[ctx.RELATIONSHIPS][ctx.relationship_index] # Extract the current relationships and manipulate the relationships # size to support new relationships raw_relationships = \ current_entities[ctx.raw_node_id][ctx.RELATIONSHIPS] self._resize_relationships(raw_relationships, ctx.relationship_index) raw_relationships[ctx.relationship_index] = new_relationship relationships = deepcopy(ctx.storage_node.relationships) relationships.append(new_relationship) ctx.storage_node.relationships = relationships ctx.storage_node.plugins = ctx.raw_node[ctx.PLUGINS] self.sm.update(ctx.storage_node) source_node = ctx.storage_node raw_source_node = current_entities[source_node.id] raw_source_node[ctx.PLUGINS] = source_node.plugins target_node = get_node(ctx.deployment_id, ctx.target_id) target_node.plugins = ctx.raw_target_node[ctx.PLUGINS] self.sm.update(target_node) current_entities[ctx.storage_target_node.id] = \ ctx.storage_target_node.to_dict() return ctx.raw_node_id, ctx.target_id
def get(self, _include=None, **kwargs): """ List nodes """ args = get_args_and_verify_arguments( [Argument('deployment_id', required=False), Argument('node_id', required=False)] ) deployment_id = args.get('deployment_id') node_id = args.get('node_id') if deployment_id and node_id: try: nodes = [get_node(deployment_id, node_id)] except manager_exceptions.NotFoundError: nodes = [] else: deployment_id_filter = ResourceManager.create_filters_dict( deployment_id=deployment_id) nodes = get_storage_manager().list( models.Node, filters=deployment_id_filter, include=_include ).items return nodes
def add(self, ctx, current_entities): self.rm._create_deployment_nodes( deployment_id=ctx.deployment_id, plan=ctx.deployment_plan, node_ids=ctx.raw_node_id, ) current_entities[ctx.raw_node_id] = ctx.storage_node.to_dict() # node_handler.raw_node # Update new node relationships target nodes. Since any relationship # with target interface requires the target node to hold a plugin # which supports the operation, we should update the mapping for # this plugin under the target node. target_ids = [ r['target_id'] for r in ctx.raw_node.get(ctx.RELATIONSHIPS, []) ] for node_id in target_ids: node = get_node(ctx.deployment_id, node_id) node.plugins = deployment_update_utils.get_raw_node( ctx.deployment_plan, node_id)['plugins'] self.sm.update(node) current_entities[node_id] = node.to_dict() return ctx.raw_node_id
def finalize(self, dep_update): """update any removed entity from nodes :param dep_update: the deployment update object itself. :return: """ removed_and_related = dep_update.deployment_update_node_instances[ NODE_MOD_TYPES.REMOVED_AND_RELATED] removed_node_instances = \ removed_and_related.get(NODE_MOD_TYPES.AFFECTED, []) removed_node_ids = deployment_update_utils.extract_ids( removed_node_instances, 'node_id') # Since not all changes are caught on the node instances (actually only # the removing/adding of relationships and nodes) we need to apply all # of the changes, thus screening all of the nodes except the ones # deleted is a valid solution. modified_nodes = [ n for n in dep_update.deployment_update_nodes if n['id'] not in removed_node_ids ] for modified_node in modified_nodes: # Any relationship deleted or inserted to a new index could create # 'None' relationships, in this final phase we remove those (if by # some reason any left). modified_node['relationships'] = \ [r for r in modified_node['relationships'] if r] node = get_node(modified_node['deployment_id'], modified_node['id']) node.number_of_instances = modified_node['number_of_instances'] node.planned_number_of_instances = modified_node[ 'planned_number_of_instances'] node.relationships = modified_node['relationships'] node.operations = modified_node['operations'] node.plugins = modified_node['plugins'] node.properties = modified_node['properties'] self.sm.update(node) for removed_node_instance in removed_node_instances: node = get_node(dep_update.deployment_id, removed_node_instance['node_id']) self.sm.delete(node)
def _mutate_plugins_list(self, ctx, current_entities, mutate_func): return_dict = {} node = get_node(ctx.deployment_id, ctx.raw_node_id) # Can be either node.plugins or node.plugins_to_install plugins_dict = getattr(node, ctx.plugin_key, {}) plugins = deepcopy(plugins_dict) plugins = mutate_func(ctx, plugins, node, return_dict) current_entities[ctx.raw_node_id][ctx.plugin_key] = plugins setattr(node, ctx.plugin_key, plugins) self.sm.update(node) return return_dict
def _get_or_create_node(storage_manager, node_id, deployment): try: return get_node(deployment.id, node_id) except manager_exceptions.NotFoundError: node = Node(id=node_id, type='', number_of_instances=1, planned_number_of_instances=1, deploy_number_of_instances=1, min_number_of_instances=1, max_number_of_instances=1) node.deployment = deployment return storage_manager.put(node)
def finalize(self, dep_update): """update any removed entity from nodes :param dep_update: the deployment update object itself. """ removed_and_related = dep_update.deployment_update_node_instances[ NODE_MOD_TYPES.REMOVED_AND_RELATED] removed_node_instances = removed_and_related.get( NODE_MOD_TYPES.AFFECTED, []) removed_node_ids = deployment_update_utils.extract_ids( removed_node_instances, 'node_id') # Since not all changes are caught on the node instances (actually only # the removing/adding of relationships and nodes) we need to apply all # of the changes, thus screening all of the nodes except the ones # deleted is a valid solution. modified_nodes = [n for n in dep_update.deployment_update_nodes if n['id'] not in removed_node_ids] for modified_node in modified_nodes: # Any relationship deleted or inserted to a new index could create # 'None' relationships, in this final phase we remove those (if by # some reason any left). modified_node['relationships'] = \ [r for r in modified_node['relationships'] if r] node = get_node(modified_node['deployment_id'], modified_node['id']) node.number_of_instances = modified_node['number_of_instances'] node.planned_number_of_instances = modified_node[ 'planned_number_of_instances'] node.relationships = modified_node['relationships'] node.operations = modified_node['operations'] node.plugins = modified_node['plugins'] node.properties = modified_node['properties'] self.sm.update(node) for removed_node_instance in removed_node_instances: node = get_node(dep_update.deployment_id, removed_node_instance['node_id']) self.sm.delete(node)
def _get_or_create_node(storage_manager, node_id, deployment): try: return get_node(deployment.id, node_id) except manager_exceptions.NotFoundError: node = Node( id=node_id, type='', number_of_instances=1, planned_number_of_instances=1, deploy_number_of_instances=1, min_number_of_instances=1, max_number_of_instances=1 ) node.deployment = deployment return storage_manager.put(node)
def _modify_relationship_operation(self, ctx, current_entities): current_node = current_entities[ctx.raw_node_id] relationships = current_node[ctx.RELATIONSHIPS] operations = relationships[ctx.relationship_index][ctx.operations_key] if ctx.modification_breadcrumbs: operation_to_update = deployment_update_utils.traverse_object( operations[ctx.operation_id], ctx.modification_breadcrumbs[:-1]) operation_to_update[ctx.modification_breadcrumbs[-1]] = \ ctx.raw_entity_value else: operations[ctx.operation_id] = ctx.raw_entity_value current_node[ctx.PLUGINS] = ctx.raw_node[ctx.PLUGINS] node = get_node(ctx.deployment_id, ctx.raw_node_id) node.relationships = deepcopy(relationships) node.plugins = ctx.raw_node[ctx.PLUGINS] self.sm.update(node) if ctx.operations_key == 'target_operations': node = get_node(ctx.deployment_id, ctx.storage_relationship['target_id']) self._update_stored_operations(ctx, node, ctx.raw_entity_value) return ctx.entity_id
def modify(self, ctx, current_entities): node = get_node(ctx.deployment_id, ctx.raw_node_id) properties = deepcopy(node.properties) properties[ctx.property_id] = deployment_update_utils.create_dict( ctx.modification_breadcrumbs, ctx.raw_entity_value) node.properties = properties self.sm.update(node) properties = current_entities[ctx.raw_node_id][ctx.PROPERTIES] if ctx.modification_breadcrumbs: property_to_update = \ deployment_update_utils.traverse_object( properties[ctx.property_id], ctx.modification_breadcrumbs[:-1]) property_to_update[ctx.modification_breadcrumbs[-1]] = \ ctx.raw_entity_value else: properties[ctx.property_id] = ctx.raw_entity_value return ctx.entity_id
def add(self, ctx, current_entities): self.rm._create_deployment_nodes(deployment_id=ctx.deployment_id, plan=ctx.deployment_plan, node_ids=ctx.raw_node_id) # node_handler.raw_node current_entities[ctx.raw_node_id] = ctx.storage_node.to_dict() # Update new node relationships target nodes. Since any relationship # with target interface requires the target node to hold a plugin # which supports the operation, we should update the mapping for # this plugin under the target node. target_ids = [r['target_id'] for r in ctx.raw_node.get(ctx.RELATIONSHIPS, [])] for node_id in target_ids: node = get_node(ctx.deployment_id, node_id) node.plugins = deployment_update_utils.get_raw_node( ctx.deployment_plan, node_id)['plugins'] self.sm.update(node) current_entities[node_id] = node.to_dict() return ctx.raw_node_id
def _modify_relationship_operation(self, ctx, current_entities): current_node = current_entities[ctx.raw_node_id] relationships = current_node[ctx.RELATIONSHIPS] operations = relationships[ctx.relationship_index][ctx.operations_key] if ctx.modification_breadcrumbs: operation_to_update = deployment_update_utils.traverse_object( operations[ctx.operation_id], ctx.modification_breadcrumbs[:-1] ) operation_to_update[ctx.modification_breadcrumbs[-1]] = \ ctx.raw_entity_value else: operations[ctx.operation_id] = ctx.raw_entity_value current_node[ctx.PLUGINS] = ctx.raw_node[ctx.PLUGINS] node = get_node(ctx.deployment_id, ctx.raw_node_id) node.relationships = deepcopy(relationships) node.plugins = ctx.raw_node[ctx.PLUGINS] self.sm.update(node) return ctx.entity_id
def modify(self, ctx, current_entities): node = get_node(ctx.deployment_id, ctx.raw_node_id) properties = deepcopy(node.properties) properties[ctx.property_id] = deployment_update_utils.create_dict( ctx.modification_breadcrumbs, ctx.raw_entity_value ) node.properties = properties self.sm.update(node) properties = current_entities[ctx.raw_node_id][ctx.PROPERTIES] if ctx.modification_breadcrumbs: property_to_update = \ deployment_update_utils.traverse_object( properties[ctx.property_id], ctx.modification_breadcrumbs[:-1]) property_to_update[ctx.modification_breadcrumbs[-1]] = \ ctx.raw_entity_value else: properties[ctx.property_id] = ctx.raw_entity_value return ctx.entity_id
def _modify_node_operation(self, ctx, current_entities): new_operation = deployment_update_utils.create_dict( ctx.modification_breadcrumbs, ctx.raw_entity_value) node = get_node(ctx.deployment_id, ctx.raw_node_id) operations = deepcopy(node.operations) operations.update({ctx.operation_id: new_operation}) node.operations = operations node.plugins = ctx.raw_node[ctx.PLUGINS] self.sm.update(node) current_node = current_entities[ctx.raw_node_id] if ctx.modification_breadcrumbs: operation_to_update = deployment_update_utils.traverse_object( current_node[ctx.OPERATIONS][ctx.operation_id], ctx.modification_breadcrumbs[:-1]) operation_to_update[ctx.modification_breadcrumbs[-1]] = \ ctx.raw_entity_value else: operation_to_update = current_node[ctx.OPERATIONS] operation_to_update[ctx.operation_id] = ctx.raw_entity_value current_node[ctx.PLUGINS] = ctx.raw_node[ctx.PLUGINS] return ctx.entity_id
def _modify_node_operation(self, ctx, current_entities): new_operation = deployment_update_utils.create_dict( ctx.modification_breadcrumbs, ctx.raw_entity_value) node = get_node(ctx.deployment_id, ctx.raw_node_id) operations = deepcopy(node.operations) operations.update({ctx.operation_id: new_operation}) node.operations = operations node.plugins = ctx.raw_node[ctx.PLUGINS] self.sm.update(node) current_node = current_entities[ctx.raw_node_id] if ctx.modification_breadcrumbs: operation_to_update = deployment_update_utils.traverse_object( current_node[ctx.OPERATIONS][ctx.operation_id], ctx.modification_breadcrumbs[:-1] ) operation_to_update[ctx.modification_breadcrumbs[-1]] = \ ctx.raw_entity_value else: operation_to_update = current_node[ctx.OPERATIONS] operation_to_update[ctx.operation_id] = ctx.raw_entity_value current_node[ctx.PLUGINS] = ctx.raw_node[ctx.PLUGINS] return ctx.entity_id
def storage_node(self): return get_node(self._deployment_id, self._top_level_entity_id) or None
def storage_target_node(self): return get_node(self._deployment_id, self.target_id) or None
def _get_storage_node(self, deployment_id, node_id): node = get_node(deployment_id, node_id) return node.to_dict() if node else {}