# coding: utf-8

"""
    Kubernetes

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: release-1.29
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json


from typing import List, Optional
from pydantic import BaseModel, Field, StrictStr, conlist
from .v1beta1_audit_annotation import V1beta1AuditAnnotation
from .v1beta1_match_condition import V1beta1MatchCondition
from .v1beta1_match_resources import V1beta1MatchResources
from .v1beta1_param_kind import V1beta1ParamKind
from .v1beta1_validation import V1beta1Validation
from .v1beta1_variable import V1beta1Variable

class V1beta1ValidatingAdmissionPolicySpec(BaseModel):
    """
    ValidatingAdmissionPolicySpec is the specification of the desired behavior of the AdmissionPolicy.  # noqa: E501
    """
    audit_annotations: Optional[conlist(V1beta1AuditAnnotation)] = Field(None, alias="auditAnnotations", description="auditAnnotations contains CEL expressions which are used to produce audit annotations for the audit event of the API request. validations and auditAnnotations may not both be empty; a least one of validations or auditAnnotations is required.")
    failure_policy: Optional[StrictStr] = Field(None, alias="failurePolicy", description="failurePolicy defines how to handle failures for the admission policy. Failures can occur from CEL expression parse errors, type check errors, runtime errors and invalid or mis-configured policy definitions or bindings.  A policy is invalid if spec.paramKind refers to a non-existent Kind. A binding is invalid if spec.paramRef.name refers to a non-existent resource.  failurePolicy does not define how validations that evaluate to false are handled.  When failurePolicy is set to Fail, ValidatingAdmissionPolicyBinding validationActions define how failures are enforced.  Allowed values are Ignore or Fail. Defaults to Fail.")
    match_conditions: Optional[conlist(V1beta1MatchCondition)] = Field(None, alias="matchConditions", description="MatchConditions is a list of conditions that must be met for a request to be validated. Match conditions filter requests that have already been matched by the rules, namespaceSelector, and objectSelector. An empty list of matchConditions matches all requests. There are a maximum of 64 match conditions allowed.  If a parameter object is provided, it can be accessed via the `params` handle in the same manner as validation expressions.  The exact matching logic is (in order):   1. If ANY matchCondition evaluates to FALSE, the policy is skipped.   2. If ALL matchConditions evaluate to TRUE, the policy is evaluated.   3. If any matchCondition evaluates to an error (but none are FALSE):      - If failurePolicy=Fail, reject the request      - If failurePolicy=Ignore, the policy is skipped")
    match_constraints: Optional[V1beta1MatchResources] = Field(None, alias="matchConstraints")
    param_kind: Optional[V1beta1ParamKind] = Field(None, alias="paramKind")
    validations: Optional[conlist(V1beta1Validation)] = Field(None, description="Validations contain CEL expressions which is used to apply the validation. Validations and AuditAnnotations may not both be empty; a minimum of one Validations or AuditAnnotations is required.")
    variables: Optional[conlist(V1beta1Variable)] = Field(None, description="Variables contain definitions of variables that can be used in composition of other expressions. Each variable is defined as a named CEL expression. The variables defined here will be available under `variables` in other expressions of the policy except MatchConditions because MatchConditions are evaluated before the rest of the policy.  The expression of a variable can refer to other variables defined earlier in the list but not those after. Thus, Variables must be sorted by the order of first appearance and acyclic.")
    __properties = ["auditAnnotations", "failurePolicy", "matchConditions", "matchConstraints", "paramKind", "validations", "variables"]

    class Config:
        """Pydantic configuration"""
        allow_population_by_field_name = True
        validate_assignment = True

    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.dict(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> V1beta1ValidatingAdmissionPolicySpec:
        """Create an instance of V1beta1ValidatingAdmissionPolicySpec from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self):
        """Returns the dictionary representation of the model using alias"""
        _dict = self.dict(by_alias=True,
                          exclude={
                          },
                          exclude_none=True)
        # override the default output from pydantic by calling `to_dict()` of each item in audit_annotations (list)
        _items = []
        if self.audit_annotations:
            for _item in self.audit_annotations:
                if _item:
                    _items.append(_item.to_dict())
            _dict['auditAnnotations'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in match_conditions (list)
        _items = []
        if self.match_conditions:
            for _item in self.match_conditions:
                if _item:
                    _items.append(_item.to_dict())
            _dict['matchConditions'] = _items
        # override the default output from pydantic by calling `to_dict()` of match_constraints
        if self.match_constraints:
            _dict['matchConstraints'] = self.match_constraints.to_dict()
        # override the default output from pydantic by calling `to_dict()` of param_kind
        if self.param_kind:
            _dict['paramKind'] = self.param_kind.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in validations (list)
        _items = []
        if self.validations:
            for _item in self.validations:
                if _item:
                    _items.append(_item.to_dict())
            _dict['validations'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in variables (list)
        _items = []
        if self.variables:
            for _item in self.variables:
                if _item:
                    _items.append(_item.to_dict())
            _dict['variables'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: dict) -> V1beta1ValidatingAdmissionPolicySpec:
        """Create an instance of V1beta1ValidatingAdmissionPolicySpec from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return V1beta1ValidatingAdmissionPolicySpec.parse_obj(obj)

        _obj = V1beta1ValidatingAdmissionPolicySpec.parse_obj({
            "audit_annotations": [V1beta1AuditAnnotation.from_dict(_item) for _item in obj.get("auditAnnotations")] if obj.get("auditAnnotations") is not None else None,
            "failure_policy": obj.get("failurePolicy"),
            "match_conditions": [V1beta1MatchCondition.from_dict(_item) for _item in obj.get("matchConditions")] if obj.get("matchConditions") is not None else None,
            "match_constraints": V1beta1MatchResources.from_dict(obj.get("matchConstraints")) if obj.get("matchConstraints") is not None else None,
            "param_kind": V1beta1ParamKind.from_dict(obj.get("paramKind")) if obj.get("paramKind") is not None else None,
            "validations": [V1beta1Validation.from_dict(_item) for _item in obj.get("validations")] if obj.get("validations") is not None else None,
            "variables": [V1beta1Variable.from_dict(_item) for _item in obj.get("variables")] if obj.get("variables") is not None else None
        })
        return _obj


