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 all 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
}
28 changes: 28 additions & 0 deletions CustomResourceDefinition/v1alpha2/bool/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

locals {
declared_default_field = contains(keys(var.manifest), "default")
}

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)

}
}

precondition {
condition = !(
local.declared_default_field && try(var.manifest.default, null) == null
)
Comment on lines +18 to +20
Copy link
Owner

Choose a reason for hiding this comment

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

Aqui não deveria ser tem declaração do default e o default for diferente de null ou não tem declaração do default (condition = (local.declared_default_field && try(var.manifest.default, null) != null) || !local.declared_default_field ?

error_message = <<-EOT
Invalid Manifest!
O valor default de um campo bool não pode ser null;
O campo ${var.field_path} tem seu default value definido como null.
(metadata: ${var.metadata_name}; path: ${var.path}; )
Comment on lines +22 to +25
Copy link
Owner

Choose a reason for hiding this comment

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

A mensagem está boa, mas para seguir o padrão do projeto ela precisa ser em inglês

EOT
}
}
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