hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.54k stars 9.52k forks source link

Allow one line block #22086

Open ofirule opened 5 years ago

ofirule commented 5 years ago

version : Terraform v0.12.4

I think using the following block syntax might be easier in some use cases. See example:

resource "aws_security_group" "backend_sg" {
  name = local.sg_name
  vpc_id = aws_vpc.backend_vpc.id
  ingress { protocol = "tcp" from_port = 22    to_port = 22    cidr_blocks = ["0.0.0.0/0"] }
  ingress { protocol = "tcp" from_port = 80    to_port = 80    cidr_blocks = ["0.0.0.0/0"] ipv6_cidr_blocks ["::/0"] }
  ingress { protocol = "tcp" from_port = 443   to_port = 443   cidr_blocks = ["0.0.0.0/0"] ipv6_cidr_blocks ["::/0"] }

  // Terraform removes the default rule
  egress  { protocol = "-1"  from_port = 0  to_port = 0  cidr_blocks = ["0.0.0.0/0"] }
}

This is the error I got:

Error: Invalid single-argument block definition

  on ../../modules/backend/backend.tf line 56, in resource "aws_security_group" "backend_sg":
  56:   ingress { protocol = "tcp" from_port = 22    to_port = 22    cidr_blocks = ["0.0.0.0/0"] }

A single-line block definition must end with a closing brace immediately after
its single argument definition.
SPhu commented 5 years ago

I don't know why this was take away but I use it quite frequently in my code for things like tags and it improves the readability of the files. I would like to see it return it possible.

jasonmoo commented 4 years ago

This is a requirement for some of the larger configuration files I have to maintain readability. Please stop breaking things.

acutchin-bitpusher commented 4 years ago

Agree. Tabular layouts are much easier to read and quickly understand than multi-line layouts.

chenrui333 commented 4 years ago

This is really annoying, is there anyway to bypass this issue.

By the way, terraform fmt does not work on the resource file either.

I am using the latest terraform (v0.12.24)

techdragon commented 4 years ago

When defining a lot of environment blocks for kubernetes resources, it gets very annoying to have short statements forced out over multiple lines like this.

env { name = "INSTANCE_IP", value_from { field_ref { field_path = "status.hostIP" } } }

and

http_get { path = "/watchman/", scheme = "HTTP", port = "5000" }

Thats how I would like to write them, comma separated, so as to be completely explicit and unambiguous. However this code here is why I have to write that out as

env {
  name = "INSTANCE_IP"
  value_from { 
    field_ref { field_path = "status.hostIP" } 
  } 
}

and

http_get { 
  path = "/watchman/" 
  scheme = "HTTP"
  port = "5000" 
}

Which when multiplied dozens of times over a single resource block, can turn a 16-32 line terraform file, into a 64 to 256 line file extremely easily which is just downright frustrating.

So it looks like this was explicitly removed by this commit here https://github.com/hashicorp/hcl/commit/bafa0c5ace186f6b3b1ed5c120ab5f11086b5bad merged by @apparentlymart who may have more to say on why this was done.

Edit (more cases where one line syntax is not allowed)

The same commit appears to have been when https://github.com/hashicorp/hcl/blob/hcl2/hclsyntax/parser.go#L179-L188 was added which prevents single line nested block definitions. Which is the cause of line count bloat from instances like this.

env_from { secret_ref { name = kubernetes_secret.environment.metadata[0].name } }

being requied to enter as

env_from {
  secret_ref { name = kubernetes_secret.environment.metadata[0].name }
}
Edit 2 - typo fixes
aep commented 2 years ago

is this a wontfix?

(edit, i guess i'll just assume yes and build my own thing. https://github.com/aep/ucli )

maksym-petrov-ct commented 1 year ago

still waiting. It shouldn't be too hard to tweak the internal terraform parser ?

giner commented 1 year ago

In the recent versions (1.5.x) terraform fmt complains with:

A single-line block definition can contain only a single argument. If you meant to define argument "secret_key_ref", use an equals sign to assign it a value. To define a nested block, place it on a line of its own within its parent block.

giner commented 1 year ago

This works:

selector { match_labels = { app = "keycloak" } }

This does not:

env { name = "KEYCLOAK_ADMIN", value = "superadm" } 
r12f commented 1 year ago

can we have a flag or something to disable this error?

crw commented 1 year ago

Per the design of HCL 2.0, it is valid to use a one-line declaration only if there are zero or one arguments inside a block. This was an intentional design limitation and most likely will not be changed in the future (at least not in HCL 2.0).

@r12f please open a new issue for the request (if you feel strongly about it).