terraform-provider-nsxt icon indicating copy to clipboard operation
terraform-provider-nsxt copied to clipboard

Questions. How to get nsxt_policy_vlan_segment.segment.path created in another Terraform project

Open Suselz opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe.

Hello. I have created nsxt_policy_vlan_segment resource in one Terraform project.

resource "nsxt_policy_segment" "vxlan-10" {
  display_name        = "vxlan-10-Service"
  description         = "Service VXLAN segment 10.0.10.0/24"
  transport_zone_path = data.nsxt_policy_transport_zone.chel-overlay-tz.path
  dhcp_config_path    = resource.nsxt_policy_dhcp_server.DHCP_vxlan-10.path
  connectivity_path   = nsxt_policy_tier1_gateway.t1-gw.path
  subnet {
    cidr        = "10.0.10.1/24"
    dhcp_ranges = ["10.0.10.100-10.0.10.200"]

    dhcp_v4_config {
      server_address = "10.0.10.2/24"
      lease_time     = 36000
      dns_servers    = ["8.8.8.8"]
    }
  }
}

Now i want to use this segment for creating IP binding when I deploy VM in another Terraform project

resource "nsxt_policy_dhcp_v4_static_binding" "test" {
  segment_path    = nsxt_policy_segment.vxlan-10.path
  display_name    = "test"
  description     = "Terraform provisioned static binding"
  gateway_address = "10.0.10.1"
  hostname        = "host1"
  ip_address      = "10.0.10.12"
  lease_time      = 6400
  mac_address     = "00:50:56:a9:3b:a3"
}

The problem is - I can't find any data resource corresponding to object nsxt_policy_segment. Is there any way to get nsxt_policy_segment.vxlan-10.path parameter?

Describe the solution you'd like

I'd like to know if there is any way to get the resource i need, or request to develop data resource nsxt_policy_segment.

Describe alternatives you've considered

No response

Additional context

No response

Suselz avatar Jul 13 '22 14:07 Suselz

Temporary solution for people with a similar problem.

You can write own submodule who request necessary data directly via REST API NSX-T

Submodule structure

Folder structure:

│
└───modules
    └───nsxt_policy_segments
              main.tf
              outputs.tf
              providers.tf
              variables.tf

main.tf:

provider "restapi" {
  uri = join("", ["https://", var.nsxt_params.ip])
  headers = {
    Authorization = join(" ", ["Basic", var.nsxt_params.credentials])
  }
  insecure = var.nsxt_params.allow_unverified_ssl
}

data "restapi_object" "request" {
  for_each     = toset(var.segments)
  path         = "/policy/api/v1/infra/segments"
  search_key   = "display_name"
  search_value = each.value
  results_key  = "results"
}

outputs.tf:

output "segment_path" {
  value = { for k, v in data.restapi_object.request :
    v.api_data.display_name => v.api_data.path
  }
}

providers.tf:

terraform {
  required_providers {
    restapi = {
      source  = "mastercard/restapi"
      version = "1.17.0"
    }
  }
}

variables.tf

variable "segments" {
  type = list(string)
}


variable "nsxt_params" {
  type = object({
    ip          = string
    credentials = string
    allow_unverified_ssl = bool
  })
}

Usage

module "nsxt_policy_segments" {
  source = "../../modules/nsxt_policy_segments"
  nsxt_params = {
    ip                   = var.nsx-t_host
    credentials          = base64encode(join("", [var.nsx-t_username, ":", var.nsx-t_password]))
    allow_unverified_ssl = var.nsx-t_allow_unverified_ssl
  }
  segments = ["segment1", "segment2"]
}

Suselz avatar Jul 19 '22 10:07 Suselz

Hi @Suselz, segment data source was added https://github.com/vmware/terraform-provider-nsxt/pull/870 (not yet released), hence closing this.

annakhm avatar Apr 25 '23 16:04 annakhm