Skip to content

Commit

Permalink
Merge pull request #1464 from vmware/vpc-attachment-resource
Browse files Browse the repository at this point in the history
Add VPC attachment resource
  • Loading branch information
annakhm authored Nov 12, 2024
2 parents 59391db + 066ad49 commit 8aa682d
Show file tree
Hide file tree
Showing 11 changed files with 306 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/policy_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestIsPolicyPath(t *testing.T) {
"/global-infra/tier-1s/mygw1",
"/orgs/infra/tier-1s/mygw1",
"/orgs/myorg/projects/myproj/domains/d",
"/orgs/myorg/projects/myproj/vpcs/nicevpc",
}

for _, test := range testData {
Expand Down
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ func Provider() *schema.Provider {
"nsxt_policy_gateway_connection": resourceNsxtPolicyGatewayConnection(),
"nsxt_policy_distributed_vlan_connection": resourceNsxtPolicyDistributedVlanConnection(),
"nsxt_vpc": resourceNsxtVpc(),
"nsxt_vpc_attachment": resourceNsxtVpcAttachment(),
"nsxt_vpc_nat_rule": resourceNsxtPolicyVpcNatRule(),
"nsxt_policy_transit_gateway_attachment": resourceNsxtPolicyTransitGatewayAttachment(),
"nsxt_vpc_external_address": resourceNsxtVpcExternalAddress(),
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
Loading

0 comments on commit 8aa682d

Please sign in to comment.