-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/aws: data source for AWS Route Table #10301
provider/aws: data source for AWS Route Table #10301
Conversation
Read: dataSourceAwsRouteTableRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"subnet_id": &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.
As of Go 1.7+ the explicit type is no longer needed here, so &schema.Schema
can be removed.
Schema: map[string]*schema.Schema{ | ||
"subnet_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, |
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 am not sure if this should be optional? It seem that rest of the attributes are also optional, is this desirable?
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.
Is this ever going to be empty? Given the first req.Filters
getting some minimal filters.
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 think we can get a Route Table with
subnet_id
tags
vpc_id
andtags
- filters
What do you think ?
rt := resp.RouteTables[0] | ||
|
||
d.SetId(*rt.RouteTableId) | ||
d.Set("id", *rt.RouteTableId) |
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.
The d.SetId()
would set id
, there is no need to have it explicitly set.
req.Filters = append(req.Filters, buildEC2CustomFilterList( | ||
d.Get("filter").(*schema.Set), | ||
)...) | ||
if len(req.Filters) == 0 { |
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.
Is this ever going to be empty? Given the first req.Filters
getting some minimal filters.
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 add an error if there is no filter
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 see, so you want to use the sheer fact that buildEC2AttributeFilterList
would return empty slice given that values in the map are empty. Which is fine, albeit it can be make a whole lot simpler should you break even before getting to this helper. You can do this as follows:
vpcId, vpcIdOk := d.GetOk("vpc_id")
subnetId, subnetIdOk := d.GetOk("subnet_id")
if !vpcIdOk && !subnetIdOk {
return fmt.Errorf("One of vpc_id or subnet_id must be assigned")
}
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"vpc-id": vpcId.(string),
"association.subnet-id": subnetId.(string),
},
)
Hi @kwilczynski,
|
}, | ||
"filter": ec2CustomFiltersSchema(), | ||
|
||
"id": { |
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.
This is not needed (as per the earlier comment, you do not to set id
explicitly - this is done via the d.SetId()
.
@mathieuherbert hi there! Thank you for your updates! I see, you are allowing to set either and then narrow the result down with either tags or filters. Which is fine, but in such a case I would add a check whether at least one is set, and issue an appropriate message should that not be the case. |
@mathieuherbert hi there again! I was wondering, how would you feel about adding |
Hi @kwilczynski, So now, I export every |
d.Set("vpc_id", rt.VpcId) | ||
d.Set("tags", tagsToMap(rt.Tags)) | ||
|
||
routes := make([]map[string]interface{}, 0, len(rt.Routes)) |
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.
Very nice! I think it might be better served as a function.
} | ||
d.Set("routes", routes) | ||
|
||
associations := make([]map[string]interface{}, 0, len(rt.Associations)) |
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.
Same as above, if we turn this into a function it would be great.
return err | ||
} | ||
if resp == nil || len(resp.RouteTables) == 0 { | ||
return fmt.Errorf("no matching Route Table found") |
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.
Check the Amazon AMI data source for an idea of more user-friendly error messages.
|
||
routes = append(routes, m) | ||
} | ||
d.Set("routes", routes) |
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.
When storing complex values, if would recommend adding an error check, for example:
if err := d.Set("routes", routes); err != nil {
return err
}
@kwilczynski , thank you very much for your help, it is very interesting ! |
@mathieuherbert thank you for working on it! |
@kwilczynski , do you have some comments on my last commit please ? Thanks ! |
|
||
d.SetId(*rt.RouteTableId) | ||
d.Set("vpc_id", rt.VpcId) | ||
d.Set("tags", tagsToMap(rt.Tags)) |
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.
if err := d.Set("tags", tagsToMap(rt.Tags)); err != nil {
return err
}
associations := make([]map[string]interface{}, 0, len(ec2Assocations)) | ||
// Loop through the routes and add them to the set | ||
for _, a := range ec2Assocations { | ||
|
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.
No need for the extra line.
@mathieuherbert hi there! Looks good! Two minor comments, and then I think ready to go. |
Massive thanks to @kwilczynski for all the work here! |
Merged manually :) |
|
||
* `filter` - (Optional) Custom filter block as described below. | ||
|
||
* `id` - (Optional) The id of the specific Route Table to retrieve. |
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.
Was this parameter implemented? I can't see any reference to it in data_source_aws_route_table.go
and get this when I try to use it,
Errors:
* data.aws_route_table.selected: : invalid or unknown key: id
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Hi,
I added an AWS provider, aws_route_table.
We can retrieve, for example a route table from a subnet id and add a route with this route table
Tests: