Skip to content

Commit

Permalink
GatewayConnection autogenerated files
Browse files Browse the repository at this point in the history
Generated with command line params below:
GatewayConnection --no-api-wrapper

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jul 25, 2024
1 parent e22edab commit edaa039
Show file tree
Hide file tree
Showing 3 changed files with 458 additions and 0 deletions.
210 changes: 210 additions & 0 deletions nsxt/resource_nsxt_policy_gateway_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/* Copyright © 2024 Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"log"
"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"
"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"
)

var gatewayConnectionSchema = map[string]*metadata.ExtendedSchema{
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()),
"path": metadata.GetExtendedSchema(getPathSchema()),
"display_name": metadata.GetExtendedSchema(getDisplayNameSchema()),
"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,
},
Metadata: metadata.Metadata{
SchemaType: "string",
SdkFieldName: "AdvertiseOutboundRouteFilter",
},
},
"tier0_path": {
Schema: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validatePolicyPath(),
Optional: true,
},
Metadata: metadata.Metadata{
SchemaType: "string",
SdkFieldName: "Tier0Path",
},
},
"aggregate_routes": {
Schema: schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
Metadata: metadata.Metadata{
SchemaType: "array",
SdkFieldName: "AggregateRoutes",
},
},
}

func resourceNsxtPolicyGatewayConnection() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtPolicyGatewayConnectionCreate,
Read: resourceNsxtPolicyGatewayConnectionRead,
Update: resourceNsxtPolicyGatewayConnectionUpdate,
Delete: resourceNsxtPolicyGatewayConnectionDelete,
Importer: &schema.ResourceImporter{
State: nsxtPolicyPathResourceImporter,
},
Schema: metadata.GetSchemaFromExtendedSchema(gatewayConnectionSchema),
}
}

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

client := clientLayer.NewGatewayConnectionsClient(sessionContext, connector)
_, err = client.Get(id)
if err == nil {
return true, nil
}

if isNotFoundError(err) {
return false, nil
}

return false, logAPIError("Error retrieving resource", err)
}

func resourceNsxtPolicyGatewayConnectionCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id, err := getOrGenerateIDWithParent(d, m, resourceNsxtPolicyGatewayConnectionExists)
if err != nil {
return err
}

displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getPolicyTagsFromSchema(d)

obj := model.GatewayConnection{
DisplayName: &displayName,
Description: &description,
Tags: tags,
}

elem := reflect.ValueOf(&obj).Elem()
if err := metadata.SchemaToStruct(elem, d, gatewayConnectionSchema, "", nil); err != nil {
return err
}

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

client := clientLayer.NewGatewayConnectionsClient(getSessionContext(d, m), connector)
err = client.Patch(id, obj)
if err != nil {
return handleCreateError("GatewayConnection", id, err)
}
d.SetId(id)
d.Set("nsx_id", id)

return resourceNsxtPolicyGatewayConnectionRead(d, m)
}

func resourceNsxtPolicyGatewayConnectionRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining GatewayConnection ID")
}

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

obj, err := client.Get(id)
if err != nil {
return handleReadError(d, "GatewayConnection", id, err)
}

setPolicyTagsInSchema(d, obj.Tags)
d.Set("nsx_id", id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
d.Set("revision", obj.Revision)
d.Set("path", obj.Path)

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

return nil
}

func resourceNsxtPolicyGatewayConnectionUpdate(d *schema.ResourceData, m interface{}) error {

connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining GatewayConnection ID")
}

description := d.Get("description").(string)
displayName := d.Get("display_name").(string)
tags := getPolicyTagsFromSchema(d)

revision := int64(d.Get("revision").(int))

obj := model.GatewayConnection{
DisplayName: &displayName,
Description: &description,
Tags: tags,
Revision: &revision,
}

elem := reflect.ValueOf(&obj).Elem()
if err := metadata.SchemaToStruct(elem, d, gatewayConnectionSchema, "", nil); err != nil {
return err
}
client := clientLayer.NewGatewayConnectionsClient(getSessionContext(d, m), connector)
_, err := client.Update(id, obj)
if err != nil {
return handleUpdateError("GatewayConnection", id, err)
}

return resourceNsxtPolicyGatewayConnectionRead(d, m)
}

func resourceNsxtPolicyGatewayConnectionDelete(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining GatewayConnection ID")
}

connector := getPolicyConnector(m)

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

if err != nil {
return handleDeleteError("GatewayConnection", id, err)
}

return nil
}
186 changes: 186 additions & 0 deletions nsxt/resource_nsxt_policy_gateway_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/* Copyright © 2024 Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

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",
}

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",
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyGatewayConnectionCheckDestroy(state, accTestPolicyGatewayConnectionUpdateAttributes["display_name"])
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyGatewayConnectionTemplate(true),
Check: resource.ComposeTestCheckFunc(
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, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
),
},
{
Config: testAccNsxtPolicyGatewayConnectionTemplate(false),
Check: resource.ComposeTestCheckFunc(
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, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
),
},
{
Config: testAccNsxtPolicyGatewayConnectionMinimalistic(),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGatewayConnectionExists(accTestPolicyGatewayConnectionCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "description", ""),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
),
},
},
})
}

func TestAccResourceNsxtPolicyGatewayConnection_importBasic(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_gateway_connection.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyGatewayConnectionCheckDestroy(state, name)
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyGatewayConnectionMinimalistic(),
},
{
ResourceName: testResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccNsxtPolicyGatewayConnectionExists(displayName string, resourceName string) resource.TestCheckFunc {
return func(state *terraform.State) error {

connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))

rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Policy GatewayConnection resource %s not found in resources", resourceName)
}

resourceID := rs.Primary.ID
if resourceID == "" {
return fmt.Errorf("Policy GatewayConnection resource ID not set in resources")
}

exists, err := resourceNsxtPolicyGatewayConnectionExists(resourceID, connector, testAccIsGlobalManager())
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Policy GatewayConnection %s does not exist", resourceID)
}

return nil
}
}

func testAccNsxtPolicyGatewayConnectionCheckDestroy(state *terraform.State, displayName string) error {
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
for _, rs := range state.RootModule().Resources {

if rs.Type != "nsxt_policy_gateway_connection" {
continue
}

resourceID := rs.Primary.Attributes["id"]
exists, err := resourceNsxtPolicyGatewayConnectionExists(resourceID, connector, testAccIsGlobalManager())
if err == nil {
return err
}

if exists {
return fmt.Errorf("Policy GatewayConnection %s still exists", displayName)
}
}
return nil
}

func testAccNsxtPolicyGatewayConnectionTemplate(createFlow bool) string {
var attrMap map[string]string
if createFlow {
attrMap = accTestPolicyGatewayConnectionCreateAttributes
} else {
attrMap = accTestPolicyGatewayConnectionUpdateAttributes
}
return fmt.Sprintf(`
resource "nsxt_policy_gateway_connection" "test" {
display_name = "%s"
description = "%s"
advertise_outbound_route_filter = %s
tier0_path = %s
aggregate_routes = [%s]
tag {
scope = "scope1"
tag = "tag1"
}
}`, attrMap["display_name"], attrMap["description"], attrMap["advertise_outbound_route_filter"], attrMap["tier0_path"], attrMap["aggregate_routes"])
}

func testAccNsxtPolicyGatewayConnectionMinimalistic() string {
return fmt.Sprintf(`
resource "nsxt_policy_gateway_connection" "test" {
display_name = "%s"
}`, accTestPolicyGatewayConnectionUpdateAttributes["display_name"])
}
Loading

0 comments on commit edaa039

Please sign in to comment.