def api_reader(component):
    """Reads each entity data of all components using nailgun helpers and returns
    the dict representation of all the entities

    Representation: {component_name:
    [{comp1_name:comp1, comp1_id:1},
     {comp2_name:comp2, comp2_networks:[
            {'id':1, name:'abc','type':'ipv4'},
            {'id':18, name:'xyz','type':'ipv6'}]
        }]
    }

    e.g:
    {'host':
    [{name:host1.ab.com, id:10},
     {name:host2.xz.com, networks:[
            {'id':1, name:'abc','type':'ipv4'},
            {'id':18, name:'xyz','type':'ipv6'}]
        }]
     }

    :param string component: Satellite component name. e.g host, capsule
    :returns dict: The dict repr of entities data of all components
    """
    comp_data = {}
    comp_entity_data = []
    comp_entity_list = api_const.api_components()[component][0].search_json()
    for unique_id in comp_entity_list['results']:
        single_entity_info = api_const.api_components(
            unique_id['id'])[component][1].read_json()
        comp_entity_data.append(single_entity_info)
    comp_data[component] = comp_entity_data
    return comp_data
Esempio n. 2
0
def set_datastore(datastore, endpoint):
    """Creates an endpoint file with all the satellite components data in json
    format

    Here data is a list representation of all satellite component properties
    in format:
    [
    {'c1':[{c1_ent1:'val', 'c1_ent2':'val'}]},
    {'c2':[{c2_ent1:'val', 'c2_ent2':'val'}]}
    ]
    where c1 and c2 are sat components e.g host, capsule, role
    ent1 and ent2 are component properties e.g host ip, capsule name

    :param str datastore: A file name without extension where all sat component
    data will be exported
    :param str endpoint: An endpoints of satellite to get the data and create
    datastore. It has to be either cli or api.

    Environment Variable:

    ORGANIZATION:
        The organization to which the components are associated, if endpoint
        is CLI
        Optional, by default 'Default_Organization'

    """
    allowed_ends = ['cli', 'api']
    if endpoint not in allowed_ends:
        raise IncorrectEndpointException(
            'Endpoints has to be one of {}'.format(allowed_ends))
    if endpoint == 'cli':
        nonorged_comps_data = [
            csv_reader(
                component, 'list') for component in cli_const.components[
                'org_not_required']]
        orged_comps_data = [
            csv_reader(
                component, 'list --organization-id 1'
                ) for component in cli_const.components['org_required']
        ]
        all_comps_data = nonorged_comps_data + orged_comps_data
    if endpoint == 'api':
        set_api_server_config()
        api_comps = list(api_const.api_components().keys())
        all_comps_data = [
            api_reader(component) for component in api_comps
        ]
    with open('{0}_{1}'.format(datastore, endpoint), 'w') as ds:
        json.dump(all_comps_data, ds)