-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 support for alias_ip_range in google_compute_instance network interface #375
Changes from 4 commits
2076f16
e1eb2a6
bb44d58
0f0f2c6
4f7ab64
838cfeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package google | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ComputeApiVersion uint8 | ||
|
@@ -71,7 +73,10 @@ func getComputeApiVersionUpdate(d TerraformResourceData, resourceVersion Compute | |
// A field of a resource and the version of the Compute API required to use it. | ||
type Feature struct { | ||
Version ComputeApiVersion | ||
Item string | ||
// Path to the beta field. Supports: | ||
// - beta field: "min_cpu_platform" | ||
// - nested beta field: "network_interface.*.alias_ip_range" | ||
Item string | ||
} | ||
|
||
// Returns true when a feature has been modified. | ||
|
@@ -84,8 +89,34 @@ func (s Feature) HasChangeBy(d TerraformResourceData) bool { | |
|
||
// Return true when a feature appears in schema or has been modified. | ||
func (s Feature) InUseBy(d TerraformResourceData) bool { | ||
_, ok := d.GetOk(s.Item) | ||
return ok || s.HasChangeBy(d) | ||
return inUseBy(d, s.Item) | ||
} | ||
|
||
func inUseBy(d TerraformResourceData, path string) bool { | ||
pos := strings.Index(path, "*") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to say I'm not a huge fan of this syntax. I appreciate that you do have to make some sort of change however to be able to see if this feature is in use for any of the If I see a '*' I would expect it to be globbing like a regex rather than having it match a sublist. Anyway to make the behavior like that? (Alternatively, if we could see if a feature is in use by using a more structural syntax I'd totally love that (similar to how we navigate the Also, can we document this syntax in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no use case for globbing on a field. I don't expect why a beta field would be present under two different field. You can see the I am not sure I follow your suggestion about structural syntax... I will add more documentation once we agree on the syntax. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you're globbing on a field right now! (or at least, you're globbing on an integer) I guess I'm saying is that if we introduce syntax like '*' but it is specific to integers, it's asking for a bug in the future when someone uses '*' like globbing. Given that globbing would work here, would it be too hard to add? For a more structural syntax, I meant being able to receive and parse the actual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supporting globbing also on field names would definitely add complexity (and potentially bugs) when there is clearly no needs for this. However, I can make sure it fails if with a nice error message if someone uses the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to block this whole PR on the syntax (especially since what's currently-implemented would work if * was a globbing symbol) but I'm starting to worry that this That said, we can probably have that discussion outside of this PR. If we document the '*' syntax I'm ok as-is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, after looking at the code, you were right and I was confused; it IS operating on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation added. |
||
if pos == -1 { | ||
_, ok := d.GetOk(path) | ||
return ok || d.HasChange(path) | ||
} | ||
|
||
prefix := path[0:pos] | ||
suffix := path[pos+1:] | ||
|
||
v, ok := d.GetOk(prefix + "#") | ||
|
||
if !ok { | ||
return false | ||
} | ||
|
||
count := v.(int) | ||
for i := 0; i < count; i++ { | ||
nestedPath := fmt.Sprintf("%s%d%s", prefix, i, suffix) | ||
if inUseBy(d, nestedPath) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func maxVersion(versionsInUse map[ComputeApiVersion]struct{}) ComputeApiVersion { | ||
|
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.
Minor quibble; this is just a bit too terse for me (and maybe i"m wrong but I don't think it works for all nested fields right? Only integer-nested fields?)
Maybe something like:
// - integer-nested field: If the Item field contains a '*', then the feature is considered to be in-use if it is present in any element of the list or set (e.g. "network_interface.*.alias_ip_range" is considered to be in-use if "network_interface.3.alias_ip_range" was set in the state
Hopefully you can come up with something better (also I like your name
path
better for the field name)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 updated the documentation incorporating some of the bits you suggested. Hopefully it is clearer now.
Yes, it works for all nested fields (not just integer nested fields). The proof is that alias_ip_range is of TypeList and network_interface is of TypeList.
It doesn't support beta field nested under a set at the moment. It would add complexity and be slow since we would need to scan the whole state to find the elements in the set (we can't rely on zero-indexed incremental indices like we do for lists). If we ever need to support this, we can implement this efficiently with a trie.
Note: I will rename the
Item
forPath
orFieldPath
in a separate PR. Since updating the field touches a lot of files, I prefer to do it in a separate PR that can get merged quickly to avoid the rebase/merge hassle and also to avoid polluting this already long PR .