Skip to content

Commit

Permalink
Merge pull request #319 from spyderbat/update/add-actions-to-cluter-p…
Browse files Browse the repository at this point in the history
…olicies

Fix schemas
  • Loading branch information
brent-spyder committed Apr 22, 2024
2 parents 8b2ee0c + c38468c commit 2ed3480
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions spyctl/schemas_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,20 @@ class SelectorExpression(BaseModel):
)
values: Optional[List[str]] = Field(None, alias=lib.VALUES_FIELD)

@model_validator(mode="before")
@classmethod
def ensure_values(cls, values):
if values["operator"] in ["Exists", "DoesNotExist"]:
if values.get("values"):
@model_validator(mode="after")
def ensure_values(self):
if self.operator in ["Exists", "DoesNotExist"]:
if self.values:
raise ValueError(
f"'{values['operator']}' operator does not accept"
f" values. Found '{values['values']}'"
f"'{self.operator}' operator does not accept"
f" values. Found '{self.values}'"
)
else:
if not values.get("values"):
if not self.values:
raise ValueError(
f"'{values['operator']}' operator requires values."
f"'{self.operator}' operator requires values."
)
return values
return self


def encode_expr(key, operator, values=None) -> str:
Expand Down Expand Up @@ -167,12 +166,12 @@ class MatchExpressionModel(BaseModel):

class LabelsMatchModel(MatchLabelsModel, MatchExpressionModel):

@model_validator(mode="before")
@classmethod
def ensure_one_field(cls, values):
@model_validator(mode="after")
def ensure_one_field(self):
values = self.model_dump(by_alias=True, exclude_unset=True)
if not any(value for value in values.values()):
raise ValueError("Need matchLabels or matchExpressions")
return values
return self

model_config = ConfigDict(extra="forbid")

Expand Down Expand Up @@ -293,21 +292,6 @@ class ContainerSelectorModel(ContainerMatchModel):
container_name: Optional[str] = Field(None, alias=lib.CONTAINER_NAME_FIELD)
container_id: Optional[str] = Field(None, alias=lib.CONTAINER_ID_FIELD)

@model_validator(mode="before")
@classmethod
def ensure_one_field(cls, values: Dict):
if not any(
value
for k, value in values.items()
if k
not in [
lib.MATCH_FIELDS_EXPRESSIONS_FIELD,
lib.MATCH_FIELDS_FIELD,
]
):
raise ValueError("")
return values

model_config = ConfigDict(extra="forbid")


Expand All @@ -316,7 +300,7 @@ class ClusterSelectorModel(ClusterMatchModel):


class ServiceSelectorModel(ServiceMatchModel):
cgroup: str = Field(alias=lib.CGROUP_FIELD)
cgroup: Optional[str] = Field(None, alias=lib.CGROUP_FIELD)
model_config = ConfigDict(extra="forbid")


Expand All @@ -328,13 +312,6 @@ class MachineSelectorModel(MachineMatchModel):
None, alias=lib.MACHINE_UID_FIELD
)

@model_validator(mode="before")
@classmethod
def ensure_one_exists(cls, values: Dict):
if not any([value for value in values.values()]):
raise ValueError("")
return values

model_config = ConfigDict(extra="forbid")


Expand Down

0 comments on commit 2ed3480

Please sign in to comment.