-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add ceritificateManagerCertificates field to ComputeTargetHttpsProxy resource #9144
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
Merged
roaks3
merged 6 commits into
GoogleCloudPlatform:main
from
Hamzawy63:compute-target-https-proxy-support-certificate-manager-certs-new-field-encoder-decoder-implementation
Oct 9, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91c3142
Add ceritificateManagerCertificates field and handle the creation pro…
9116f38
Add custom_expand function that constructs the full URL if the provid…
8247a3d
update the documetation of certificateManagerCertificates field
0d7cd33
Update the example
9e2a808
Fix inconsistent spaces in the resource example
251bfcd
Remove unnecessary ruby template in the custom_expand function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...mplates/terraform/custom_expand/certificate_manager_certificate_construct_full_url.go.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<%# The license inside this block applies to this file. | ||
# Copyright 2017 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 tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
Hamzawy63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if v == nil { | ||
return nil, nil | ||
} | ||
l := v.([]interface{}) | ||
req := make([]interface{}, 0, len(l)) | ||
for _, raw := range l { | ||
if raw == nil { | ||
return nil, fmt.Errorf("Invalid value for <%= property.name.underscore -%>: nil") | ||
} | ||
if strings.HasPrefix(raw.(string), "//") || strings.HasPrefix(raw.(string), "https://") { | ||
// Any full URL will be passed to the API request (regardless of the resource type). This is to allow self_links of CertificateManagerCeritificate resources. | ||
// If the full URL is an invalid reference, that should be handled by the API. | ||
req = append(req, raw.(string)) | ||
} else if reg,_ := regexp.Compile("projects/(.*)/locations/(.*)/certificates/(.*)") ; reg.MatchString(raw.(string)) { | ||
// If the input is the id pattern of CertificateManagerCertificate resource, a prefix will be added to construct the full URL before constructing the API request. | ||
self_link := "https://certificatemanager.googleapis.com/v1/" + raw.(string) | ||
req = append(req, self_link) | ||
} else { | ||
return nil, fmt.Errorf("Invalid value for <%= property.name.underscore -%>: %v is an invalid format for a certificateManagerCertificate resource", raw.(string)) | ||
} | ||
} | ||
return req, nil | ||
} |
15 changes: 15 additions & 0 deletions
15
mmv1/templates/terraform/decoders/compute_target_https_proxy.go.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Since both sslCertificates and certificateManagerCertificates maps to the same API field (sslCertificates), we need to check the types | ||
// of certificates that exist in the array and decide whether to change the field to certificateManagerCertificate or not. | ||
// The decoder logic depends on the fact that the API does not allow mixed type of certificates and it returns | ||
// certificate manager certificates in the format of //certificatemanager.googleapis.com/projects/*/locations/*/certificates/* | ||
if sslCertificates, ok := res["sslCertificates"].([]interface{}); ok && len(sslCertificates) > 0 { | ||
regPat, _ := regexp.Compile("//certificatemanager.googleapis.com/projects/(.*)/locations/(.*)/certificates/(.*)") | ||
|
||
if regPat.MatchString(sslCertificates[0].(string)) { | ||
// It is enough to check only the type of one of the provided certificates beacuse all the certificates should be the same type. | ||
log.Printf("[DEBUG] The field sslCertificates contains certificateManagerCertificates, the field name will be converted to certificateManagerCertificates") | ||
res["certificateManagerCertificates"] = res["sslCertificates"] | ||
delete(res, "sslCertificates") | ||
} | ||
} | ||
return res, nil |
10 changes: 10 additions & 0 deletions
10
mmv1/templates/terraform/encoders/compute_target_https_proxy.go.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
if _, ok := obj["certificateManagerCertificates"]; ok { | ||
// The field certificateManagerCertificates should not be included in the API request, and it should be renamed to `sslCertificates` | ||
// The API does not allow using both certificate manager certificates and sslCertificates. If that changes | ||
// in the future, the encoder logic should change accordingly because this will mean that both fields are no longer mutual exclusive. | ||
log.Printf("[DEBUG] converting the field CertificateManagerCertificates to sslCertificates before sending the request") | ||
obj["sslCertificates"] = obj["certificateManagerCertificates"] | ||
delete(obj, "certificateManagerCertificates") | ||
} | ||
return obj, nil |
45 changes: 45 additions & 0 deletions
45
mmv1/templates/terraform/examples/target_https_proxy_certificate_manager_certificate.tf.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
resource "google_compute_target_https_proxy" "<%= ctx[:primary_resource_id] %>" { | ||
name = "<%= ctx[:vars]['target_https_proxy_name'] %>" | ||
url_map = google_compute_url_map.default.id | ||
certificate_manager_certificates = ["//certificatemanager.googleapis.com/${google_certificate_manager_certificate.default.id}"] # [google_certificate_manager_certificate.default.id] is also acceptable | ||
Hamzawy63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
resource "google_certificate_manager_certificate" "default" { | ||
name = "<%= ctx[:vars]['certificate_manager_certificate_name'] %>" | ||
scope = "ALL_REGIONS" | ||
self_managed { | ||
pem_certificate = file("test-fixtures/cert.pem") | ||
pem_private_key = file("test-fixtures/private-key.pem") | ||
} | ||
} | ||
|
||
resource "google_compute_url_map" "default" { | ||
name = "<%= ctx[:vars]['url_map_name'] %>" | ||
description = "a description" | ||
|
||
default_service = google_compute_backend_service.default.id | ||
|
||
host_rule { | ||
hosts = ["mysite.com"] | ||
path_matcher = "allpaths" | ||
} | ||
|
||
path_matcher { | ||
name = "allpaths" | ||
default_service = google_compute_backend_service.default.id | ||
|
||
path_rule { | ||
paths = ["/*"] | ||
service = google_compute_backend_service.default.id | ||
} | ||
} | ||
} | ||
|
||
resource "google_compute_backend_service" "default" { | ||
name = "<%= ctx[:vars]['backend_service_name'] %>" | ||
port_name = "http" | ||
protocol = "HTTP" | ||
timeout_sec = 10 | ||
load_balancing_scheme = "INTERNAL_MANAGED" | ||
} |
19 changes: 19 additions & 0 deletions
19
mmv1/third_party/terraform/services/compute/test-fixtures/cert.pem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDDzCCAfegAwIBAgIUDOiCLH9QNMMYnjPZVf4VwO9blsEwDQYJKoZIhvcNAQEL | ||
BQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wIBcNMjIwODI0MDg0MDUxWhgPMzAy | ||
MTEyMjUwODQwNTFaMBYxFDASBgNVBAMMC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG | ||
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvOT925GG4lKV9HvAHsbecMhGPAqjhVRC26iZ | ||
UJC8oSWOu95lWJSX5ZhbiF6Nz192wDGV/VAh3Lxj8RYtcn75eDxQKTcKouDld+To | ||
CGIStPFWbR6rbysLuZqFVEXVOTvp2QIegInfrvnGC4j7Qpic7zrFB9HzJx+0HpeE | ||
yO4gkdzJfEK/gMmolUgJrKX59o+0+Rj+Jq3EtcQxL1fVBVJSx0NvpoR1eYpnHMr/ | ||
rJKZkUUZ2xE86hrtpiP6OEYQTi00rmf4GnZF5QfGGD0xuoQXtR7Tu+XhKibXIhxc | ||
D4RzPLX1QS040PXvmMPLDb4YlUQ6V3Rs42JDvkkDwIMXZvn8awIDAQABo1MwUTAd | ||
BgNVHQ4EFgQURuo1CCZZAUv7xi02f2nC5tRbf18wHwYDVR0jBBgwFoAURuo1CCZZ | ||
AUv7xi02f2nC5tRbf18wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC | ||
AQEAqx3tDxurnYr9EUPhF5/LlDPYM+VI7EgrKdRnuIqUlZI0tm3vOGME0te6dBTC | ||
YLNaHLW3m/4Tm4M2eg0Kpz6CxJfn3109G31dCi0xwzSDHf5TPUWvqIVhq5WRgMIf | ||
n8KYBlQSmqdJBRztUIQH/UPFnSbxymlS4s5qwDgTH5ag9EEBcnWsQ2LZjKi0eqve | ||
MaqAvvB+j8RGZzYY4re94bSJI42zIZ6nMWPtXwRuDc30xl/u+E0jWIgWbPwSd6Km | ||
3wnJnGiU2ezPGq3zEU+Rc39VVIFKQpciNeYuF3neHPJvYOf58qW2Z8s0VH0MR1x3 | ||
3DoO/e30FIr9j+PRD+s5BPKF2A== | ||
-----END CERTIFICATE----- |
28 changes: 28 additions & 0 deletions
28
mmv1/third_party/terraform/services/compute/test-fixtures/private-key.pem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC85P3bkYbiUpX0 | ||
e8Aext5wyEY8CqOFVELbqJlQkLyhJY673mVYlJflmFuIXo3PX3bAMZX9UCHcvGPx | ||
Fi1yfvl4PFApNwqi4OV35OgIYhK08VZtHqtvKwu5moVURdU5O+nZAh6Aid+u+cYL | ||
iPtCmJzvOsUH0fMnH7Qel4TI7iCR3Ml8Qr+AyaiVSAmspfn2j7T5GP4mrcS1xDEv | ||
V9UFUlLHQ2+mhHV5imccyv+skpmRRRnbETzqGu2mI/o4RhBOLTSuZ/gadkXlB8YY | ||
PTG6hBe1HtO75eEqJtciHFwPhHM8tfVBLTjQ9e+Yw8sNvhiVRDpXdGzjYkO+SQPA | ||
gxdm+fxrAgMBAAECggEAV4/A24TQpV4KFBw/WSTvnRFBeXinB1mhamhztWR6hCrA | ||
SPcVPKQY632eRI8sJmpGxl3V/Ogl4khT/cA9jfstEl7G++v/WrRsupCaPLSVnlnX | ||
KdsTNgOauk1WK9P5PMA4rPcuA4Cl91riQpubeWn8KWsxRWg90i+Ak8PB8lBsOaB1 | ||
QzjigWlrRWSpodaw0MBIMZFDL2BYK8HEr+wyATYIyGvDQc9zCnMQIQIZyEPYepLO | ||
04Dw17YcjgnoJ5gLAFiTvDrCpTMewud1RQzvW5TAvG2piw34sf3QMGPM7aXNrfuZ | ||
4ZPC/MwVQgq9Nc+jeDsjApQmJKJ+3a8OdIPU89ArTQKBgQDCpHHQe1RzpHmIx47/ | ||
9N5r+NPBhh8flDYmvgi6zPeBfrAaLWhidS8c7Voa6HwvMxbhryDEvc0YqI3vllfy | ||
xnRF+DfSryozW0gjrkXDGoOzqOJ3EuQwLSJnyX6La2lmufqsRFazwYJ5sxcjoGHK | ||
/sbwZkIUj1ejuH44ve+ZJQFfpwKBgQD4cLJrJhqImUDhHZRx9jBvxyeHy/RjmHK6 | ||
70xQVDi9ZqeExHwtoSbolhXKLB1RtBnw+t5Csy7IDNBDsbUg9fXU8KyCTIdmsyws | ||
bDb5hdKsUF76rkKzlpttiXMRVWGS3CMKWahBpnL3lFB3tdtmskemkBTXVn4VgKAH | ||
xk9XnZ11nQKBgDbQSJ0FnkrSzscOK984/ko50Kh3NNyXyIgwjBTPFASLwNweXX8c | ||
sR/cV7usLQy9vnvf7cJ6EQAYt5/5Httnt+bceBwE6EV+N1qVAWBoXx6BOQV/dHN8 | ||
wmun+tMYdJ5RUZ6hwCjvHedX3/RQfjnEdhHNOl6/31Zj5mfkVU0zdqeRAoGAcvIh | ||
erXMfPr7K6y16+xOCMmKHqhc0F/OZXMmSdxNzEPcqe8GzU3MZLxcJIg4oH7FqdtI | ||
Tm/86w4Spd9owHFMZlNcXYTu+LNZcsw2u0gRayxcZXuO3OyHySxZEuIAHSTBCZ7l | ||
3EoY0zfJ6zk249MEl6n+GouoFmbGpBI6z3zbR3kCgYEAlCNZVH4uJrP5beTOZTTR | ||
VJRk7BXvEC6HsM140YtIN7NHy2GtzrgmmY/ZAFB/hX8Ft4ex2MxbIp3hvxroTqGn | ||
bfu7uv97NoPQqbjtc3Mz8h2IaXTVDUnWYY5gDu6rM2w+Z75/sWIGiTWrsdYX4ohb | ||
ujngzJ7Ew7GgKSboj6mtlVM= | ||
-----END PRIVATE KEY----- |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this necessary? I'm not sure I understand how it is being used
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.
Do you mean the
diff_suppress_func
or something else?The API expects each item to be in full URL format (e.g.
//certificatemanager.googleapis.com/projects/../locations/...
.However, I allowed the users to only provide the
Id
, so customers can just provide each reference on the format ofprojects/../locations/../certficates/..
, and I construct the full URL before sending the API request using the custom_expand function.So, we end up having the following:
projects/../locations/../certificates/..
//certificatemanager.googleapis.com/projects/../locations/.../..certificates/..
When I run the test, TF triggers an update request(because the field value has changed) and that causes the test to fail.
Is there a better way to handle this situation?
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.
One more question, Are there any issues with allowing the user to provide the
id
and then convert it to a full URL before sending the request?According to rileykarson's comment here #8941 (comment), it's not encouraged to let the user enter the name of the resource unless the API allows doing this, I wonder if it's also the same with letting the user provide an
id
if the API is expecting a full URL format.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.
Yes, I'm talking about the
diff_suppress_func
. I understand that you are allowing the user to provide an id (which I believe is fine here), and that needs to be converted before being sent to the API, but isn't this all being handled by the expand/encoder/decoder?I may just be missing something, but I'm wondering what caused the test failure. And for context, I'm partly asking about the diff suppress because I believe the implementation doesn't consider all parts of the id (like location), and that may cause problems.
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.
1- The encoder and the decoder parts are responsible for merging or splitting the fields
sslCertificates
andcertificate_manager_certificates
before constructing the API request and after receiving the response respectively. They have nothing to do with the format of the input provided by the user.2- The custom expand is responsible for making sure that any certificate manager certificate resource is sent in the API request in the format of
//certificatemanager.googleapis.com/projects/../locations/.../..certificates/..
. So, if the user uses theid
to reference the certificate, the custom expand will convert it before sending the API request.The response that we receive from the API will contain the certificates in the full URL format (
//certificatemanager.googleapis.com/projects/../locations/.../..certificates/..
). Hence if the user provides only theid
, there will be a difference between the input provided by the user and the response from the API.Error Message:
id
to reference a certificate resourceThere 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.
Got it, that all makes sense, thanks for clarifying. Despite my concern with the other parts of the id diffing (like location and project), it appears this same function is used in many other fields with similar structure, so seems good to me.