Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn Host kwargs into object's attributes every time #213

Conversation

ogajduse
Copy link
Member

Let's have the following host

class IPAHost(Host):
    def __init__(self, sat_obj, **kwargs):
        self.satellite = sat_obj
        kwargs['hostname'] = kwargs.get('hostname', settings.ipa.hostname)
        # Allow the class to be constructed from kwargs
        kwargs['from_dict'] = True
        kwargs.update(
            {
                'base_dn': settings.ipa.basedn,
                'disabled_user_ipa': settings.ipa.disabled_ipa_user,
            }
        )
        super().__init__(**kwargs)

Then the following code snippet

sat = Host('foo.bar')
ipahost = IPAHost(sat)
ipahost.hostname

will fail with AttributeError: 'IPAHost' object has no attribute 'hostname'

The Host __init__ stores the hostname and the name in args, not in kwargs. So we need to set name and hostname directly regardless of what from_dict is set to.

Ref: SatelliteQE/robottelo#11445
This is most likely the first host that is using from_dict. 😏

@ogajduse ogajduse added the bug Something isn't working label May 11, 2023
@ogajduse ogajduse self-assigned this May 11, 2023
@ogajduse ogajduse changed the title Assign name and hostname to the host if it is constructed Assign name and hostname to the host if it is constructed with from_dict=True May 11, 2023
@ogajduse
Copy link
Member Author

Another approach I can think of is to remove the from_dict logic and update __dict__ every time.

@JacobCallahan
Copy link
Member

I like your last comment. Additionally, now that hostname and name aren't technically required. We can remove them as named arguments and just take it all in as kwargs. If we go that route though, it would be good to add a docstring telling people which combination of arguments are required.

@Griffin-Sullivan
Copy link
Collaborator

Should I review this version or are we changing the PR? @ogajduse

@ogajduse
Copy link
Member Author

@Griffin-Sullivan @JacobCallahan Implemented.
Feel free to review it now.

broker/hosts.py Outdated Show resolved Hide resolved
broker/hosts.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@Griffin-Sullivan Griffin-Sullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran some functional tests and used ipython to make sure host creation works with hostname and IP and doesn't work when missing either. I also verified that changing the hostname and IP to null before checkin doesn't throw an error once the checkin completes.

broker/hosts.py Show resolved Hide resolved
@ogajduse ogajduse force-pushed the fix/from-dict-host-construction-hostname branch 2 times, most recently from f0736b0 to f22f7ee Compare May 16, 2023 20:39
@ogajduse ogajduse changed the title Assign name and hostname to the host if it is constructed with from_dict=True Turn Host kwargs into object's attributes every time May 16, 2023
@ogajduse ogajduse force-pushed the fix/from-dict-host-construction-hostname branch from f22f7ee to c9ff479 Compare May 17, 2023 15:45
@JacobCallahan JacobCallahan merged commit 220609f into SatelliteQE:master May 17, 2023
@ogajduse ogajduse deleted the fix/from-dict-host-construction-hostname branch May 22, 2023 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants