Ejemplo n.º 1
0
 def test_data_map(self):
     """Test a mapping of data from a response dictionary."""
     map_dict = {'id': 'uuid', 'color': 'new::color'}
     data = {'uuid': '100', 'new::color': 'blue', 'key': 'value'}
     expected = {'id': '100', 'color': 'blue'}
     mapped = data_map(map_dict, data)
     self.assertEqual(mapped, expected)
Ejemplo n.º 2
0
def host_fields(api_version, response):
    """Obtain the fields for a given host id.

    :param api_version: The version of the Satellite api
    :param response: The response returned from the fields
        endpoint
    :returns: dictionary of facts created from response object.
    """
    fields = response
    host_info = {}
    sub_virt_host = None
    sub_virt_guest = None
    cf_errata_counts = None
    sub_facet_attributes = fields.get(SUBSCRIPTION_FACET)
    content_facet_attributes = fields.get(CONTENT_FACET)
    facts = raw_facts_template()
    facts.update(fields.get(FACTS, {}))
    virtual_host = fields.get(VIRTUAL_HOST, {})
    virtual_guests = fields.get(VIRTUAL_GUESTS)
    errata_counts = fields.get(ERRATA_COUNTS, {})

    if sub_facet_attributes:
        sub_virt_host = sub_facet_attributes.get(VIRTUAL_HOST)
        sub_virt_guest = sub_facet_attributes.get(VIRTUAL_GUESTS)
    if content_facet_attributes:
        cf_errata_counts = content_facet_attributes.get(ERRATA_COUNTS)

    host_info.update(utils.data_map(FIELDS_MAPPING, fields))
    host_info.update(utils.data_map(SUBS_FACET_MAPPING,
                                    sub_facet_attributes))
    host_info.update(utils.data_map(VIRTUAL_HOST_MAPPING,
                                    sub_virt_host))
    host_info.update(utils.data_map(CONTENT_FACET_MAPPING,
                                    content_facet_attributes))
    host_info.update(utils.data_map(ERRATA_MAPPING,
                                    cf_errata_counts))
    if api_version == 1:
        host_info.update(utils.data_map(FACTS_MAPPING,
                                        facts))
    else:
        host_info.update(utils.data_map(FACTS_V2_MAPPING,
                                        facts))
    host_info.update(utils.data_map(VIRTUAL_HOST_MAPPING,
                                    virtual_host))
    host_info.update(utils.data_map(ERRATA_MAPPING,
                                    errata_counts))

    if sub_virt_guest:
        host_info[VIRTUAL_GUESTS] = [x[NAME] for x in sub_virt_guest]
        host_info[NUM_VIRTUAL_GUESTS] = len(sub_virt_guest)

    if virtual_guests:
        host_info[VIRTUAL_GUESTS] = [x[NAME] for x in virtual_guests]
        host_info[NUM_VIRTUAL_GUESTS] = len(virtual_guests)

    if host_info.get(VIRTUAL_GUESTS):
        host_info[VIRTUAL] = HYPERVISOR

    host_info[LOCATION] = fields.get(LOCATION_NAME)

    ipv4s = []
    macs = []
    for key in facts:
        net_interface = (key.startswith(NET_INTER_PERIOD) or
                         key.startswith(NET_INTER_COLON))
        net_interface_lo = (key.startswith(NET_INTER_LO_PERIOD) or
                            key.startswith(NET_INTER_LO_COLON))
        if net_interface and not net_interface_lo:
            ipv4_addr = (key.endswith(IPV4_PERIOD) or
                         key.endswith(IPV4_COLON))
            mac_addr = (key.endswith(MAC_PERIOD) or
                        key.endswith(MAC_COLON))
            if ipv4_addr and facts[key]:
                ipv4s.append(facts[key].lower())
            if mac_addr and facts[key]:
                macs.append(facts[key].lower())

    macs = list(set(macs))
    ipv4s = list(set(ipv4s))

    host_info[IP_ADDRESSES] = ipv4s
    host_info[MAC_ADDRESSES] = macs

    os_release = host_info.get(OS_RELEASE)
    if os_release:
        os_name = os_release.split(' ')[0]
        if os_release.lower().startswith('red hat enterprise linux'):
            os_name = 'Red Hat Enterprise Linux'
        elif os_release.lower().startswith('red hat'):
            os_name = 'Red Hat'
        host_info[OS_NAME] = os_name
        host_info[OS_VERSION] = os_release.rsplit(' ').pop()

    host_info.pop(VIRTUAL_GUESTS, None)
    return host_info
Ejemplo n.º 3
0
def host_fields(scan_task, api_version, url, org_id, host_id):
    """Obtain the fields for a given host id.

    :param scan_task: The scan task being executed
    :param api_version: The version of the Satellite api
    :param url: The endpoint URL to get data from
    :param org_id: The organization identifier
    :param host_id: The identifier of the host being queried.
    :returns: dictionary of facts from fields endpoint
    """
    response, url = utils.execute_request(scan_task,
                                          url=url,
                                          org_id=org_id,
                                          host_id=host_id,
                                          query_params=QUERY_PARAMS_FIELDS)
    # pylint: disable=no-member
    if response.status_code != requests.codes.ok:
        raise SatelliteException('Invalid response code %s'
                                 ' for url: %s' % (response.status_code, url))
    fields = response.json()
    host_info = {}
    sub_virt_host = None
    sub_virt_guest = None
    cf_errata_counts = None
    sub_facet_attributes = fields.get(SUBSCRIPTION_FACET)
    content_facet_attributes = fields.get(CONTENT_FACET)
    facts = fields.get(FACTS, {})
    virtual_host = fields.get(VIRTUAL_HOST, {})
    virtual_guests = fields.get(VIRTUAL_GUESTS)
    errata_counts = fields.get(ERRATA_COUNTS, {})

    if sub_facet_attributes:
        sub_virt_host = sub_facet_attributes.get(VIRTUAL_HOST)
        sub_virt_guest = sub_facet_attributes.get(VIRTUAL_GUESTS)
    if content_facet_attributes:
        cf_errata_counts = content_facet_attributes.get(ERRATA_COUNTS)

    host_info.update(utils.data_map(FIELDS_MAPPING, fields))
    host_info.update(utils.data_map(SUBS_FACET_MAPPING, sub_facet_attributes))
    host_info.update(utils.data_map(VIRTUAL_HOST_MAPPING, sub_virt_host))
    host_info.update(
        utils.data_map(CONTENT_FACET_MAPPING, content_facet_attributes))
    host_info.update(utils.data_map(ERRATA_MAPPING, cf_errata_counts))
    if api_version == 1:
        host_info.update(utils.data_map(FACTS_MAPPING, facts))
    else:
        host_info.update(utils.data_map(FACTS_V2_MAPPING, facts))
    host_info.update(utils.data_map(VIRTUAL_HOST_MAPPING, virtual_host))
    host_info.update(utils.data_map(ERRATA_MAPPING, errata_counts))

    if sub_virt_guest:
        host_info[VIRTUAL_GUESTS] = [x[NAME] for x in sub_virt_guest]
        host_info[NUM_VIRTUAL_GUESTS] = len(sub_virt_guest)

    if virtual_guests:
        host_info[VIRTUAL_GUESTS] = [x[NAME] for x in virtual_guests]
        host_info[NUM_VIRTUAL_GUESTS] = len(virtual_guests)

    if host_info.get(VIRTUAL_GUESTS):
        host_info[VIRTUAL] = HYPERVISOR

    host_info[LOCATION] = fields.get(LOCATION_NAME)

    ipv4s = []
    macs = []
    for key in facts:
        net_interface = (key.startswith(NET_INTER_PERIOD)
                         or key.startswith(NET_INTER_COLON))
        net_interface_lo = (key.startswith(NET_INTER_LO_PERIOD)
                            or key.startswith(NET_INTER_LO_COLON))
        if net_interface and not net_interface_lo:
            ipv4_addr = (key.endswith(IPV4_PERIOD) or key.endswith(IPV4_COLON))
            mac_addr = (key.endswith(MAC_PERIOD) or key.endswith(MAC_COLON))
            if ipv4_addr and facts[key]:
                ipv4s.append(facts[key])
            if mac_addr and facts[key]:
                macs.append(facts[key])
    host_info[IP_ADDRESSES] = ipv4s
    host_info[MAC_ADDRESSES] = macs

    os_release = host_info.get(OS_RELEASE)
    if os_release:
        os_name = os_release.split(' ')[0]
        if os_release.lower().startswith('red hat enterprise linux'):
            os_name = 'Red Hat Enterprise Linux'
        elif os_release.lower().startswith('red hat'):
            os_name = 'Red Hat'
        host_info[OS_NAME] = os_name
        host_info[OS_VERSION] = os_release.rsplit(' ').pop()

    host_info.pop(VIRTUAL_GUESTS, None)
    return host_info