Skip to content

Commit e0af072

Browse files
authored
Add support for networksecurity.googleapis.com/BackendAuthenticationConfig to TGC cai2hcl (#15044)
1 parent 2235055 commit e0af072

File tree

8 files changed

+398
-16
lines changed

8 files changed

+398
-16
lines changed

mmv1/third_party/cai2hcl/convert_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ func TestConvertNetworksecurity(t *testing.T) {
2929
"./services/networksecurity/testdata",
3030
[]string{
3131
"server_tls_policy",
32+
"backend_authentication_config",
3233
})
3334
}

mmv1/third_party/cai2hcl/converter_map.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var AssetTypeToConverter = map[string]string{
2424
resourcemanager.ProjectAssetType: "google_project",
2525
resourcemanager.ProjectBillingAssetType: "google_project",
2626

27-
networksecurity.ServerTLSPolicyAssetType: "google_network_security_server_tls_policy",
27+
networksecurity.ServerTLSPolicyAssetType: "google_network_security_server_tls_policy",
28+
networksecurity.BackendAuthenticationConfigAssetType: "google_network_security_backend_authentication_config",
2829
}
2930

3031
// ConverterMap is a collection of converters instances, indexed by name.
@@ -39,5 +40,6 @@ var ConverterMap = map[string]common.Converter{
3940

4041
"google_project": resourcemanager.NewProjectConverter(provider),
4142

42-
"google_network_security_server_tls_policy": networksecurity.NewServerTLSPolicyConverter(provider),
43+
"google_network_security_server_tls_policy": networksecurity.NewServerTLSPolicyConverter(provider),
44+
"google_network_security_backend_authentication_config": networksecurity.NewBackendAuthenticationConfigConverter(provider),
4345
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package networksecurity
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/GoogleCloudPlatform/terraform-google-conversion/v6/cai2hcl/common"
7+
"github.com/GoogleCloudPlatform/terraform-google-conversion/v6/caiasset"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
netsecapi "google.golang.org/api/networksecurity/v1"
10+
)
11+
12+
// BackendAuthenticationConfigAssetType is the CAI asset type name.
13+
const BackendAuthenticationConfigAssetType string = "networksecurity.googleapis.com/BackendAuthenticationConfig"
14+
15+
// BackendAuthenticationConfigSchemaName is the TF resource schema name.
16+
const BackendAuthenticationConfigSchemaName string = "google_network_security_backend_authentication_config"
17+
18+
// BackendAuthenticationConfigConverter for networksecurity backend authentication config resource.
19+
type BackendAuthenticationConfigConverter struct {
20+
name string
21+
schema map[string]*schema.Schema
22+
}
23+
24+
// NewBackendAuthenticationConfigConverter returns an HCL converter.
25+
func NewBackendAuthenticationConfigConverter(provider *schema.Provider) common.Converter {
26+
schema := provider.ResourcesMap[BackendAuthenticationConfigSchemaName].Schema
27+
28+
return &BackendAuthenticationConfigConverter{
29+
name: BackendAuthenticationConfigSchemaName,
30+
schema: schema,
31+
}
32+
}
33+
34+
// Convert converts CAI assets to HCL resource blocks (Provider version: 7.0.1)
35+
func (c *BackendAuthenticationConfigConverter) Convert(assets []*caiasset.Asset) ([]*common.HCLResourceBlock, error) {
36+
var blocks []*common.HCLResourceBlock
37+
var err error
38+
39+
for _, asset := range assets {
40+
if asset == nil {
41+
continue
42+
} else if asset.Resource == nil || asset.Resource.Data == nil {
43+
return nil, fmt.Errorf("INVALID_ARGUMENT: Asset resource data is nil")
44+
} else if asset.Type != BackendAuthenticationConfigAssetType {
45+
return nil, fmt.Errorf("INVALID_ARGUMENT: Expected asset of type %s, but received %s", BackendAuthenticationConfigAssetType, asset.Type)
46+
}
47+
block, errConvert := c.convertResourceData(asset)
48+
blocks = append(blocks, block)
49+
if errConvert != nil {
50+
err = errors.Join(err, errConvert)
51+
}
52+
}
53+
return blocks, err
54+
}
55+
56+
func (c *BackendAuthenticationConfigConverter) convertResourceData(asset *caiasset.Asset) (*common.HCLResourceBlock, error) {
57+
if asset == nil || asset.Resource == nil || asset.Resource.Data == nil {
58+
return nil, fmt.Errorf("INVALID_ARGUMENT: Asset resource data is nil")
59+
}
60+
61+
hcl, _ := flattenBackendAuthenticationConfig(asset.Resource)
62+
63+
ctyVal, err := common.MapToCtyValWithSchema(hcl, c.schema)
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
resourceName := hcl["name"].(string)
69+
return &common.HCLResourceBlock{
70+
Labels: []string{c.name, resourceName},
71+
Value: ctyVal,
72+
}, nil
73+
}
74+
75+
func flattenBackendAuthenticationConfig(resource *caiasset.AssetResource) (map[string]any, error) {
76+
result := make(map[string]any)
77+
78+
var backendAuthenticationConfig *netsecapi.BackendAuthenticationConfig
79+
if err := common.DecodeJSON(resource.Data, &backendAuthenticationConfig); err != nil {
80+
return nil, err
81+
}
82+
83+
result["name"] = flattenName(backendAuthenticationConfig.Name)
84+
result["labels"] = backendAuthenticationConfig.Labels
85+
result["description"] = backendAuthenticationConfig.Description
86+
result["client_certificate"] = backendAuthenticationConfig.ClientCertificate
87+
result["trust_config"] = backendAuthenticationConfig.TrustConfig
88+
result["well_known_roots"] = backendAuthenticationConfig.WellKnownRoots
89+
result["project"] = flattenProjectName(backendAuthenticationConfig.Name)
90+
91+
result["location"] = resource.Location
92+
93+
return result, nil
94+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package networksecurity_test
2+
3+
import (
4+
cai2hcl_testing "github.com/GoogleCloudPlatform/terraform-google-conversion/v6/cai2hcl/testing"
5+
"testing"
6+
)
7+
8+
func TestBackendAuthenticationConfig(t *testing.T) {
9+
cai2hcl_testing.AssertTestFiles(
10+
t,
11+
"./testdata",
12+
[]string{"backend_authentication_config"})
13+
}

mmv1/third_party/cai2hcl/services/networksecurity/server_tls_policy.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/GoogleCloudPlatform/terraform-google-conversion/v6/caiasset"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
netsecapi "google.golang.org/api/networksecurity/v1"
10-
"strings"
1110
)
1211

1312
// ServerTLSPolicyAssetType is the CAI asset type name.
@@ -94,11 +93,6 @@ func flattenServerTLSPolicy(resource *caiasset.AssetResource) (map[string]any, e
9493
return result, nil
9594
}
9695

97-
func flattenName(name string) string {
98-
tokens := strings.Split(name, "/")
99-
return tokens[len(tokens)-1]
100-
}
101-
10296
func flattenServerCertificate(certificate *netsecapi.GoogleCloudNetworksecurityV1CertificateProvider) []map[string]any {
10397
if certificate == nil {
10498
return nil
@@ -163,11 +157,3 @@ func flattenClientValidationCA(cas []*netsecapi.ValidationCA) []map[string]any {
163157

164158
return result
165159
}
166-
167-
func flattenProjectName(name string) string {
168-
tokens := strings.Split(name, "/")
169-
if len(tokens) < 2 || tokens[0] != "projects" {
170-
return ""
171-
}
172-
return tokens[1]
173-
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
[
2+
{
3+
"ancestors": ["projects/307841421122"],
4+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
5+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test1",
6+
"resource": {
7+
"data": {
8+
"createTime": "2025-09-01T12:06:59.449575828Z",
9+
"etag": "tyOJj9L43CxYuKifz5lEwq4SkQqs5426-4H7BCgyUMw",
10+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test1",
11+
"updateTime": "2025-09-01T12:07:03.557427913Z",
12+
"wellKnownRoots": "PUBLIC_ROOTS"
13+
},
14+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
15+
"discovery_name": "BackendAuthenticationConfig",
16+
"location": "global",
17+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
18+
"version": "v1"
19+
},
20+
"updateTime": "2025-09-01T12:07:03.557427913Z"
21+
},
22+
{
23+
"ancestors": ["projects/307841421122"],
24+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
25+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test2",
26+
"resource": {
27+
"data": {
28+
"createTime": "2025-09-01T12:22:50.489447184Z",
29+
"etag": "hI87OGITW_38twEfrG1qMbgXTjulOs0PvGVm5zgpNfQ",
30+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test2",
31+
"trustConfig": "projects/ccm-breakit/locations/global/trustConfigs/id-2de0d4b7-89cf-476f-893d-4567b3791ca9",
32+
"updateTime": "2025-09-01T12:22:56.430273835Z",
33+
"wellKnownRoots": "PUBLIC_ROOTS"
34+
},
35+
36+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
37+
"discovery_name": "BackendAuthenticationConfig",
38+
"location": "global",
39+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
40+
"version": "v1"
41+
},
42+
"updateTime": "2025-09-01T12:22:56.430273835Z"
43+
},
44+
{
45+
"ancestors": ["projects/307841421122"],
46+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
47+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test3",
48+
"resource": {
49+
"data": {
50+
"clientCertificate": "projects/ccm-breakit/locations/global/certificates/anatolisaukhin-27101",
51+
"createTime": "2025-09-01T12:23:21.187162159Z",
52+
"etag": "TbNENVDPeneynkqLnTmLvn757xA-GnuI_XTsk2F00y0",
53+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test3",
54+
"updateTime": "2025-09-01T12:23:25.982840761Z",
55+
"wellKnownRoots": "PUBLIC_ROOTS"
56+
},
57+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
58+
"discovery_name": "BackendAuthenticationConfig",
59+
"location": "global",
60+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
61+
"version": "v1"
62+
},
63+
"updateTime": "2025-09-01T12:23:25.982840761Z"
64+
},
65+
{
66+
"ancestors": ["projects/307841421122"],
67+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
68+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test4",
69+
"resource": {
70+
"data": {
71+
"clientCertificate": "projects/ccm-breakit/locations/global/certificates/anatolisaukhin-27101",
72+
"createTime": "2025-09-01T12:23:59.175425527Z",
73+
"etag": "MtMLKQSPwOIjp25H_ndWUe9zKCcVvtHdoHxv5XvBvHU",
74+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test4",
75+
"trustConfig": "projects/ccm-breakit/locations/global/trustConfigs/id-2de0d4b7-89cf-476f-893d-4567b3791ca9",
76+
"updateTime": "2025-09-01T12:24:09.189436548Z",
77+
"wellKnownRoots": "PUBLIC_ROOTS"
78+
},
79+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
80+
"discovery_name": "BackendAuthenticationConfig",
81+
"location": "global",
82+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
83+
"version": "v1"
84+
},
85+
"updateTime": "2025-09-01T12:24:09.189436548Z"
86+
},
87+
{
88+
"ancestors": ["projects/307841421122"],
89+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
90+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test5",
91+
"resource": {
92+
"data": {
93+
"createTime": "2025-09-01T12:24:38.165237290Z",
94+
"etag": "q-3pJ_Ae7LorXoNfPMbuxSiwH4JiS4KMlk6Ojm50qbo",
95+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test5",
96+
"trustConfig": "projects/ccm-breakit/locations/global/trustConfigs/id-2de0d4b7-89cf-476f-893d-4567b3791ca9",
97+
"updateTime": "2025-09-01T12:24:42.574599551Z",
98+
"wellKnownRoots": "NONE"
99+
},
100+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
101+
"discovery_name": "BackendAuthenticationConfig",
102+
"location": "global",
103+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
104+
"version": "v1"
105+
},
106+
"updateTime": "2025-09-01T12:24:42.574599551Z"
107+
},
108+
{
109+
"ancestors": ["projects/307841421122"],
110+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
111+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test7",
112+
"resource": {
113+
"data": {
114+
"clientCertificate": "projects/ccm-breakit/locations/global/certificates/anatolisaukhin-27101",
115+
"createTime": "2025-09-01T12:25:29.338526364Z",
116+
"etag": "5dYcNBll7z2KaHuJd2nxr9Qp4U1JPuPBzJtFkVdRO_k",
117+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test7",
118+
"trustConfig": "projects/ccm-breakit/locations/global/trustConfigs/id-2de0d4b7-89cf-476f-893d-4567b3791ca9",
119+
"updateTime": "2025-09-01T12:25:36.419856961Z",
120+
"wellKnownRoots": "NONE"
121+
},
122+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
123+
"discovery_name": "BackendAuthenticationConfig",
124+
"location": "global",
125+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
126+
"version": "v1"
127+
},
128+
"updateTime": "2025-09-01T12:25:36.419856961Z"
129+
},
130+
{
131+
"ancestors": ["projects/307841421122"],
132+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
133+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test8",
134+
"resource": {
135+
"data": {
136+
"createTime": "2025-09-01T12:28:43.012225935Z",
137+
"description": "My test description",
138+
"etag": "jAgYExhvS1-odwm8v6WzKxXcWqnMgOqyQNxz0LpLzcE",
139+
"labels": {
140+
"foo": "bar"
141+
},
142+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test8",
143+
"updateTime": "2025-09-01T12:28:48.571512164Z",
144+
"wellKnownRoots": "PUBLIC_ROOTS"
145+
},
146+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
147+
"discovery_name": "BackendAuthenticationConfig",
148+
"location": "global",
149+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
150+
"version": "v1"
151+
},
152+
"updateTime": "2025-09-01T12:28:48.571512164Z"
153+
},
154+
155+
{
156+
"ancestors": ["projects/307841421122"],
157+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
158+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/europe-west1/backendAuthenticationConfigs/laurenzk-test9",
159+
"resource": {
160+
"data": {
161+
"createTime": "2025-09-01T12:37:43.341940613Z",
162+
"etag": "DQgzWLri0AvaD72f8Xk5SBtT6nEoH4B3krtcsjS7V2A",
163+
"name": "projects/ccm-breakit/locations/europe-west1/backendAuthenticationConfigs/laurenzk-test9",
164+
"updateTime": "2025-09-01T12:37:43.402977101Z",
165+
"wellKnownRoots": "PUBLIC_ROOTS"
166+
},
167+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
168+
"discovery_name": "BackendAuthenticationConfig",
169+
"location": "europe-west1",
170+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
171+
"version": "v1"
172+
},
173+
"updateTime": "2025-09-01T12:37:43.402977101Z"
174+
},
175+
{
176+
"ancestors": ["projects/307841421122"],
177+
"asset_type": "networksecurity.googleapis.com/BackendAuthenticationConfig",
178+
"name": "//networksecurity.googleapis.com/projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test10",
179+
"resource": {
180+
"data": {
181+
"createTime": "2025-09-01T12:28:42.936655254Z",
182+
"etag": "7hxmlYo8sLeaML92DAHdBZGpNDehxn6ksGJsTQDXcHE",
183+
"name": "projects/ccm-breakit/locations/global/backendAuthenticationConfigs/laurenzk-test10",
184+
"updateTime": "2025-09-01T12:28:46.761498301Z",
185+
"wellKnownRoots": "PUBLIC_ROOTS"
186+
},
187+
"discovery_document_uri": "https://networksecurity.googleapis.com/$discovery/rest",
188+
"discovery_name": "BackendAuthenticationConfig",
189+
"location": "global",
190+
"parent": "//cloudresourcemanager.googleapis.com/projects/307841421122",
191+
"version": "v1"
192+
},
193+
"updateTime": "2025-09-01T12:28:46.761498301Z"
194+
}
195+
]

0 commit comments

Comments
 (0)