diff --git a/nsxt/provider.go b/nsxt/provider.go index aa7592d60..98ef0f2b3 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -428,6 +428,7 @@ func Provider() *schema.Provider { "nsxt_transport_node": resourceNsxtTransportNode(), "nsxt_failure_domain": resourceNsxtFailureDomain(), "nsxt_cluster_virtual_ip": resourceNsxtClusterVirualIP(), + "nsxt_policy_host_transport_node_profile": resourceNsxtPolicyHostTransportNodeProfile(), }, ConfigureFunc: providerConfigure, diff --git a/nsxt/resource_nsxt_policy_host_transport_node_profile.go b/nsxt/resource_nsxt_policy_host_transport_node_profile.go new file mode 100644 index 000000000..7d2a73320 --- /dev/null +++ b/nsxt/resource_nsxt_policy_host_transport_node_profile.go @@ -0,0 +1,177 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + + "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/infra" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func resourceNsxtPolicyHostTransportNodeProfile() *schema.Resource { + return &schema.Resource{ + Create: resourceNsxtPolicyHostTransportNodeProfileCreate, + Read: resourceNsxtPolicyHostTransportNodeProfileRead, + Update: resourceNsxtPolicyHostTransportNodeProfileUpdate, + Delete: resourceNsxtPolicyHostTransportNodeProfileDelete, + Importer: &schema.ResourceImporter{ + State: nsxtPolicyPathResourceImporter, + }, + Schema: map[string]*schema.Schema{ + "nsx_id": getNsxIDSchema(), + "path": getPathSchema(), + "display_name": getDisplayNameSchema(), + "description": getDescriptionSchema(), + "revision": getRevisionSchema(), + "tag": getTagsSchema(), + // host_switch_spec + "standard_host_switch": getStandardHostSwitchSchema(), + "preconfigured_host_switch": getPreconfiguredHostSwitchSchema(), + "ignore_overridden_hosts": { + Type: schema.TypeBool, + Default: false, + Description: "Determines if cluster-level configuration should be applied on overridden hosts", + Optional: true, + }, + }, + } +} + +func resourceNsxtPolicyHostTransportNodeProfileExists(id string, connector client.Connector, isGlobal bool) (bool, error) { + client := infra.NewHostTransportNodeProfilesClient(connector) + + _, err := client.Get(id) + if err == nil { + return true, nil + } + + if isNotFoundError(err) { + return false, nil + } + + return false, logAPIError("Error retrieving IP Block", err) +} + +func resourceNsxtPolicyHostTransportNodeProfileCreate(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + client := infra.NewHostTransportNodeProfilesClient(connector) + + id, err := getOrGenerateID(d, m, resourceNsxtPolicyHostTransportNodeProfileExists) + if err != nil { + return err + } + + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + tags := getPolicyTagsFromSchema(d) + ignoreOverridenHosts := d.Get("ignore_overridden_hosts").(bool) + + hostSwitchSpec, err := getHostSwitchSpecFromSchema(d) + if err != nil { + return err + } + + obj := model.PolicyHostTransportNodeProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + HostSwitchSpec: hostSwitchSpec, + IgnoreOverriddenHosts: &ignoreOverridenHosts, + } + + _, err = client.Update(id, obj, nil) + if err != nil { + return handleCreateError("Policy Host Transport Node Profile", id, err) + } + + d.SetId(id) + d.Set("nsx_id", id) + return resourceNsxtPolicyHostTransportNodeProfileRead(d, m) +} + +func resourceNsxtPolicyHostTransportNodeProfileRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + client := infra.NewHostTransportNodeProfilesClient(connector) + + id := d.Id() + if id == "" { + return fmt.Errorf("error obtaining Policy Host Transport Node Profile ID") + } + + obj, err := client.Get(id) + if err != nil { + return handleReadError(d, "Policy Host Transport Node Profile", id, err) + } + + d.Set("display_name", obj.DisplayName) + d.Set("description", obj.Description) + setPolicyTagsInSchema(d, obj.Tags) + d.Set("nsx_id", obj.Id) + d.Set("path", obj.Path) + d.Set("revision", obj.Revision) + + err = setHostSwitchSpecInSchema(d, obj.HostSwitchSpec) + if err != nil { + return err + } + + return nil +} + +func resourceNsxtPolicyHostTransportNodeProfileUpdate(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + client := infra.NewHostTransportNodeProfilesClient(connector) + + id := d.Id() + if id == "" { + return fmt.Errorf("error obtaining Policy Host Transport Node Profile ID") + } + + // Read the rest of the configured parameters + displayName := d.Get("display_name").(string) + description := d.Get("description").(string) + revision := int64(d.Get("revision").(int)) + tags := getPolicyTagsFromSchema(d) + ignoreOverridenHosts := d.Get("ignore_overridden_hosts").(bool) + + hostSwitchSpec, err := getHostSwitchSpecFromSchema(d) + if err != nil { + return err + } + + obj := model.PolicyHostTransportNodeProfile{ + DisplayName: &displayName, + Description: &description, + Tags: tags, + HostSwitchSpec: hostSwitchSpec, + IgnoreOverriddenHosts: &ignoreOverridenHosts, + Revision: &revision, + } + + _, err = client.Update(id, obj, nil) + if err != nil { + return handleUpdateError("Policy Host Transport Node Profile", id, err) + } + + return resourceNsxtPolicyHostTransportNodeProfileRead(d, m) +} + +func resourceNsxtPolicyHostTransportNodeProfileDelete(d *schema.ResourceData, m interface{}) error { + id := d.Id() + if id == "" { + return fmt.Errorf("error obtaining Policy Host Transport Node Profile ID") + } + + connector := getPolicyConnector(m) + client := infra.NewHostTransportNodeProfilesClient(connector) + err := client.Delete(id) + if err != nil { + return handleDeleteError("Policy Host Transport Node Profile", id, err) + } + + return nil +} diff --git a/nsxt/resource_nsxt_transport_node.go b/nsxt/resource_nsxt_transport_node.go index f38b7a6c7..f59d5715c 100644 --- a/nsxt/resource_nsxt_transport_node.go +++ b/nsxt/resource_nsxt_transport_node.go @@ -1405,11 +1405,14 @@ func getHostSwitchSpecFromSchema(d *schema.ResourceData) (*data.StructValue, err IpAssignmentSpec: iPAssignmentSpec, IsMigratePnics: &isMigratePNics, Pnics: pNics, - PortgroupTransportZoneId: &portGroupTZID, TransportNodeProfileSubConfigs: transportNodeSubProfileCfg, TransportZoneEndpoints: transportZoneEndpoints, Uplinks: uplinks, } + if portGroupTZID != "" { + hsw.PortgroupTransportZoneId = &portGroupTZID + + } hostSwitchSpec := model.StandardHostSwitchSpec{ HostSwitches: []model.StandardHostSwitch{hsw}, diff --git a/website/docs/r/policy_host_transport_node_profile.html.markdown b/website/docs/r/policy_host_transport_node_profile.html.markdown new file mode 100644 index 000000000..8ed90c4a2 --- /dev/null +++ b/website/docs/r/policy_host_transport_node_profile.html.markdown @@ -0,0 +1,120 @@ +--- +subcategory: "Fabric" +layout: "nsxt" +page_title: "NSXT: nsxt_policy_host_transport_node_profile" +description: A resource to configure a Policy Host Transport Node Profile. +--- + +# nsxt_transport_node + +This resource provides a method for the management of a Policy Host Transport Node Profile. +This resource is supported with NSX 4.1.0 onwards. + +## Example Usage +```hcl +resource "nsxt_policy_host_transport_node_profile" "test" { + display_name = "test_policy_host_tnp" + + standard_host_switch { + host_switch_mode = "STANDARD" + host_switch_type = "NVDS" + ip_assignment { + assigned_by_dhcp = true + } + transport_zone_endpoint { + transport_zone_id = data.nsxt_transport_zone.tz1.id + } + host_switch_profile_id = [nsxt_uplink_host_switch_profile.hsw_profile1.id] + is_migrate_pnics = false + pnic { + device_name = "fp-eth0" + uplink_name = "uplink1" + } + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) Display name of the resource. +* `description` - (Optional) Description of the resource. +* `tag` - (Optional) A list of scope + tag pairs to associate with this resource. +* `ignore_overridden_hosts` - (Optional) Determines if cluster-level configuration should be applied on overridden hosts +* `standard_host_switch` - (Optional) Standard host switch specification. + * `cpu_config` - (Optional) Enhanced Networking Stack enabled HostSwitch CPU configuration. + * `num_lcores` - (Required) Number of Logical cpu cores (Lcores) to be placed on a specified NUMA node. + * `numa_node_index` - (Required) Unique index of the Non Uniform Memory Access (NUMA) node. + * `host_switch_id` - (Optional) The host switch id. This ID will be used to reference a host switch. + * `host_switch_mode` - (Optional) Operational mode of a HostSwitch. Accepted values - 'STANDARD', 'ENS', 'ENS_INTERRUPT' or 'LEGACY'. The default value is 'STANDARD'. + * `host_switch_profile_id` - (Optional) Identifiers of host switch profiles to be associated with this host switch. + * `host_switch_type` - (Optional) Type of HostSwitch. Accepted values - 'NVDS' or 'VDS'. The default value is 'NVDS'. + * `ip_assignment` - (Required) - Specification for IPs to be used with host switch virtual tunnel endpoints. Should contain exatly one of the below: + * `assigned_by_dhcp` - (Optional) Enables DHCP assignment. Should be set to true. + * `static_ip` - (Optional) IP assignment specification for Static IP List. + * `ip_addresses` - (Required) List of IPs for transport node host switch virtual tunnel endpoints. + * `subnet_mask` - (Required) Subnet mask. + * `default_gateway` - (Required) Gateway IP. + * `static_ip_mac` - (Optional) IP and MAC assignment specification for Static IP List. + * `default_gateway` - (Required) Gateway IP. + * `subnet_mask` - (Required) Subnet mask. + * `ip_mac_pair` - (Required) List of IPs and MACs for transport node host switch virtual tunnel endpoints. + * `ip` - (Required) IP address. + * `mac` - (Required) MAC address. + * `static_ip_pool_id` - (Optional) IP assignment specification for Static IP Pool. + * `is_migrate_pnics` - (Optional) Migrate any pnics which are in use. + * `pnic` - (Optional) Physical NICs connected to the host switch. + * `device_name` - (Required) Device name or key. + * `uplink_name` - (Required) Uplink name for this Pnic. + * `portgroup_transport_zone_id` - (Optional) Transport Zone ID representing the DVS used in NSX on DVPG. + * `transport_node_profile_sub_config` - (Optional) Transport Node Profile sub-configuration Options. + * `host_switch_config_option` - (Required) Subset of the host switch configuration. + * `host_switch_id` - (Optional) The host switch id. This ID will be used to reference a host switch. + * `host_switch_profile_id` - (Optional) Identifiers of host switch profiles to be associated with this host switch. + * `ip_assignment` - (Required) - Specification for IPs to be used with host switch virtual tunnel endpoints. Should contain exatly one of the below: + * `assigned_by_dhcp` - (Optional) Enables DHCP assignment. Should be set to true. + * `static_ip` - (Optional) IP assignment specification for Static IP List. + * `ip_addresses` - (Required) List of IPs for transport node host switch virtual tunnel endpoints. + * `subnet_mask` - (Required) Subnet mask. + * `default_gateway` - (Required) Gateway IP. + * `static_ip_mac` - (Optional) IP and MAC assignment specification for Static IP List. + * `default_gateway` - (Required) Gateway IP. + * `subnet_mask` - (Required) Subnet mask. + * `ip_mac_pair` - (Required) List of IPs and MACs for transport node host switch virtual tunnel endpoints. + * `ip` - (Required) IP address. + * `mac` - (Required) MAC address. + * `static_ip_pool_id` - (Optional) IP assignment specification for Static IP Pool. + * `uplink` - (Optional) Uplink/LAG of VMware vSphere Distributed Switch connected to the HostSwitch. + * `uplink_name` - (Required) Uplink name from UplinkHostSwitch profile. + * `vds_lag_name` - (Optional) Link Aggregation Group (LAG) name of Virtual Distributed Switch. + * `vds_uplink_name` - (Optional) Uplink name of VMware vSphere Distributed Switch (VDS). + * `name` - (Required) Name of the transport node profile config option. + * `transport_zone_endpoint` - (Optional) Transport zone endpoints + * `transport_zone_id` - (Required) Unique ID identifying the transport zone for this endpoint. + * `transport_zone_profile_id` - (Optional) Identifiers of the transport zone profiles associated with this transport zone endpoint on this transport node. + * `uplink` - (Optional) Uplink/LAG of VMware vSphere Distributed Switch connected to the HostSwitch. + * `uplink_name` - (Required) Uplink name from UplinkHostSwitch profile. + * `vds_lag_name` - (Optional) Link Aggregation Group (LAG) name of Virtual Distributed Switch. + * `vds_uplink_name` - (Optional) Uplink name of VMware vSphere Distributed Switch (VDS). + * `vmk_install_migration` - (Optional) The vmknic and logical switch mappings. + * `destination_network` - (Required) The network id to which the ESX vmk interface will be migrated. + * `device_name` - (Required) ESX vmk interface name. +* `preconfigured_host_switch` - (Optional) Preconfigured host switch. + * `endpoint` - (Optional) Name of the virtual tunnel endpoint which is preconfigured on this host switch. + * `host_switch_id` - (Required) External Id of the preconfigured host switch. + * `transport_zone_endpoint` - (Optional) Transport zone endpoints + * `transport_zone_id` - (Required) Unique ID identifying the transport zone for this endpoint. + * `transport_zone_profile_id` - (Optional) Identifiers of the transport zone profiles associated with this transport zone endpoint on this transport node. + +In addition to arguments listed above, the following attributes are exported: + +* `id` - ID of the resource. +* `revision` - Indicates current revision number of the object as seen by NSX-T API server. This attribute can be useful for debugging. + +[docs-import]: https://www.terraform.io/cli/import + +``` +terraform import nsxt_policy_host_transport_node_profile.test POLICY_PATH +``` +The above command imports Policy Host Transport Node Profile named `test` with NSX policy path `POLICY_PATH`.