Skip to content

Commit

Permalink
Add VPC attachment resource
Browse files Browse the repository at this point in the history
This is the new way of attaching connectivity profile to vpc,
hence the profile attribute is also removed from vpc resource

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Nov 10, 2024
1 parent 0eb8cad commit 8aab5af
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 47 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/vmware/terraform-provider-nsxt
go 1.19

replace (
github.com/vmware/vsphere-automation-sdk-go/lib => github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20240829205651-501d8b74a2f2
github.com/vmware/vsphere-automation-sdk-go/runtime => github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20240829205651-501d8b74a2f2
github.com/vmware/vsphere-automation-sdk-go/services/nsxt => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20240829205651-501d8b74a2f2
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20240829205651-501d8b74a2f2
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20240829205651-501d8b74a2f2
github.com/vmware/vsphere-automation-sdk-go/lib => github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/runtime => github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/services/nsxt => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20241110024359-e42c5fcc5482
)

require (
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95/go.mod h1:EjA
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20240829205651-501d8b74a2f2 h1:g/LOjqOLwM3/zkMDqxcWiPo9f4cD2TARKpNodisecDw=
github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20240829205651-501d8b74a2f2/go.mod h1:ADkX8BkdnvT1Kc9ZfqHaV4qzaaD+9L8Ok2+pxK4xoD8=
github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20240829205651-501d8b74a2f2 h1:jZasoSshbh7jFHHNXDhJ06SNSu7DRWu72IlwGpp/eV4=
github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20240829205651-501d8b74a2f2/go.mod h1:DzLetYAmw1+vj7bqElRWEpuy40WYE/woL3alsymYa/c=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20240829205651-501d8b74a2f2 h1:PWqW/+aaPewj39/nmvER8ZGF4OwS7AFVCd4tg7FZgH0=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20240829205651-501d8b74a2f2/go.mod h1:upLH9b9zpG86P0wwO4+gREf0lBXr8gYcs7P1FRZ9n30=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20240829205651-501d8b74a2f2 h1:06nwNUgwFfRBf6tytT9UHPYmmPKYPEyo4KmatvfPZLE=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20240829205651-501d8b74a2f2/go.mod h1:gcEvyczWPFMZX2gkBiBVpOwvUGSNXSpxU19Sx9aiouY=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20240829205651-501d8b74a2f2 h1:mlyh0vvF9Zwb6ZvGNfdP3KfFJ/J1ioAciz6CTkY2g4k=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20240829205651-501d8b74a2f2/go.mod h1:ugk9I4YM62SSAox57l5NAVBCRIkPQ1RNLb3URxyTADc=
github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20241110024359-e42c5fcc5482 h1:aSqiTMwQQso+tTULhSf/26iAkhTHVfdi9P1Q/X+HpxQ=
github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20241110024359-e42c5fcc5482/go.mod h1:ADkX8BkdnvT1Kc9ZfqHaV4qzaaD+9L8Ok2+pxK4xoD8=
github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20241110024359-e42c5fcc5482 h1:wWKGPhmqvvpxO0G4mlNtZ27ThzPCuOG/kPXgbu0CR8s=
github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20241110024359-e42c5fcc5482/go.mod h1:DzLetYAmw1+vj7bqElRWEpuy40WYE/woL3alsymYa/c=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20241110024359-e42c5fcc5482 h1:qDHIIezjQTDqgv9YlFee9R3vBWDlbFJGUZRxvomtkgk=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20241110024359-e42c5fcc5482/go.mod h1:NSjO9WqelbsTEDb3pVxpYYz4zjgX0XPp43dKNT4Y+9k=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20241110024359-e42c5fcc5482 h1:QkLzAnPXPDxqICfRfo8QMVjim0mY1nYfM+ie0r/pyLQ=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20241110024359-e42c5fcc5482/go.mod h1:gcEvyczWPFMZX2gkBiBVpOwvUGSNXSpxU19Sx9aiouY=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20241110024359-e42c5fcc5482 h1:/+pwbYwlKBuZAwJVtZhSQorwtDmIGbo/EqRnFkgE7dk=
github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20241110024359-e42c5fcc5482/go.mod h1:ugk9I4YM62SSAox57l5NAVBCRIkPQ1RNLb3URxyTADc=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
Expand Down
2 changes: 1 addition & 1 deletion nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func parseStandardPolicyPath(path string) ([]string, error) {
infraPath = false
}
}
if len(segments) <= idx {
if len(segments) < idx {
return nil, fmt.Errorf("unexpected policy path %s", path)
}
if infraPath {
Expand Down
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func Provider() *schema.Provider {
"nsxt_vpc_static_route": resourceNsxtVpcStaticRoutes(),
"nsxt_policy_project_ip_address_allocation": resourceNsxtPolicyProjectIpAddressAllocation(),
"nsxt_vpc_dhcp_v4_static_binding": resourceNsxtVpcSubnetDhcpV4StaticBindingConfig(),
"nsxt_vpc_attachment": resourceNsxtVpcAttachment(),
},

ConfigureFunc: providerConfigure,
Expand Down
13 changes: 0 additions & 13 deletions nsxt/resource_nsxt_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,6 @@ var vpcSchema = map[string]*metadata.ExtendedSchema{
OmitIfEmpty: true,
},
},
"vpc_connectivity_profile": {
Schema: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validatePolicyPath(),
Optional: true,
Computed: true,
},
Metadata: metadata.Metadata{
SchemaType: "string",
SdkFieldName: "VpcConnectivityProfile",
OmitIfEmpty: true,
},
},
}

// VPC resource needs dedicated importer since its path is VPC path,
Expand Down
206 changes: 206 additions & 0 deletions nsxt/resource_nsxt_vpc_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/* 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"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
clientLayer "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs"

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

var vpcAttachmentSchema = 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()),
"parent_path": metadata.GetExtendedSchema(getPolicyPathSchema(true, true, "Policy path of the parent")),
"vpc_connectivity_profile": {
Schema: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validatePolicyPath(),
Required: true,
},
Metadata: metadata.Metadata{
SchemaType: "string",
SdkFieldName: "VpcConnectivityProfile",
},
},
}

func resourceNsxtVpcAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtVpcAttachmentCreate,
Read: resourceNsxtVpcAttachmentRead,
Update: resourceNsxtVpcAttachmentUpdate,
Delete: resourceNsxtVpcAttachmentDelete,
Importer: &schema.ResourceImporter{
State: nsxtParentPathResourceImporter,
},
Schema: metadata.GetSchemaFromExtendedSchema(vpcAttachmentSchema),
}
}

func resourceNsxtVpcAttachmentExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
var err error
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
if pathErr != nil {
return false, pathErr
}
client := clientLayer.NewAttachmentsClient(connector)
_, err = client.Get(parents[0], parents[1], parents[2], id)
if err == nil {
return true, nil
}

if isNotFoundError(err) {
return false, nil
}

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

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

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

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
if pathErr != nil {
return pathErr
}
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getPolicyTagsFromSchema(d)

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

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

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

client := clientLayer.NewAttachmentsClient(connector)
err = client.Patch(parents[0], parents[1], parents[2], id, obj)
if err != nil {
return handleCreateError("VpcAttachment", id, err)
}
d.SetId(id)
d.Set("nsx_id", id)

return resourceNsxtVpcAttachmentRead(d, m)
}

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

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

client := clientLayer.NewAttachmentsClient(connector)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
if pathErr != nil {
return pathErr
}
obj, err := client.Get(parents[0], parents[1], parents[2], id)
if err != nil {
return handleReadError(d, "VpcAttachment", 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, vpcAttachmentSchema, "", nil)
}

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

connector := getPolicyConnector(m)

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

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
if pathErr != nil {
return pathErr
}
description := d.Get("description").(string)
displayName := d.Get("display_name").(string)
tags := getPolicyTagsFromSchema(d)

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

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

elem := reflect.ValueOf(&obj).Elem()
if err := metadata.SchemaToStruct(elem, d, vpcAttachmentSchema, "", nil); err != nil {
return err
}
client := clientLayer.NewAttachmentsClient(connector)
_, err := client.Update(parents[0], parents[1], parents[2], id, obj)
if err != nil {
return handleUpdateError("VpcAttachment", id, err)
}

return resourceNsxtVpcAttachmentRead(d, m)
}

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

connector := getPolicyConnector(m)
parentPath := d.Get("parent_path").(string)

parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
if pathErr != nil {
return pathErr
}

client := clientLayer.NewAttachmentsClient(connector)
err := client.Delete(parents[0], parents[1], parents[2], id)

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

return nil
}
8 changes: 0 additions & 8 deletions nsxt/resource_nsxt_vpc_service_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ var vpcServiceProfileModeValues = []string{
model.VpcProfileDhcpConfig_MODE_DEACTIVATED,
}

var vpcServiceProfileLogLevelValues = []string{
model.PolicyVpcDnsForwarder_LOG_LEVEL_DEBUG,
model.PolicyVpcDnsForwarder_LOG_LEVEL_INFO,
model.PolicyVpcDnsForwarder_LOG_LEVEL_ERROR,
model.PolicyVpcDnsForwarder_LOG_LEVEL_WARNING,
model.PolicyVpcDnsForwarder_LOG_LEVEL_FATAL,
}

var vpcServiceProfileSchema = map[string]*metadata.ExtendedSchema{
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()),
"path": metadata.GetExtendedSchema(getPathSchema()),
Expand Down
23 changes: 14 additions & 9 deletions nsxt/resource_nsxt_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var accTestVpcUpdateAttributes = map[string]string{

func TestAccResourceNsxtVpc_basic(t *testing.T) {
testResourceName := "nsxt_vpc.test"
attachmentResourceName := "nsxt_vpc_attachment.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccOnlyVPC(t) },
Expand All @@ -55,11 +56,12 @@ func TestAccResourceNsxtVpc_basic(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "load_balancer_vpc_endpoint.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "load_balancer_vpc_endpoint.0.enabled", accTestVpcCreateAttributes["enabled"]),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_service_profile"),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_connectivity_profile"),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
resource.TestCheckResourceAttrSet(attachmentResourceName, "parent_path"),
resource.TestCheckResourceAttrSet(attachmentResourceName, "vpc_connectivity_profile"),
),
},
{
Expand All @@ -74,19 +76,19 @@ func TestAccResourceNsxtVpc_basic(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "load_balancer_vpc_endpoint.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "load_balancer_vpc_endpoint.0.enabled", accTestVpcUpdateAttributes["enabled"]),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_service_profile"),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_connectivity_profile"),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
resource.TestCheckResourceAttrSet(attachmentResourceName, "parent_path"),
resource.TestCheckResourceAttrSet(attachmentResourceName, "vpc_connectivity_profile"),
),
},
{
Config: testAccNsxtVpcMinimalistic(),
Check: resource.ComposeTestCheckFunc(
testAccNsxtVpcExists(accTestVpcCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_service_profile"),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_connectivity_profile"),
resource.TestCheckResourceAttr(testResourceName, "short_id", accTestVpcUpdateAttributes["short_id"]),
resource.TestCheckResourceAttr(testResourceName, "load_balancer_vpc_endpoint.#", "0"),
resource.TestCheckResourceAttr(testResourceName, "description", ""),
Expand All @@ -101,7 +103,6 @@ func TestAccResourceNsxtVpc_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccNsxtVpcExists(accTestVpcCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_service_profile"),
resource.TestCheckResourceAttrSet(testResourceName, "vpc_connectivity_profile"),
resource.TestCheckResourceAttr(testResourceName, "load_balancer_vpc_endpoint.#", "0"),
resource.TestCheckResourceAttr(testResourceName, "description", ""),
resource.TestCheckResourceAttr(testResourceName, "short_id", accTestVpcUpdateAttributes["short_id"]),
Expand Down Expand Up @@ -226,8 +227,7 @@ resource "nsxt_vpc" "test" {
private_ips = ["%s"]
short_id = "%s"
vpc_service_profile = nsxt_vpc_service_profile.test.path
vpc_connectivity_profile = nsxt_vpc_connectivity_profile.test.path
vpc_service_profile = nsxt_vpc_service_profile.test.path
load_balancer_vpc_endpoint {
enabled = %s
Expand All @@ -237,7 +237,14 @@ resource "nsxt_vpc" "test" {
scope = "scope1"
tag = "tag1"
}
}`, testAccNsxtProjectContext(), attrMap["display_name"], attrMap["description"], attrMap["private_ips"], attrMap["short_id"], attrMap["enabled"])
}
resource "nsxt_vpc_attachment" "test" {
display_name = "%s"
parent_path = nsxt_vpc.test.path
vpc_connectivity_profile = nsxt_vpc_connectivity_profile.test.path
}
`, testAccNsxtProjectContext(), attrMap["display_name"], attrMap["description"], attrMap["private_ips"], attrMap["short_id"], attrMap["enabled"], attrMap["display_name"])
}

func testAccNsxtVpcMinimalistic() string {
Expand All @@ -249,7 +256,6 @@ resource "nsxt_vpc" "test" {
short_id = "%s"
# TODO - remove when default profiles are supported
vpc_service_profile = nsxt_vpc_service_profile.test.path
vpc_connectivity_profile = nsxt_vpc_connectivity_profile.test.path
}`, testAccNsxtProjectContext(), accTestVpcUpdateAttributes["display_name"], accTestVpcUpdateAttributes["short_id"])
}

Expand All @@ -262,6 +268,5 @@ resource "nsxt_vpc" "test" {
display_name = "%s"
# TODO - remove when default profiles are supported
vpc_service_profile = nsxt_vpc_service_profile.test.path
vpc_connectivity_profile = nsxt_vpc_connectivity_profile.test.path
}`, testAccNsxtProjectContext(), accTestVpcUpdateAttributes["short_id"], accTestVpcUpdateAttributes["display_name"])
}
Loading

0 comments on commit 8aab5af

Please sign in to comment.