Hi, I’m writing a custom terraform provider and I have a dilemma. The API we are writing this provider for has a design-and-then-apply-changes kind of flow:
infrastructure_create
instance_array_create (depends on infrastrucure’s id)
instance_array_create (depends on infrastrucure’s id)
drive_array_create (depends on infrastrucure’s id and instance_array’s id)
…
infrastructure_deploy (depends on infrastrucure’s id)
The update flow is about the same, the deploy step must be executed if there are changes on the above resources.
The current implementation uses a single big resource for resource_1 and resource_2 and 3 are “flattened” into the first one. This allows the create and upgrade code of resource_1 to execute the “deploy changes” step when there are differences to be applied.
The code is available here: https://github.com/bigstepinc/terraform-provider-metalcloud
The current resource definition is something like this:
resource "metalcloud_infrastructure" "my-infra97" {
infrastructure_label = "my-terraform-infra97"
datacenter_name = "us-santaclara"
instance_array {
instance_array_label = "my-instance-array"
...
drive_array{
drive_array_storage_type = "iscsi_hdd"
...
}
...
}
}
While this works just fine now, perhaps there are other, better options available by splitting into separate resources. However, this would require to somehow create a two way relationship between the “infrastructure” resource and all other elements or find some other way to call the deploy function when needed.
An option that I’m considering is to create a ‘deployer’ resource that will be explicitly dependent on all the others. However, this feels a bit “hackish” to me. Is this the right approach?
resource "metalcloud_infrastructure" "my-infra" {
infrastructure_label = "my-terraform-infra104"
datacenter_name = var.datacenter
}
resource "metalcloud_instance_array" "my-infra" {
infrastructure_id = metalcloud_infrastructure.my-infra.id
...
}
resource "metalcloud_deployer" "my-infra" {
depends_on ["${metalcloud_infrastructure.*}","${metalcloud_instance_array.*}","${metalcloud_drive_array.*}"]
}
Any hints would be much appreciated.