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();
}
}
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);
}
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);
}
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;
}
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);
}
Aggregations