Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def serialize_model(self):
"name": lookup.name,
"description": lookup.description,
"filename": lookup.filename.name,
"default_match": "true" if lookup.default_match else "false",
"default_match": lookup.default_match,
"case_sensitive_match": "true"
if lookup.case_sensitive_match
else "false",
Expand Down
19 changes: 16 additions & 3 deletions contentctl/objects/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import re
from enum import StrEnum, auto
from functools import cached_property
from typing import TYPE_CHECKING, Annotated, Any, Literal, Optional, Self
from typing import TYPE_CHECKING, Annotated, Any, Literal, Self

from pydantic import (
BeforeValidator,
Field,
FilePath,
NonNegativeInt,
Expand Down Expand Up @@ -69,7 +70,19 @@ class Lookup_Type(StrEnum):

# TODO (#220): Split Lookup into 2 classes
class Lookup(SecurityContentObject, abc.ABC):
default_match: Optional[bool] = None
# We need to make sure that this is converted to a string because we widely
# use the string "False" in our lookup content. However, PyYAML reads this
# as a BOOL and this causes parsing to fail. As such, we will always
# convert this to a string if it is passed as a bool
default_match: Annotated[
str, BeforeValidator(lambda dm: str(dm).lower() if isinstance(dm, bool) else dm)
] = Field(
default="",
description="This field is given a default value of ''"
"because it is the default value specified in the transforms.conf "
"docs. Giving it a type of str rather than str | None simplifies "
"the typing for the field.",
)
# Per the documentation for transforms.conf, EXACT should not be specified in this list,
# so we include only WILDCARD and CIDR
match_type: list[Annotated[str, Field(pattern=r"(^WILDCARD|CIDR)\(.+\)$")]] = Field(
Expand All @@ -88,7 +101,7 @@ def serialize_model(self):

# All fields custom to this model
model = {
"default_match": "true" if self.default_match is True else "false",
"default_match": self.default_match,
"match_type": self.match_type_to_conf_format,
"min_matches": self.min_matches,
"max_matches": self.max_matches,
Expand Down
4 changes: 2 additions & 2 deletions contentctl/output/templates/transforms.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ filename = {{ lookup.app_filename.name }}
collection = {{ lookup.collection }}
external_type = kvstore
{% endif %}
{% if lookup.default_match is defined and lookup.default_match != None %}
default_match = {{ lookup.default_match | lower }}
{% if lookup.default_match != '' %}
default_match = {{ lookup.default_match }}
{% endif %}
{% if lookup.case_sensitive_match is defined and lookup.case_sensitive_match != None %}
case_sensitive_match = {{ lookup.case_sensitive_match | lower }}
Expand Down
Loading