I have created an API client library and Terraform provider for Okta's Management API. I am using Okta's Trusted Origin Management API within the Terraform provider to automate the provisioning of Trusted Origins in Okta.
I need help understanding how to import pre-existing Trusted Origins into the Terraform state file for a list of pre-existing Trusted Origins.
Say that Okta Trusted Origins are represented by the okta_trusted_origin
Terraform resource and I have a list of URLs called host_urls
.
variable "host_urls" {
description = "All the URLs for the Trusted Origin hosts"
type = "list"
default = [
"http://foo.com",
"http://bar.com",
"http://example.com"
]
}
resource "okta_trusted_origin" "host-urls" {
count = "${length(var.host_urls)}"
name = "${element(var.host_urls, count.index)}"
origin = "${element(var.host_Urls, count.index)}"
scopes = ["CORS", "REDIRECT"]
}
The “meta-parameter” count
above defines how many copies of the resource to create. Each resource is created based on the Trusted Origins defined in the host_urls
variable. In this case three Terraform resources will be created: host-urls[0], host-urls[1], and host-urls[2].
The problem I have is with the resource name under these conditions. Terraform imports resources based on the resource name, and this makes it difficult if the resource is identified by an ID in the data source where it pre-exists. Each Trusted Origin is uniquely identified in Okta by it's name, origin or ID, but Okta's API only allows GET operations based on ID. In other words, there is no way to get a Trusted Origin by the name or origin fields.
Here's what I'd like to do:
terraform import okta_trusted_origin.<origin-url>
where <origin-url>
is a URL of a pre-existing Trusted Origin in Okta.
What is the best way to achieve what I am trying to do? Terraform's documentation is not leading me down any promising paths. Your help would be much appreciated!