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 synthetics locations data source #309

Merged
merged 16 commits into from
Jul 29, 2020

Conversation

dabcoder
Copy link
Contributor

@dabcoder dabcoder commented Sep 2, 2019

@ghost ghost added the size/S label Sep 2, 2019
@dabcoder dabcoder force-pushed the davidb/synthetics-data-source branch 2 times, most recently from 33fc6af to c38e1ef Compare October 11, 2019 17:13
@dabcoder dabcoder force-pushed the davidb/synthetics-data-source branch from 3584150 to ece5da1 Compare October 22, 2019 09:40
@dabcoder dabcoder marked this pull request as ready for review October 22, 2019 09:40
@dabcoder dabcoder changed the title Add draft synthetics locations data source Add synthetics locations data source Oct 25, 2019
Copy link
Contributor

@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.

Overall looks good, thanks for this. Left a few small notes/questions.

Comment on lines 44 to 41
if len(syntheticsLocations) > 0 {
d.SetId("datadog-synthetics-location")
d.Set("locations", locationsList)
} else {
d.Set("locations", []string{})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Whats this if section for ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In case the list of locations returned by the Go client is nil, we still set a locations attribute but with no values. Similar to https://github.com/terraform-providers/terraform-provider-datadog/blob/master/datadog/data_source_datadog_ip_ranges.go#L113:L118

website/docs/d/synthetics_locations.html.markdown Outdated Show resolved Hide resolved

// Locations are a list of string
Schema: map[string]*schema.Schema{
"locations": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any use case for returning the rest of the values this API endpoint returns (id, region, etc) ? We can add new schemas here later but changing this locations attribute to include them later will be challenging if we want them.

Copy link
Contributor Author

@dabcoder dabcoder Oct 31, 2019

Choose a reason for hiding this comment

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

I don't think so, in order to create a test (with this data source location), the "name" is the only attribute used: https://docs.datadoghq.com/api/?lang=bash#create-a-test.
If we go that route we may want to update the attribute to locations_name maybe?
If we want to fetch all attributes from the locations I am not sure how this would work, with a list of maps perhaps (my testing isn't working in that case). EDIT: maybe with something similar to https://github.com/terraform-providers/terraform-provider-github/blob/0a3a420af42e3124b7972ff2e4fb56d949688c4f/github/data_source_github_collaborators.go#L157-L181.

Copy link
Contributor

Choose a reason for hiding this comment

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

@btoueg We need your input on this one 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend we keep at least region, display_name and name, e.g.:

[{"region":"Asia Pacific","display_name":"Tokyo (AWS)","name":"aws:ap-northeast-1"}, ...]

How is that data supposed to be used? Can we provide an example in the doc?

I'm afraid an array will be less usable than a map

Copy link
Contributor Author

@dabcoder dabcoder Jan 3, 2020

Choose a reason for hiding this comment

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

Thanks for the feedback.

My understanding is that it would be defined as a data source such as in the below:

data "synthetics_locations" "example" {}

... in order to fetch all active locations and use them when creating/editing a test using something along the lines of:

resource "datadog_synthetics_test" "test_api" {
  type = "api"
  subtype = "http"
  request = {
    method = "GET"
    url = "https://www.example.org"
  }
  request_headers = {
    Content-Type = "application/json"
    Authentication = "Token: 1234566789"
  }
  assertions = [
    {
      type = "statusCode"
      operator = "is"
      target = "200"
    }
  ]
  locations = data.synthetics_locations.example.location_names
...
}

Instead of hardcoding all active locations one by one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid an array will be less usable than a map

According to https://www.terraform.io/docs/providers/datadog/r/synthetics.html#example-usage-synthetics-api-test-, locations needs to be a list, correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes it does need to be a list. It all depends how you want to use it, for instance, it could be something like:

resource "datadog_synthetics_test" "test_api" {
  ...
  locations = [
    data.synthetics_locations["Tokyo"],
    data.synthetics_locations["Sidney"],
  ]
...
}

Example above is not syntactically sound but you get the idea

Copy link
Contributor

@bkabrda bkabrda Jan 20, 2020

Choose a reason for hiding this comment

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

So assuming the name (e.g. aws:us-west-2) is unique for all the locations (CC @btoueg - is it guaranteed to be unique?), I think the optimal solution would be to have locations as map:

"aws:us-west-2": {
    "id": 123,
    "display_name":"Oregon (AWS)",
    ... 
}

and then users would be able to set up locations for the datadog_synthetics_test such as:

resource "datadog_synthetics_test" "test_api" {
    locations = data.synthetics_locations.example.locations.keys
}

That seems reasonably easy to use and allows us to carry additional data if/when necessary. Assuming it's not guaranteed that location names are unique, we'll have to go with list of maps and figure out a clever oneliner to access all the names (which, thinking about it, wouldn't make much sense if names weren't unique, so they probably are...) :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

name should be unique indeed, AWS regions don't include duplicates.
I've created a map here: https://github.com/terraform-providers/terraform-provider-datadog/pull/309/files#diff-247b9e616dd8b78804cde056db1f776aR23 and filled it with region, display_name and name so far.
Is that the right approach you think? Should we fetch more keys such as the ids, is_active?

Copy link
Contributor

Choose a reason for hiding this comment

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

@dabcoder what I meant was to make the top-level locations attribute not a list, but a map - so that it would become more easily accessible. IOW, the current attribute you use to set locations is locationsList and is a []string - I'm suggesting that you make this a map[string]LocationMap. Does that make sense?

// Fill locationsList with location names
for _, location := range syntheticsLocations {
// access the pointer of the struct element DisplayName
locationsList = append(locationsList, location.GetDisplayName())
Copy link
Contributor

Choose a reason for hiding this comment

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

If there's only one field to keep, it's Name rather than DisplayName

Suggested change
locationsList = append(locationsList, location.GetDisplayName())
locationsList = append(locationsList, location.GetName())


// Locations are a list of string
Schema: map[string]*schema.Schema{
"locations": {
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend we keep at least region, display_name and name, e.g.:

[{"region":"Asia Pacific","display_name":"Tokyo (AWS)","name":"aws:ap-northeast-1"}, ...]

How is that data supposed to be used? Can we provide an example in the doc?

I'm afraid an array will be less usable than a map

jirikuncar
jirikuncar previously approved these changes Jul 27, 2020
website/docs/d/synthetics_locations.html.markdown Outdated Show resolved Hide resolved
Comment on lines 39 to 41
if len(locationsMap) > 0 {
d.SetId("datadog-synthetics-location")
d.Set("locations", locationsMap)
} else {
d.Set("locations", locationsMap)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this for ?

Copy link
Contributor

Choose a reason for hiding this comment

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

We only set the ID if it's not empty, but we still set the map. I guess it should be outside the condition, let me fix it.

David Bouchare and others added 9 commits July 28, 2020 14:00
@therve therve force-pushed the davidb/synthetics-data-source branch from 35af645 to 0f84401 Compare July 28, 2020 12:01
@jirikuncar
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@therve
Copy link
Contributor

therve commented Jul 29, 2020

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@therve therve merged commit 890e5b3 into DataDog:master Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants