Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementa suporte a campos bool com default value #22

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion CustomResourceDefinition/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ module "v1alpha1" {
manifest = var.manifest
}

module "v1alpha2" {
source = "./v1alpha2"
count = split("/", var.manifest.apiVersion)[1] == "v1alpha2" ? 1 : 0

path = var.path
manifest = var.manifest
}

output "schema" {
value = one(flatten([module.v1alpha1[*].schema]))
value = one(
flatten([
module.v1alpha1[*].schema,
module.v1alpha2[*].schema
]
))
}
138 changes: 138 additions & 0 deletions CustomResourceDefinition/v1alpha2/array/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
module "string" {
source = "../string"
count = try(var.manifest.items.type == "string", false) ? 1 : 0

metadata_name = var.metadata_name
path = var.path
field_path = "${var.field_path}.items"
manifest = var.manifest.items
}

module "integer" {
source = "../integer"
count = try(var.manifest.items.type == "integer", false) ? 1 : 0

metadata_name = var.metadata_name
path = var.path
field_path = "${var.field_path}.items"
manifest = var.manifest.items
}

module "bool" {
source = "../bool"
count = try(var.manifest.items.type == "bool", false) ? 1 : 0

metadata_name = var.metadata_name
path = var.path
field_path = "${var.field_path}.items"
manifest = var.manifest.items
}

module "reduced_object" {
source = "../reduced_object/"
count = try(var.manifest.items.type == "object", false) ? 1 : 0

metadata_name = var.metadata_name
path = var.path
field_path = "${var.field_path}.items"
manifest = var.manifest.items
}

output "schema" {
value = {
type = "array"
version = "v0"
subItem = one(flatten([
module.string[*].schema, module.integer[*].schema,
module.bool[*].schema, module.reduced_object[*].schema,
]))
validations = {
minItems = try(tonumber(var.manifest.minItems), null)
maxItems = try(tonumber(var.manifest.maxItems), null)
}
}

precondition {
condition = can(var.manifest.items)
error_message = <<-EOT
Invalid "items" value.
The field "${var.field_path}.items" are required.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = !can(var.manifest.items) || can(var.manifest.items.type)
error_message = <<-EOT
Invalid "items" value.
The field "${var.field_path}.items.type" are required.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = !can(var.manifest.items) || !can(var.manifest.items.type) || try(
contains(["string", "integer", "bool", "object"], var.manifest.items.type),
true,
)
error_message = <<-EOT
Invalid "items.type" value.
The field "${var.field_path}.items.type" must be one of "string", "integer", "bool" or "object".
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = can(var.manifest.items.type)
error_message = <<-EOT
Invalid "items" value.
The field "${var.field_path}.items.type" are required.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = can(tonumber(try(var.manifest.minItems, null)))
error_message = <<-EOT
Invalid "minItems" value.
The field "${var.field_path}.minItems" must be a number.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = try(tonumber(var.manifest.minItems) >= 0, true)
error_message = <<-EOT
Invalid "minItems" value.
The field "${var.field_path}.minItems" must be greater than or equal to 0.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = can(tonumber(try(var.manifest.maxItems, null)))
error_message = <<-EOT
Invalid "maxItems" value.
The field "${var.field_path}.maxItems" must be a number.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = try(tonumber(var.manifest.maxItems) >= 1, true)
error_message = <<-EOT
Invalid "maxItems" value.
The field "${var.field_path}.maxItems" must be greater than or equal to 1.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = try(var.manifest.minItems < var.manifest.maxItems, true)
error_message = <<-EOT
Invalid "minItems" and "maxItems" values.
The field "${var.field_path}.minItems" must be less than "${var.field_path}.maxItems".
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}
}
15 changes: 15 additions & 0 deletions CustomResourceDefinition/v1alpha2/array/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "metadata_name" {
type = string
}

variable "path" {
type = string
}

variable "field_path" {
type = string
}

variable "manifest" {
type = any
}
11 changes: 11 additions & 0 deletions CustomResourceDefinition/v1alpha2/bool/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "schema" {
value = {
type = "bool"
version = "v1"
subItem = null
validations = {
has_default_value = can(var.manifest.default) ? true : false
default_value = try(var.manifest.default, null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O schema deveria carregar o campo já em seu valor final. Com isso em mente, a validação se o campo default possui um valor válido, deveria estar aqui.

Suggested change
default_value = try(var.manifest.default, null)
default_value = try(tobool(var.manifest.default), null)

}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Senti falta de um precondition para o caso de ter um manifesto com default: null.

Precisamos validar se o parâmetro passado no manifesto se encaixa com o tipo definido e mostrar uma mensagem de erro bonitinha para quando não for.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qual a ideia nesse caso?

  • validar que o valor passado no default: é compatível com o campo do manifesto?
  • Validar que não podemos passar null como um valor default?

A primeira acho que faz mais sentido. Mas a segundo eu acho que não faria agora pois ela terá unfluência na implementação de nulidade dos campos, que inclusove pode acabar vindo "de graça" depois da implementação de default value pra todo mundo.

Tavez nesse caso aqui, por ser boolean, podemos até impedir um null (pois parece não fazer sentido mesmo), mas para os outros campos eu deixaria em aberto a questão do null.

(vou anotar a validação da compatibilidade de tipos entre o default e o tipo do campo pra fazer depois)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feito. Commits 8f1f89e e b53eac8

15 changes: 15 additions & 0 deletions CustomResourceDefinition/v1alpha2/bool/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "metadata_name" {
type = string
}

variable "path" {
type = string
}

variable "field_path" {
type = string
}

variable "manifest" {
type = any
}
38 changes: 38 additions & 0 deletions CustomResourceDefinition/v1alpha2/integer/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
output "schema" {
value = {
type = "integer"
version = "v0"
subItem = null
validations = {
minimum = try(tonumber(var.manifest.minimum), null)
maximum = try(tonumber(var.manifest.maximum), null)
}
}

precondition {
condition = can(tonumber(try(var.manifest.minimum, null)))
error_message = <<-EOT
Invalid "minimum" value.
The field "${var.field_path}.minimum" must be a number.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = can(tonumber(try(var.manifest.maximum, null)))
error_message = <<-EOT
Invalid "maximum" value.
The field "${var.field_path}.maximum" must be a number.
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}

precondition {
condition = try(var.manifest.minimum < var.manifest.maximum, true)
error_message = <<-EOT
Invalid "minimum" and "maximum" values.
The field "${var.field_path}.minimum" must be less than "${var.field_path}.maximum".
(metadata.name: "${var.metadata_name}", path: "${var.path}")
EOT
}
}
15 changes: 15 additions & 0 deletions CustomResourceDefinition/v1alpha2/integer/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "metadata_name" {
type = string
}

variable "path" {
type = string
}

variable "field_path" {
type = string
}

variable "manifest" {
type = any
}
55 changes: 55 additions & 0 deletions CustomResourceDefinition/v1alpha2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module "version" {
source = "./version"
count = try(length(var.manifest.spec.versions), 0)

metadata_name = var.manifest.metadata.name
path = var.path
field_path = "spec.versions[${count.index}]"
manifest = try(var.manifest.spec.versions[count.index], null)
}

output "schema" {
value = {
path = var.path
name = var.manifest.metadata.name
group = try(var.manifest.spec.group, null)
kind = try(var.manifest.spec.kind, null)
versions = { for _, value in module.version : value.schema.name => value.schema }
}

precondition {
condition = can(var.manifest.spec.group)
error_message = <<-EOT
Invalid manifest.
The "spec.group" field is required.
(metadata.name: "${var.manifest.metadata.name}", path: "${var.path}")
EOT
}

precondition {
condition = can(var.manifest.spec.kind)
error_message = <<-EOT
Invalid manifest.
The "spec.kind" field is required.
(metadata.name: "${var.manifest.metadata.name}", path: "${var.path}")
EOT
}

precondition {
condition = can(var.manifest.spec.versions)
error_message = <<-EOT
Invalid manifest.
The "spec.versions" field is required.
(metadata.name: "${var.manifest.metadata.name}", path: "${var.path}")
EOT
}

precondition {
condition = !can(var.manifest.spec.versions) || can(length(var.manifest.spec.versions))
error_message = <<-EOT
Invalid manifest.
The "spec.versions" field must be an array.
(metadata.name: "${var.manifest.metadata.name}", path: "${var.path}")
EOT
}
}
Loading