# 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, conlist
from .v1_network_policy_peer import V1NetworkPolicyPeer
from .v1_network_policy_port import V1NetworkPolicyPort

class V1NetworkPolicyIngressRule(BaseModel):
    """
    NetworkPolicyIngressRule describes a particular set of traffic that is allowed to the pods matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and from.  # noqa: E501
    """
    var_from: Optional[conlist(V1NetworkPolicyPeer)] = Field(None, alias="from", description="from is a list of sources which should be able to access the pods selected for this rule. Items in this list are combined using a logical OR operation. If this field is empty or missing, this rule matches all sources (traffic not restricted by source). If this field is present and contains at least one item, this rule allows traffic only if the traffic matches at least one item in the from list.")
    ports: Optional[conlist(V1NetworkPolicyPort)] = Field(None, description="ports is a list of ports which should be made accessible on the pods selected for this rule. Each item in this list is combined using a logical OR. If this field is empty or missing, this rule matches all ports (traffic not restricted by port). If this field is present and contains at least one item, then this rule allows traffic only if the traffic matches at least one port in the list.")
    __properties = ["from", "ports"]

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

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

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

        _obj = V1NetworkPolicyIngressRule.parse_obj({
            "var_from": [V1NetworkPolicyPeer.from_dict(_item) for _item in obj.get("from")] if obj.get("from") is not None else None,
            "ports": [V1NetworkPolicyPort.from_dict(_item) for _item in obj.get("ports")] if obj.get("ports") is not None else None
        })
        return _obj


