-
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 1 commit
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 |
---|---|---|
|
@@ -23,6 +23,10 @@ var InstanceVersionedFeatures = []Feature{ | |
Version: v0beta, | ||
Item: "min_cpu_platform", | ||
}, | ||
{ | ||
Version: v0beta, | ||
Item: "network_interface.*.alias_ip_range", | ||
}, | ||
} | ||
|
||
func stringScopeHashcode(v interface{}) int { | ||
|
@@ -337,6 +341,27 @@ func resourceComputeInstance() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
|
||
"alias_ip_range": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"ip_cidr_range": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: ipCidrRangeDiffSuppress, | ||
}, | ||
"subnetwork_range_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -793,6 +818,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err | |
iface.Network = networkLink | ||
iface.Subnetwork = subnetworkLink | ||
iface.NetworkIP = address | ||
iface.AliasIpRanges = expandAliasIpRanges(d.Get(prefix + ".alias_ip_range").([]interface{})) | ||
|
||
// Handle access_config structs | ||
accessConfigsCount := d.Get(prefix + ".access_config.#").(int) | ||
|
@@ -1032,9 +1058,10 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error | |
"name": iface.Name, | ||
"address": iface.NetworkIP, | ||
"network": iface.Network, | ||
"subnetwork": iface.Subnetwork, | ||
"subnetwork": ConvertSelfLinkToV1(iface.Subnetwork), | ||
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. Is 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. The problem is that subnetwork can be specified using the If a user specify a name in the tf config, the diff is suppressed correctly using The other option is to change the DiffSuppressFunc to something along these lines: DiffSuppressFunc: func(...) bool {
return compareSelfLinkRelativePaths(...) || linkDiffSuppress(...)
} We could factored this logic into its own method 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. That makes sense, thanks! Looking at how As I understand it, we are always going to store a full self link in state, so
That way we can cover both cases with the same diff function, and this should work for Zonal resources too if there is a similar case, without adding a third function for that as well. 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. Done |
||
"subnetwork_project": getProjectFromSubnetworkLink(iface.Subnetwork), | ||
"access_config": accessConfigs, | ||
"alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges), | ||
}) | ||
} | ||
} | ||
|
@@ -1573,6 +1600,18 @@ func expandGuestAccelerators(zone string, configs []interface{}) []*computeBeta. | |
return guestAccelerators | ||
} | ||
|
||
func expandAliasIpRanges(ranges []interface{}) []*computeBeta.AliasIpRange { | ||
ipRanges := make([]*computeBeta.AliasIpRange, 0, len(ranges)) | ||
for _, raw := range ranges { | ||
data := raw.(map[string]interface{}) | ||
ipRanges = append(ipRanges, &computeBeta.AliasIpRange{ | ||
IpCidrRange: data["ip_cidr_range"].(string), | ||
SubnetworkRangeName: data["subnetwork_range_name"].(string), | ||
}) | ||
} | ||
return ipRanges | ||
} | ||
|
||
func flattenGuestAccelerators(zone string, accelerators []*computeBeta.AcceleratorConfig) []map[string]interface{} { | ||
acceleratorsSchema := make([]map[string]interface{}, 0, len(accelerators)) | ||
for _, accelerator := range accelerators { | ||
|
@@ -1605,6 +1644,17 @@ func flattenBetaScheduling(scheduling *computeBeta.Scheduling) []map[string]inte | |
return result | ||
} | ||
|
||
func flattenAliasIpRange(ranges []*computeBeta.AliasIpRange) []map[string]interface{} { | ||
rangesSchema := make([]map[string]interface{}, 0, len(ranges)) | ||
for _, ipRange := range ranges { | ||
rangesSchema = append(rangesSchema, map[string]interface{}{ | ||
"ip_cidr_range": ipRange.IpCidrRange, | ||
"subnetwork_range_name": ipRange.SubnetworkRangeName, | ||
}) | ||
} | ||
return rangesSchema | ||
} | ||
|
||
func getProjectFromSubnetworkLink(subnetwork string) string { | ||
r := regexp.MustCompile(SubnetworkLinkRegex) | ||
if !r.MatchString(subnetwork) { | ||
|
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 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
network_interface
properties.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
schema.ResourceData
struct)Also, can we document this syntax in the
inUseBy
method?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.
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.
inUseBy
is only use for api versions.You can see the
*
as globbing on the indices. Usually, you would havefield.5.nested_field
.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 comment
The 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
schema.ResourceData
struct usingGetOk
and other syntax to see if a given feature is in use, rather than using the serialize string data.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.
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
*
on a field name instead of an indice. This way, we prevent somebody from misusing it without knowing they are misusing it.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 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
inUseBy
method is an anti-pattern. Right now it depends on the serialized state and not the providedschema.ResourceData
which provides a well-defined set of methods for dealing with the schema. My understanding is thatinUseBy
is used because it's convenient, but if we ever depend on values like optionals or validation then we'll regret using it here.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 comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused -
inUseBy
uses the methods fromschema.ResourceData
;TerraformResourceData
is just an interface for the two methodsinUseBy
needs, since we don't need the rest of the behaviour of the class and are able to unit test this code w/ a mock of that interface.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.
OK, after looking at the code, you were right and I was confused; it IS operating on
schema.ResourceData
; I had no idea you could directly access attributes likefoobar.#
to get the size of a list for example using the Get or GetOk method. For some reason I assumed it was using something similar to the the attributes map that we constantly assert on in unit tests.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.
Documentation added.