-
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/google: datasource subnetwork and network #12442
provider/google: datasource subnetwork and network #12442
Conversation
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.
Thanks @mathieuherbert! This looks really good so far; most of my change requests are minor. I didn't get a chance to read all the way through the end of the documentation so I'll be sure to get to that next week.
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"subnetworks_self_links": { |
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.
nit: mind adding a newline right before here for consistency?
return fmt.Errorf("root module has no resource called %s", name) | ||
} | ||
|
||
networkOrigin, ok := s.RootModule().Resources["google_compute_network.foobar"] |
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.
A few ideas to make this function more reusable (in case more tests come for this down the road):
- google_compute_network.foobar could be a parameter
- All attributes could be compared resource vs data source (right now id and self_link are doing that, but name and description are using hardcoded values)
Sound reasonable?
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"network_self_link": &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.
Hmm, so there's an equivalent field in the subnetwork resource which is just called network
. I like that your name is more descriptive, but for consistency's sake it may be better to just go with that.
}) | ||
} | ||
|
||
func testAccDataSourceGoogleNetworktCheck(name string) resource.TestCheckFunc { |
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.
Does the "t" after Network stand for anything or is that a typo?
}) | ||
} | ||
|
||
func testAccDataSourceGoogleSubnetworktCheck(name string) resource.TestCheckFunc { |
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 comment about the "t"
if !ok { | ||
return fmt.Errorf("root module has no resource called %s", name) | ||
} | ||
network, ok := s.RootModule().Resources["google_compute_network.foobar"] |
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 comments here as the other test
--- | ||
layout: "google" | ||
page_title: "Google: google_compute_network" | ||
sidebar_current: "docs-google-datasource-compute-etwork" |
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.
typo: network
|
||
# google\_compute\_network | ||
|
||
Get a network within GCE from his 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.
his -> its
|
||
## Example Usage | ||
|
||
```js |
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.
You can use tf for the language now! (I think this is a relatively new development)
|
||
## Attributes Reference | ||
|
||
In addition to the arguments listed above, the following computed attributes are |
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 other gcp data sources docs seem to be using this phrasing: "The following attributes are exported:"
Hi @danawillow, |
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 all the comments! Just trying to make sure the code that's checked in is the highest possible quality :)
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleNetworktCheck(name string) resource.TestCheckFunc { | ||
func testAccDataSourceGoogleNetworkCheck(name string, network_name string) resource.TestCheckFunc { |
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'd love it if this test (and the subnetwork one) had clearer names for things. For example, "name" could be the name of anything, so how about something like data_source_name
. And then network_name
could be resource_name
.
From there, attr
could be renamed to ds_attr
and then you could have a similar variable called resource_attr
. Then it would be super clear that at the end, you're comparing what was set in the data source to what was set in the resource. Does that make sense?
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("root module has no resource called %s", name) | ||
} | ||
|
||
networkOrigin, ok := s.RootModule().Resources["google_compute_network.foobar"] | ||
networkOrigin, ok := s.RootModule().Resources[network_name] | ||
if !ok { | ||
return fmt.Errorf("can't find google_compute_network.foobar in state") |
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.
replace google_compute_network.foobar
with the variable name containing it
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"project": &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.
formatting nitpick: can you add a newline before "project"?
|
||
attr := rs.Primary.Attributes | ||
|
||
if attr["id"] != networkOrigin.Primary.Attributes["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.
Since all the checks are approximately the same, you could add a function that just does one check and returns the error. So it would take in the attr name, the data source attr, and the resource attr and return nil or an error. https://github.com/hashicorp/terraform/blob/master/builtin/providers/google/resource_container_cluster_test.go#L181 has an example of this, though it's a fair bit more complicated than what you'll want here. Let me know if you want any help constructing this.
d.Set("gateway_address", subnetwork.GatewayAddress) | ||
d.Set("network", subnetwork.Network) | ||
|
||
//To put subnet id see resource compute subnetnetwork details |
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 not sure what this comment means. Are you trying to say that the creation of the subnet id occurs in resource_compute_subnetwork
? If so, maybe phrasing like subnet id creation is defined in resource_compute_subnetwork.go
would be more clear.
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { | ||
// The resource doesn't exist anymore | ||
|
||
return fmt.Errorf("Network Not 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.
Maybe put the network name in the error message
if !ok { | ||
return fmt.Errorf("root module has no resource called %s", name) | ||
} | ||
network, ok := s.RootModule().Resources[subnetwork_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.
Shouldn't this be network_name?
|
||
* `description` - Description of this network. | ||
|
||
* `ip_v4_range` - (DEPRECATED) The IPv4 address range that machines in this network |
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 this is deprecated do you actually need to be exporting it?
(also the field name you used in the data source was ipv4_range)
* `description` - Description of this network. | ||
|
||
* `ip_v4_range` - (DEPRECATED) The IPv4 address range that machines in this network | ||
are assigned to, represented as a CIDR block.. |
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.
extra period
|
||
* `gateway_ipv4` - The IP address of the gateway. | ||
|
||
* `subnetworks_self_links` - the list of subnetworks which belongs to the network |
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.
grammar nitpick: "which belong"
@danawillow thank you for the review. |
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.
Almost there! Also it looks like some merge conflicts snuck in recently.
## Example Usage | ||
|
||
```tf | ||
datasource "google_compute_network" "my-network" { |
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.
datasource -> data
## Example Usage | ||
|
||
```tf | ||
datasource "google_compute_subnetwork" "my-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.
And here
5eec194
to
7d81973
Compare
@danawillow I did a rebase which fix the conflict |
Thanks @mathieuherbert! This looks good to me; I'll merge it now. |
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,
This PR adds
google_compute_subnetwork
andgoogle_compute_network
.Thanks for your review :)