Beispiel #1
0
def parse_config(a10_obj_type, config_api, client, *args):
    '''
    Passes configuration onto processing functions. Preforms
    CRUD calls per object.

    Args:
        a10_obj_type (string): root ACOS object being operated on
        config_api (string): config api to access
        client (object): AXAPI REST client 
        *args: list of values and child objects of root object 

    Returns (dict):
        post/delete results 
    '''
    a10_obj = '{}_{}'.format(config_api, a10_obj_type)

    config = {}
    for k in args:
        config.update(k)

    root = obj_tree.parse_tree(a10_obj, config)
    forest_list = [root]

    forest = ForestGen()
    forest.dfs_cut(root)
    if forest.node_list:
        forest_list.extend(forest.node_list)

    obj_dict_list = []
    for tree in forest_list:
        if isinstance(tree, InterNode):
            for child in tree.children:
                if not isinstance(child, InterNode):
                    obj_dict_list.append(_build_obj_dict(child, tree.ref))
                else:
                    val_dict = _build_obj_dict(child, child.ref)
                    # Need something more than just a ref
                    if val_dict and len(val_dict) > 1:
                        obj_dict_list.append(val_dict)
                      
        else:
            obj_dict_list.append(_build_obj_dict(tree, tree.ref))


    post_result = {}
    cnt = 0

    # As objects with the most dependencies are at the top,
    # we will need to delete the objects starting at the bottom
    # of the object tree. 
    for i in range(len(obj_dict_list)-1, 0, -1):
        if obj_dict_list[i].get('absent'):
            ref = obj_dict_list[i]['ref']
            del obj_dict_list[i]['ref']
            existing_url = a10_helper.existing_url(ref, **obj_dict_list[i])
  
            ref = '{}_{}'.format(ref, cnt)
            post_result[ref] = client.delete(existing_url)
            del obj_dict_list[i]
            cnt += 1

    for a10_obj_val in obj_dict_list:
        ref = a10_obj_val['ref']
        del a10_obj_val['ref']

        avail_props = a10_helper.get_props(ref)
        obj_type = a10_helper.get_obj_type(ref)

        existing_url = a10_helper.existing_url(ref, **a10_obj_val)
        new_url = a10_helper.new_url(ref, **a10_obj_val)

        parent_keys = a10_helper.get_parent_keys(ref)

        del_list = []
        a10_obj_fin = {}
        for k,v in a10_obj_val.items():
            if k not in parent_keys:
                a10_obj_fin[k.replace('-', '_')] = v

        payload = _build_json(obj_type, avail_props, **a10_obj_fin)

        create = False
        try:
            need_update = False
            resp = client.get(existing_url)
            if not resp:
                raise a10_ex.NotFound 
            for k,v in a10_obj_fin.items():
               if k.replace('_', '-') in resp[obj_type]:
                    if v != resp[obj_type][k.replace('_', '-')]:
                        need_update = True
                        break
            if need_update:
                ref = '{}_{}'.format(ref, cnt)
                post_result[ref] = client.post(existing_url, payload)
        except a10_ex.NotFound:
            create = True

        if create:
            ref = '{}_{}'.format(ref, cnt)
            post_result[ref] = client.post(new_url, payload)
        cnt += 1

    return post_result
Beispiel #2
0
 def test_dfs_cut_called(self):
     obj_tree.parse_tree('', {})
     self.cut_mock.assert_called_with(self.fake_config, self.root_mock())
Beispiel #3
0
 def test_addChild_called(self):
     obj_tree.parse_tree('', {})
     self.root_mock().addChild.assert_called()
Beispiel #4
0
 def test_addValDict_called(self):
     obj_tree.parse_tree('', {})
     self.root_mock().addValDict.assert_called_with(fake_key='fake_val')
Beispiel #5
0
 def test_ref_set(self):
     root_actual = obj_tree.parse_tree('coffee', {})
     self.assertEquals('a10_coffee', root_actual.ref)
Beispiel #6
0
 def test_id_set(self):
     root_actual = obj_tree.parse_tree('', {})
     self.assertEquals('ada', root_actual.id)