Skip to content

Commit

Permalink
TF Support for NCC Star Topology (#12540)
Browse files Browse the repository at this point in the history
[upstream:765f91ac4678f6684ce41cf5c296351be9f8ffbc]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician committed Dec 17, 2024
1 parent fd6ab7b commit eaa86e6
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/12540.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `preset_topology ` field to `google_network_connectivity_hub` resource
```
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions website/docs/r/network_connectivity_hub.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ resource "google_network_connectivity_hub" "primary" {
export_psc = true
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=network_connectivity_hub_mesh_topology&open_in_editor=main.tf" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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"
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=network_connectivity_hub_star_topology&open_in_editor=main.tf" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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

Expand All @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions website/docs/r/network_connectivity_spoke.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,51 @@ resource "google_network_connectivity_spoke" "primary" {
depends_on = [google_network_connectivity_spoke.linked_vpc_spoke]
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=network_connectivity_spoke_center_group&open_in_editor=main.tf" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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

Expand Down

0 comments on commit eaa86e6

Please sign in to comment.