Example usage for com.amazonaws.services.ec2.model Subnet getCidrBlock

List of usage examples for com.amazonaws.services.ec2.model Subnet getCidrBlock

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model Subnet getCidrBlock.

Prototype


public String getCidrBlock() 

Source Link

Document

The IPv4 CIDR block assigned to the subnet.

Usage

From source file:com.infinitechaos.vpcviewer.web.rest.dto.SubnetDTO.java

License:Open Source License

public SubnetDTO(final Subnet subnet) {
    this.subnetId = subnet.getSubnetId();
    this.vpcId = subnet.getVpcId();
    this.state = subnet.getState();
    this.availabilityZone = subnet.getAvailabilityZone();
    this.cidrBlock = subnet.getCidrBlock();

    this.tags.addAll(subnet.getTags().stream().map(TagDTO::new).collect(Collectors.toList()));

    this.name = subnet.getTags().stream().filter(t -> t.getKey().equals("Name")).findFirst().map(Tag::getValue)
            .orElse("n/a");
}

From source file:com.urbancode.terraform.tasks.aws.SubnetTask.java

License:Apache License

/**
 * @return false if ID is null, or true if it is non-null
 *///  w  w w.j  a  v a  2  s.c o  m
public boolean verify() {
    // will return false if the id is null
    boolean result = false;
    if (subnetId != null) {
        if (ec2Client == null) {
            ec2Client = context.fetchEC2Client();
        }

        List<String> id = new ArrayList<String>();
        id.add(subnetId);

        List<Subnet> subnets = helper.getSubnets(id, ec2Client);
        if (subnets != null && !subnets.isEmpty()) {
            for (Subnet subnet : subnets) {
                if (subnet.getAvailabilityZone().equalsIgnoreCase(zone)) {
                    if (subnet.getCidrBlock().equals(cidr)) {
                        result = true;
                    }
                }
            }
        }
    }
    return result;
}

From source file:com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.java

License:Open Source License

public static String getSubnetFromDescription(AWSAllocation aws) {
    String subnetId = getFromCustomProperties(aws.child.description, AWSConstants.AWS_SUBNET_ID);

    if (subnetId != null) {
        AWSNetworkService netSvc = new AWSNetworkService();
        Subnet subnet = netSvc.getSubnet(subnetId, aws.amazonEC2Client);
        return subnet.getCidrBlock();
    }/*from  w ww  .  ja v a2 s . c o  m*/

    return null;
}

From source file:de.unibi.cebitec.bibigrid.meta.aws.CreateClusterEnvironmentAWS.java

@Override
public CreateClusterEnvironmentAWS createSubnet() {

    ///////////////////////////////////////////////////////////////////////
    ///// check for unused Subnet Cidr and create one
    DescribeSubnetsRequest describesubnetsreq = new DescribeSubnetsRequest();
    DescribeSubnetsResult describesubnetres = cluster.getEc2().describeSubnets(describesubnetsreq);
    List<Subnet> loSubnets = describesubnetres.getSubnets();

    List<String> listofUsedCidr = new ArrayList<>(); // contains all subnet.cidr which are in current vpc
    for (Subnet sn : loSubnets) {
        if (sn.getVpcId().equals(vpc.getVpcId())) {
            listofUsedCidr.add(sn.getCidrBlock());
        }/*from   www.j  av a  2s.co m*/
    }

    SubNets subnets = new SubNets(vpc.getCidrBlock(), 24);
    String SUBNETCIDR = subnets.nextCidr(listofUsedCidr);

    log.debug(V, "Use {} for generated SubNet.", SUBNETCIDR);

    // create new subnetdir      
    CreateSubnetRequest createsubnetreq = new CreateSubnetRequest(vpc.getVpcId(), SUBNETCIDR);
    createsubnetreq.withAvailabilityZone(cluster.getConfig().getAvailabilityZone());
    CreateSubnetResult createsubnetres = cluster.getEc2().createSubnet(createsubnetreq);
    subnet = createsubnetres.getSubnet();

    return this;
}

From source file:jp.primecloud.auto.api.instance.DescribeInstance.java

License:Open Source License

/**
 * ??//  w  ww .  ja v  a2s . co  m
 * 
 * @param instanceNo ?
 * @return DescribeInstanceResponse
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public DescribeInstanceResponse describeInstance(@QueryParam(PARAM_NAME_INSTANCE_NO) String instanceNo) {

    // ?
    // InstanceNo
    ApiValidate.validateInstanceNo(instanceNo);

    // ??
    Instance instance = getInstance(Long.parseLong(instanceNo));

    // ??
    User user = checkAndGetUser(instance);

    //?
    Platform platform = platformDao.read(instance.getPlatformNo());
    if (platform == null) {
        // ????
        throw new AutoApplicationException("EAPI-100000", "Platform", PARAM_NAME_PLATFORM_NO,
                instance.getPlatformNo());
    }

    //
    InstanceResponse instanceResponse = new InstanceResponse(instance);
    // TODO CLOUD BRANCHING
    if (PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) {
        //AWS
        AwsInstance awsInstance = awsInstanceDao.read(Long.parseLong(instanceNo));
        if (awsInstance == null) {
            // AWS????
            throw new AutoApplicationException("EAPI-100000", "AwsInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        AwsInstanceResponse awsResponse = new AwsInstanceResponse(awsInstance);

        //SUBNET?
        if (StringUtils.isNotEmpty(awsInstance.getSubnetId())) {
            List<Subnet> subnets = awsDescribeService.getSubnets(user.getUserNo(), platform.getPlatformNo());
            for (Subnet subnet : subnets) {
                if (StringUtils.equals(subnet.getSubnetId(), awsInstance.getSubnetId())) {
                    awsResponse.setSubnet(subnet.getCidrBlock());
                    break;
                }
            }
        }

        // ?
        List<AwsAddress> awsAddresses = awsAddressDao.readByUserNo(user.getUserNo());
        for (AwsAddress awsAddress : awsAddresses) {
            if (platform.getPlatformNo().equals(awsAddress.getPlatformNo())
                    && instance.getInstanceNo().equals(awsAddress.getInstanceNo())) {
                awsResponse.setAwsAddress(new AwsAddressResponse(awsAddress));
                break;
            }
        }

        //AWS
        instanceResponse.setAws(awsResponse);
    } else if (PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) {
        //CloudStack
        CloudstackInstance cloudstackInstance = cloudstackInstanceDao.read(Long.parseLong(instanceNo));
        if (cloudstackInstance == null) {
            // CloudStack????
            throw new AutoApplicationException("EAPI-100000", "CloudstackInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        //CloudStack
        instanceResponse.setCloudstack(new CloudstackInstanceResponse(cloudstackInstance));
    } else if (PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType())) {
        //VMWare
        VmwareInstance vmwareInstance = vmwareInstanceDao.read(Long.parseLong(instanceNo));
        if (vmwareInstance == null) {
            // VMWare????
            throw new AutoApplicationException("EAPI-100000", "VmwareInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        VmwareInstanceResponse vmResponse = new VmwareInstanceResponse(vmwareInstance);

        //VMWARE_ADDRESS?
        VmwareAddress vmwareAddress = vmwareAddressDao.readByInstanceNo(Long.parseLong(instanceNo));
        if (vmwareAddress != null && BooleanUtils.isTrue(vmwareAddress.getEnabled())) {
            vmResponse.setIsStaticIp(true);
            vmResponse.setSubnetMask(vmwareAddress.getSubnetMask());
            vmResponse.setDefaultGateway(vmwareAddress.getDefaultGateway());
        } else {
            vmResponse.setIsStaticIp(false);
        }

        //VMWARE_KEYPAIR?
        List<VmwareKeyPair> keyPairs = vmwareDescribeService.getKeyPairs(user.getUserNo(),
                instance.getPlatformNo());
        for (VmwareKeyPair keyPair : keyPairs) {
            if (keyPair.getKeyNo().equals(vmwareInstance.getKeyPairNo())) {
                vmResponse.setKeyName(keyPair.getKeyName());
                break;
            }
        }
        //VMWare
        instanceResponse.setVmware(vmResponse);
    } else if (PLATFORM_TYPE_NIFTY.equals(platform.getPlatformType())) {
        //Nifty
        NiftyInstance niftyInstance = niftyInstanceDao.read(Long.parseLong(instanceNo));
        if (niftyInstance == null) {
            // /Nifty????
            throw new AutoApplicationException("EAPI-100000", "NiftyInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        NiftyInstanceResponse niftyResponse = new NiftyInstanceResponse(niftyInstance);

        //NIFTY_KEYPAIR?
        List<NiftyKeyPair> keyPairs = niftyDescribeService.getKeyPairs(user.getUserNo(),
                instance.getPlatformNo());
        for (NiftyKeyPair keyPair : keyPairs) {
            if (keyPair.getKeyNo().equals(niftyInstance.getKeyPairNo())) {
                niftyResponse.setKeyName(keyPair.getKeyName());
                break;
            }
        }

        //Nifty
        instanceResponse.setNifty(niftyResponse);
    } else if (PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) {
        //VCLOUD_INSTANCE
        VcloudInstance vcloudInstance = vcloudInstanceDao.read(Long.parseLong(instanceNo));
        if (vcloudInstance == null) {
            // VMWare????
            throw new AutoApplicationException("EAPI-100000", "VcloudInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        VcloudInstanceResponse vcloudInstanceResponse = new VcloudInstanceResponse(vcloudInstance);

        //PLATFORM_VCLOUD_STORAGE_TYPE?
        List<PlatformVcloudStorageType> storageTypes = platformVcloudStorageTypeDao
                .readByPlatformNo(platform.getPlatformNo());
        for (PlatformVcloudStorageType storageType : storageTypes) {
            if (storageType.getStorageTypeNo().equals(vcloudInstance.getStorageTypeNo())) {
                vcloudInstanceResponse.setStorageTypeName(storageType.getStorageTypeName());
                break;
            }
        }

        //VCLOUD_KEYPAIR?
        List<KeyPairDto> keyPairs = iaasDescribeService.getKeyPairs(user.getUserNo(), instance.getPlatformNo());
        for (KeyPairDto keyPair : keyPairs) {
            if (keyPair.getKeyNo().equals(vcloudInstance.getKeyPairNo())) {
                vcloudInstanceResponse.setKeyName(keyPair.getKeyName());
                break;
            }
        }

        // VCloudNetwork?
        List<VcloudInstanceNetwork> vcloudInstanceNetworks = vcloudInstanceNetworkDao
                .readByInstanceNo(Long.parseLong(instanceNo));
        for (VcloudInstanceNetwork vcloudInstanceNetwork : vcloudInstanceNetworks) {
            // VCloudNetwork
            VcloudInstanceNetworkResponse vcloudInstanceNetworkResponse = new VcloudInstanceNetworkResponse(
                    vcloudInstanceNetwork);
            // 
            if (BooleanUtils.isTrue(vcloudInstanceNetwork.getIsPrimary())) {
                vcloudInstanceNetworkResponse.setIsPrimary(true);
            } else {
                vcloudInstanceNetworkResponse.setIsPrimary(false);
            }
            vcloudInstanceResponse.getVcloudNetwoks().add(vcloudInstanceNetworkResponse);
        }

        //VCloud
        instanceResponse.setVcloud(vcloudInstanceResponse);
    } else if (PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) {
        //OpenStack
        OpenstackInstance openstackInstance = openstackInstanceDao.read(Long.parseLong(instanceNo));
        if (openstackInstance == null) {
            // OpenStack????
            throw new AutoApplicationException("EAPI-100000", "OpenstackInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        //OpenStack
        instanceResponse.setOpenstack(new OpenstackInstanceResponse(openstackInstance));
    } else if (PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) {
        //Azure
        AzureInstance azureInstance = azureInstanceDao.read(Long.parseLong(instanceNo));
        if (azureInstance == null) {
            // Azure????
            throw new AutoApplicationException("EAPI-100000", "AzureInstance", PARAM_NAME_INSTANCE_NO,
                    instanceNo);
        }
        //Azure
        instanceResponse.setAzure(new AzureInstanceResponse(azureInstance));
    }

    DescribeInstanceResponse response = new DescribeInstanceResponse(instanceResponse);

    return response;
}

From source file:jp.primecloud.auto.api.instance.EditInstanceAws.java

License:Open Source License

private Subnet getSubnet(Long userNo, Long platformNo, String cidrBlock) {
    List<Subnet> subnets = awsDescribeService.getSubnets(userNo, platformNo);
    for (Subnet subnet : subnets) {
        if (subnet.getCidrBlock().equals(cidrBlock)) {
            return subnet;
        }// w  ww.  j a  va2  s  .c  o  m
    }

    return null;
}

From source file:jp.primecloud.auto.api.lb.DescribeLoadBalancer.java

License:Open Source License

/**
 * ??//from   ww  w. j  av a  2s.co m
 *
 * @param loadBalancerNo ??
 * @return DescribeLoadBalancerResponse
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public DescribeLoadBalancerResponse describeLoadBalancer(
        @QueryParam(PARAM_NAME_LOAD_BALANCER_NO) String loadBalancerNo) {

    // ?
    // LoadBalancerNo
    ApiValidate.validateLoadBalancerNo(loadBalancerNo);

    // ??
    LoadBalancer loadBalancer = getLoadBalancer(Long.parseLong(loadBalancerNo));

    // ??
    User user = checkAndGetUser(loadBalancer);

    //?
    LoadBalancerResponse loadBalancerResponse = new LoadBalancerResponse(loadBalancer);

    //?
    List<LoadBalancerListener> listeners = loadBalancerListenerDao
            .readByLoadBalancerNo(Long.parseLong(loadBalancerNo));
    if (listeners.isEmpty() == false) {
        //
        Collections.sort(listeners, Comparators.COMPARATOR_LOAD_BALANCER_LISTENER);
    }
    for (LoadBalancerListener listener : listeners) {
        //
        loadBalancerResponse.getListeners().add(new LoadBalancerListenerResponse(listener));
    }

    //??
    LoadBalancerHealthCheck healthCheck = loadBalancerHealthCheckDao.read(Long.parseLong(loadBalancerNo));
    if (healthCheck != null) {
        //?
        loadBalancerResponse.setHealthCheck(new LoadBalancerHealthCheckResponse(healthCheck));
    }

    //???
    List<ComponentInstance> componentInstances = componentInstanceDao
            .readByComponentNo(loadBalancer.getComponentNo());
    if (componentInstances.isEmpty() == false) {
        //
        Collections.sort(componentInstances, Comparators.COMPARATOR_COMPONENT_INSTANCE);
    }

    for (ComponentInstance componentInstance : componentInstances) {
        // ?????????
        if (BooleanUtils.isNotTrue(componentInstance.getAssociate())) {
            ComponentInstanceStatus status = ComponentInstanceStatus.fromStatus(componentInstance.getStatus());
            if (status == ComponentInstanceStatus.STOPPED) {
                continue;
            }
        }
        //??
        LoadBalancerInstance loadBalancerInstance = loadBalancerInstanceDao.read(Long.parseLong(loadBalancerNo),
                componentInstance.getInstanceNo());

        LoadBalancerInstanceResponse loadBalancerInstanceResponse;
        if (loadBalancerInstance == null) {
            //??????????
            LoadBalancerInstance loadBalancerInstance2 = new LoadBalancerInstance();
            loadBalancerInstance2.setInstanceNo(componentInstance.getInstanceNo());
            loadBalancerInstance2.setEnabled(false);
            loadBalancerInstance2.setStatus(LoadBalancerInstanceStatus.STOPPED.toString());
            loadBalancerInstanceResponse = new LoadBalancerInstanceResponse(loadBalancerInstance2);
        } else {
            loadBalancerInstanceResponse = new LoadBalancerInstanceResponse(loadBalancerInstance);
        }
        //?
        loadBalancerResponse.getInstances().add(loadBalancerInstanceResponse);
    }

    // AWS
    if (LB_TYPE_ELB.equals(loadBalancer.getType())) {
        AwsLoadBalancer awsLoadBalancer = awsLoadBalancerDao.read(loadBalancer.getLoadBalancerNo());
        AwsLoadBalancerResponse awsLoadBalancerResponse = new AwsLoadBalancerResponse(awsLoadBalancer);

        // Subnet
        if (StringUtils.isNotEmpty(awsLoadBalancer.getSubnetId())) {
            List<Subnet> subnets = awsDescribeService.getSubnets(user.getUserNo(),
                    loadBalancer.getPlatformNo());

            List<String> cidrBlocks = new ArrayList<String>();
            for (String subnetId : StringUtils.split(awsLoadBalancer.getSubnetId(), ",")) {
                for (Subnet subnet : subnets) {
                    if (StringUtils.equals(subnet.getSubnetId(), subnetId)) {
                        cidrBlocks.add(subnet.getCidrBlock());
                        break;
                    }
                }
            }

            awsLoadBalancerResponse.setSubnets(StringUtils.join(cidrBlocks, ","));
        }

        loadBalancerResponse.setAws(awsLoadBalancerResponse);
    }

    DescribeLoadBalancerResponse response = new DescribeLoadBalancerResponse(loadBalancerResponse);

    return response;
}

From source file:jp.primecloud.auto.api.lb.EditLoadBalancer.java

License:Open Source License

/**
 * ???// w  w  w. j a  v  a2  s  .com
 *
 * @param userNo ?
 * @param platformNo ?
 * @param vpcId vpcId
 * @param cidrBlocks cidrBlock?
 * @return List<Subnet> cidrBlock???????
 */
private List<Subnet> getSubnet(Long userNo, Long platformNo, String vpcId, List<String> cidrBlocks) {
    List<Subnet> subnets = new ArrayList<Subnet>();
    if (cidrBlocks.size() > 0) {
        List<Subnet> subnets2 = awsDescribeService.getSubnets(userNo, platformNo);
        for (Subnet subnet : subnets2) {
            if (cidrBlocks.contains(subnet.getCidrBlock())) {
                subnets.add(subnet);
            }
        }
    }
    return subnets;
}

From source file:jp.primecloud.auto.api.platform.DescribePlatform.java

License:Open Source License

private PlatformAwsResponse getAwsDetail(Long userNo, Long platformNo) {
    PlatformAwsResponse response = new PlatformAwsResponse();
    PlatformAws aws = platformAwsDao.read(platformNo);

    // ??//  w w w  .java2s. co m
    List<KeyPairInfo> keyPairs = awsDescribeService.getKeyPairs(userNo, platformNo);
    for (KeyPairInfo keyPair : keyPairs) {
        response.getKeyNames().add(keyPair.getKeyName());
    }

    // 
    List<SecurityGroup> securityGroups = awsDescribeService.getSecurityGroups(userNo, platformNo);
    for (SecurityGroup securityGroup : securityGroups) {
        response.getSecurityGroups().add(securityGroup.getGroupName());
    }

    // 
    AwsCertificate certificate = awsCertificateDao.read(userNo, platformNo);
    response.setDefKeyPair(
            StringUtils.isEmpty(certificate.getDefKeypair()) ? null : certificate.getDefKeypair());

    // ?
    if (BooleanUtils.isTrue(aws.getVpc())) {
        List<Subnet> subnets = awsDescribeService.getSubnets(userNo, platformNo);
        for (Subnet subnet : subnets) {
            response.getSubnets().add(subnet.getCidrBlock());
        }

        // ?
        if (StringUtils.isNotEmpty(certificate.getDefSubnet())) {
            for (Subnet subnet : subnets) {
                if (StringUtils.equals(subnet.getSubnetId(), certificate.getDefSubnet())) {
                    response.setDefSubnet(subnet.getCidrBlock());
                }
            }
        }

        // ELB??
        if (StringUtils.isNotEmpty(certificate.getDefLbSubnet())) {
            for (Subnet subnet : subnets) {
                if (StringUtils.equals(subnet.getSubnetId(), certificate.getDefLbSubnet())) {
                    response.setDefLbSubnet(subnet.getCidrBlock());
                }
            }
        }
    }

    return response;
}

From source file:jp.primecloud.auto.ui.WinServerEdit.java

License:Open Source License

private void updateAwsInstance() {
    // ?//w ww. j  a v  a  2 s.  com
    String comment = (String) basicTab.commentField.getValue();
    String keyName = (String) awsDetailTab.keySelect.getValue();
    String groupName = (String) awsDetailTab.grpSelect.getValue();
    String serverSize = (String) awsDetailTab.sizeSelect.getValue();
    String zoneName = null;
    String subnetId = null;
    String privateIp = null;
    String rootSize = (String) awsDetailTab.rootSizeField.getValue();
    Long addressNo = (awsDetailTab.elasticIpSelect == null) ? null
            : (Long) awsDetailTab.elasticIpSelect.getValue();

    Subnet subnet = null;
    if (BooleanUtils.isTrue(platform.getPlatformAws().getVpc())) {
        subnetId = (String) awsDetailTab.subnetSelect.getValue();
        subnet = awsDetailTab.findSubnet(subnetId);
        zoneName = subnet.getAvailabilityZone();
        privateIp = (String) awsDetailTab.privateIpField.getValue();
    } else {
        zoneName = (String) awsDetailTab.zoneSelect.getValue();
    }

    if (awsDetailTab.NULL_ADDRESS.equals(addressNo)) {
        addressNo = null;
    }

    // ?
    basicTab.commentField.validate();
    awsDetailTab.sizeSelect.validate();
    awsDetailTab.keySelect.validate();
    awsDetailTab.grpSelect.validate();
    if (BooleanUtils.isTrue(platform.getPlatformAws().getVpc())) {
        awsDetailTab.subnetSelect.validate();
        awsDetailTab.privateIpField.validate();
    } else {
        awsDetailTab.zoneSelect.validate();
    }
    awsDetailTab.rootSizeField.validate();
    if (awsDetailTab.elasticIpSelect != null) {
        awsDetailTab.elasticIpSelect.validate();
    }

    // IP????????
    if (BooleanUtils.isTrue(platform.getPlatformAws().getVpc()) && StringUtils.isNotEmpty(privateIp)) {
        long privateIpAddress = IpAddressUtils.parse(privateIp);
        long networkAddress = IpAddressUtils.getNetworkAddress(subnet.getCidrBlock());
        long broadcastAddress = IpAddressUtils.getBroadcastAddress(subnet.getCidrBlock());

        // AWS?????4???1??IP???????????
        if (privateIpAddress < networkAddress + 4 || broadcastAddress - 1 < privateIpAddress) {
            throw new AutoApplicationException("IUI-000109", IpAddressUtils.format(networkAddress + 4),
                    IpAddressUtils.format(broadcastAddress - 1));
        }
    }

    // ????ElasticIP??????
    if (addressNo != null) {
        AwsAddress awsAddress = awsDetailTab.findAwsAddress(addressNo);
        if (awsAddress.getInstanceNo() != null) {
            if (instance.getAwsAddress() == null
                    || !instance.getAwsAddress().getAddressNo().equals(awsAddress.getAddressNo())) {
                throw new AutoApplicationException("IUI-000064");
            }
        }
    }

    Integer rootSize2 = null;
    try {
        rootSize2 = Integer.valueOf(rootSize);
    } catch (Exception ignore) {
    }

    // 
    OperationLogger.writeInstance("SERVER", "Edit Server", instanceNo, null);

    // AWS?
    InstanceService instanceService = BeanContext.getBean(InstanceService.class);
    instanceService.updateAwsInstance(instanceNo, instance.getInstance().getInstanceName(), comment, keyName,
            serverSize, groupName, zoneName, addressNo, subnetId, rootSize2, privateIp);

    // ???
    if (basicTab.componentNos != null && basicTab.attachService) {
        instanceService.associateComponents(instanceNo, basicTab.componentNos);
    }

    // ??
    close();
}