Skip to content
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

Merged
merged 9 commits into from
Apr 13, 2017

Conversation

mathieuherbert
Copy link
Contributor

Hi,

This PR adds google_compute_subnetwork and google_compute_network.

Thanks for your review :)

TF_ACC=true go test -v github.com/hashicorp/terraform/builtin/providers/google -run ^TestAccDataSourceGoogleNetwork$
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/google	0.016s

TF_ACC=true go test -v github.com/hashicorp/terraform/builtin/providers/google -run TestAccDataSourceGoogleSubnetwork
=== RUN   TestAccDataSourceGoogleSubnetwork
--- PASS: TestAccDataSourceGoogleSubnetwork (86.76s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/google	86.777s

Copy link
Contributor

@danawillow danawillow left a 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": {
Copy link
Contributor

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"]
Copy link
Contributor

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{
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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"]
Copy link
Contributor

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"
Copy link
Contributor

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.
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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:"

@mathieuherbert
Copy link
Contributor Author

Hi @danawillow,
Thank you for the review, in the last 2 commits I make changes thanks to your comments.

Copy link
Contributor

@danawillow danawillow left a 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 {
Copy link
Contributor

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")
Copy link
Contributor

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{
Copy link
Contributor

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"] {
Copy link
Contributor

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
Copy link
Contributor

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")
Copy link
Contributor

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]
Copy link
Contributor

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
Copy link
Contributor

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..
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grammar nitpick: "which belong"

@mathieuherbert
Copy link
Contributor Author

@danawillow thank you for the review.
Your comments are very interesting for me !
About tests, I create an array with every fields that I want to tests and iterate over it.
Also I remove ip_v4_range field.

Copy link
Contributor

@danawillow danawillow left a 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" {
Copy link
Contributor

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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

@mathieuherbert
Copy link
Contributor Author

@danawillow I did a rebase which fix the conflict
Also I fix the change requested on documentation

@danawillow
Copy link
Contributor

Thanks @mathieuherbert! This looks good to me; I'll merge it now.

@danawillow danawillow merged commit 25cbbde into hashicorp:master Apr 13, 2017
@mathieuherbert mathieuherbert deleted the datasource-network-gce branch April 13, 2017 20:32
@ghost
Copy link

ghost commented Apr 14, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants