Skip to content

Commit

Permalink
Allow google_container_cluster.id reference for the value for the g…
Browse files Browse the repository at this point in the history
…ke_cluster of a `google_gke_hub_membership` (GoogleCloudPlatform#4985)

Co-authored-by: upodroid <[email protected]>
  • Loading branch information
upodroid authored and khajduczenia committed Oct 12, 2021
1 parent 026bb08 commit 4425178
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mmv1/products/gkehub/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ objects:
Self-link of the GCP resource for the GKE cluster.
For example: `//container.googleapis.com/projects/my-project/zones/us-west1-a/clusters/my-cluster`.
It can be at the most 1000 characters in length. If the cluster is provisioned with Terraform,
this is `"//container.googleapis.com/${google_container_cluster.my-cluster.id}"`.
this can be `"//container.googleapis.com/${google_container_cluster.my-cluster.id}"` or
`google_container_cluster.my-cluster.id`.
input: true
required: true
- !ruby/object:Api::Type::NestedObject
Expand Down
6 changes: 6 additions & 0 deletions mmv1/products/gkehub/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ overrides: !ruby/object:Overrides::ResourceOverrides
skip_sweeper: true
id_format: "{{name}}"
import_format: ["{{%name}}"]
properties:
endpoint.gkeCluster.resourceLink: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: suppressGkeHubEndpointSelfLinkDiff
custom_expand: 'templates/terraform/custom_expand/gke_hub_membership.erb'
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/gke_hub_membership_diff.go
# This is for copying files over
files: !ruby/object:Provider::Config::Files
# These files have templating (ERB) code that will be run.
Expand Down
9 changes: 9 additions & 0 deletions mmv1/templates/terraform/constants/gke_hub_membership_diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func suppressGkeHubEndpointSelfLinkDiff(_, old, new string, _ *schema.ResourceData) bool {
// The custom expander injects //container.googleapis.com/ if a selflink is supplied.
selfLink := strings.TrimPrefix(old, "//container.googleapis.com/")
if selfLink == new {
return true
}

return false
}
22 changes: 22 additions & 0 deletions mmv1/templates/terraform/custom_expand/gke_hub_membership.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%# The license inside this block applies to this file.
# Copyright 2021 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
if strings.HasPrefix(v.(string), "//container.googleapis.com/") {
return v, nil
} else {
v = "//container.googleapis.com/" + v.(string)
return v, nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resource "google_gke_hub_membership" "membership" {
membership_id = "<%= ctx[:vars]['name'] %>"
endpoint {
gke_cluster {
resource_link = "//container.googleapis.com/${google_container_cluster.primary.id}"
resource_link = google_container_cluster.primary.id
}
}
authority {
Expand Down

0 comments on commit 4425178

Please sign in to comment.