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

New datasource: google_container_cluster #740

Merged
merged 4 commits into from
Dec 20, 2017
Merged

New datasource: google_container_cluster #740

merged 4 commits into from
Dec 20, 2017

Conversation

sl1pm4t
Copy link
Contributor

@sl1pm4t sl1pm4t commented Nov 15, 2017

Adds the google_kubernetes_cluster datasource.

This is useful for retrieving the cluster credentials and feeding into the Terraform Kubernetes provider, but has other use cases (e.g. feed cluster CIDR range into firewall/VPC route resources).

I've named the datasource google_kubernetes_cluster rather than google_container_cluster to match the new Google product name, but this means the datasource naming is not consistent with the equivalent resource. Let me know if this is acceptable.

Fixes #732

@sl1pm4t
Copy link
Contributor Author

sl1pm4t commented Nov 20, 2017

Hi @danawillow - any chance we could get this reviewed before it gets stale? Thanks!

@danawillow
Copy link
Contributor

Hey @sl1pm4t, thanks for the PR! I've been drowning in code reviews lately but I'll try to get to this before the Thanksgiving break (and of course don't let that stop you from continuing to contribute!)

Right now I think keeping the data source name consistent with the resource name is probably the way to go though.

@danawillow danawillow self-requested a review November 20, 2017 19:19
@danawillow danawillow self-assigned this Nov 20, 2017
@sl1pm4t
Copy link
Contributor Author

sl1pm4t commented Nov 20, 2017

No worries @danawillow, I felt bad asking because I know you're swamped. I really appreciate your massive effort!

@sl1pm4t sl1pm4t changed the title New datasource: google_kubernetes_cluster New datasource: google_container_cluster Nov 21, 2017
@sl1pm4t
Copy link
Contributor Author

sl1pm4t commented Nov 21, 2017

Hi @danawillow - I've renamed the datasource and fixed a couple other issues I spotted in the tests, and missing reference to the docs page in google.erb.
Thanks!


"cluster_ipv4_cidr": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Except for name, zone, and project, we probably shouldn't allow setting fields since they don't get used to make the request

"master_auth": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise, I don't think ForceNew really has meaning for data sources

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Will fix this.

}
}

func datasourceContainerClusterRead(d *schema.ResourceData, meta interface{}) error {
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 curious why you decided to have only a subset of the resource schema fields in the data source schema. If you had the same set of fields, then you would be able to reuse the read function (https://github.com/terraform-providers/terraform-provider-google/blob/master/google/data_source_google_compute_instance_group.go is a good example of this). If there is a reason why certain schema elements are excluded, I'd love to see a comment explaining why.

Copy link
Contributor Author

@sl1pm4t sl1pm4t Nov 22, 2017

Choose a reason for hiding this comment

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

Great idea, I hadn't considered using the resources Read func, but that makes a lot of sense.
I stripped down the schema to a subset that I thought would be useful in a datasource, based on the notion if it wasn't going to be useful, remove it so there's less to maintain in the read func. Now that you've pointed out we can re-use the read func from the resource it makes sense to keep the whole schema.

@sl1pm4t
Copy link
Contributor Author

sl1pm4t commented Dec 11, 2017

Hi @danawillow - sorry for the delay fixing this up.

As per your suggestion, the datasource now uses the existing google_container_cluster resource read func rather than being re-implemented for the datasource.
It also implements the full resource schema rather than just a subset. This is done by generating the datasource schema directly from the resource schema (see datasourceSchemaFromResourceSchema in google/utils.go) so future maintenance should be minimal if the attributes are added / modified / deleted on the resource.

Thanks!

Add documentation for google_kubernetes_cluster datasource

Rename datasource to google_container_cluster

To be consistent with the equivalent resource.

Rename datasource in docs.
google_kubernetes_cluster -> google_container_cluster.
Also add reference in google.erb file.

WIP

Datasource read needs to set an ID, then call resource read func

Add additional cluster attributes to datasource schema
Datasource documentation also updated.
@sl1pm4t
Copy link
Contributor Author

sl1pm4t commented Dec 19, 2017

I rebased on master branch to resolve conflicts.

}
}

func dummyCustomSetFunc(v interface{}) int {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this is unused

google/utils.go Outdated
fallthrough
case schema.TypeList:
// List & Set types are generally used for 2 cases:
// - a list/set of simple primative values (e.g. list of strings)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: s/primative/primitive

google/utils.go Outdated
Schema: datasourceSchemaFromResourceSchema(elem.Schema),
}
} else {
// handle simple primative case
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 :)

google/utils.go Outdated
// example) and therefore the attribute flags were not set appropriately when
// first added to the schema definition. Currently only supports top-level
// schema elements.
func fixDatasourceSchemaFlags(schema map[string]*schema.Schema, required bool, keys ...string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional idea: it might be a bit more readable in the resources themselves to have two additional fns: addRequiredFieldsToSchema and addOptionalFieldsToSchema (or something along those lines), that just call into this one with the appropriate bool value.


# google\_container\_cluster

Get info about a cluster within GCE from its name and zone.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/GCE/GKE

google/utils.go Outdated
// - all attributes have Computed = true
// - all attributes have ForceNew, Required = false
// - Validation funcs and attributes (e.g. MaxItems) are not copied
func datasourceSchemaFromResourceSchema(rs map[string]*schema.Schema) map[string]*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.

Great idea, thanks so much for adding this! For a bit more visibility within the provider, it might make more sense to move these helper fns into their own file (datasource_helpers.go or something like that)

@sl1pm4t
Copy link
Contributor Author

sl1pm4t commented Dec 20, 2017

@danawillow - thanks for the review. The requested changes have been implemented. 👨‍💻

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.

Looks great, thanks so much @sl1pm4t!

@danawillow danawillow merged commit 23ef50f into hashicorp:master Dec 20, 2017
@sl1pm4t sl1pm4t deleted the gke-cluster-datasource branch December 21, 2017 02:56
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
* Add google_kubernetes_cluster datasource

Add documentation for google_kubernetes_cluster datasource

Rename datasource to google_container_cluster

To be consistent with the equivalent resource.

Rename datasource in docs.
google_kubernetes_cluster -> google_container_cluster.
Also add reference in google.erb file.

WIP

Datasource read needs to set an ID, then call resource read func

Add additional cluster attributes to datasource schema

* Generate datasource schema from resource

Datasource documentation also updated.

* add test for datasourceSchemaFromResourceSchema

* Code review changes
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
@ghost
Copy link

ghost commented Mar 30, 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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

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

Successfully merging this pull request may close these issues.

GKE cluster data source
2 participants