# 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 .v1_label_selector import V1LabelSelector
from .v1_network_policy_egress_rule import V1NetworkPolicyEgressRule
from .v1_network_policy_ingress_rule import V1NetworkPolicyIngressRule

class V1NetworkPolicySpec(BaseModel):
    """
    NetworkPolicySpec provides the specification of a NetworkPolicy  # noqa: E501
    """
    egress: Optional[conlist(V1NetworkPolicyEgressRule)] = Field(None, description="egress is a list of egress rules to be applied to the selected pods. Outgoing traffic is allowed if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic matches at least one egress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy limits all outgoing traffic (and serves solely to ensure that the pods it selects are isolated by default). This field is beta-level in 1.8")
    ingress: Optional[conlist(V1NetworkPolicyIngressRule)] = Field(None, description="ingress is a list of ingress rules to be applied to the selected pods. Traffic is allowed to a pod if there are no NetworkPolicies selecting the pod (and cluster policy otherwise allows the traffic), OR if the traffic source is the pod's local node, OR if the traffic matches at least one ingress rule across all of the NetworkPolicy objects whose podSelector matches the pod. If this field is empty then this NetworkPolicy does not allow any traffic (and serves solely to ensure that the pods it selects are isolated by default)")
    pod_selector: V1LabelSelector = Field(..., alias="podSelector")
    policy_types: Optional[conlist(StrictStr)] = Field(None, alias="policyTypes", description="policyTypes is a list of rule types that the NetworkPolicy relates to. Valid options are [\"Ingress\"], [\"Egress\"], or [\"Ingress\", \"Egress\"]. If this field is not specified, it will default based on the existence of ingress or egress rules; policies that contain an egress section are assumed to affect egress, and all policies (whether or not they contain an ingress section) are assumed to affect ingress. If you want to write an egress-only policy, you must explicitly specify policyTypes [ \"Egress\" ]. Likewise, if you want to write a policy that specifies that no egress is allowed, you must specify a policyTypes value that include \"Egress\" (since such a policy would not include an egress section and would otherwise default to just [ \"Ingress\" ]). This field is beta-level in 1.8")
    __properties = ["egress", "ingress", "podSelector", "policyTypes"]

    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) -> V1NetworkPolicySpec:
        """Create an instance of V1NetworkPolicySpec 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 egress (list)
        _items = []
        if self.egress:
            for _item in self.egress:
                if _item:
                    _items.append(_item.to_dict())
            _dict['egress'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in ingress (list)
        _items = []
        if self.ingress:
            for _item in self.ingress:
                if _item:
                    _items.append(_item.to_dict())
            _dict['ingress'] = _items
        # override the default output from pydantic by calling `to_dict()` of pod_selector
        if self.pod_selector:
            _dict['podSelector'] = self.pod_selector.to_dict()
        return _dict

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

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

        _obj = V1NetworkPolicySpec.parse_obj({
            "egress": [V1NetworkPolicyEgressRule.from_dict(_item) for _item in obj.get("egress")] if obj.get("egress") is not None else None,
            "ingress": [V1NetworkPolicyIngressRule.from_dict(_item) for _item in obj.get("ingress")] if obj.get("ingress") is not None else None,
            "pod_selector": V1LabelSelector.from_dict(obj.get("podSelector")) if obj.get("podSelector") is not None else None,
            "policy_types": obj.get("policyTypes")
        })
        return _obj


