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

Fix netbox url with string concat #49943

Merged
merged 5 commits into from Mar 14, 2019

Conversation

heathdbrown
Copy link
Contributor

SUMMARY

Fixes #46705

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME
  • netbox_inventory
ADDITIONAL INFORMATION

The fix in #47655 made the problem worse.

The issue was with how urljoin() removes paths from the configuration.

>>> from urlparse import urljoin
>>> urljoin('http://netbox.internalurl.com/netbox', '/api/dcim/platforms/?limit=0')
'http://netbox.internalurl.com/api/dcim/platforms/?limit=0'
>>> urljoin('http://netbox.internalurl.com/netbox', 'api/dcim/platforms/?limit=0')
'http://netbox.internalurl.com/api/dcim/platforms/?limit=0'
>>> urljoin('netbox.internalurl.com/netbox/', 'api/dcim/platforms/?limit=0')
'netbox.internalurl.com/netbox/api/dcim/platforms/?limit=0'

The way the netbox.py is written the api urls are joined but they all have /api and when merged with /netbox overwrites to /api only which is a missing URL and causes 404s.

Fixing this would mean adjusting /api URL fragments to api OR adjusting the netbox.py to not use urljoin() and use string concat.

@ansibot
Copy link
Contributor

ansibot commented Dec 14, 2018

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. community_review In order to be merged, this PR must follow the community review workflow. inventory Inventory category needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:community This issue/PR relates to code supported by the Ansible community. labels Dec 14, 2018
@remyleone
Copy link
Contributor

If this patch we would need to be sure that there is no trailing slash for the base_url. // are not welcomed by netbox AFAIK

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_triage Needs a first human triage before being processed. community_review In order to be merged, this PR must follow the community review workflow. labels Dec 14, 2018
@heathdbrown
Copy link
Contributor Author

@sieben What would be the suggestion in handling this?

(I do not have a ton of coding experience)

Documentation?
Data validation on the api_endpoint?

@gundalow
Copy link
Contributor

@heathdbrown if api_endpoint ends with a / you could trim it that variable is used

@winem
Copy link
Contributor

winem commented Dec 19, 2018

Did a test on our netbox setup and API calls work with any number of slashes (tested with 1-6 out of curiosity) between the domain part and the actual path. A // in the path itself fails with a 404.

Examples:
working:

not working:

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Dec 19, 2018
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Dec 27, 2018
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Feb 5, 2019
@gundalow
Copy link
Contributor

@heathdbrown Could you please rebase this so we can get it merged

@heathdbrown
Copy link
Contributor Author

heathdbrown commented Feb 21, 2019

@gundalow I rebased and I believe this is ready now.

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Feb 21, 2019
@gundalow
Copy link
Contributor

@Anthony25 @FragmentedPacket @nikkytub @pilou- @sieben could we please get a final review on this?

@ansibot
Copy link
Contributor

ansibot commented Feb 28, 2019

@heathdbrown This PR contains @ mentions in at least one commit message. Those mentions can cause cascading notifications through GitHub and need to be removed. Please squash or amend your commits to remove the mentions.

click here for bot help

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Feb 28, 2019
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Feb 28, 2019
@FragmentedPacket
Copy link
Contributor

Sorry about this. Just reviewing it, might be a little cleaner way.

        REMOVE: self.api_endpoint = self.get_option("api_endpoint")
        # Handle extra "/" from api_endpoint configuration and trim if necessary, see PR#49943
        if self.get_option("api_endpoint").endswith("/"):
            self.api_endpoint = self.get_option("api_endpoint").strip('/')
        else:
            self.api_endpoint = self.get_option("api_endpoint")

@heathdbrown
Copy link
Contributor Author

@FragmentedPacket that fails as the self.api_endpoint above the if is the first assignment.

After commenting it out the plugin fails.

@FragmentedPacket
Copy link
Contributor

@heathdbrown Did you change the if statement to look at the self.get_option?

@heathdbrown
Copy link
Contributor Author

@FragmentedPacket well that was embarrassing. Thanks for the patience. I have included the complete suggestion.

@winem
Copy link
Contributor

winem commented Feb 28, 2019

Imho, there is no need for the if if you use .strip('/'). Instead you can just use it when you assign the variable.

.strip('/') will neither do anything nor fail if there is no trailing slash.

self.api_endpoint = self.get_option("api_endpoint").strip('/') should just work.

>>> api_endpoint='netbox.com/'
>>> api_endpoint.strip('/')
'netbox.com'
>>> api_endpoint='netbox.com////'
>>> api_endpoint.strip('/')
'netbox.com'
>>> api_endpoint='netbox.com'
>>> api_endpoint.strip('/')
'netbox.com'

@heathdbrown
Copy link
Contributor Author

@winem updated to your suggestion and removed the if logic.

@winem
Copy link
Contributor

winem commented Feb 28, 2019

Looks good to me!

@FragmentedPacket
Copy link
Contributor

@heathdbrown Hey i'm in the same boat as you lol

Looks good to me as well

@FragmentedPacket
Copy link
Contributor

@sieben Do you mind looking this over and approving? 2.8 RC stuff starts the 14th of March

@remyleone
Copy link
Contributor

LGTM

@remyleone
Copy link
Contributor

@Anthony25 What do you think about it?

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Mar 11, 2019
@nikkytub
Copy link
Contributor

LGTM as well

@ansibot ansibot added shipit This PR is ready to be merged by Core and removed community_review In order to be merged, this PR must follow the community review workflow. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Mar 11, 2019
@resmo
Copy link
Contributor

resmo commented Mar 14, 2019

rebuild_merge

@ansibot ansibot merged commit 53b5dff into ansible:devel Mar 14, 2019
@ansible ansible locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. inventory Inventory category new_contributor This PR is the first contribution by a new community member. shipit This PR is ready to be merged by Core support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Netbox inventory urljoin vs string concat produces different results
8 participants