From eaa86e634516e4542081914422ddfa598f852ca0 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Tue, 17 Dec 2024 18:09:53 +0000 Subject: [PATCH] TF Support for NCC Star Topology (#12540) [upstream:765f91ac4678f6684ce41cf5c296351be9f8ffbc] Signed-off-by: Modular Magician --- .changelog/12540.txt | 3 + .../resource_network_connectivity_hub.go | 26 +++++++ ...network_connectivity_hub_generated_test.go | 76 +++++++++++++++++++ ...twork_connectivity_spoke_generated_test.go | 65 ++++++++++++++++ .../r/network_connectivity_hub.html.markdown | 41 ++++++++++ .../network_connectivity_spoke.html.markdown | 45 +++++++++++ 6 files changed, 256 insertions(+) create mode 100644 .changelog/12540.txt diff --git a/.changelog/12540.txt b/.changelog/12540.txt new file mode 100644 index 0000000000..42e27639d5 --- /dev/null +++ b/.changelog/12540.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: added `preset_topology ` field to `google_network_connectivity_hub` resource +``` \ No newline at end of file diff --git a/google-beta/services/networkconnectivity/resource_network_connectivity_hub.go b/google-beta/services/networkconnectivity/resource_network_connectivity_hub.go index 946a700173..68bd4c602d 100644 --- a/google-beta/services/networkconnectivity/resource_network_connectivity_hub.go +++ b/google-beta/services/networkconnectivity/resource_network_connectivity_hub.go @@ -30,6 +30,7 @@ import ( "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/verify" ) func ResourceNetworkConnectivityHub() *schema.Resource { @@ -83,6 +84,14 @@ func ResourceNetworkConnectivityHub() *schema.Resource { Please refer to the field 'effective_labels' for all of the labels present on the resource.`, Elem: &schema.Schema{Type: schema.TypeString}, }, + "preset_topology": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + ValidateFunc: verify.ValidateEnum([]string{"MESH", "STAR", ""}), + Description: `Optional. The topology implemented in this hub. Currently, this field is only used when policyMode = PRESET. The available preset topologies are MESH and STAR. If presetTopology is unspecified and policyMode = PRESET, the presetTopology defaults to MESH. When policyMode = CUSTOM, the presetTopology is set to PRESET_TOPOLOGY_UNSPECIFIED. Possible values: ["MESH", "STAR"]`, + }, "create_time": { Type: schema.TypeString, Computed: true, @@ -162,6 +171,12 @@ func resourceNetworkConnectivityHubCreate(d *schema.ResourceData, meta interface } else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { obj["description"] = descriptionProp } + presetTopologyProp, err := expandNetworkConnectivityHubPresetTopology(d.Get("preset_topology"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("preset_topology"); !tpgresource.IsEmptyValue(reflect.ValueOf(presetTopologyProp)) && (ok || !reflect.DeepEqual(v, presetTopologyProp)) { + obj["presetTopology"] = presetTopologyProp + } exportPscProp, err := expandNetworkConnectivityHubExportPsc(d.Get("export_psc"), d, config) if err != nil { return err @@ -297,6 +312,9 @@ func resourceNetworkConnectivityHubRead(d *schema.ResourceData, meta interface{} if err := d.Set("routing_vpcs", flattenNetworkConnectivityHubRoutingVpcs(res["routingVpcs"], d, config)); err != nil { return fmt.Errorf("Error reading Hub: %s", err) } + if err := d.Set("preset_topology", flattenNetworkConnectivityHubPresetTopology(res["presetTopology"], d, config)); err != nil { + return fmt.Errorf("Error reading Hub: %s", err) + } if err := d.Set("export_psc", flattenNetworkConnectivityHubExportPsc(res["exportPsc"], d, config)); err != nil { return fmt.Errorf("Error reading Hub: %s", err) } @@ -545,6 +563,10 @@ func flattenNetworkConnectivityHubRoutingVpcsUri(v interface{}, d *schema.Resour return v } +func flattenNetworkConnectivityHubPresetTopology(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + func flattenNetworkConnectivityHubExportPsc(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { return v } @@ -576,6 +598,10 @@ func expandNetworkConnectivityHubDescription(v interface{}, d tpgresource.Terraf return v, nil } +func expandNetworkConnectivityHubPresetTopology(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { + return v, nil +} + func expandNetworkConnectivityHubExportPsc(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { return v, nil } diff --git a/google-beta/services/networkconnectivity/resource_network_connectivity_hub_generated_test.go b/google-beta/services/networkconnectivity/resource_network_connectivity_hub_generated_test.go index ceb1169d03..db666dc5e5 100644 --- a/google-beta/services/networkconnectivity/resource_network_connectivity_hub_generated_test.go +++ b/google-beta/services/networkconnectivity/resource_network_connectivity_hub_generated_test.go @@ -102,6 +102,82 @@ resource "google_network_connectivity_hub" "primary" { `, context) } +func TestAccNetworkConnectivityHub_networkConnectivityHubMeshTopologyExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckNetworkConnectivityHubDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccNetworkConnectivityHub_networkConnectivityHubMeshTopologyExample(context), + }, + { + ResourceName: "google_network_connectivity_hub.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccNetworkConnectivityHub_networkConnectivityHubMeshTopologyExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_network_connectivity_hub" "primary" { + name = "mesh%{random_suffix}" + description = "A sample mesh hub" + labels = { + label-one = "value-one" + } +} +`, context) +} + +func TestAccNetworkConnectivityHub_networkConnectivityHubStarTopologyExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckNetworkConnectivityHubDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccNetworkConnectivityHub_networkConnectivityHubStarTopologyExample(context), + }, + { + ResourceName: "google_network_connectivity_hub.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"labels", "terraform_labels"}, + }, + }, + }) +} + +func testAccNetworkConnectivityHub_networkConnectivityHubStarTopologyExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_network_connectivity_hub" "primary" { + name = "star%{random_suffix}" + description = "A sample star hub" + labels = { + label-one = "value-one" + } + preset_topology = "STAR" + +} +`, context) +} + func testAccCheckNetworkConnectivityHubDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { for name, rs := range s.RootModule().Resources { diff --git a/google-beta/services/networkconnectivity/resource_network_connectivity_spoke_generated_test.go b/google-beta/services/networkconnectivity/resource_network_connectivity_spoke_generated_test.go index c3e101ded3..f3ea05aa3a 100644 --- a/google-beta/services/networkconnectivity/resource_network_connectivity_spoke_generated_test.go +++ b/google-beta/services/networkconnectivity/resource_network_connectivity_spoke_generated_test.go @@ -568,6 +568,71 @@ resource "google_network_connectivity_spoke" "primary" { `, context) } +func TestAccNetworkConnectivitySpoke_networkConnectivitySpokeCenterGroupExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckNetworkConnectivitySpokeDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccNetworkConnectivitySpoke_networkConnectivitySpokeCenterGroupExample(context), + }, + { + ResourceName: "google_network_connectivity_spoke.primary", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"hub", "labels", "location", "terraform_labels"}, + }, + }, + }) +} + +func testAccNetworkConnectivitySpoke_networkConnectivitySpokeCenterGroupExample(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_compute_network" "network" { + name = "tf-net" + auto_create_subnetworks = false +} + +resource "google_network_connectivity_hub" "star_hub" { + name = "tf-test-hub-basic%{random_suffix}" + preset_topology = "STAR" +} + +resource "google_network_connectivity_group" "center_group" { + name = "center" # (default , center , edge) + hub = google_network_connectivity_hub.star_hub.id + auto_accept { + auto_accept_projects = [ + "foo%{random_suffix}", + "bar%{random_suffix}", + ] + } +} + +resource "google_network_connectivity_spoke" "primary" { + name = "tf-test-vpc-spoke%{random_suffix}" + location = "global" + description = "A sample spoke" + labels = { + label-one = "value-one" + } + hub = google_network_connectivity_hub.star_hub.id + group = google_network_connectivity_group.center_group.id + + linked_vpc_network { + uri = google_compute_network.network.self_link + } +} +`, context) +} + func testAccCheckNetworkConnectivitySpokeDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { for name, rs := range s.RootModule().Resources { diff --git a/website/docs/r/network_connectivity_hub.html.markdown b/website/docs/r/network_connectivity_hub.html.markdown index b538d0fcd5..94328ab6c2 100644 --- a/website/docs/r/network_connectivity_hub.html.markdown +++ b/website/docs/r/network_connectivity_hub.html.markdown @@ -60,6 +60,42 @@ resource "google_network_connectivity_hub" "primary" { export_psc = true } ``` + +## Example Usage - Network Connectivity Hub Mesh Topology + + +```hcl +resource "google_network_connectivity_hub" "primary" { + name = "mesh" + description = "A sample mesh hub" + labels = { + label-one = "value-one" + } +} +``` + +## Example Usage - Network Connectivity Hub Star Topology + + +```hcl +resource "google_network_connectivity_hub" "primary" { + name = "star" + description = "A sample star hub" + labels = { + label-one = "value-one" + } + preset_topology = "STAR" + +} +``` ## Argument Reference @@ -84,6 +120,11 @@ The following arguments are supported: (Optional) An optional description of the hub. +* `preset_topology` - + (Optional) + Optional. The topology implemented in this hub. Currently, this field is only used when policyMode = PRESET. The available preset topologies are MESH and STAR. If presetTopology is unspecified and policyMode = PRESET, the presetTopology defaults to MESH. When policyMode = CUSTOM, the presetTopology is set to PRESET_TOPOLOGY_UNSPECIFIED. + Possible values are: `MESH`, `STAR`. + * `export_psc` - (Optional) Whether Private Service Connect transitivity is enabled for the hub. If true, Private Service Connect endpoints in VPC spokes attached to the hub are made accessible to other VPC spokes attached to the hub. The default value is false. diff --git a/website/docs/r/network_connectivity_spoke.html.markdown b/website/docs/r/network_connectivity_spoke.html.markdown index 4c63ed8c60..2dfaf10fca 100644 --- a/website/docs/r/network_connectivity_spoke.html.markdown +++ b/website/docs/r/network_connectivity_spoke.html.markdown @@ -445,6 +445,51 @@ resource "google_network_connectivity_spoke" "primary" { depends_on = [google_network_connectivity_spoke.linked_vpc_spoke] } ``` + +## Example Usage - Network Connectivity Spoke Center Group + + +```hcl +resource "google_compute_network" "network" { + name = "tf-net" + auto_create_subnetworks = false +} + +resource "google_network_connectivity_hub" "star_hub" { + name = "hub-basic" + preset_topology = "STAR" +} + +resource "google_network_connectivity_group" "center_group" { + name = "center" # (default , center , edge) + hub = google_network_connectivity_hub.star_hub.id + auto_accept { + auto_accept_projects = [ + "foo%{random_suffix}", + "bar%{random_suffix}", + ] + } +} + +resource "google_network_connectivity_spoke" "primary" { + name = "vpc-spoke" + location = "global" + description = "A sample spoke" + labels = { + label-one = "value-one" + } + hub = google_network_connectivity_hub.star_hub.id + group = google_network_connectivity_group.center_group.id + + linked_vpc_network { + uri = google_compute_network.network.self_link + } +} +``` ## Argument Reference