Skip to content

Commit 1e5d4a1

Browse files
authored
feat: support svpc host project in export-terraform (#910)
1 parent f1759e7 commit 1e5d4a1

File tree

10 files changed

+100
-3
lines changed

10 files changed

+100
-3
lines changed

functions/go/export-terraform/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The goal is to make the generated output as close to possible as what a human wo
1616
The following KCC resources are supported:
1717
- Folder
1818
- Project
19+
- ComputeSharedVPCHostProject
1920
- IAMPartialPolicy
2021
- IAMPolicy
2122
- IAMPolicyMember

functions/go/export-terraform/terraformgenerator/network.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,31 @@ func (resource *terraformResource) GetFirewallAllowPortsProtocol() []firewallAll
3131
}
3232
return firewallAllows
3333
}
34+
35+
// IsSVPCHost checks if the resource is a SVPC Host project.
36+
// The resource is a SVPC Host project if and only if it is of kind Project
37+
// and has a corresponding ComputeSharedVPCHostProject child resource.
38+
func (resource *terraformResource) IsSVPCHost() bool {
39+
if resource.Kind != "Project" {
40+
return false
41+
}
42+
projectID, found, err := resource.Item.GetString("metadata", "name")
43+
if !found || err != nil {
44+
return false
45+
}
46+
for _, child := range resource.Children {
47+
if child.Kind != "ComputeSharedVPCHostProject" {
48+
continue
49+
}
50+
// ComputeSharedVPCHostProject has no spec and relies on anno
51+
// https://cloud.google.com/config-connector/docs/reference/resource-docs/compute/computesharedvpchostproject#annotations
52+
svpcHostProjectID, found, err := child.Item.GetString("metadata", "annotations", "cnrm.cloud.google.com/project-id")
53+
if !found || err != nil {
54+
continue
55+
}
56+
if projectID == svpcHostProjectID {
57+
return true
58+
}
59+
}
60+
return false
61+
}

functions/go/export-terraform/terraformgenerator/templates/projects.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module "{{ $project.GetResourceName }}" {
77
project_id = "{{ $project.GetResourceName }}"{{end}}
88
org_id = {{ $project.GetOrganization.GetTerraformId false }}{{if eq $project.Parent.Kind "Folder"}}
99
folder_id = {{ $project.Parent.GetTerraformId false }}{{end}}
10-
10+
{{ if $project.IsSVPCHost }}
11+
enable_shared_vpc_host_project = true{{end}}
1112
billing_account = {{ $project.References.BillingAccount.GetTerraformId false }}{{if $project.GetBool "metadata" "annotations" "cnrm.cloud.google.com/auto-create-network"}}
1213
auto_create_network = true{{end}}
1314
}

functions/go/export-terraform/terraformgenerator/terraform_generator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func Processor(rl *sdk.ResourceList) error {
5555
"ServiceNetworkingConnection": true,
5656
"ComputeFirewall": true,
5757
"LoggingLogBucket": true,
58+
"ComputeSharedVPCHostProject": true,
5859
}
5960

6061
for _, item := range rl.Items {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2022 Google LLC
2+
#
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+
apiVersion: compute.cnrm.cloud.google.com/v1beta1
15+
kind: ComputeSharedVPCHostProject
16+
metadata:
17+
name: prj-network-host
18+
namespace: networking # kpt-set: ${namespace}
19+
annotations:
20+
cnrm.cloud.google.com/project-id: this-project-does-not-exist
21+
cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.1

functions/go/export-terraform/testdata/multi-network/input/net2/projects.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ metadata:
2020
annotations:
2121
cnrm.cloud.google.com/blueprint: 'kpt-fn'
2222
spec:
23-
name: prj-network2 # kpt-set: ${project-id}
23+
name: prj-network2-diff-name # kpt-set: ${project-id}
2424
billingAccountRef:
2525
external: AAAAAA-AAAAAA-AAAAAA
2626
organizationRef:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2022 Google LLC
2+
#
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+
apiVersion: compute.cnrm.cloud.google.com/v1beta1
15+
kind: ComputeSharedVPCHostProject
16+
metadata:
17+
name: prj-network-host
18+
namespace: networking # kpt-set: ${namespace}
19+
annotations:
20+
cnrm.cloud.google.com/project-id: prj-network2
21+
cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.1

functions/go/export-terraform/testdata/multi-network/tf/projects.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ module "prj-network2" {
1212
source = "terraform-google-modules/project-factory/google"
1313
version = "~> 12.0"
1414

15-
name = "prj-network2"
15+
name = "prj-network2-diff-name"
16+
project_id = "prj-network2"
1617
org_id = var.org_id
1718

19+
enable_shared_vpc_host_project = true
1820
billing_account = var.billing_account
1921
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2022 Google LLC
2+
#
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+
apiVersion: compute.cnrm.cloud.google.com/v1beta1
15+
kind: ComputeSharedVPCHostProject
16+
metadata:
17+
name: prj-network-host
18+
namespace: networking # kpt-set: ${namespace}
19+
annotations:
20+
cnrm.cloud.google.com/project-id: prj-network
21+
cnrm.cloud.google.com/blueprint: cnrm/landing-zone:networking/v0.4.1

functions/go/export-terraform/testdata/network/tf/projects.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module "prj-network" {
55
name = "prj-network"
66
org_id = var.org_id
77

8+
enable_shared_vpc_host_project = true
89
billing_account = var.billing_account
910
}

0 commit comments

Comments
 (0)