Esempio n. 1
0
    def create(self, name: str, **kwargs) -> Network:
        """Create a Network.

        Args:
            name: Name of network to be created

        Keyword Args:
            attachable (bool): Ignored, always False.
            check_duplicate (bool): Ignored, always False.
            disabled_dns (bool): When True, do not provision DNS for this network.
            driver (str): Which network driver to use when creating network.
            enable_ipv6 (bool): Enable IPv6 on the network.
            ingress (bool): Ignored, always False.
            internal (bool): Restrict external access to the network.
            ipam (IPAMConfig): Optional custom IP scheme for the network.
            labels (Dict[str, str]):  Map of labels to set on the network.
            macvlan (str):
            options (Dict[str, Any]): Driver options.
            scope (str): Ignored, always "local".

        Raises:
            APIError when Podman service reports an error
        """
        data = {
            "DisabledDNS": kwargs.get("disabled_dns"),
            "Driver": kwargs.get("driver"),
            "Internal": kwargs.get("internal"),
            "IPv6": kwargs.get("enable_ipv6"),
            "Labels": kwargs.get("labels"),
            "MacVLAN": kwargs.get("macvlan"),
            "Options": kwargs.get("options"),
        }

        with suppress(KeyError):
            ipam = kwargs["ipam"]
            if len(ipam["Config"]) > 0:

                if len(ipam["Config"]) > 1:
                    raise ValueError(
                        "Podman service only supports one IPAM config.")

                ip_config = ipam["Config"][0]
                data["Gateway"] = ip_config.get("Gateway")

                if "IPRange" in ip_config:
                    iprange = ipaddress.ip_network(ip_config["IPRange"])
                    iprange, mask = api.prepare_cidr(iprange)
                    data["Range"] = {
                        "IP": iprange,
                        "Mask": mask,
                    }

                if "Subnet" in ip_config:
                    subnet = ipaddress.ip_network(ip_config["Subnet"])
                    subnet, mask = api.prepare_cidr(subnet)
                    data["Subnet"] = {
                        "IP": subnet,
                        "Mask": mask,
                    }

        response = self.client.post(
            "/networks/create",
            params={"name": name},
            data=podman.api.http_utils.prepare_body(data),
            headers={"Content-Type": "application/json"},
        )
        data = response.json()

        if response.status_code != requests.codes.okay:
            raise APIError(data["cause"],
                           response=response,
                           explanation=data["message"])

        return self.get(network_id=name, **kwargs)
Esempio n. 2
0
 def test_prepare_cidr(self):
     net = ipaddress.IPv4Network("127.0.0.0/24")
     self.assertEqual(api.prepare_cidr(net), ("127.0.0.0", "////AA=="))