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

introduce PACKET_API_URL env variable #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

displague
Copy link
Member

Just as PACKET_AUTH_TOKEN can be used today to set an authentication token, PACKET_API_URL is being added to allow developers and users to set custom API URL targets. Some applications may already provide overrides via configuration files and environment. This environment variable will provide flexibility where applications did offer this.

The existing default value has been preserved for NewClient, NewClientWithAuth, and NewClientWithBaseURL. The former can now take advantage of PACKET_API_URL.

Fixes #322

Just as PACKET_AUTH_TOKEN can be used today to set an authentication token, PACKET_API_URL is being added to allow developers and users to set custom API URL targets. Some applications may already provide overrides via configuration files and environment. This environment variable will provide flexibility where applications did offer this.

The existing default value has been preserved for NewClient,
NewClientWithAuth, and NewClientWithBaseURL. The former can now take
advatange of PACKET_API_URL.

Signed-off-by: Marques Johansson <mjohansson@equinix.com>
@ctreatma
Copy link
Contributor

I'm not clear what we gain by adding this. We already support alternative base URLs as an argument when creating a new packngo client, and we're using that in both metal-cli and terraform-provider-equinix.

@displague
Copy link
Member Author

displague commented Feb 23, 2023

Oh! Terraform picked this up in the move to the terraform-provider-equinix provider, borrowing from the EQUINIX_API_ENDPOINT environment and endpoint provider config options. Looks like I may have introduced that for Metal too 🙃. This means that we can see how Prism reacts when Terraform E2E tests run against the API spec (#322 (comment)).

Metal CLI appears to be using the NewClientWithBaseURL function, but the base URL is not configurable today. This PR wouldn't help metal-cli out, which was one of the intents. https://github.com/equinix/metal-cli/blob/a5e101858bce400e75c0eee011770e8c75dd35e7/cmd/cli.go#L46-L55
The better approach for Metal CLI will be to make the API endpoint configurable through metal configuration options. equinix/metal-cli#169

With those two benefits knocked out, we would get external configurability in any other tool built against packngo with this change and using NewClient (and not the NewClientWithBaseURL function). These would be any client not providing a bespoke configurable base URL option.

Given the primary targets would not be addressed by this PR, I'd be willing to close this and the parent issue in favor of deferring to tool owners and metal-go alternatives.

The PR is mostly a cleanup/refactor of how the URL is passed between client constructors and tests with the addition of the environment variable that is consulted for an override.

If this PR stands, more other question to consider is PACKNGO_API_BASE_URL vs PACKET_API_URL. The latter fits the default auth token environment, and the former fits the debug option.

@displague
Copy link
Member Author

I attempted to use this feature, not remembering that the PR was stalled.

$ PACKNGO_DEBUG=1 PACKET_API_URL=https://api.packet.net/ metal devices get
2023/04/24 10:23:25
=======[REQUEST]=============
GET /metal/v1/projects/.../devices?include=facility HTTP/1.1
Host: api.equinix.com

I was looking for a simple way to confirm the following packet-python issue:
packethost/packet-python#135

I say this as additional context for how this type of ENV override feature can be handy. For example, packet-python does not provide a way to override the API URL: https://github.com/packethost/packet-python/blob/master/packet/baseapi.py#L57-L71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Base URL alternative should be configurable via the environment
2 participants