Skip to content

Commit b35a8cb

Browse files
add certificate manager public CA external account key resource (#8171) (#14983)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]>
1 parent 7764742 commit b35a8cb

10 files changed

+361
-2
lines changed

.changelog/8171.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
`google_certificate_manager_public_ca_external_account_key`
3+
```

google/config_test_utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func configureTestBasePaths(c *transport_tpg.Config, url string) {
9393
c.OSConfigBasePath = url
9494
c.OSLoginBasePath = url
9595
c.PrivatecaBasePath = url
96+
c.PublicCABasePath = url
9697
c.PubsubBasePath = url
9798
c.PubsubLiteBasePath = url
9899
c.RedisBasePath = url

google/framework_provider.go

+6
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
533533
transport_tpg.CustomEndpointValidator(),
534534
},
535535
},
536+
"public_ca_custom_endpoint": &schema.StringAttribute{
537+
Optional: true,
538+
Validators: []validator.String{
539+
transport_tpg.CustomEndpointValidator(),
540+
},
541+
},
536542
"pubsub_custom_endpoint": &schema.StringAttribute{
537543
Optional: true,
538544
Validators: []validator.String{

google/fwmodels/provider_model.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type ProviderModel struct {
9494
OSConfigCustomEndpoint types.String `tfsdk:"os_config_custom_endpoint"`
9595
OSLoginCustomEndpoint types.String `tfsdk:"os_login_custom_endpoint"`
9696
PrivatecaCustomEndpoint types.String `tfsdk:"privateca_custom_endpoint"`
97+
PublicCACustomEndpoint types.String `tfsdk:"public_ca_custom_endpoint"`
9798
PubsubCustomEndpoint types.String `tfsdk:"pubsub_custom_endpoint"`
9899
PubsubLiteCustomEndpoint types.String `tfsdk:"pubsub_lite_custom_endpoint"`
99100
RedisCustomEndpoint types.String `tfsdk:"redis_custom_endpoint"`

google/fwtransport/framework_config.go

+10
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ type FrameworkProviderConfig struct {
118118
OSConfigBasePath string
119119
OSLoginBasePath string
120120
PrivatecaBasePath string
121+
PublicCABasePath string
121122
PubsubBasePath string
122123
PubsubLiteBasePath string
123124
RedisBasePath string
@@ -249,6 +250,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
249250
p.OSConfigBasePath = data.OSConfigCustomEndpoint.ValueString()
250251
p.OSLoginBasePath = data.OSLoginCustomEndpoint.ValueString()
251252
p.PrivatecaBasePath = data.PrivatecaCustomEndpoint.ValueString()
253+
p.PublicCABasePath = data.PublicCACustomEndpoint.ValueString()
252254
p.PubsubBasePath = data.PubsubCustomEndpoint.ValueString()
253255
p.PubsubLiteBasePath = data.PubsubLiteCustomEndpoint.ValueString()
254256
p.RedisBasePath = data.RedisCustomEndpoint.ValueString()
@@ -949,6 +951,14 @@ func (p *FrameworkProviderConfig) HandleDefaults(ctx context.Context, data *fwmo
949951
data.PrivatecaCustomEndpoint = types.StringValue(customEndpoint.(string))
950952
}
951953
}
954+
if data.PublicCACustomEndpoint.IsNull() {
955+
customEndpoint := transport_tpg.MultiEnvDefault([]string{
956+
"GOOGLE_PUBLIC_CA_CUSTOM_ENDPOINT",
957+
}, transport_tpg.DefaultBasePaths[transport_tpg.PublicCABasePathKey])
958+
if customEndpoint != nil {
959+
data.PublicCACustomEndpoint = types.StringValue(customEndpoint.(string))
960+
}
961+
}
952962
if data.PubsubCustomEndpoint.IsNull() {
953963
customEndpoint := transport_tpg.MultiEnvDefault([]string{
954964
"GOOGLE_PUBSUB_CUSTOM_ENDPOINT",

google/provider/provider.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import (
8282
"github.com/hashicorp/terraform-provider-google/google/services/osconfig"
8383
"github.com/hashicorp/terraform-provider-google/google/services/oslogin"
8484
"github.com/hashicorp/terraform-provider-google/google/services/privateca"
85+
"github.com/hashicorp/terraform-provider-google/google/services/publicca"
8586
"github.com/hashicorp/terraform-provider-google/google/services/pubsub"
8687
"github.com/hashicorp/terraform-provider-google/google/services/pubsublite"
8788
"github.com/hashicorp/terraform-provider-google/google/services/redis"
@@ -564,6 +565,11 @@ func Provider() *schema.Provider {
564565
Optional: true,
565566
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
566567
},
568+
"public_ca_custom_endpoint": {
569+
Type: schema.TypeString,
570+
Optional: true,
571+
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
572+
},
567573
"pubsub_custom_endpoint": {
568574
Type: schema.TypeString,
569575
Optional: true,
@@ -920,9 +926,9 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
920926
})
921927
}
922928

923-
// Generated resources: 291
929+
// Generated resources: 292
924930
// Generated IAM resources: 195
925-
// Total generated resources: 486
931+
// Total generated resources: 487
926932
func ResourceMap() map[string]*schema.Resource {
927933
resourceMap, _ := ResourceMapWithErrors()
928934
return resourceMap
@@ -1338,6 +1344,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
13381344
"google_privateca_certificate_template_iam_binding": tpgiamresource.ResourceIamBinding(privateca.PrivatecaCertificateTemplateIamSchema, privateca.PrivatecaCertificateTemplateIamUpdaterProducer, privateca.PrivatecaCertificateTemplateIdParseFunc),
13391345
"google_privateca_certificate_template_iam_member": tpgiamresource.ResourceIamMember(privateca.PrivatecaCertificateTemplateIamSchema, privateca.PrivatecaCertificateTemplateIamUpdaterProducer, privateca.PrivatecaCertificateTemplateIdParseFunc),
13401346
"google_privateca_certificate_template_iam_policy": tpgiamresource.ResourceIamPolicy(privateca.PrivatecaCertificateTemplateIamSchema, privateca.PrivatecaCertificateTemplateIamUpdaterProducer, privateca.PrivatecaCertificateTemplateIdParseFunc),
1347+
"google_public_ca_external_account_key": publicca.ResourcePublicCAExternalAccountKey(),
13411348
"google_pubsub_schema": pubsub.ResourcePubsubSchema(),
13421349
"google_pubsub_subscription": pubsub.ResourcePubsubSubscription(),
13431350
"google_pubsub_topic": pubsub.ResourcePubsubTopic(),
@@ -1710,6 +1717,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
17101717
config.OSConfigBasePath = d.Get("os_config_custom_endpoint").(string)
17111718
config.OSLoginBasePath = d.Get("os_login_custom_endpoint").(string)
17121719
config.PrivatecaBasePath = d.Get("privateca_custom_endpoint").(string)
1720+
config.PublicCABasePath = d.Get("public_ca_custom_endpoint").(string)
17131721
config.PubsubBasePath = d.Get("pubsub_custom_endpoint").(string)
17141722
config.PubsubLiteBasePath = d.Get("pubsub_lite_custom_endpoint").(string)
17151723
config.RedisBasePath = d.Get("redis_custom_endpoint").(string)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This file is automatically generated by Magic Modules and manual
11+
// changes will be clobbered when the file is regenerated.
12+
//
13+
// Please read more about how to change this file in
14+
// .github/CONTRIBUTING.md.
15+
//
16+
// ----------------------------------------------------------------------------
17+
18+
package google
19+
20+
import (
21+
"testing"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
24+
25+
"github.com/hashicorp/terraform-provider-google/google/acctest"
26+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
27+
)
28+
29+
func TestAccPublicCAExternalAccountKey_publicCaExternalAccountKeyExample(t *testing.T) {
30+
t.Parallel()
31+
32+
context := map[string]interface{}{
33+
"project": acctest.GetTestProjectFromEnv(),
34+
"random_suffix": RandString(t, 10),
35+
}
36+
37+
VcrTest(t, resource.TestCase{
38+
PreCheck: func() { acctest.AccTestPreCheck(t) },
39+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
40+
Steps: []resource.TestStep{
41+
{
42+
Config: testAccPublicCAExternalAccountKey_publicCaExternalAccountKeyExample(context),
43+
},
44+
},
45+
})
46+
}
47+
48+
func testAccPublicCAExternalAccountKey_publicCaExternalAccountKeyExample(context map[string]interface{}) string {
49+
return tpgresource.Nprintf(`
50+
resource "google_public_ca_external_account_key" "prod" {
51+
project = "%{project}"
52+
}
53+
`, context)
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This file is automatically generated by Magic Modules and manual
11+
// changes will be clobbered when the file is regenerated.
12+
//
13+
// Please read more about how to change this file in
14+
// .github/CONTRIBUTING.md.
15+
//
16+
// ----------------------------------------------------------------------------
17+
18+
package publicca
19+
20+
import (
21+
"fmt"
22+
"log"
23+
"time"
24+
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
26+
27+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
28+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
29+
)
30+
31+
func ResourcePublicCAExternalAccountKey() *schema.Resource {
32+
return &schema.Resource{
33+
Create: resourcePublicCAExternalAccountKeyCreate,
34+
Read: resourcePublicCAExternalAccountKeyRead,
35+
Delete: resourcePublicCAExternalAccountKeyDelete,
36+
37+
Timeouts: &schema.ResourceTimeout{
38+
Create: schema.DefaultTimeout(20 * time.Minute),
39+
Delete: schema.DefaultTimeout(20 * time.Minute),
40+
},
41+
42+
Schema: map[string]*schema.Schema{
43+
"location": {
44+
Type: schema.TypeString,
45+
Optional: true,
46+
ForceNew: true,
47+
Description: `Location for the externalAccountKey. Currently only 'global' is supported.`,
48+
Default: "global",
49+
},
50+
"b64_mac_key": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
Description: `Base64-URL-encoded HS256 key. It is generated by the PublicCertificateAuthorityService
54+
when the ExternalAccountKey is created.`,
55+
Sensitive: true,
56+
},
57+
"key_id": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: `It is generated by the PublicCertificateAuthorityService when the ExternalAccountKey is created.`,
61+
Sensitive: true,
62+
},
63+
"name": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
Description: `Resource name. projects/{project}/locations/{location}/externalAccountKeys/{keyId}.`,
67+
},
68+
"project": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Computed: true,
72+
ForceNew: true,
73+
},
74+
},
75+
UseJSONNumber: true,
76+
}
77+
}
78+
79+
func resourcePublicCAExternalAccountKeyCreate(d *schema.ResourceData, meta interface{}) error {
80+
config := meta.(*transport_tpg.Config)
81+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
82+
if err != nil {
83+
return err
84+
}
85+
86+
obj := make(map[string]interface{})
87+
88+
url, err := tpgresource.ReplaceVars(d, config, "{{PublicCABasePath}}projects/{{project}}/locations/{{location}}/externalAccountKeys")
89+
if err != nil {
90+
return err
91+
}
92+
93+
log.Printf("[DEBUG] Creating new ExternalAccountKey: %#v", obj)
94+
billingProject := ""
95+
96+
project, err := tpgresource.GetProject(d, config)
97+
if err != nil {
98+
return fmt.Errorf("Error fetching project for ExternalAccountKey: %s", err)
99+
}
100+
billingProject = project
101+
102+
// err == nil indicates that the billing_project value was found
103+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
104+
billingProject = bp
105+
}
106+
107+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
108+
Config: config,
109+
Method: "POST",
110+
Project: billingProject,
111+
RawURL: url,
112+
UserAgent: userAgent,
113+
Body: obj,
114+
Timeout: d.Timeout(schema.TimeoutCreate),
115+
})
116+
if err != nil {
117+
return fmt.Errorf("Error creating ExternalAccountKey: %s", err)
118+
}
119+
if err := d.Set("name", flattenPublicCAExternalAccountKeyName(res["name"], d, config)); err != nil {
120+
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
121+
}
122+
if err := d.Set("key_id", flattenPublicCAExternalAccountKeyKeyId(res["keyId"], d, config)); err != nil {
123+
return fmt.Errorf(`Error setting computed identity field "key_id": %s`, err)
124+
}
125+
if err := d.Set("b64_mac_key", flattenPublicCAExternalAccountKeyB64MacKey(res["b64MacKey"], d, config)); err != nil {
126+
return fmt.Errorf(`Error setting computed identity field "b64_mac_key": %s`, err)
127+
}
128+
129+
// Store the ID now
130+
id, err := tpgresource.ReplaceVars(d, config, "{{name}}")
131+
if err != nil {
132+
return fmt.Errorf("Error constructing id: %s", err)
133+
}
134+
d.SetId(id)
135+
136+
log.Printf("[DEBUG] Finished creating ExternalAccountKey %q: %#v", d.Id(), res)
137+
138+
return resourcePublicCAExternalAccountKeyRead(d, meta)
139+
}
140+
141+
func resourcePublicCAExternalAccountKeyRead(d *schema.ResourceData, meta interface{}) error {
142+
// This resource could not be read from the API.
143+
return nil
144+
}
145+
146+
func resourcePublicCAExternalAccountKeyDelete(d *schema.ResourceData, meta interface{}) error {
147+
log.Printf("[WARNING] PublicCA ExternalAccountKey resources"+
148+
" cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+
149+
" state, but will still be present on Google Cloud.", d.Id())
150+
d.SetId("")
151+
152+
return nil
153+
}
154+
155+
func flattenPublicCAExternalAccountKeyName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
156+
return v
157+
}
158+
159+
func flattenPublicCAExternalAccountKeyKeyId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
160+
return v
161+
}
162+
163+
func flattenPublicCAExternalAccountKeyB64MacKey(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
164+
return v
165+
}

google/transport/config.go

+9
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ type Config struct {
252252
OSConfigBasePath string
253253
OSLoginBasePath string
254254
PrivatecaBasePath string
255+
PublicCABasePath string
255256
PubsubBasePath string
256257
PubsubLiteBasePath string
257258
RedisBasePath string
@@ -361,6 +362,7 @@ const NotebooksBasePathKey = "Notebooks"
361362
const OSConfigBasePathKey = "OSConfig"
362363
const OSLoginBasePathKey = "OSLogin"
363364
const PrivatecaBasePathKey = "Privateca"
365+
const PublicCABasePathKey = "PublicCA"
364366
const PubsubBasePathKey = "Pubsub"
365367
const PubsubLiteBasePathKey = "PubsubLite"
366368
const RedisBasePathKey = "Redis"
@@ -464,6 +466,7 @@ var DefaultBasePaths = map[string]string{
464466
OSConfigBasePathKey: "https://osconfig.googleapis.com/v1/",
465467
OSLoginBasePathKey: "https://oslogin.googleapis.com/v1/",
466468
PrivatecaBasePathKey: "https://privateca.googleapis.com/v1/",
469+
PublicCABasePathKey: "https://publicca.googleapis.com/v1/",
467470
PubsubBasePathKey: "https://pubsub.googleapis.com/v1/",
468471
PubsubLiteBasePathKey: "https://{{region}}-pubsublite.googleapis.com/v1/admin/",
469472
RedisBasePathKey: "https://redis.googleapis.com/v1/",
@@ -910,6 +913,11 @@ func HandleSDKDefaults(d *schema.ResourceData) error {
910913
"GOOGLE_PRIVATECA_CUSTOM_ENDPOINT",
911914
}, DefaultBasePaths[PrivatecaBasePathKey]))
912915
}
916+
if d.Get("public_ca_custom_endpoint") == "" {
917+
d.Set("public_ca_custom_endpoint", MultiEnvDefault([]string{
918+
"GOOGLE_PUBLIC_CA_CUSTOM_ENDPOINT",
919+
}, DefaultBasePaths[PublicCABasePathKey]))
920+
}
913921
if d.Get("pubsub_custom_endpoint") == "" {
914922
d.Set("pubsub_custom_endpoint", MultiEnvDefault([]string{
915923
"GOOGLE_PUBSUB_CUSTOM_ENDPOINT",
@@ -1910,6 +1918,7 @@ func ConfigureBasePaths(c *Config) {
19101918
c.OSConfigBasePath = DefaultBasePaths[OSConfigBasePathKey]
19111919
c.OSLoginBasePath = DefaultBasePaths[OSLoginBasePathKey]
19121920
c.PrivatecaBasePath = DefaultBasePaths[PrivatecaBasePathKey]
1921+
c.PublicCABasePath = DefaultBasePaths[PublicCABasePathKey]
19131922
c.PubsubBasePath = DefaultBasePaths[PubsubBasePathKey]
19141923
c.PubsubLiteBasePath = DefaultBasePaths[PubsubLiteBasePathKey]
19151924
c.RedisBasePath = DefaultBasePaths[RedisBasePathKey]

0 commit comments

Comments
 (0)