Skip to content

AtLeastOneOf doesn't work for Optional attributes (references self) #705

@dak1n1

Description

@dak1n1

SDK version

github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3

Relevant provider source code

The problem happens when I include AtLeastOneOf in one of my optional provider attributes.

func Provider() *schema.Provider {
	p := &schema.Provider{
		Schema: map[string]*schema.Schema{
			"host": {
				Type:             schema.TypeString,
				Optional:         true,
				Description:      "The hostname (in form of URI) of Kubernetes master.",
				AtLeastOneOf: []string{"token", "exec", "username", "password", "client_certificate", "client_key"},
			},

When this code runs, the attribute references itself as one of the required attributes. (host appears in the list below).

$ terraform plan
╷
│ Error: AtLeastOne
│ 
│ "host": one of `client_certificate,client_key,exec,host,password,token,username` must be specified
╵

Terraform Configuration Files

$ cat main.tf 
terraform {
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "9.9.9" # this is my dev version
    }
  }
}

provider "kubernetes" {
  config_path = "~/.kube/config"
  # note that `host` is omitted
}

resource "kubernetes_namespace" "test" {
  metadata {
    name = "test"
  }
}

Debug Output

https://gist.githubusercontent.com/dak1n1/61c8ee5525332b1fc4da7ad334cf574f/raw/d3559213f651df7cfd1872c2ac966728b5c8dc4c/gistfile1.txt

Expected Behavior

I read the docs and they say:

AtLeastOneOf is a set of schema keys that, when set, at least one of the keys in that list must be specified.

I expected to get an error when I specify host without specifying one of the options that are needed with host. Specifically, client_certificate, client_key, exec, password, token, username.

Actual Behavior

I didn't specify host, because it's optional. But AtLeastOneOf tells me I need to specify host.

Steps to Reproduce

  1. Grab your favorite provider to do some development with, or fetch my branch here.
  2. If needed, add AtLeastOneOf to an optional schema attribute. (This is already done on my branch).
  3. Build the provider and initialize it using a configuration that specifies at least one resource (like my example config above). Omit the attribute that has AtLeastOneOf from your provider configuration.
  4. terraform plan or terraform apply.

References

This is my PR where I'm trying to use AtLeastOneOf. hashicorp/terraform-provider-kubernetes#1141

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions