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

Add IP ranges #259

Merged
merged 5 commits into from
Sep 13, 2019
Merged

Add IP ranges #259

merged 5 commits into from
Sep 13, 2019

Conversation

dabcoder
Copy link
Contributor

@dabcoder dabcoder commented Aug 9, 2019

Could be used in the terraform provider as there is an open feature request for that:
https://github.com/terraform-providers/terraform-provider-datadog/issues/216.

WIP, so the approach may not be the best.

@bkabrda
Copy link
Collaborator

bkabrda commented Aug 13, 2019

Hey David, thanks for submitting this PR. The GetIPRanges function should look more like any other function for getting all entities, e.g. have a look at https://github.com/zorkian/go-datadog-api/blob/master/api_keys.go#L74 - Most of the boilerplate code you've written is actually covered by the client.doJsonRequest, so I'd suggest using that. Does that make sense?

@dabcoder dabcoder force-pushed the davidb/ip-ranges branch 2 times, most recently from 2d61547 to abf3dac Compare August 14, 2019 11:03
@dabcoder dabcoder changed the title [WIP] Add IP ranges Add IP ranges Aug 23, 2019
Copy link
Collaborator

@bkabrda bkabrda left a comment

Choose a reason for hiding this comment

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

Just two more minor nits to fix and then we'll be able to merge. Thanks for the great work 👍

request.go Outdated
apiBase, err := url.Parse(client.baseUrl + "/api" + api)
var err error
// If api is an endpoint starting with v1, we construct the apiBase URL using the baseUrl to which we append "/api" and the actual value of api
if strings.Contains(api, "v1") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

TBH I'm a little unsure about this condition. We could theoretically be passing in full URLs that we expect to just return and which might still contain v1. I'd much rather that this was if !(strings.HasPrefix("https://") or strings.HasPrefix("http://"))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I also started in that direction but both URL types will start with https:

  • client.baseUrl + "/api" + api will be https://api.datadoghq.com/api/v1....
  • IP ranges URLs: https://ip-ranges.datadoghq.com/ or https://ip-ranges.datadoghq.eu/
    So not sure if we should compare based on https or http?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey, sorry I'm a bit late here.
Hmm, this is a good point, because we also aren't sending the API/APP keys in the query params based on this if condition. Due to that and the fact that we have some v2 endpoints already (see the dash list v2 endpoint), we can't use this condition, otherwise those requests will be sent without auth and fail.

Let me know if I'm reading any of this incorrectly 😄 At this point in the code, api is either a URI from the caller, like /v2/dashboards/... or /v1/hosts/... or it's the whole URL for IP ranges https://ip-ranges.datadoghq.com/.
(the https:api.datadog.... is only added later) So I think the proposed change Slavek made would work here. And then we only add authentication (api/app keys) to paths that are URIs, and not if a full URL is passed into this function.

request.go Outdated
@@ -31,17 +31,43 @@ type Response struct {
// uriForAPI is to be called with something like "/v1/events" and it will give
// the proper request URI to be posted to.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please amend the docstring of this function to cover the change done below?

Copy link
Collaborator

@nmuesch nmuesch left a comment

Choose a reason for hiding this comment

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

LGTM, assuming this was tested against the API 😄

request.go Outdated
apiBase, err := url.Parse(client.baseUrl + "/api" + api)
var err error
// If api is a URI such as /v1/hosts/, /v2/dashboards... add credentials and return a properly formatted URL
if !strings.HasPrefix(api, "https://") || !strings.HasPrefix(api, "http://") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

So I didn't actually try this out, but I don't think this condition is right. If the api value is e.g. https://something, the second part would still evaluate as true and so the if clause would actually be entered. I think what you need to do is !(strings.HasPrefix(api, "https://") || strings.HasPrefix(api, "http://"))

Copy link
Collaborator

@bkabrda bkabrda left a comment

Choose a reason for hiding this comment

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

LGTM now 👍 thanks for all your work on this!

@bkabrda bkabrda merged commit 5048796 into zorkian:master Sep 13, 2019
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.

3 participants