Skip to content

Commit 2a9c7e6

Browse files
authored
Add support for regional secret version resource google_secret_manager_regional_secret_version (#11699)
1 parent 8420234 commit 2a9c7e6

17 files changed

+676
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Copyright 2024 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
--- !ruby/object:Api::Resource
15+
name: RegionalSecretVersion
16+
base_url: '{{name}}'
17+
self_link: '{{name}}'
18+
create_url: '{{secret}}:addVersion'
19+
delete_url: '{{name}}:destroy'
20+
delete_verb: :POST
21+
description: |
22+
A regional secret version resource.
23+
# Sweeper skipped as this resource has customized deletion.
24+
skip_sweeper: true
25+
import_format:
26+
['projects/{{%project}}/locations/{{%location}}/secrets/{{%secret_id}}/versions/{{%version}}']
27+
examples:
28+
- !ruby/object:Provider::Terraform::Examples
29+
name: 'regional_secret_version_basic'
30+
primary_resource_id: 'regional_secret_version_basic'
31+
vars:
32+
secret_id: 'secret-version'
33+
data: 'secret-data'
34+
- !ruby/object:Provider::Terraform::Examples
35+
name: 'regional_secret_version_with_base64_data'
36+
primary_resource_id: 'regional_secret_version_base64'
37+
vars:
38+
secret_id: 'secret-version'
39+
data: 'secret-data.pfx'
40+
test_vars_overrides:
41+
data: '"./test-fixtures/binary-file.pfx"'
42+
ignore_read_extra:
43+
- 'is_secret_data_base64'
44+
- !ruby/object:Provider::Terraform::Examples
45+
name: 'regional_secret_version_disabled'
46+
primary_resource_id: 'regional_secret_version_disabled'
47+
vars:
48+
secret_id: 'secret-version'
49+
data: 'secret-data'
50+
- !ruby/object:Provider::Terraform::Examples
51+
name: 'regional_secret_version_deletion_policy_abandon'
52+
primary_resource_id: 'regional_secret_version_deletion_policy'
53+
vars:
54+
secret_id: 'secret-version'
55+
data: 'secret-data'
56+
ignore_read_extra:
57+
- 'deletion_policy'
58+
- !ruby/object:Provider::Terraform::Examples
59+
name: 'regional_secret_version_deletion_policy_disable'
60+
primary_resource_id: 'regional_secret_version_deletion_policy'
61+
vars:
62+
secret_id: 'secret-version'
63+
data: 'secret-data'
64+
ignore_read_extra:
65+
- 'deletion_policy'
66+
custom_code: !ruby/object:Provider::Terraform::CustomCode
67+
post_create: templates/terraform/post_create/regional_secret_version.go.erb
68+
decoder: templates/terraform/decoders/treat_destroyed_state_as_gone.erb
69+
pre_delete: templates/terraform/pre_delete/regional_secret_version_deletion_policy.go.erb
70+
pre_read: templates/terraform/pre_read/secret_manager_regional_secret_version.go.erb
71+
pre_create: templates/terraform/pre_create/secret_manager_regional_secret_version.go.erb
72+
extra_schema_entry: templates/terraform/extra_schema_entry/secret_version_is_secret_data_base64.go.erb
73+
custom_update: templates/terraform/custom_update/regional_secret_version.go.erb
74+
custom_import: templates/terraform/custom_import/regional_secret_version.go.erb
75+
docs: !ruby/object:Provider::Terraform::Docs
76+
optional_properties: |
77+
* `is_secret_data_base64` - (Optional) If set to 'true', the secret data is expected to be base64-encoded string and would be sent as is.
78+
virtual_fields:
79+
- !ruby/object:Api::Type::String
80+
name: deletion_policy
81+
description: |
82+
The deletion policy for the regional secret version. Setting `ABANDON` allows the resource
83+
to be abandoned rather than deleted. Setting `DISABLE` allows the resource to be
84+
disabled rather than deleted. Default is `DELETE`. Possible values are:
85+
* DELETE
86+
* DISABLE
87+
* ABANDON
88+
default_value: DELETE
89+
parameters:
90+
- !ruby/object:Api::Type::ResourceRef
91+
name: secret
92+
url_param_only: true
93+
resource: RegionalSecret
94+
imports: name
95+
required: true
96+
immutable: true
97+
description: |
98+
Secret Manager regional secret resource.
99+
- !ruby/object:Api::Type::String
100+
name: location
101+
url_param_only: true
102+
output: true
103+
description: |
104+
Location of Secret Manager regional secret resource.
105+
properties:
106+
- !ruby/object:Api::Type::String
107+
name: name
108+
output: true
109+
description: |
110+
The resource name of the regional secret version. Format:
111+
`projects/{{project}}/locations/{{location}}/secrets/{{secret_id}}/versions/{{version}}`
112+
- !ruby/object:Api::Type::String
113+
name: createTime
114+
output: true
115+
description: |
116+
The time at which the regional secret version was created.
117+
- !ruby/object:Api::Type::String
118+
name: destroyTime
119+
output: true
120+
description: |
121+
The time at which the regional secret version was destroyed. Only present if state is DESTROYED.
122+
- !ruby/object:Api::Type::NestedObject
123+
name: customerManagedEncryption
124+
output: true
125+
description: |
126+
The customer-managed encryption configuration of the regional secret.
127+
properties:
128+
- !ruby/object:Api::Type::String
129+
name: kmsKeyVersionName
130+
output: true
131+
description: |
132+
The resource name of the Cloud KMS CryptoKey used to encrypt secret payloads.
133+
- !ruby/object:Api::Type::String
134+
name: version
135+
output: true
136+
description: |
137+
The version of the Regional Secret.
138+
custom_flatten: templates/terraform/custom_flatten/regional_secret_version_version.go.erb
139+
- !ruby/object:Api::Type::Boolean
140+
name: enabled
141+
api_name: state
142+
default_value: true
143+
description: |
144+
The current state of the regional secret version.
145+
custom_flatten: templates/terraform/custom_flatten/secret_version_enable.go.erb
146+
custom_expand: templates/terraform/custom_expand/regional_secret_version_enable.go.erb
147+
- !ruby/object:Api::Type::NestedObject
148+
name: payload
149+
description: The secret payload of the Regional SecretVersion.
150+
required: true
151+
flatten_object: true
152+
custom_flatten: templates/terraform/custom_flatten/regional_secret_version_access.go.erb
153+
properties:
154+
- !ruby/object:Api::Type::String
155+
name: secret_data
156+
api_name: data
157+
required: true
158+
immutable: true
159+
sensitive: true
160+
description: The secret data. Must be no larger than 64KiB.
161+
custom_expand: templates/terraform/custom_expand/secret_version_secret_data.go.erb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<%- # the license inside this block applies to this file
2+
# Copyright 2024 Google Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
-%>
15+
func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
16+
name := d.Get("name").(string)
17+
if name == "" {
18+
return "", nil
19+
}
20+
21+
url, err := tpgresource.ReplaceVars(d, config, "{{SecretManagerRegionalBasePath}}{{name}}")
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
if v == true {
27+
url = fmt.Sprintf("%s:enable", url)
28+
} else {
29+
url = fmt.Sprintf("%s:disable", url)
30+
}
31+
32+
parts := strings.Split(name, "/")
33+
project := parts[1]
34+
35+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
41+
Config: config,
42+
Method: "POST",
43+
Project: project,
44+
RawURL: url,
45+
UserAgent: userAgent,
46+
})
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
return nil, nil
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<%- # the license inside this block applies to this file
2+
# Copyright 2024 Google Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
-%>
15+
func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
16+
transformed := make(map[string]interface{})
17+
18+
// if this secret version is disabled, the api will return an error, as the value cannot be accessed, return what we have
19+
if d.Get("enabled").(bool) == false {
20+
transformed["secret_data"] = d.Get("secret_data")
21+
return []interface{}{transformed}
22+
}
23+
24+
url, err := tpgresource.ReplaceVars(d, config, "{{SecretManagerRegionalBasePath}}{{name}}:access")
25+
if err != nil {
26+
return err
27+
}
28+
29+
parts := strings.Split(d.Get("name").(string), "/")
30+
project := parts[1]
31+
32+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
33+
if err != nil {
34+
return err
35+
}
36+
37+
accessRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
38+
Config: config,
39+
Method: "GET",
40+
Project: project,
41+
RawURL: url,
42+
UserAgent: userAgent,
43+
})
44+
if err != nil {
45+
return err
46+
}
47+
48+
if d.Get("is_secret_data_base64").(bool) {
49+
transformed["secret_data"] = accessRes["payload"].(map[string]interface{})["data"].(string)
50+
} else {
51+
data, err := base64.StdEncoding.DecodeString(accessRes["payload"].(map[string]interface{})["data"].(string))
52+
if err != nil {
53+
return err
54+
}
55+
transformed["secret_data"] = string(data)
56+
}
57+
return []interface{}{transformed}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%- # the license inside this block applies to this file
2+
# Copyright 2024 Google Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
-%>
15+
func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
16+
name := d.Get("name").(string)
17+
secretRegex := regexp.MustCompile("projects/(.+)/locations/(.+)/secrets/(.+)/versions/(.+)$")
18+
19+
parts := secretRegex.FindStringSubmatch(name)
20+
if len(parts) != 5 {
21+
return fmt.Errorf("Version name does not fit the format `projects/{{project}}/locations/{{location}}/secrets/{{secret}}/versions/{{version}}`")
22+
}
23+
24+
return parts[4]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<%- # the license inside this block applies to this file
2+
# Copyright 2024 Google Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
-%>
15+
config := meta.(*transport_tpg.Config)
16+
17+
// current import_formats can't import fields with forward slashes in their value
18+
if err := tpgresource.ParseImportId([]string{"(?P<name>.+)"}, d, config); err != nil {
19+
return nil, err
20+
}
21+
22+
name := d.Get("name").(string)
23+
secretRegex := regexp.MustCompile("(projects/.+/locations/.+/secrets/.+)/versions/.+$")
24+
versionRegex := regexp.MustCompile("projects/(.+)/locations/(.+)/secrets/(.+)/versions/(.+)$")
25+
26+
parts := secretRegex.FindStringSubmatch(name)
27+
if len(parts) != 2 {
28+
return nil, fmt.Errorf("Version name does not fit the format `projects/{{project}}/locations/{{location}}/secrets/{{secret}}/versions/{{version}}`")
29+
}
30+
if err := d.Set("secret", parts[1]); err != nil {
31+
return nil, fmt.Errorf("Error setting secret: %s", err)
32+
}
33+
34+
parts = versionRegex.FindStringSubmatch(name)
35+
36+
if err := d.Set("version", parts[4]); err != nil {
37+
return nil, fmt.Errorf("Error setting version: %s", err)
38+
}
39+
40+
// Explicitly set virtual fields to default values on import
41+
if err := d.Set("deletion_policy", "DELETE"); err != nil {
42+
return nil, fmt.Errorf("Error setting deletion policy: %s", err)
43+
}
44+
45+
if err := d.Set("location", parts[2]); err != nil {
46+
return nil, fmt.Errorf("Error setting location: %s", err)
47+
}
48+
49+
return []*schema.ResourceData{d}, nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%- # the license inside this block applies to this file
2+
# Copyright 2024 Google Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
-%>
15+
_, err := expandSecretManagerRegionalRegionalSecretVersionEnabled(d.Get("enabled"), d, config)
16+
if err != nil {
17+
return err
18+
}
19+
20+
return resourceSecretManagerRegionalRegionalSecretVersionRead(d, meta)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "google_secret_manager_regional_secret" "secret-basic" {
2+
secret_id = "<%= ctx[:vars]['secret_id'] %>"
3+
location = "us-central1"
4+
}
5+
6+
resource "google_secret_manager_regional_secret_version" "<%= ctx[:primary_resource_id] %>" {
7+
secret = google_secret_manager_regional_secret.secret-basic.id
8+
secret_data = "<%= ctx[:vars]['data'] %>"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "google_secret_manager_regional_secret" "secret-basic" {
2+
secret_id = "<%= ctx[:vars]['secret_id'] %>"
3+
location = "us-central1"
4+
}
5+
6+
resource "google_secret_manager_regional_secret_version" "<%= ctx[:primary_resource_id] %>" {
7+
secret = google_secret_manager_regional_secret.secret-basic.id
8+
secret_data = "<%= ctx[:vars]['data'] %>"
9+
deletion_policy = "ABANDON"
10+
}

0 commit comments

Comments
 (0)