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 data source load balancer pools #1228

Merged

Conversation

wayt
Copy link
Contributor

@wayt wayt commented Sep 30, 2021

This adds a Data Source for Load Balancer Pools resources.

i.e:

data "cloudflare_load_balancer_pools" "pools" {
}

Since there is no actual filter on the API client, the data source doesn't offer any either.

⚠️ this is my first time contributing here, let me know if I missed any guidelines.

Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
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 hesitated to directly use resourceCloudflareLoadBalancerPool().Schema here, although it doesn't offer the id field.

Let me know if you think otherwise.

Copy link
Member

@jacobbednarz jacobbednarz Oct 1, 2021

Choose a reason for hiding this comment

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

why not append the id if you need it?

func dataSourceLoadBalancerPoolSchema() map[string]*schema.Schema {
	schema := resourceCloudflareLoadBalancerPool().Schema

	schema["id"] = *schema.Schema{
		Type:     schema.TypeString,
                Computed: true,
 
	}

	return schema
}

func dataSourceCloudflareLoadBalancerPools() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceCloudflareLoadBalancerPoolsRead,
		Schema: dataSourceLoadBalancerPoolSchema(),
        }
}

to be honest though, i'm not sure why you'd need anything other than the id on it's own. what is your use case for having everything you've listed? 🤷‍♂️ from me on the rest of them as i personally wouldn't reference them anywhere.

Copy link
Member

Choose a reason for hiding this comment

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

actually, no, you don't want the resourceCloudflareLoadBalancerPool schema as the fields won't line up correctly as a data source 🤦‍♂️ it's geared towards being a managed resource as opposed to a data source.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😓 So we leave it as is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what is your use case for having everything you've listed?

We manage pools outside of Terraform (because of automation). But load balancers are in Terraform, so to avoid Terraform wiping them away we need that kind of data source.

We do some other filtering later within Terraform, unfortunately, that's not possible with the API.

Copy link
Contributor

Choose a reason for hiding this comment

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

tbh that would be weird having a data source that does not provide all the information about a pool; different usecases miight need different information from the pools, so might as well have the complete data source right away! (even if it's bound to evolve with lb pools evolutions)

Copy link
Member

Choose a reason for hiding this comment

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

i guess it really comes down to what you're using it for. the API itself only references IDs so things like the other properties aren't able to be used as references anywhere but i'm not phased if you want to include everything else here.

@jacobbednarz
Copy link
Member

thanks for solid start here @wayt, it's good! i've got some documentation i'm working on locally for PR minimums but for now, here is the tl;dr for this PR

  • website documentation is required (see other data source documentation for examples)
  • changelog note (see changelog-process)
  • passing tests which you've almost got here

@wayt
Copy link
Contributor Author

wayt commented Oct 1, 2021

There we go, let me know if I missed anything @jacobbednarz

Comment on lines 104 to 141
func dataSourceCloudflareLoadBalancerPoolsRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading Load Balancer Pools")
client := meta.(*cloudflare.API)

poolsObj, err := client.ListLoadBalancerPools(context.Background())
if err != nil {
return fmt.Errorf("error listing load balancer pools: %w", err)
}

poolIds := make([]string, 0)
pools := make([]map[string]interface{}, 0, len(poolsObj))
for _, p := range poolsObj {
pools = append(pools, map[string]interface{}{
"id": p.ID,
"name": p.Name,
"origins": flattenLoadBalancerOrigins(d, p.Origins),
"enabled": p.Enabled,
"minimum_origins": p.MinimumOrigins,
"latitude": p.Latitude,
"longitude": p.Longitude,
"check_regions": p.CheckRegions,
"description": p.Description,
"monitor": p.Monitor,
"notification_email": p.NotificationEmail,
"load_shedding": flattenLoadBalancerLoadShedding(p.LoadShedding),
"created_on": p.CreatedOn.Format(time.RFC3339Nano),
"modified_on": p.ModifiedOn.Format(time.RFC3339Nano),
})
poolIds = append(poolIds, p.ID)
}

if err := d.Set("pools", pools); err != nil {
return fmt.Errorf("error setting load balaner pools: %w", err)
}

d.SetId(stringListChecksum(poolIds))
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great to have filtering handled there too.
Even if not needed for the specific usecase here, I believe that it's kind of important to provide ways to filter the pools, either directly from the API request (if the API allows) or with basic pattern matching directly in the provider. Other data resources have similar approaches, waf groups as an example:

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 can add this if @jacobbednarz agrees here.

Copy link
Member

@jacobbednarz jacobbednarz Oct 12, 2021

Choose a reason for hiding this comment

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

filtering is definitely a nice to have for targeting a specific resource. say you know the name is "example pool" you can chuck that in and have multiple data sources instead of referencing them directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay, I just added filtering on the name and the corresponding doc.

Let me know what else is missing here.

@xens
Copy link
Contributor

xens commented Dec 6, 2022

@jacobbednarz I'd like to help on that PR, what's in your opinion the best way to contribute considering that it's been stalled for >1year?

@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2022

changelog detected ✅

@jacobbednarz
Copy link
Member

@xens i've gone ahead and got this up to speed with the new conventions however, i'll have a play later this week to make sure it's working as expected. feel free to pull it locally and give it a whirl too.

@jacobbednarz jacobbednarz merged commit 78a4322 into cloudflare:master Dec 14, 2022
@github-actions github-actions bot added this to the v3.30.0 milestone Dec 14, 2022
github-actions bot pushed a commit that referenced this pull request Dec 14, 2022
@github-actions
Copy link
Contributor

This functionality has been released in v3.30.0 of the Terraform Cloudflare Provider.

Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

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.

4 participants