Search in sources :

Example 36 with Vpc

use of com.amazonaws.services.ec2.model.Vpc in project cloudbreak by hortonworks.

the class AwsResourceConnectorTest method testFindNonOverLappingCIDRWithNon24Subnets2.

@Test
public void testFindNonOverLappingCIDRWithNon24Subnets2() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[] { 76 }));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
    when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
    when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/20");
    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
    Assert.assertEquals("10.0.76.0/24", cidr);
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Vpc(com.amazonaws.services.ec2.model.Vpc) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Test(org.junit.Test)

Example 37 with Vpc

use of com.amazonaws.services.ec2.model.Vpc in project eureka by Netflix.

the class EIPManager method unbindEIP.

/**
     * Unbind the EIP that this instance is associated with.
     */
public void unbindEIP() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myPublicIP = null;
    if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
        myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
        if (myPublicIP == null) {
            logger.info("Instance is not associated with an EIP. Will not try to unbind");
            return;
        }
        try {
            AmazonEC2 ec2Service = getEC2Service();
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(myPublicIP);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
                String domain = eipAddress.getDomain();
                if ("vpc".equals(domain)) {
                    dissociateRequest.setAssociationId(eipAddress.getAssociationId());
                } else {
                    dissociateRequest.setPublicIp(eipAddress.getPublicIp());
                }
                ec2Service.disassociateAddress(dissociateRequest);
                logger.info("Dissociated the EIP {} from this instance", myPublicIP);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Cannot dissociate address from this instance", e);
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 38 with Vpc

use of com.amazonaws.services.ec2.model.Vpc in project eureka by Netflix.

the class EIPManager method bindEIP.

/**
     * Checks if an EIP is bound and optionally binds the EIP.
     *
     * The list of EIPs are arranged with the EIPs allocated in the zone first
     * followed by other EIPs.
     *
     * If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
     * an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
     * for a specified interval.
     *
     * One of the following scenarios can happen here :
     *
     *  1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
     *  2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
     *  3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
     *     the EIP is bound to this instance.
     */
public void bindEIP() {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);
    Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);
    AmazonEC2 ec2Service = getEC2Service();
    boolean isMyinstanceAssociatedWithEIP = false;
    Address selectedEIP = null;
    for (String eipEntry : candidateEIPs) {
        try {
            String associatedInstanceId;
            // Check with AWS, if this EIP is already been used by another instance
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(eipEntry);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                associatedInstanceId = eipAddress.getInstanceId();
                // already marked.
                if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
                    if (selectedEIP == null) {
                        selectedEIP = eipAddress;
                    }
                } else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
                    // This EIP is associated with an instance, check if this is the same as the current instance.
                    // If it is the same, stop searching for an EIP as this instance is already associated with an
                    // EIP
                    selectedEIP = eipAddress;
                    break;
                } else {
                    // The EIP is used by some other instance, hence skip it
                    logger.warn("The selected EIP {} is associated with another instance {} according to AWS," + " hence skipping this", eipEntry, associatedInstanceId);
                }
            }
        } catch (Throwable t) {
            logger.error("Failed to bind elastic IP: {} to {}", eipEntry, myInstanceId, t);
        }
    }
    if (null != selectedEIP) {
        String publicIp = selectedEIP.getPublicIp();
        // Only bind if the EIP is not already associated
        if (!isMyinstanceAssociatedWithEIP) {
            AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withInstanceId(myInstanceId);
            String domain = selectedEIP.getDomain();
            if ("vpc".equals(domain)) {
                associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
            } else {
                associateAddressRequest.setPublicIp(publicIp);
            }
            ec2Service.associateAddress(associateAddressRequest);
            logger.info("\n\n\nAssociated {} running in zone: {} to elastic IP: {}", myInstanceId, myZone, publicIp);
        }
        logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
    } else {
        logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: {}", candidateEIPs);
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 39 with Vpc

use of com.amazonaws.services.ec2.model.Vpc in project GNS by MobilityFirst.

the class AWSEC2 method associateAddress.

/**
   *
   * @param ec2
   * @param ip
   * @param instance
   */
public static void associateAddress(AmazonEC2 ec2, String ip, Instance instance) {
    Address address;
    if ((address = findElasticIP(ec2, ip)) != null) {
        if (address.getDomain().equals("vpc")) {
            System.out.println("VPC Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest().withInstanceId(instance.getInstanceId()).withAllocationId(address.getAllocationId()));
        } else {
            System.out.println("EC2 Classic Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest(instance.getInstanceId(), ip));
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest)

Example 40 with Vpc

use of com.amazonaws.services.ec2.model.Vpc in project GNS by MobilityFirst.

the class AWSEC2 method createInstanceAndWait.

/**
   * Create an Instance
   *
   * @param ec2
   * @param amiRecord
   * @param key
   * @param securityGroup
   * @return the instanceID string
   */
public static String createInstanceAndWait(AmazonEC2 ec2, AMIRecord amiRecord, String key, SecurityGroup securityGroup) {
    RunInstancesRequest runInstancesRequest;
    if (amiRecord.getVpcSubnet() != null) {
        System.out.println("subnet: " + amiRecord.getVpcSubnet() + " securityGroup: " + securityGroup.getGroupName());
        // new VPC
        runInstancesRequest = new RunInstancesRequest().withMinCount(1).withMaxCount(1).withImageId(amiRecord.getName()).withInstanceType(amiRecord.getInstanceType()).withKeyName(key).withSubnetId(amiRecord.getVpcSubnet()).withSecurityGroupIds(Arrays.asList(securityGroup.getGroupId()));
    } else {
        runInstancesRequest = new RunInstancesRequest(amiRecord.getName(), 1, 1);
        runInstancesRequest.setInstanceType(amiRecord.getInstanceType());
        runInstancesRequest.setSecurityGroups(new ArrayList<>(Arrays.asList(securityGroup.getGroupName())));
        runInstancesRequest.setKeyName(key);
    }
    RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
    Instance instance = runInstancesResult.getReservation().getInstances().get(0);
    String createdInstanceId = instance.getInstanceId();
    System.out.println("Waiting for instance " + amiRecord.getName() + " to start");
    long startTime = System.currentTimeMillis();
    do {
        ThreadUtils.sleep(1000);
        if (System.currentTimeMillis() - startTime > 90000) {
            // give it a minute and a half
            System.out.println(createdInstanceId + " timed out waiting for start.");
            return null;
        }
        // regrab the instance data from the server
        instance = findInstance(ec2, createdInstanceId);
        //System.out.print(instance.getState().getName());
        System.out.print(".");
    } while (instance != null && !instance.getState().getName().equals(InstanceStateRecord.RUNNING.getName()));
    System.out.println();
    return createdInstanceId;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest)

Aggregations

Vpc (com.amazonaws.services.ec2.model.Vpc)27 HashMap (java.util.HashMap)25 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)21 Test (org.junit.Test)21 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)19 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)18 ArrayList (java.util.ArrayList)15 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)14 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)14 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)14 Group (com.sequenceiq.cloudbreak.cloud.model.Group)14 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)14 Location (com.sequenceiq.cloudbreak.cloud.model.Location)14 Network (com.sequenceiq.cloudbreak.cloud.model.Network)14 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)14 Filter (com.amazonaws.services.ec2.model.Filter)12 Subnet (com.amazonaws.services.ec2.model.Subnet)10 HashSet (java.util.HashSet)8 RouteTable (com.amazonaws.services.ec2.model.RouteTable)7 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)7