Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AlloyDB] PSC Outbound Connectivity Support #21701

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/13223.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
alloydb: added `psc_instance_config.psc_interface_configs` field to ``google_alloydb_instance` resource
```
74 changes: 74 additions & 0 deletions google/services/alloydb/resource_alloydb_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ These should be specified as project numbers only.`,
ValidateFunc: verify.ValidateRegexp(`^\d+$`),
},
},
"psc_interface_configs": {
Type: schema.TypeList,
Optional: true,
Description: `Configurations for setting up PSC interfaces attached to the instance
which are used for outbound connectivity. Currently, AlloyDB supports only 0 or 1 PSC interface.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"network_attachment_resource": {
Type: schema.TypeString,
Optional: true,
Description: `The network attachment resource created in the consumer project to which the PSC interface will be linked.
This is of the format: "projects/${CONSUMER_PROJECT}/regions/${REGION}/networkAttachments/${NETWORK_ATTACHMENT_NAME}".
The network attachment must be in the same region as the instance.`,
},
},
},
},
"psc_dns_name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1147,6 +1164,8 @@ func flattenAlloydbInstancePscInstanceConfig(v interface{}, d *schema.ResourceDa
flattenAlloydbInstancePscInstanceConfigAllowedConsumerProjects(original["allowedConsumerProjects"], d, config)
transformed["psc_dns_name"] =
flattenAlloydbInstancePscInstanceConfigPscDnsName(original["pscDnsName"], d, config)
transformed["psc_interface_configs"] =
flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigs(original["pscInterfaceConfigs"], d, config)
return []interface{}{transformed}
}
func flattenAlloydbInstancePscInstanceConfigServiceAttachmentLink(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand All @@ -1161,6 +1180,28 @@ func flattenAlloydbInstancePscInstanceConfigPscDnsName(v interface{}, d *schema.
return v
}

func flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigs(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"network_attachment_resource": flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(original["networkAttachmentResource"], d, config),
})
}
return transformed
}
func flattenAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenAlloydbInstanceNetworkConfig(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -1451,6 +1492,13 @@ func expandAlloydbInstancePscInstanceConfig(v interface{}, d tpgresource.Terrafo
transformed["pscDnsName"] = transformedPscDnsName
}

transformedPscInterfaceConfigs, err := expandAlloydbInstancePscInstanceConfigPscInterfaceConfigs(original["psc_interface_configs"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPscInterfaceConfigs); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["pscInterfaceConfigs"] = transformedPscInterfaceConfigs
}

return transformed, nil
}

Expand All @@ -1466,6 +1514,32 @@ func expandAlloydbInstancePscInstanceConfigPscDnsName(v interface{}, d tpgresour
return v, nil
}

func expandAlloydbInstancePscInstanceConfigPscInterfaceConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedNetworkAttachmentResource, err := expandAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(original["network_attachment_resource"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedNetworkAttachmentResource); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["networkAttachmentResource"] = transformedNetworkAttachmentResource
}

req = append(req, transformed)
}
return req, nil
}

func expandAlloydbInstancePscInstanceConfigPscInterfaceConfigsNetworkAttachmentResource(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbInstanceNetworkConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fields:
- field: 'outbound_public_ip_addresses'
- field: 'psc_instance_config.allowed_consumer_projects'
- field: 'psc_instance_config.psc_dns_name'
- field: 'psc_instance_config.psc_interface_configs.network_attachment_resource'
- field: 'psc_instance_config.service_attachment_link'
- field: 'public_ip_address'
- field: 'query_insights_config.query_plans_per_minute'
Expand Down
81 changes: 81 additions & 0 deletions google/services/alloydb/resource_alloydb_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,84 @@ resource "google_alloydb_cluster" "default" {
data "google_project" "project" {}
`, context)
}

func TestAccAlloydbInstance_createInstanceWithPscInterfaceConfigs(t *testing.T) {
t.Parallel()

networkName := acctest.BootstrapSharedTestNetwork(t, "tf-test-alloydb-network")
subnetworkName := acctest.BootstrapSubnet(t, "tf-test-alloydb-subnetwork", networkName)

random_suffix := acctest.RandString(t, 10)
context := map[string]interface{}{
"random_suffix": random_suffix,
"networkAttachmentName": acctest.BootstrapNetworkAttachment(t, "tf-test-alloydb-create-na", subnetworkName),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbInstance_pscInterfaceConfigs(context),
},
},
})
}

func testAccAlloydbInstance_pscInterfaceConfigs(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_alloydb_instance" "default" {
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"
machine_config {
cpu_count = 2
}
psc_instance_config {
allowed_consumer_projects = ["${data.google_project.project.number}"]
psc_interface_configs {
network_attachment_resource = "projects/${data.google_project.project.number}/regions/${google_alloydb_cluster.default.location}/networkAttachments/%{networkAttachmentName}"
}
}
}
resource "google_alloydb_cluster" "default" {
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
psc_config {
psc_enabled = true
}
initial_user {
password = "tf-test-alloydb-cluster%{random_suffix}"
}
}
data "google_project" "project" {}
`, context)
}

func TestAccAlloydbInstance_updateInstanceWithPscInterfaceConfigs(t *testing.T) {
t.Parallel()

networkName := acctest.BootstrapSharedTestNetwork(t, "tf-test-alloydb-network")
subnetworkName := acctest.BootstrapSubnet(t, "tf-test-alloydb-subnetwork", networkName)

random_suffix := acctest.RandString(t, 10)
context := map[string]interface{}{
"random_suffix": random_suffix,
"networkAttachmentName": acctest.BootstrapNetworkAttachment(t, "tf-test-alloydb-update-na", subnetworkName),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbInstance_pscInstanceConfig(context),
},
{
Config: testAccAlloydbInstance_pscInterfaceConfigs(context),
},
},
})
}
15 changes: 15 additions & 0 deletions website/docs/r/alloydb_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ The following arguments are supported:
The DNS name of the instance for PSC connectivity.
Name convention: <uid>.<uid>.<region>.alloydb-psc.goog

* `psc_interface_configs` -
(Optional)
Configurations for setting up PSC interfaces attached to the instance
which are used for outbound connectivity. Currently, AlloyDB supports only 0 or 1 PSC interface.
Structure is [documented below](#nested_psc_instance_config_psc_interface_configs).


<a name="nested_psc_instance_config_psc_interface_configs"></a>The `psc_interface_configs` block supports:

* `network_attachment_resource` -
(Optional)
The network attachment resource created in the consumer project to which the PSC interface will be linked.
This is of the format: "projects/${CONSUMER_PROJECT}/regions/${REGION}/networkAttachments/${NETWORK_ATTACHMENT_NAME}".
The network attachment must be in the same region as the instance.

<a name="nested_network_config"></a>The `network_config` block supports:

* `authorized_external_networks` -
Expand Down