Пример #1
0
    def create_network_with_extra_group(self, project_id=None,
                                        num_nodes=10,
                                        ret_full_net=True,
                                        new_proj=False,
                                        map_projection='EPSG:4326'):
        """
            Test adding data to a network through a scenario.
            This test adds attributes to one node and then assigns data to them.
            It assigns a descriptor, array and timeseries to the
            attributes node.
        """
        network = self.create_network_with_data(
                                 project_id=project_id,
                                 num_nodes=num_nodes,
                                 ret_full_net=ret_full_net,
                                 new_proj=new_proj,
                                 map_projection=map_projection)

        group = JSONObject({})
        group.network_id = network.id
        group.id = -1
        group.name = 'test new group'
        group.description = 'test new group'

        template_id = network.types[0].template_id
        template = JSONObject(self.client.get_template(template_id))

        type_summary_arr = []

        type_summary = JSONObject({})
        type_summary.id = template.id
        type_summary.name = template.name
        type_summary.id = template.templatetypes[2].id
        type_summary.name = template.templatetypes[2].name

        type_summary_arr.append(type_summary)

        group.types = type_summary_arr

        new_group = self.client.add_group(network.id, group)

        group_attr_ids = []
        for resource_attr in new_group.attributes:
            group_attr_ids.append(resource_attr.attr_id)

        updated_network = self.client.get_network(network.id)

        return updated_network
Пример #2
0
def clone_rule(rule_id,
               target_ref_key=None,
               target_ref_id=None,
               scenario_id_map={},
               **kwargs):
    """
        Clone a rule
        args:
            rule_id (int): The rule to clone
            target_ref_key (string): If the rule is to be cloned into a different
                                     resource, specify the new resources type
            target_ref_id (int): If the rule is to be cloned into a
                                 different resources, specify the resource ID.
            scenario_id_map (dict): If the old rule is specified in a scenario,
                                    then provide a dictionary mapping from the
                                    old scenario ID to the new one, like {123 : 456}
        Cloning will only occur into a different resource if both ref_key AND
        ref_id are provided. Otherwise it will maintain its original ref_key and ref_id.

        return:
            SQLAlchemy ORM object
    """

    user_id = kwargs.get('user_id')

    rule_i = _get_rule(rule_id, user_id, check_write=True)

    #lazy load types
    rule_i.types

    #lazy load owners
    rule_i.owners

    rule_j = JSONObject(rule_i)

    #Unset the reference ID for the rule in case the target resource type
    #has changed, then apply the new ref_key and ref_id
    if target_ref_key is not None and target_ref_id is not None:
        rule_j.network_id = None
        rule_j.node_id = None
        rule_j.link_id = None
        rule_j.group_id = None
        rule_j.ref_key = target_ref_key
        if target_ref_key == 'NODE':
            rule_j.node_id = target_ref_id
        elif target_ref_key == 'LINK':
            rule_j.link_id = target_ref_id
        elif target_ref_key == 'GROUP':
            rule_j.group_id = target_ref_id
        elif target_ref_key == 'NETWORK':
            rule_j.network_id = target_ref_id

        #this should only be done if theres a possibility that this is being cloned
        #to a new network -- the most likely scenario.
        rule_j.scenario_id = scenario_id_map.get(rule_i.scenario_id)

    #This is a blunt way of dealing with a situation where a rule is being cloned
    #into a new network, but it has a scenario ID pointing to the original. We must
    #ensure that there is no cross-network inconsistency, so simply make the
    #rule non-scenario specific.
    if len(scenario_id_map) == 0 and rule_i.scenario_id is not None:
        rule_i.scenario_id = None

    cloned_rule = add_rule(rule_j, **kwargs)

    return cloned_rule