-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support vpc nat resource and its friends
This PR offers support for: 1. vpc nat rule resource 2. vpc ip address allocation resource only addresses allocated from vpc subnet can be used as dest in nat rule 3. vpc nat data source this is helpful for specifying parent_path for nat rule Signed-off-by: Anna Khmelnitsky <[email protected]>
- Loading branch information
Showing
7 changed files
with
909 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"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/services/nsxt/model" | ||
) | ||
|
||
var vpcNatTypes = []string{ | ||
model.PolicyNat_NAT_TYPE_INTERNAL, | ||
model.PolicyNat_NAT_TYPE_USER, | ||
model.PolicyNat_NAT_TYPE_DEFAULT, | ||
model.PolicyNat_NAT_TYPE_NAT64, | ||
} | ||
|
||
func dataSourceNsxtVpcNat() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceNsxtVpcNatRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": getDataSourceIDSchema(), | ||
"nat_type": { | ||
Type: schema.TypeString, | ||
Description: "Nat Type", | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(vpcNatTypes, false), | ||
}, | ||
"display_name": getDataSourceExtendedDisplayNameSchema(), | ||
"description": getDataSourceDescriptionSchema(), | ||
"path": getPathSchema(), | ||
"context": getContextSchema(false, false, true), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNsxtVpcNatRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
natType := d.Get("nat_type").(string) | ||
query := make(map[string]string) | ||
query["nat_type"] = natType | ||
|
||
_, err := policyDataSourceResourceReadWithValidation(d, connector, getSessionContext(d, m), "PolicyNat", query, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
/* 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/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 vpcIPAddressAllocationIPAddressTypeValues = []string{ | ||
model.VpcIpAddressAllocation_IP_ADDRESS_TYPE_IPV4, | ||
model.VpcIpAddressAllocation_IP_ADDRESS_TYPE_IPV6, | ||
} | ||
|
||
var vpcIPAddressAllocationIPAddressBlockVisibilityValues = []string{ | ||
model.VpcIpAddressAllocation_IP_ADDRESS_BLOCK_VISIBILITY_EXTERNAL, | ||
model.VpcIpAddressAllocation_IP_ADDRESS_BLOCK_VISIBILITY_PRIVATE, | ||
} | ||
|
||
var vpcIPAddressAllocationSchema = 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, true)), | ||
"ip_address_type": { | ||
Schema: schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.StringInSlice(vpcIPAddressAllocationIPAddressTypeValues, false), | ||
Optional: true, | ||
Default: model.VpcIpAddressAllocation_IP_ADDRESS_TYPE_IPV4, | ||
}, | ||
Metadata: metadata.Metadata{ | ||
SchemaType: "string", | ||
SdkFieldName: "IpAddressType", | ||
}, | ||
}, | ||
"allocation_ip": { | ||
Schema: schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
Metadata: metadata.Metadata{ | ||
SchemaType: "string", | ||
SdkFieldName: "AllocationIp", | ||
OmitIfEmpty: true, | ||
}, | ||
}, | ||
"ip_address_block_visibility": { | ||
Schema: schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.StringInSlice(vpcIPAddressAllocationIPAddressBlockVisibilityValues, false), | ||
Optional: true, | ||
Default: model.VpcIpAddressAllocation_IP_ADDRESS_BLOCK_VISIBILITY_EXTERNAL, | ||
}, | ||
Metadata: metadata.Metadata{ | ||
SchemaType: "string", | ||
SdkFieldName: "IpAddressBlockVisibility", | ||
}, | ||
}, | ||
} | ||
|
||
func resourceNsxtVpcIPAddressAllocation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNsxtVpcIPAddressAllocationCreate, | ||
Read: resourceNsxtVpcIPAddressAllocationRead, | ||
Update: resourceNsxtVpcIPAddressAllocationUpdate, | ||
Delete: resourceNsxtVpcIPAddressAllocationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: nsxtPolicyPathResourceImporter, | ||
}, | ||
Schema: metadata.GetSchemaFromExtendedSchema(vpcIPAddressAllocationSchema), | ||
} | ||
} | ||
|
||
func resourceNsxtVpcIPAddressAllocationExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) { | ||
var err error | ||
parents := getVpcParentsFromContext(sessionContext) | ||
client := clientLayer.NewIpAddressAllocationsClient(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 resourceNsxtVpcIPAddressAllocationCreate(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
id, err := getOrGenerateIDWithParent(d, m, resourceNsxtVpcIPAddressAllocationExists) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
parents := getVpcParentsFromContext(getSessionContext(d, m)) | ||
displayName := d.Get("display_name").(string) | ||
description := d.Get("description").(string) | ||
tags := getPolicyTagsFromSchema(d) | ||
|
||
obj := model.VpcIpAddressAllocation{ | ||
DisplayName: &displayName, | ||
Description: &description, | ||
Tags: tags, | ||
} | ||
|
||
elem := reflect.ValueOf(&obj).Elem() | ||
if err := metadata.SchemaToStruct(elem, d, vpcIPAddressAllocationSchema, "", nil); err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[INFO] Creating VpcIpAddressAllocation with ID %s", id) | ||
|
||
client := clientLayer.NewIpAddressAllocationsClient(connector) | ||
err = client.Patch(parents[0], parents[1], parents[2], id, obj) | ||
if err != nil { | ||
return handleCreateError("VpcIpAddressAllocation", id, err) | ||
} | ||
d.SetId(id) | ||
d.Set("nsx_id", id) | ||
|
||
return resourceNsxtVpcIPAddressAllocationRead(d, m) | ||
} | ||
|
||
func resourceNsxtVpcIPAddressAllocationRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("Error obtaining VpcIpAddressAllocation ID") | ||
} | ||
|
||
client := clientLayer.NewIpAddressAllocationsClient(connector) | ||
parents := getVpcParentsFromContext(getSessionContext(d, m)) | ||
obj, err := client.Get(parents[0], parents[1], parents[2], id) | ||
if err != nil { | ||
return handleReadError(d, "VpcIpAddressAllocation", 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, vpcIPAddressAllocationSchema, "", nil) | ||
} | ||
|
||
func resourceNsxtVpcIPAddressAllocationUpdate(d *schema.ResourceData, m interface{}) error { | ||
|
||
connector := getPolicyConnector(m) | ||
|
||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("Error obtaining VpcIpAddressAllocation ID") | ||
} | ||
|
||
parents := getVpcParentsFromContext(getSessionContext(d, m)) | ||
description := d.Get("description").(string) | ||
displayName := d.Get("display_name").(string) | ||
tags := getPolicyTagsFromSchema(d) | ||
|
||
revision := int64(d.Get("revision").(int)) | ||
|
||
obj := model.VpcIpAddressAllocation{ | ||
DisplayName: &displayName, | ||
Description: &description, | ||
Tags: tags, | ||
Revision: &revision, | ||
} | ||
|
||
elem := reflect.ValueOf(&obj).Elem() | ||
if err := metadata.SchemaToStruct(elem, d, vpcIPAddressAllocationSchema, "", nil); err != nil { | ||
return err | ||
} | ||
client := clientLayer.NewIpAddressAllocationsClient(connector) | ||
_, err := client.Update(parents[0], parents[1], parents[2], id, obj) | ||
if err != nil { | ||
return handleUpdateError("VpcIpAddressAllocation", id, err) | ||
} | ||
|
||
return resourceNsxtVpcIPAddressAllocationRead(d, m) | ||
} | ||
|
||
func resourceNsxtVpcIPAddressAllocationDelete(d *schema.ResourceData, m interface{}) error { | ||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("Error obtaining VpcIpAddressAllocation ID") | ||
} | ||
|
||
connector := getPolicyConnector(m) | ||
parents := getVpcParentsFromContext(getSessionContext(d, m)) | ||
|
||
client := clientLayer.NewIpAddressAllocationsClient(connector) | ||
err := client.Delete(parents[0], parents[1], parents[2], id) | ||
|
||
if err != nil { | ||
return handleDeleteError("VpcIpAddressAllocation", id, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.