schubergphilis / towerlib

A python library to interface with ansible tower's (awx) api.
MIT License
43 stars 39 forks source link

if entity_name else f'{self._tower.host}{url}', when url=None, var url returns string None #104

Closed ningen1 closed 2 years ago

ningen1 commented 2 years ago
class EntityManager:
    """Manages entities by making them act like iterables but also implements contains and other useful stuff."""

    # pylint: disable=too-many-arguments
    def __init__(self, tower_instance, entity_object, primary_match_field, entity_name=None, url=None):
        if not any([entity_name, url]):
            raise ValueError('Either entity_name or url needs to be provided, received none.')
        self._tower = tower_instance
        self._object_type = entity_object
        self._primary_match_field = primary_match_field
        self._name = entity_name
        self._next_state = None
        self._url = f'{self._tower.api}/{entity_name}' if entity_name else f'{self._tower.host}{url}'

i think i am hitting this if entity_name else f'{self._tower.host}{url}' and getting jobs has failed with exception: HTTPSConnectionPool(host='localhostnone', port=443): Max retries exceeded with url: / (Caused by NewConnection Error('<urllib3.connection.HTTPSConnection object at 0x7f9aa2b4e520>: Failed to establish a new connection: [Errno -2] Name or service not known'))

what do you think

costastf commented 2 years ago

I think you are right. You seem to be instantiating the main tower object with host set to localhost is that correct?

ningen1 commented 2 years ago

in my case i am doing:

tower = towerlib.Tower('localhost', username, password, secure=True, ssl_verify=False)
tower.get_jobs_by_name(template_name)

localhost i changed here because i use my public test.domain.name

i am not sure right now why there is a difference between self._tower.api and self._tower.host because other filters and parts of the code seems to work fine when doing queries entity_name=None i need to check where entity_name comes from and why it picks _tower.host instead of _tower.api

costastf commented 2 years ago

EntityManager is an abstruction over the entities that you can get from the service. There are two ways to get them, one is providing the entity name (type in this case) like User and the other one is targeting the actual url itself for that object like tower_host/url The reason is that some paginations are easier one way and some the other, if I remember correctly. (its been a while)

costastf commented 2 years ago

for example projects get instantiated like:

    @property
    def projects(self):
        """The projects that the user is part of.

        Returns:
            EntityManager: EntityManager of the projects.

        """
        url = self._data.get('related', {}).get('projects')
        return EntityManager(self._tower,
                             entity_object='Project',
                             primary_match_field='name',
                             url=url)

where organizations like :

    @property
    def organizations(self):
        """The organizations configured in tower.

        Returns:
            EntityManager: The manager object for organizations.

        """
        return EntityManager(self,
                             entity_name='organizations',
                             entity_object='Organization',
                             primary_match_field='name')
ningen1 commented 2 years ago

sorry i think i was not correct... i think error comes from somewhere else... closing this