From cfe4b119863e3faf3f2099395b140c0c04bd7993 Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 20 Oct 2022 08:34:40 -0700 Subject: [PATCH] Added private connectivity field to datastream_connection_profile (#6651) (#12844) * Added private connectivity field to datastream_connection_profile * Corrected example filename * Added missing example var * Corrected space -> tab inconsistency Signed-off-by: Modular Magician Signed-off-by: Modular Magician --- .changelog/6651.txt | 3 + .../resource_datastream_connection_profile.go | 76 +++++++++++++++++++ ...tream_connection_profile_generated_test.go | 29 ++++++- ...atastream_connection_profile.html.markdown | 38 +++++++++- 4 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 .changelog/6651.txt diff --git a/.changelog/6651.txt b/.changelog/6651.txt new file mode 100644 index 00000000000..1ae9ef838a5 --- /dev/null +++ b/.changelog/6651.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +datastream: added `private_connectivity` field to `google_datastream_connection_profile` +``` diff --git a/google/resource_datastream_connection_profile.go b/google/resource_datastream_connection_profile.go index 20dbba5ff79..726f172915d 100644 --- a/google/resource_datastream_connection_profile.go +++ b/google/resource_datastream_connection_profile.go @@ -110,6 +110,7 @@ func resourceDatastreamConnectionProfile() *schema.Resource { }, }, }, + ConflictsWith: []string{"private_connectivity"}, }, "gcs_profile": { Type: schema.TypeList, @@ -306,6 +307,22 @@ If this field is used then the 'client_certificate' and the }, ExactlyOneOf: []string{"oracle_profile", "gcs_profile", "mysql_profile", "bigquery_profile", "postgresql_profile"}, }, + "private_connectivity": { + Type: schema.TypeList, + Optional: true, + Description: `Private connectivity.`, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "private_connection": { + Type: schema.TypeString, + Required: true, + Description: `A reference to a private connection resource. Format: 'projects/{project}/locations/{location}/privateConnections/{name}'`, + }, + }, + }, + ConflictsWith: []string{"forward_ssh_connectivity"}, + }, "name": { Type: schema.TypeString, Computed: true, @@ -378,6 +395,12 @@ func resourceDatastreamConnectionProfileCreate(d *schema.ResourceData, meta inte } else if v, ok := d.GetOkExists("forward_ssh_connectivity"); !isEmptyValue(reflect.ValueOf(forwardSshConnectivityProp)) && (ok || !reflect.DeepEqual(v, forwardSshConnectivityProp)) { obj["forwardSshConnectivity"] = forwardSshConnectivityProp } + privateConnectivityProp, err := expandDatastreamConnectionProfilePrivateConnectivity(d.Get("private_connectivity"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("private_connectivity"); !isEmptyValue(reflect.ValueOf(privateConnectivityProp)) && (ok || !reflect.DeepEqual(v, privateConnectivityProp)) { + obj["privateConnectivity"] = privateConnectivityProp + } url, err := replaceVars(d, config, "{{DatastreamBasePath}}projects/{{project}}/locations/{{location}}/connectionProfiles?connectionProfileId={{connection_profile_id}}") if err != nil { @@ -500,6 +523,9 @@ func resourceDatastreamConnectionProfileRead(d *schema.ResourceData, meta interf if err := d.Set("forward_ssh_connectivity", flattenDatastreamConnectionProfileForwardSshConnectivity(res["forwardSshConnectivity"], d, config)); err != nil { return fmt.Errorf("Error reading ConnectionProfile: %s", err) } + if err := d.Set("private_connectivity", flattenDatastreamConnectionProfilePrivateConnectivity(res["privateConnectivity"], d, config)); err != nil { + return fmt.Errorf("Error reading ConnectionProfile: %s", err) + } return nil } @@ -568,6 +594,12 @@ func resourceDatastreamConnectionProfileUpdate(d *schema.ResourceData, meta inte } else if v, ok := d.GetOkExists("forward_ssh_connectivity"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, forwardSshConnectivityProp)) { obj["forwardSshConnectivity"] = forwardSshConnectivityProp } + privateConnectivityProp, err := expandDatastreamConnectionProfilePrivateConnectivity(d.Get("private_connectivity"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("private_connectivity"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, privateConnectivityProp)) { + obj["privateConnectivity"] = privateConnectivityProp + } url, err := replaceVars(d, config, "{{DatastreamBasePath}}projects/{{project}}/locations/{{location}}/connectionProfiles/{{connection_profile_id}}") if err != nil { @@ -608,6 +640,10 @@ func resourceDatastreamConnectionProfileUpdate(d *schema.ResourceData, meta inte if d.HasChange("forward_ssh_connectivity") { updateMask = append(updateMask, "forwardSshConnectivity") } + + if d.HasChange("private_connectivity") { + updateMask = append(updateMask, "privateConnectivity") + } // updateMask is a URL parameter but not present in the schema, so replaceVars // won't set it url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) @@ -1013,6 +1049,23 @@ func flattenDatastreamConnectionProfileForwardSshConnectivityPrivateKey(v interf return d.Get("forward_ssh_connectivity.0.private_key") } +func flattenDatastreamConnectionProfilePrivateConnectivity(v interface{}, d *schema.ResourceData, config *Config) interface{} { + if v == nil { + return nil + } + original := v.(map[string]interface{}) + if len(original) == 0 { + return nil + } + transformed := make(map[string]interface{}) + transformed["private_connection"] = + flattenDatastreamConnectionProfilePrivateConnectivityPrivateConnection(original["privateConnection"], d, config) + return []interface{}{transformed} +} +func flattenDatastreamConnectionProfilePrivateConnectivityPrivateConnection(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func expandDatastreamConnectionProfileLabels(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) { if v == nil { return map[string]string{}, nil @@ -1436,3 +1489,26 @@ func expandDatastreamConnectionProfileForwardSshConnectivityPassword(v interface func expandDatastreamConnectionProfileForwardSshConnectivityPrivateKey(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } + +func expandDatastreamConnectionProfilePrivateConnectivity(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + l := v.([]interface{}) + if len(l) == 0 || l[0] == nil { + return nil, nil + } + raw := l[0] + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedPrivateConnection, err := expandDatastreamConnectionProfilePrivateConnectivityPrivateConnection(original["private_connection"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedPrivateConnection); val.IsValid() && !isEmptyValue(val) { + transformed["privateConnection"] = transformedPrivateConnection + } + + return transformed, nil +} + +func expandDatastreamConnectionProfilePrivateConnectivityPrivateConnection(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_datastream_connection_profile_generated_test.go b/google/resource_datastream_connection_profile_generated_test.go index 65cf3ae021d..67b9756e93e 100644 --- a/google/resource_datastream_connection_profile_generated_test.go +++ b/google/resource_datastream_connection_profile_generated_test.go @@ -63,7 +63,7 @@ resource "google_datastream_connection_profile" "default" { `, context) } -func TestAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryExample(t *testing.T) { +func TestAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryPrivateConnectionExample(t *testing.T) { t.Parallel() context := map[string]interface{}{ @@ -76,7 +76,7 @@ func TestAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryExamp CheckDestroy: testAccCheckDatastreamConnectionProfileDestroyProducer(t), Steps: []resource.TestStep{ { - Config: testAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryExample(context), + Config: testAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryPrivateConnectionExample(context), }, { ResourceName: "google_datastream_connection_profile.default", @@ -88,14 +88,37 @@ func TestAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryExamp }) } -func testAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryExample(context map[string]interface{}) string { +func testAccDatastreamConnectionProfile_datastreamConnectionProfileBigqueryPrivateConnectionExample(context map[string]interface{}) string { return Nprintf(` +resource "google_datastream_private_connection" "private_connection" { + display_name = "Connection profile" + location = "us-central1" + private_connection_id = "tf-test-my-connection%{random_suffix}" + + labels = { + key = "value" + } + + vpc_peering_config { + vpc = google_compute_network.default.id + subnet = "10.0.0.0/29" + } +} + +resource "google_compute_network" "default" { + name = "tf-test-my-network%{random_suffix}" +} + resource "google_datastream_connection_profile" "default" { display_name = "Connection profile" location = "us-central1" connection_profile_id = "tf-test-my-profile%{random_suffix}" bigquery_profile {} + + private_connectivity { + private_connection = google_datastream_private_connection.private_connection.id + } } `, context) } diff --git a/website/docs/r/datastream_connection_profile.html.markdown b/website/docs/r/datastream_connection_profile.html.markdown index fb0385ec8f2..2680576828c 100644 --- a/website/docs/r/datastream_connection_profile.html.markdown +++ b/website/docs/r/datastream_connection_profile.html.markdown @@ -53,20 +53,43 @@ resource "google_datastream_connection_profile" "default" { } ``` -## Example Usage - Datastream Connection Profile Bigquery +## Example Usage - Datastream Connection Profile Bigquery Private Connection ```hcl +resource "google_datastream_private_connection" "private_connection" { + display_name = "Connection profile" + location = "us-central1" + private_connection_id = "my-connection" + + labels = { + key = "value" + } + + vpc_peering_config { + vpc = google_compute_network.default.id + subnet = "10.0.0.0/29" + } +} + +resource "google_compute_network" "default" { + name = "my-network" +} + resource "google_datastream_connection_profile" "default" { display_name = "Connection profile" location = "us-central1" connection_profile_id = "my-profile" bigquery_profile {} + + private_connectivity { + private_connection = google_datastream_private_connection.private_connection.id + } } ```
@@ -219,6 +242,11 @@ The following arguments are supported: Forward SSH tunnel connectivity. Structure is [documented below](#nested_forward_ssh_connectivity). +* `private_connectivity` - + (Optional) + Private connectivity. + Structure is [documented below](#nested_private_connectivity). + * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. @@ -364,6 +392,12 @@ The following arguments are supported: SSH private key. **Note**: This property is sensitive and will not be displayed in the plan. +The `private_connectivity` block supports: + +* `private_connection` - + (Required) + A reference to a private connection resource. Format: `projects/{project}/locations/{location}/privateConnections/{name}` + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are exported: