Skip to content

Commit

Permalink
Manual edits for GatewayConnection
Browse files Browse the repository at this point in the history
Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jul 29, 2024
1 parent edaa039 commit 61dbcf3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 47 deletions.
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ func Provider() *schema.Provider {
"nsxt_vpc_gateway_policy": resourceNsxtVPCGatewayPolicy(),
"nsxt_policy_share": resourceNsxtPolicyShare(),
"nsxt_policy_shared_resource": resourceNsxtPolicySharedResource(),
"nsxt_policy_gateway_connection": resourceNsxtPolicyGatewayConnection(),
},

ConfigureFunc: providerConfigure,
Expand Down
27 changes: 11 additions & 16 deletions nsxt/resource_nsxt_policy_gateway_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
clientLayer "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

clientLayer "github.com/vmware/terraform-provider-nsxt/api/infra"
utl "github.com/vmware/terraform-provider-nsxt/api/utl"
"github.com/vmware/terraform-provider-nsxt/nsxt/metadata"
)

Expand All @@ -25,12 +23,11 @@ var gatewayConnectionSchema = map[string]*metadata.ExtendedSchema{
"description": metadata.GetExtendedSchema(getDescriptionSchema()),
"revision": metadata.GetExtendedSchema(getRevisionSchema()),
"tag": metadata.GetExtendedSchema(getTagsSchema()),
"context": metadata.GetExtendedSchema(getContextSchema(false, false, false)),
"context": metadata.GetExtendedSchema(getContextSchema(false, false, false)),
"advertise_outbound_route_filter": {
Schema: schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
ValidateFunc: validatePolicyPath(),
Optional: true,
},
Metadata: metadata.Metadata{
SchemaType: "string",
Expand Down Expand Up @@ -76,10 +73,10 @@ func resourceNsxtPolicyGatewayConnection() *schema.Resource {
}
}

func resourceNsxtPolicyGatewayConnectionExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
func resourceNsxtPolicyGatewayConnectionExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) {
var err error

client := clientLayer.NewGatewayConnectionsClient(sessionContext, connector)
client := clientLayer.NewGatewayConnectionsClient(connector)
_, err = client.Get(id)
if err == nil {
return true, nil
Expand All @@ -95,7 +92,7 @@ func resourceNsxtPolicyGatewayConnectionExists(sessionContext utl.SessionContext
func resourceNsxtPolicyGatewayConnectionCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id, err := getOrGenerateIDWithParent(d, m, resourceNsxtPolicyGatewayConnectionExists)
id, err := getOrGenerateID(d, m, resourceNsxtPolicyGatewayConnectionExists)
if err != nil {
return err
}
Expand All @@ -117,7 +114,7 @@ func resourceNsxtPolicyGatewayConnectionCreate(d *schema.ResourceData, m interfa

log.Printf("[INFO] Creating GatewayConnection with ID %s", id)

client := clientLayer.NewGatewayConnectionsClient(getSessionContext(d, m), connector)
client := clientLayer.NewGatewayConnectionsClient(connector)
err = client.Patch(id, obj)
if err != nil {
return handleCreateError("GatewayConnection", id, err)
Expand All @@ -136,7 +133,7 @@ func resourceNsxtPolicyGatewayConnectionRead(d *schema.ResourceData, m interface
return fmt.Errorf("Error obtaining GatewayConnection ID")
}

client := clientLayer.NewGatewayConnectionsClient(getSessionContext(d, m), connector)
client := clientLayer.NewGatewayConnectionsClient(connector)

obj, err := client.Get(id)
if err != nil {
Expand All @@ -152,8 +149,6 @@ func resourceNsxtPolicyGatewayConnectionRead(d *schema.ResourceData, m interface

elem := reflect.ValueOf(&obj).Elem()
return metadata.StructToSchema(elem, d, gatewayConnectionSchema, "", nil)

return nil
}

func resourceNsxtPolicyGatewayConnectionUpdate(d *schema.ResourceData, m interface{}) error {
Expand Down Expand Up @@ -182,7 +177,7 @@ func resourceNsxtPolicyGatewayConnectionUpdate(d *schema.ResourceData, m interfa
if err := metadata.SchemaToStruct(elem, d, gatewayConnectionSchema, "", nil); err != nil {
return err
}
client := clientLayer.NewGatewayConnectionsClient(getSessionContext(d, m), connector)
client := clientLayer.NewGatewayConnectionsClient(connector)
_, err := client.Update(id, obj)
if err != nil {
return handleUpdateError("GatewayConnection", id, err)
Expand All @@ -199,7 +194,7 @@ func resourceNsxtPolicyGatewayConnectionDelete(d *schema.ResourceData, m interfa

connector := getPolicyConnector(m)

client := clientLayer.NewGatewayConnectionsClient(getSessionContext(d, m), connector)
client := clientLayer.NewGatewayConnectionsClient(connector)
err := client.Delete(id)

if err != nil {
Expand Down
48 changes: 28 additions & 20 deletions nsxt/resource_nsxt_policy_gateway_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@ import (
)

var accTestPolicyGatewayConnectionCreateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform created",
"advertise_outbound_route_filter": "test-create",
"tier0_path": "test-create",
"aggregate_routes": "test-create",
"display_name": getAccTestResourceName(),
"description": "terraform created",
"aggregate_routes": "192.168.240.0/24",
}

var accTestPolicyGatewayConnectionUpdateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform updated",
"advertise_outbound_route_filter": "test-update",
"tier0_path": "test-update",
"aggregate_routes": "test-update",
"display_name": getAccTestResourceName(),
"description": "terraform updated",
"aggregate_routes": "192.168.241.0/24",
}

func TestAccResourceNsxtPolicyGatewayConnection_basic(t *testing.T) {
testResourceName := "nsxt_policy_gateway_connection.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() {
testAccPreCheck(t)
testAccOnlyVPC(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyGatewayConnectionCheckDestroy(state, accTestPolicyGatewayConnectionUpdateAttributes["display_name"])
Expand All @@ -43,9 +42,8 @@ func TestAccResourceNsxtPolicyGatewayConnection_basic(t *testing.T) {
testAccNsxtPolicyGatewayConnectionExists(accTestPolicyGatewayConnectionCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyGatewayConnectionCreateAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyGatewayConnectionCreateAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "advertise_outbound_route_filter", accTestPolicyGatewayConnectionCreateAttributes["advertise_outbound_route_filter"]),
resource.TestCheckResourceAttr(testResourceName, "tier0_path", accTestPolicyGatewayConnectionCreateAttributes["tier0_path"]),
resource.TestCheckResourceAttr(testResourceName, "aggregate_routes.0", accTestPolicyGatewayConnectionCreateAttributes["aggregate_routes"]),
resource.TestCheckResourceAttrSet(testResourceName, "tier0_path"),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand All @@ -59,9 +57,8 @@ func TestAccResourceNsxtPolicyGatewayConnection_basic(t *testing.T) {
testAccNsxtPolicyGatewayConnectionExists(accTestPolicyGatewayConnectionUpdateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyGatewayConnectionUpdateAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyGatewayConnectionUpdateAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "advertise_outbound_route_filter", accTestPolicyGatewayConnectionUpdateAttributes["advertise_outbound_route_filter"]),
resource.TestCheckResourceAttr(testResourceName, "tier0_path", accTestPolicyGatewayConnectionUpdateAttributes["tier0_path"]),
resource.TestCheckResourceAttr(testResourceName, "aggregate_routes.0", accTestPolicyGatewayConnectionUpdateAttributes["aggregate_routes"]),
resource.TestCheckResourceAttrSet(testResourceName, "tier0_path"),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
Expand Down Expand Up @@ -89,7 +86,10 @@ func TestAccResourceNsxtPolicyGatewayConnection_importBasic(t *testing.T) {
testResourceName := "nsxt_policy_gateway_connection.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() {
testAccPreCheck(t)
testAccOnlyVPC(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyGatewayConnectionCheckDestroy(state, name)
Expand Down Expand Up @@ -163,18 +163,26 @@ func testAccNsxtPolicyGatewayConnectionTemplate(createFlow bool) string {
attrMap = accTestPolicyGatewayConnectionUpdateAttributes
}
return fmt.Sprintf(`
data "nsxt_policy_edge_cluster" "EC" {
display_name = "%s"
}
resource "nsxt_policy_tier0_gateway" "test" {
display_name = "terraformt0gw"
edge_cluster_path = data.nsxt_policy_edge_cluster.EC.path
}
resource "nsxt_policy_gateway_connection" "test" {
display_name = "%s"
description = "%s"
advertise_outbound_route_filter = %s
tier0_path = %s
aggregate_routes = [%s]
tier0_path = nsxt_policy_tier0_gateway.test.path
aggregate_routes = ["%s"]
tag {
scope = "scope1"
tag = "tag1"
}
}`, attrMap["display_name"], attrMap["description"], attrMap["advertise_outbound_route_filter"], attrMap["tier0_path"], attrMap["aggregate_routes"])
}`, getEdgeClusterName(), attrMap["display_name"], attrMap["description"], attrMap["aggregate_routes"])
}

func testAccNsxtPolicyGatewayConnectionMinimalistic() string {
Expand Down
23 changes: 12 additions & 11 deletions website/docs/r/policy_gateway_connection.html.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
subcategory: "FIXME"
subcategory: "Beta"
layout: "nsxt"
page_title: "NSXT: nsxt_policy_gateway_connection"
description: A resource to configure a GatewayConnection.
Expand All @@ -9,17 +9,20 @@ description: A resource to configure a GatewayConnection.

This resource provides a method for the management of a GatewayConnection.

This resource is applicable to NSX Global Manager, NSX Policy Manager and VMC.
This resource is applicable to NSX Policy Manager.

## Example Usage

```hcl
data "nsxt_policy_tier0_gateway" "test" {
display_name = "test-t0gw"
}
resource "nsxt_policy_gateway_connection" "test" {
display_name = "test"
description = "Terraform provisioned GatewayConnection"
advertise_outbound_route_filter = FILL VALUE FOR schema.TypeString
tier0_path = FILL VALUE FOR schema.TypeString
aggregate_routes = FILL VALUE FOR schema.TypeString
display_name = "test"
description = "Terraform provisioned GatewayConnection"
tier0_path = data.nsxt_policy_tier0_gateway.test.path
aggregate_routes = ["192.168.240.0/24"]
}
```
Expand All @@ -33,9 +36,7 @@ The following arguments are supported:
* `tag` - (Optional) A list of scope + tag pairs to associate with this resource.
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
* `advertise_outbound_route_filter` - (Optional) Path of a prefixlist object that will have Transit gateway to tier-0 gateway advertise route filter.

* `tier0_path` - (Optional) Tier-0 gateway object path

* `aggregate_routes` - (Optional) Configure aggregate TGW_PREFIXES routes on Tier-0 gateway for prefixes owned by TGW gateway.
If not specified then in-use prefixes are configured as TGW_PREFIXES routes on Tier-0 gateway.

Expand All @@ -56,7 +57,7 @@ An existing object can be [imported][docs-import] into this resource, via the fo
[docs-import]: https://www.terraform.io/cli/import

```
terraform import nsxt_policy_gateway_connection.test UUID
terraform import nsxt_policy_gateway_connection.test PATH
```

The above command imports GatewayConnection named `test` with the NSX ID `UUID`.
The above command imports GatewayConnection named `test` with the policy path `PATH`.

0 comments on commit 61dbcf3

Please sign in to comment.