Skip to content

Commit a76a130

Browse files
[AlloyDB] PSC Outbound Connectivity Support (#13223) (#21701)
[upstream:5be776873ae9bc044f8a486649321dbd5817625b] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent a19158d commit a76a130

5 files changed

+174
-0
lines changed

.changelog/13223.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
alloydb: added `psc_instance_config.psc_interface_configs` field to ``google_alloydb_instance` resource
3+
```

google/services/alloydb/resource_alloydb_instance.go

+74
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,23 @@ These should be specified as project numbers only.`,
240240
ValidateFunc: verify.ValidateRegexp(`^\d+$`),
241241
},
242242
},
243+
"psc_interface_configs": {
244+
Type: schema.TypeList,
245+
Optional: true,
246+
Description: `Configurations for setting up PSC interfaces attached to the instance
247+
which are used for outbound connectivity. Currently, AlloyDB supports only 0 or 1 PSC interface.`,
248+
Elem: &schema.Resource{
249+
Schema: map[string]*schema.Schema{
250+
"network_attachment_resource": {
251+
Type: schema.TypeString,
252+
Optional: true,
253+
Description: `The network attachment resource created in the consumer project to which the PSC interface will be linked.
254+
This is of the format: "projects/${CONSUMER_PROJECT}/regions/${REGION}/networkAttachments/${NETWORK_ATTACHMENT_NAME}".
255+
The network attachment must be in the same region as the instance.`,
256+
},
257+
},
258+
},
259+
},
243260
"psc_dns_name": {
244261
Type: schema.TypeString,
245262
Computed: true,
@@ -1147,6 +1164,8 @@ func flattenAlloydbInstancePscInstanceConfig(v interface{}, d *schema.ResourceDa
11471164
flattenAlloydbInstancePscInstanceConfigAllowedConsumerProjects(original["allowedConsumerProjects"], d, config)
11481165
transformed["psc_dns_name"] =
11491166
flattenAlloydbInstancePscInstanceConfigPscDnsName(original["pscDnsName"], d, config)
1167+
transformed["psc_interface_configs"] =
1168+
flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigs(original["pscInterfaceConfigs"], d, config)
11501169
return []interface{}{transformed}
11511170
}
11521171
func flattenAlloydbInstancePscInstanceConfigServiceAttachmentLink(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
@@ -1161,6 +1180,28 @@ func flattenAlloydbInstancePscInstanceConfigPscDnsName(v interface{}, d *schema.
11611180
return v
11621181
}
11631182

1183+
func flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigs(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1184+
if v == nil {
1185+
return v
1186+
}
1187+
l := v.([]interface{})
1188+
transformed := make([]interface{}, 0, len(l))
1189+
for _, raw := range l {
1190+
original := raw.(map[string]interface{})
1191+
if len(original) < 1 {
1192+
// Do not include empty json objects coming back from the api
1193+
continue
1194+
}
1195+
transformed = append(transformed, map[string]interface{}{
1196+
"network_attachment_resource": flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(original["networkAttachmentResource"], d, config),
1197+
})
1198+
}
1199+
return transformed
1200+
}
1201+
func flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1202+
return v
1203+
}
1204+
11641205
func flattenAlloydbInstanceNetworkConfig(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
11651206
if v == nil {
11661207
return nil
@@ -1451,6 +1492,13 @@ func expandAlloydbInstancePscInstanceConfig(v interface{}, d tpgresource.Terrafo
14511492
transformed["pscDnsName"] = transformedPscDnsName
14521493
}
14531494

1495+
transformedPscInterfaceConfigs, err := expandAlloydbInstancePscInstanceConfigPscInterfaceConfigs(original["psc_interface_configs"], d, config)
1496+
if err != nil {
1497+
return nil, err
1498+
} else if val := reflect.ValueOf(transformedPscInterfaceConfigs); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1499+
transformed["pscInterfaceConfigs"] = transformedPscInterfaceConfigs
1500+
}
1501+
14541502
return transformed, nil
14551503
}
14561504

@@ -1466,6 +1514,32 @@ func expandAlloydbInstancePscInstanceConfigPscDnsName(v interface{}, d tpgresour
14661514
return v, nil
14671515
}
14681516

1517+
func expandAlloydbInstancePscInstanceConfigPscInterfaceConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1518+
l := v.([]interface{})
1519+
req := make([]interface{}, 0, len(l))
1520+
for _, raw := range l {
1521+
if raw == nil {
1522+
continue
1523+
}
1524+
original := raw.(map[string]interface{})
1525+
transformed := make(map[string]interface{})
1526+
1527+
transformedNetworkAttachmentResource, err := expandAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(original["network_attachment_resource"], d, config)
1528+
if err != nil {
1529+
return nil, err
1530+
} else if val := reflect.ValueOf(transformedNetworkAttachmentResource); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1531+
transformed["networkAttachmentResource"] = transformedNetworkAttachmentResource
1532+
}
1533+
1534+
req = append(req, transformed)
1535+
}
1536+
return req, nil
1537+
}
1538+
1539+
func expandAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1540+
return v, nil
1541+
}
1542+
14691543
func expandAlloydbInstanceNetworkConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
14701544
l := v.([]interface{})
14711545
if len(l) == 0 || l[0] == nil {

google/services/alloydb/resource_alloydb_instance_generated_meta.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fields:
3232
- field: 'outbound_public_ip_addresses'
3333
- field: 'psc_instance_config.allowed_consumer_projects'
3434
- field: 'psc_instance_config.psc_dns_name'
35+
- field: 'psc_instance_config.psc_interface_configs.network_attachment_resource'
3536
- field: 'psc_instance_config.service_attachment_link'
3637
- field: 'public_ip_address'
3738
- field: 'query_insights_config.query_plans_per_minute'

google/services/alloydb/resource_alloydb_instance_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,84 @@ resource "google_alloydb_cluster" "default" {
829829
data "google_project" "project" {}
830830
`, context)
831831
}
832+
833+
func TestAccAlloydbInstance_createInstanceWithPscInterfaceConfigs(t *testing.T) {
834+
t.Parallel()
835+
836+
networkName := acctest.BootstrapSharedTestNetwork(t, "tf-test-alloydb-network")
837+
subnetworkName := acctest.BootstrapSubnet(t, "tf-test-alloydb-subnetwork", networkName)
838+
839+
random_suffix := acctest.RandString(t, 10)
840+
context := map[string]interface{}{
841+
"random_suffix": random_suffix,
842+
"networkAttachmentName": acctest.BootstrapNetworkAttachment(t, "tf-test-alloydb-create-na", subnetworkName),
843+
}
844+
845+
acctest.VcrTest(t, resource.TestCase{
846+
PreCheck: func() { acctest.AccTestPreCheck(t) },
847+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
848+
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
849+
Steps: []resource.TestStep{
850+
{
851+
Config: testAccAlloydbInstance_pscInterfaceConfigs(context),
852+
},
853+
},
854+
})
855+
}
856+
857+
func testAccAlloydbInstance_pscInterfaceConfigs(context map[string]interface{}) string {
858+
return acctest.Nprintf(`
859+
resource "google_alloydb_instance" "default" {
860+
cluster = google_alloydb_cluster.default.name
861+
instance_id = "tf-test-alloydb-instance%{random_suffix}"
862+
instance_type = "PRIMARY"
863+
machine_config {
864+
cpu_count = 2
865+
}
866+
psc_instance_config {
867+
allowed_consumer_projects = ["${data.google_project.project.number}"]
868+
psc_interface_configs {
869+
network_attachment_resource = "projects/${data.google_project.project.number}/regions/${google_alloydb_cluster.default.location}/networkAttachments/%{networkAttachmentName}"
870+
}
871+
}
872+
}
873+
resource "google_alloydb_cluster" "default" {
874+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
875+
location = "us-central1"
876+
psc_config {
877+
psc_enabled = true
878+
}
879+
initial_user {
880+
password = "tf-test-alloydb-cluster%{random_suffix}"
881+
}
882+
}
883+
data "google_project" "project" {}
884+
`, context)
885+
}
886+
887+
func TestAccAlloydbInstance_updateInstanceWithPscInterfaceConfigs(t *testing.T) {
888+
t.Parallel()
889+
890+
networkName := acctest.BootstrapSharedTestNetwork(t, "tf-test-alloydb-network")
891+
subnetworkName := acctest.BootstrapSubnet(t, "tf-test-alloydb-subnetwork", networkName)
892+
893+
random_suffix := acctest.RandString(t, 10)
894+
context := map[string]interface{}{
895+
"random_suffix": random_suffix,
896+
"networkAttachmentName": acctest.BootstrapNetworkAttachment(t, "tf-test-alloydb-update-na", subnetworkName),
897+
}
898+
899+
acctest.VcrTest(t, resource.TestCase{
900+
PreCheck: func() { acctest.AccTestPreCheck(t) },
901+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
902+
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
903+
Steps: []resource.TestStep{
904+
{
905+
Config: testAccAlloydbInstance_pscInstanceConfig(context),
906+
},
907+
{
908+
Config: testAccAlloydbInstance_pscInterfaceConfigs(context),
909+
},
910+
},
911+
})
912+
}

website/docs/r/alloydb_instance.html.markdown

+15
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,21 @@ The following arguments are supported:
367367
The DNS name of the instance for PSC connectivity.
368368
Name convention: <uid>.<uid>.<region>.alloydb-psc.goog
369369

370+
* `psc_interface_configs` -
371+
(Optional)
372+
Configurations for setting up PSC interfaces attached to the instance
373+
which are used for outbound connectivity. Currently, AlloyDB supports only 0 or 1 PSC interface.
374+
Structure is [documented below](#nested_psc_instance_config_psc_interface_configs).
375+
376+
377+
<a name="nested_psc_instance_config_psc_interface_configs"></a>The `psc_interface_configs` block supports:
378+
379+
* `network_attachment_resource` -
380+
(Optional)
381+
The network attachment resource created in the consumer project to which the PSC interface will be linked.
382+
This is of the format: "projects/${CONSUMER_PROJECT}/regions/${REGION}/networkAttachments/${NETWORK_ATTACHMENT_NAME}".
383+
The network attachment must be in the same region as the instance.
384+
370385
<a name="nested_network_config"></a>The `network_config` block supports:
371386

372387
* `authorized_external_networks` -

0 commit comments

Comments
 (0)