Scalr\Modules\Platforms\Ec2\Ec2PlatformModule::AllocateNewSubnet PHP Метод

AllocateNewSubnet() публичный Метод

public AllocateNewSubnet ( Ec2 $ec2, $vpcId, $availZone, $subnetLength = 24 )
$ec2 Scalr\Service\Aws\Ec2
    public function AllocateNewSubnet(\Scalr\Service\Aws\Ec2 $ec2, $vpcId, $availZone, $subnetLength = 24)
    {
        // HARDCODE THIS
        $subnetLength = 24;
        $subnetsList = $ec2->subnet->describe(null, array(array('name' => SubnetFilterNameType::vpcId(), 'value' => $vpcId)));
        $subnets = array();
        foreach ($subnetsList as $subnet) {
            @(list($ip, $len) = explode('/', $subnet->cidrBlock));
            $subnets[] = array('min' => ip2long($ip), 'max' => ip2long($ip) | (1 << 32 - $len) - 1);
        }
        $vpcInfo = $ec2->vpc->describe($vpcId);
        /* @var $vpc \Scalr\Service\Aws\Ec2\DataType\VpcData */
        $vpc = $vpcInfo->get(0);
        $info = explode("/", $vpc->cidrBlock);
        $startIp = ip2long($info[0]);
        $maxIp = $startIp | (1 << 32 - $info[1]) - 1;
        while ($startIp < $maxIp) {
            $sIp = $startIp;
            $eIp = $sIp | (1 << 32 - $subnetLength) - 1;
            foreach ($subnets as $subnet) {
                $checkRange = $subnet['min'] <= $sIp && $sIp <= $subnet['max'] && $subnet['min'] <= $eIp && $eIp <= $subnet['max'];
                if ($checkRange) {
                    break;
                }
            }
            if ($checkRange) {
                $startIp = $eIp + 1;
            } else {
                $subnetIp = long2ip($startIp);
                break;
            }
        }
        return $ec2->subnet->create($vpcId, "{$subnetIp}/{$subnetLength}", $availZone);
    }