Skip to content

Commit

Permalink
Merge pull request #1259 from ksamoray/gateway-connection
Browse files Browse the repository at this point in the history
Gateway connection resource for VPC v2.0
  • Loading branch information
ksamoray authored Jul 30, 2024
2 parents e22edab + 61dbcf3 commit c07330b
Show file tree
Hide file tree
Showing 4 changed files with 463 additions and 0 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
205 changes: 205 additions & 0 deletions nsxt/resource_nsxt_policy_gateway_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/* 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/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"

"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()),
"advertise_outbound_route_filter": {
Schema: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validatePolicyPath(),
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(id string, connector client.Connector, isGlobalManager bool) (bool, error) {
var err error

client := clientLayer.NewGatewayConnectionsClient(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 := getOrGenerateID(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(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(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)
}

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(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(connector)
err := client.Delete(id)

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

return nil
}
Loading

0 comments on commit c07330b

Please sign in to comment.