예제 #1
0
    def init_horey_cached_type(self, attr_name, value):
        """
        Init automatically cached values
        @param attr_name:
        @param value: {self.SELF_CACHED_TYPE_KEY_NAME: datetime/region/ip..., "value": value_to_init}
        @return:
        """

        if value.get(self.SELF_CACHED_TYPE_KEY_NAME) == "datetime":
            # Example: datetime.datetime.strptime('2017-07-26 15:54:10.000000+0000', '%Y-%m-%d %H:%M:%S.%f%z')
            new_value = datetime.datetime.strptime(value["value"], "%Y-%m-%d %H:%M:%S.%f%z")
        elif value.get(self.SELF_CACHED_TYPE_KEY_NAME) == "ip":
            new_value = IP(value["value"], from_dict=True)
        elif value.get(self.SELF_CACHED_TYPE_KEY_NAME) == "region":
            inited_region = Region()
            inited_region.init_from_dict(value["value"])

            new_value = Region.get_region(inited_region.region_mark)
            if inited_region.region_name is not None:
                if new_value.region_name is not None:
                    if new_value.region_name != inited_region.region_name:
                        raise ValueError(f"{new_value.region_name} != {inited_region.region_name}")
                else:
                    new_value.region_name = inited_region.region_name
        else:
            raise ValueError(f"{attr_name} : {value}")

        self.init_default_attr(attr_name, new_value)
예제 #2
0
        def init_ip_addresses(self, _, lst_src):
            """
            Init ip address objects.
            :param _:
            :param lst_src:
            :return:
            """

            for dict_src in lst_src:
                if "CidrIp" in dict_src:
                    ip = IP(dict_src["CidrIp"])
                elif "CidrIpv6" in dict_src:
                    ip = IP(dict_src["CidrIpv6"])
                else:
                    raise NotImplementedError()

                description = dict_src[
                    "Description"] if "Description" in dict_src else None
                addr = self.Address(ip, description=description)

                if addr.ip.type == IP.Types.IPV4:
                    self.ipv4_ranges.append(addr)
                elif addr.ip.type == IP.Types.IPV6:
                    self.ipv6_ranges.append(addr)
                else:
                    raise NotImplementedError()
예제 #3
0
    def get_security_groups_endpoints(self):
        """
        Get all endpoints reached by security groups.
        :return:
        """

        lst_ret = []

        all_addresses = []
        for sec_grp in self.groups:
            for dict_addr in self.private_ip_addresses:
                # Public
                description = "Inteface: SubnetId: {} NetworkInterfaceId: {}- '{}'".format(
                    self.subnet_id, self.id, self.description)
                dict_ret = {
                    "sg_id": sec_grp["GroupId"],
                    "sg_name": sec_grp["GroupName"],
                    "description": description
                }
                if "Association" in dict_addr:
                    all_addresses.append(dict_addr["Association"]["PublicIp"])
                    dict_ret["ip"] = IP(dict_addr["Association"]["PublicIp"],
                                        int_mask=32)
                    dict_ret["dns"] = dict_addr["Association"]["PublicDnsName"]
                    lst_ret.append(dict_ret)

                # Private
                dict_ret = {
                    "sg_id": sec_grp["GroupId"],
                    "sg_name": sec_grp["GroupName"],
                    "description": description
                }
                all_addresses.append(dict_addr["PrivateIpAddress"])
                dict_ret["ip"] = IP(dict_addr["PrivateIpAddress"], int_mask=32)
                if "PrivateDnsName" in dict_addr:
                    dict_ret["dns"] = dict_addr["PrivateDnsName"]

                lst_ret.append(dict_ret)

            if self.private_ip_address.str_address not in all_addresses:
                raise Exception

            if self.dict_src["Ipv6Addresses"]:
                pdb.set_trace()

            for dict_addr in self.ipv6_addresses:
                pdb.set_trace()
                all_addresses.append(dict_addr["Association"]["PublicIp"])
                all_addresses.append(dict_addr["PrivateIpAddress"])
            if self.private_ip_address.str_address not in all_addresses:
                raise Exception
        return lst_ret
예제 #4
0
    def _init_private_ip_address(self, _, value):
        """
        Init private address from dict.
        :param _:
        :param value:
        :return:
        """

        self.private_ip_address = IP(value + "/32")
예제 #5
0
 def _init_private_ip_address_from_cache(self, _, value):
     """
     Init object from cache dict
     :param _:
     :param value:
     :return:
     """
     if self.private_ip_address is not None:
         raise NotImplementedError()
     self.private_ip_address = IP(value, from_dict=True)
예제 #6
0
def test_convert_short_to_long_ipv6(self):
    assert IP.convert_short_to_long_lst_ipv6("::") == [
        "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000"
    ]
    assert IP.convert_short_to_long_lst_ipv6("::1100") == [
        "0000", "0000", "0000", "0000", "0000", "0000", "0000", "1100"
    ]
    assert IP.convert_short_to_long_lst_ipv6("0::1") == [
        "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0001"
    ]
    assert IP.convert_short_to_long_lst_ipv6("1:10::1") == [
        "0001", "0010", "0000", "0000", "0000", "0000", "0000", "0001"
    ]
    assert IP.convert_short_to_long_lst_ipv6("1:10::10:1") == [
        "0001", "0010", "0000", "0000", "0000", "0000", "0010", "0001"
    ]
    assert IP.convert_short_to_long_lst_ipv6("1::10:1") == [
        "0001", "0000", "0000", "0000", "0000", "0000", "0010", "0001"
    ]
    assert IP.convert_short_to_long_lst_ipv6("1:1:1:1:1:1:1:1") == [
        "0001", "0001", "0001", "0001", "0001", "0001", "0001", "0001"
    ]
예제 #7
0
 def __init__(self, ip, description=None, from_cache=False):
     super(EC2SecurityGroup.IpPermission.Address, self).__init__({})
     if from_cache is True:
         ip = IP(ip, from_dict=True)
     self.ip = ip
     self.description = description
예제 #8
0
 def get_public_addresses(self):
     return [IP(self.association["PublicIp"] + "/32")]
예제 #9
0
def test_split_2():
    ip = IP("192.168.16.0/21")
    lst_ret = ip.split(24)
    assert len(lst_ret) == 8
예제 #10
0
def test_split():
    ip = IP("10.0.0.0/22")
    lst_ret = ip.split(24)
    assert len(lst_ret) == 4
예제 #11
0
def test_first_in_net(self):
    ip = IP("10.0.0.1/24")
    self.assertEqual(ip.first_in_net(), IP("10.0.0.0/24"))
예제 #12
0
def test_address_from_str_binary(self):
    ip = IP("10.0.0.1/24")
    self.assertEqual(
        ip.address_from_str_binary("00001010000000000000000000000001"),
        "10.0.0.1")
예제 #13
0
def test_init_str_bit_address(self):
    ip = IP("10.0.0.1/24")
    self.assertEqual(ip.init_str_bit_address(),
                     "00001010000000000000000000000001")
예제 #14
0
def test_compare(self):
    self.assertEqual(IP("10.0.0.1/24"), IP("10.0.0.1/24"))
예제 #15
0
def test_ip_init(self):
    ip = IP("10.0.0.1/24")
    self.assertTrue(isinstance(ip, IP))
예제 #16
0
 def get_associated_cidr_ips(self):
     return [
         IP(association_set["CidrBlock"])
         for association_set in self.cidr_block_association_set
     ]