Search in sources :

Example 21 with Address

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

the class EC2Runner method initAndUpdateEC2Host.

/**
   * This is called to initialize an EC2 host for use as A GNS server in a region. It starts the host, loads all the necessary
   * software and copies the JAR files over. We also collect info about this host, like it's IP address and geographic location.
   * When every host is initialized and we have collected all the IPs, phase two is called.
   *
   * @param region - the EC2 region where we are starting this host
   * @param runSetName - so we can terminate them all together
   * @param id - the GNS ID of this server
   * @param elasticIP
   * @param timeout
   */
public static void initAndUpdateEC2Host(RegionRecord region, String runSetName, String id, String elasticIP, int timeout) {
    String installScript;
    AMIRecord amiRecord = AMIRecord.getAMI(amiRecordType, region);
    if (amiRecord == null) {
        System.out.println("Invalid combination of " + amiRecordType + " and Region " + region.name());
        return;
    }
    switch(dataStoreType) {
        case CASSANDRA:
            installScript = cassandraInstallScript;
            break;
        default:
            // MONGO
            if (amiRecordType.toString().contains("Amazon_Linux")) {
                installScript = mongoInstallScript;
            } else {
                switch(amiRecordType) {
                    case MongoDB_2_4_8_with_1000_IOPS:
                        installScript = mongoShortInstallScript;
                        break;
                    case Mongo_2014_5_6:
                        installScript = null;
                        break;
                    case Mongo_2014_5_6_micro:
                        installScript = null;
                        break;
                    case Mongo_2015_6_25_vpc:
                        installScript = null;
                        break;
                    case Mongo_2016_6_16_micro:
                        installScript = null;
                        break;
                    default:
                        System.out.println("Invalid combination of " + amiRecordType + " and " + dataStoreType);
                        return;
                }
            }
    }
    String idString = id.toString();
    //    StatusModel.getInstance().queueUpdate(id, region.name() + ": [Unknown hostname]", null, null);
    try {
        AWSCredentials credentials = new PropertiesCredentials(new File(CREDENTIALSFILE));
        //Create Amazon Client object
        AmazonEC2 ec2 = new AmazonEC2Client(credentials);
        String nodeName = "GNS Node " + idString;
        System.out.println("Starting install for " + nodeName + " in " + region.name() + " as part of run set " + runSetName);
        HashMap<String, String> tags = new HashMap<>();
        tags.put("runset", runSetName);
        tags.put("id", idString);
        //      StatusModel.getInstance().queueUpdate(id, "Creating instance");
        // create an instance
        Instance instance = AWSEC2.createAndInitInstance(ec2, region, amiRecord, nodeName, keyName, amiRecord.getSecurityGroup(), installScript, tags, elasticIP, timeout);
        if (instance != null) {
            //        StatusModel.getInstance().queueUpdate(id, "Instance created");
            //        StatusModel.getInstance().queueUpdate(id, StatusEntry.State.INITIALIZING);
            // toString our ip
            String hostname = instance.getPublicDnsName();
            InetAddress inetAddress = InetAddress.getByName(hostname);
            String ip = inetAddress.getHostAddress();
            // and take a guess at the location (lat, long) of this host
            Point2D location = GEOLocator.lookupIPLocation(ip);
            //        StatusModel.getInstance().queueUpdate(id, hostname, ip, location);
            // update our table of instance information
            hostTable.put(id, new HostInfo(id, hostname, location));
        // and we're done
        //        StatusModel.getInstance().queueUpdate(id, "Waiting for other servers");
        } else {
            System.out.println("EC2 Instance " + idString + " in " + region.name() + " did not in start.");
            //        StatusModel.getInstance().queueUpdate(id, StatusEntry.State.ERROR, "Did not start");
            hostsThatDidNotStart.put(id, id);
        }
    } catch (IOException e) {
        System.out.println("Problem creating EC2 instance " + idString + " in " + region.name() + ": " + e);
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        System.out.println("Problem creating EC2 instance " + idString + " in " + region.name() + ": " + e);
        e.printStackTrace();
    }
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AMIRecord(edu.umass.cs.aws.support.AMIRecord) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) IOException(java.io.IOException) AWSCredentials(com.amazonaws.auth.AWSCredentials) Point2D(java.awt.geom.Point2D) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) File(java.io.File) InetAddress(java.net.InetAddress)

Example 22 with Address

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

the class Amazontool method main.

/**
   * @param args the command line arguments
   * @throws java.io.IOException
   */
public static void main(String[] args) throws IOException {
    AWSCredentials credentials = new PropertiesCredentials(AWSEC2.class.getResourceAsStream("resources/AwsCredentials.properties"));
    //Create Amazon Client object
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
    AWSEC2.describeAllEndpoints(ec2);
//    Instance instance = AWSEC2.findInstance(ec2, "i-86983af6");
//    Address elasticIP = AWSEC2.findElasticIP(ec2, "23.21.120.250");
//    System.out.println(instance.getPublicDnsName());
//    System.out.println(elasticIP.getPublicIp());
//AWSEC2.associateAddress(ec2, "23.21.120.250", instance);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AWSCredentials(com.amazonaws.auth.AWSCredentials)

Example 23 with Address

use of com.amazonaws.services.ec2.model.Address in project data-transfer-project by google.

the class GoogleContactsImportConversionTest method testConversionToGoogleAddresses.

@Test
public void testConversionToGoogleAddresses() {
    // Set up vCard with a primary address and a secondary address
    String primaryStreet = "221B Baker St";
    String primaryLocality = "London";
    ezvcard.property.Address primaryAddress = new ezvcard.property.Address();
    primaryAddress.setStreetAddress(primaryStreet);
    primaryAddress.setLocality(primaryLocality);
    primaryAddress.setPref(VCARD_PRIMARY_PREF);
    String altStreet = "42 Wallaby Way";
    String altLocality = "Sydney";
    ezvcard.property.Address altAddress = new ezvcard.property.Address();
    altAddress.setStreetAddress(altStreet);
    altAddress.setLocality(altLocality);
    altAddress.setPref(VCARD_PRIMARY_PREF + 1);
    // Add addresses to vCard.  Order shouldn't matter.
    VCard vCard = defaultVCard;
    vCard.addAddress(primaryAddress);
    vCard.addAddress(altAddress);
    // Run test
    Person person = GoogleContactsImporter.convert(vCard);
    // Check results
    // Check correct number of addresses
    assertThat(person.getAddresses().size()).isEqualTo(2);
    // Check primary address
    List<Address> actualPrimaryAddresses = person.getAddresses().stream().filter(a -> a.getMetadata().getPrimary()).collect(Collectors.toList());
    List<String> actualPrimaryAddressStreets = getValuesFromFields(actualPrimaryAddresses, Address::getStreetAddress);
    assertThat(actualPrimaryAddressStreets).containsExactly(primaryStreet);
    // Check secondary address
    List<Address> actualSecondaryAddresses = person.getAddresses().stream().filter(a -> !a.getMetadata().getPrimary()).collect(Collectors.toList());
    List<String> actualSecondaryAddressStreets = getValuesFromFields(actualSecondaryAddresses, Address::getStreetAddress);
    assertThat(actualSecondaryAddressStreets).containsExactly(altStreet);
}
Also used : VCard(ezvcard.VCard) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Telephone(ezvcard.property.Telephone) Test(org.junit.Test) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) Truth.assertThat(com.google.common.truth.Truth.assertThat) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) StructuredName(ezvcard.property.StructuredName) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SOURCE_PARAM_NAME_TYPE) Pair(com.google.gdata.util.common.base.Pair) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) CONTACT_SOURCE_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.CONTACT_SOURCE_TYPE) Nullable(com.google.gdata.util.common.base.Nullable) Address(com.google.api.services.people.v1.model.Address) Email(ezvcard.property.Email) VCARD_PRIMARY_PREF(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.VCARD_PRIMARY_PREF) Name(com.google.api.services.people.v1.model.Name) Collections(java.util.Collections) Before(org.junit.Before) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Address(com.google.api.services.people.v1.model.Address) VCard(ezvcard.VCard) Person(com.google.api.services.people.v1.model.Person) Test(org.junit.Test)

Example 24 with Address

use of com.amazonaws.services.ec2.model.Address in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method updateNICState.

/**
 * For each NetworkInterfaceState, obtain the corresponding AWS NIC, and generate POST operation
 * to update its private address
 */
private NetworkInterfaceState updateNICState(AWSComputeStateCreationContext context, Instance instance, NetworkInterfaceState existingNicState) {
    InstanceNetworkInterface awsNic = instance.getNetworkInterfaces().stream().filter(currentAwsNic -> currentAwsNic.getAttachment().getDeviceIndex() == existingNicState.deviceIndex).findFirst().orElse(null);
    // create a new NetworkInterfaceState for updating the address
    NetworkInterfaceState updateNicState = new NetworkInterfaceState();
    if (StringUtils.isEmpty(updateNicState.endpointLink)) {
        updateNicState.endpointLink = context.request.endpointLink;
    }
    // endpoint link.
    if (existingNicState.endpointLinks == null) {
        updateNicState.endpointLinks = new HashSet<>();
    } else {
        updateNicState.endpointLinks = existingNicState.endpointLinks;
    }
    updateNicState.endpointLinks.add(context.request.endpointLink);
    updateNicState.address = awsNic.getPrivateIpAddress();
    if (context.request.enumeratedSecurityGroups != null) {
        for (GroupIdentifier awsSG : awsNic.getGroups()) {
            // we should have updated the list of SG Ids before this step and should have
            // ensured that all the SGs exist locally
            String securityGroupLink = context.request.enumeratedSecurityGroups.securityGroupStates.get(awsSG.getGroupId());
            if (securityGroupLink == null || securityGroupLink.isEmpty()) {
                continue;
            }
            if (updateNicState.securityGroupLinks == null) {
                updateNicState.securityGroupLinks = new ArrayList<>();
            }
            updateNicState.securityGroupLinks.add(securityGroupLink);
        }
    }
    // create update operation and add it for batch execution on the next stage
    Operation updateNicOperation = createPatchOperation(this, updateNicState, existingNicState.documentSelfLink);
    context.enumerationOperations.add(updateNicOperation);
    // If existing network state does not have an internal tag then create dedicated
    // patch to update the internal tag link.
    String networkInterfaceInternalTagLink = context.internalTagLinksMap.get(ec2_net_interface.toString()).iterator().next();
    if (existingNicState.tagLinks == null || (existingNicState.tagLinks != null && !existingNicState.tagLinks.contains(networkInterfaceInternalTagLink))) {
        Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(NetworkInterfaceState.FIELD_NAME_TAG_LINKS, Collections.singletonList(networkInterfaceInternalTagLink));
        Map<String, Collection<Object>> collectionsToRemoveMap = Collections.singletonMap(NetworkInterfaceState.FIELD_NAME_TAG_LINKS, Collections.emptyList());
        ServiceStateCollectionUpdateRequest updateTagLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, collectionsToRemoveMap);
        context.enumerationOperations.add(Operation.createPatch(this.getHost(), existingNicState.documentSelfLink).setReferer(this.getUri()).setBody(updateTagLinksRequest));
    }
    return updateNicState;
}
Also used : NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Example 25 with Address

use of com.amazonaws.services.ec2.model.Address in project photon-model by vmware.

the class AWSNetworkClient method allocateElasticIPAddress.

/**
 * Allocate an elastic IP address
 */
public DeferredResult<String> allocateElasticIPAddress() {
    AllocateAddressRequest req = new AllocateAddressRequest().withDomain(DomainType.Vpc);
    String message = "Allocate AWS Elastic IP Address for use with instances in a VPC.";
    AWSDeferredResultAsyncHandler<AllocateAddressRequest, AllocateAddressResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
    this.client.allocateAddressAsync(req, handler);
    return handler.toDeferredResult().thenApply(AllocateAddressResult::getAllocationId);
}
Also used : AllocateAddressRequest(com.amazonaws.services.ec2.model.AllocateAddressRequest) AllocateAddressResult(com.amazonaws.services.ec2.model.AllocateAddressResult)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)8 Address (com.amazonaws.services.ec2.model.Address)7 Instance (com.amazonaws.services.ec2.model.Instance)7 Reservation (com.amazonaws.services.ec2.model.Reservation)7 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)6 Filter (com.amazonaws.services.ec2.model.Filter)6 List (java.util.List)6 Test (org.junit.Test)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)5 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)5 ArrayList (java.util.ArrayList)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)4 Address (com.google.api.services.people.v1.model.Address)4 Person (com.google.api.services.people.v1.model.Person)4 Address (org.hl7.fhir.dstu3.model.Address)4 AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)3 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)3 Tag (com.amazonaws.services.ec2.model.Tag)3 Function (com.google.common.base.Function)3 TransportAddress (org.elasticsearch.common.transport.TransportAddress)3