hashicorp / hashicorp/terraform-plugin-sdk

Using ConflictsWith with Lists or Sets

Open
#71 6 comments 14 reactions 0 assignees View on GitHub
enhancement subsystem/types terraform-plugin-framework
Dominant language
Go
Stars
485
Forks
244
Avg merge
19h 57m
Merged PRs (30d)
4

Description

I'm trying to use `ConflictsWith` on a `TypeList` and it isn't working. I think I've narrowed down the issue, but I'm not entirely sure.

The `openstack_compute_instance_v2` resource has a [`network`](https://github.com/hashicorp/terraform/blob/master/builtin/providers/openstack/resource_openstack_compute_instance_v2.go#L107) attribute of `TypeList`. The list defines several nested attributes that can all be used to create a network.

Some of the attributes can't be used together. For example, [`port`](https://github.com/hashicorp/terraform/blob/master/builtin/providers/openstack/resource_openstack_compute_instance_v2.go#L126) and [`floating_ip`](https://github.com/hashicorp/terraform/blob/master/builtin/providers/openstack/resource_openstack_compute_instance_v2.go#L144). So I made the following changes:

```hcl
"port": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"network.floating_ip"},
},
"floating_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"network.port"},
},
```

Then, given something simple like:

```hcl
resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
network {
floating_ip = "foo"
port = "bar"
}
}
```

I would expect an error to be throw, however, one isn't. _But_, if I change the schema to:

```hcl
"port": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"network.0.floating_ip"},
},
"floating_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"network.0.port"},
},
```

Then I get a validation error. Of course, that is only applicable to the first defined network.

Throwing some debug output into `helper/schema/schema.go` and `terraform/resource.go`, I _think_ the following is happening:

During the [loop](https://github.com/hashicorp/terraform/blob/master/helper/schema/schema.go#L1125-L1130) for a conflicting key, `s` is `network.0.port` but `conflicting_key` is `network.floating_ip`. When `network.floating_ip` is checked [here](https://github.com/hashicorp/terraform/blob/master/terraform/resource.go#L257), since no index is specified, no value is returned.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.