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

compute_ssl_certificate: support write-only fields certificate_wo + private_key_wo #13552

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ func (r Resource) InIdFormat(prop Type) bool {
// Functions used to create slices of resource properties that could not otherwise be called from within generating templates.
func (r Resource) ReadProperties() []*Type {
return google.Reject(r.GettableProperties(), func(p *Type) bool {
return p.IgnoreRead
return p.IgnoreRead || p.WriteOnly
})
}

Expand Down
45 changes: 42 additions & 3 deletions mmv1/products/compute/SslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ async:
collection_url_key: 'items'
custom_code:
extra_schema_entry: 'templates/terraform/extra_schema_entry/ssl_certificate.tmpl'
encoder: 'templates/terraform/encoders/ssl_certificate.go.tmpl'
examples:
- name: 'ssl_certificate_basic'
primary_resource_id: 'default'
ignore_read_extra:
- 'name_prefix'
# Uses id.UniqueId
skip_vcr: true
- name: 'ssl_certificate_basic_wo'
primary_resource_id: 'default'
ignore_read_extra:
- 'name_prefix'
# Uses id.UniqueId
skip_vcr: true
- name: 'ssl_certificate_random_provider'
primary_resource_id: 'default'
external_providers: ["random", "time"]
Expand All @@ -80,8 +87,24 @@ properties:
The certificate in PEM format.
The certificate chain must be no greater than 5 certs long.
The chain must include at least one intermediate cert.
required: true
at_least_one_of:
- 'certificate'
- 'certificateWo'
sensitive: true
- name: 'certificateWo'
type: String
description: 'The write-only certificate in PEM format.'
required_with:
- 'certificateWoVersion'
at_least_one_of:
- 'certificate'
- 'certificateWo'
write_only: true
- name: 'certificateWoVersion'
type: String
description: 'The write-only version of the certificate.'
immutable: true
ignore_read: true
- name: 'creationTimestamp'
type: Time
description: 'Creation timestamp in RFC3339 text format.'
Expand Down Expand Up @@ -116,10 +139,26 @@ properties:
function: 'verify.ValidateGCEName'
- name: 'privateKey'
type: String
description: 'The write-only private key in PEM format.'
required: true
description: 'The private key in PEM format.'
at_least_one_of:
- 'privateKey'
- 'privateKeyWo'
immutable: true
ignore_read: true
sensitive: true
diff_suppress_func: 'sha256DiffSuppress'
custom_flatten: 'templates/terraform/custom_flatten/sha256.tmpl'
- name: 'privateKeyWo'
type: String
description: 'The write-only private key in PEM format.'
required_with:
- 'privateKeyWoVersion'
at_least_one_of:
- 'privateKey'
- 'privateKeyWo'
write_only: true
- name: 'privateKeyWoVersion'
type: String
description: 'The write-only version of the private key.'
immutable: true
ignore_read: true
4 changes: 4 additions & 0 deletions mmv1/templates/terraform/encoders/ssl_certificate.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// cannot include activate prop in the body
delete(obj, "certificate_wo_version")
delete(obj, "private_key_wo_version")
return obj, nil
11 changes: 11 additions & 0 deletions mmv1/templates/terraform/examples/ssl_certificate_basic_wo.tf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "google_compute_ssl_certificate" "default" {
name_prefix = "my-certificate-"
description = "a description"
private_key_wo = file("path/to/private.key")
private_key_wo_version = 1
certificate_wo = file("path/to/certificate.crt")
certificate_wo_version = 1
lifecycle {
create_before_destroy = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@ func TestAccComputeSslCertificate_no_name(t *testing.T) {
})
}

func TestAccComputeSslCertificate_update_wo(t *testing.T) {
// Randomness
acctest.SkipIfVcr(t)
t.Parallel()

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeSslCertificateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeSslCertificate_wo(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeSslCertificateExists(
t, "google_compute_ssl_certificate.foobar"),
),
},
{
ResourceName: "google_compute_ssl_certificate.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"private_key"},
},
{
Config: testAccComputeSslCertificate_update_wo(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeSslCertificateExists(
t, "google_compute_ssl_certificate.foobar"),
),
},
{
ResourceName: "google_compute_ssl_certificate.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"private_key"},
},
},
})
}

func TestUnitComputeManagedSslCertificate_AbsoluteDomainSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
Expand Down Expand Up @@ -114,3 +154,27 @@ resource "google_compute_ssl_certificate" "foobar" {
}
`)
}

func testAccComputeSslCertificate_wo() string {
return fmt.Sprintf(`
resource "google_compute_ssl_certificate" "foobar" {
description = "really descriptive"
private_key_wo = file("test-fixtures/test.key")
private_key_wo_version = 1
certificate_wo = file("test-fixtures/test.crt")
certificate_wo_version = 1
}
`)
}

func testAccComputeSslCertificate_update_wo() string {
return fmt.Sprintf(`
resource "google_compute_ssl_certificate" "foobar" {
description = "really descriptive"
private_key_wo = file("test-fixtures/test.key")
private_key_wo_version = 2
certificate_wo = file("test-fixtures/test.crt")
certificate_wo_version = 2
}
`)
}
Loading