-
Notifications
You must be signed in to change notification settings - Fork 630
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
Add data source load balancer pools #1228
Conversation
Optional: true, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
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
|
There we go, let me know if I missed anything @jacobbednarz |
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 | ||
} |
There was a problem hiding this comment.
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:
- https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/cloudflare/data_source_waf_groups.go#L29-L46
- https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/cloudflare/data_source_waf_groups.go#L92-L96
- https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/cloudflare/data_source_waf_groups.go#L122-L128
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-authored-by: Raphaël Beamonte <[email protected]>
@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? |
changelog detected ✅ |
@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. |
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! |
This adds a Data Source for Load Balancer Pools resources.
i.e:
Since there is no actual filter on the API client, the data source doesn't offer any either.